Fork function of Process Control

Source: Internet
Author: User

1. Fork Function

# Inlcude <unistd. h>

Pid_t fork (void );

A new process created by fork is called a child process ). The fork function is called once and returns two times. The only difference between the two responses is that the return value of the child process is 0, while that of the parent process is the ID of the new Child process. A child process is a copy of the parent process. Parent and Child processes do not share the storage space (data space, heap, and stack ). Parent and Child processes share the body segment.

Generally, after fork, it is not clear whether the parent process is executed first or the child process is executed first. The solution is to use the Parent and Child processes for signal synchronization.

2. File Sharing

When the standard output of the parent process is redirected, the standard output of the child process is also redirected. In fact, one feature of fork is that all open file descriptors of the parent process are copied to the child process.

There are two common scenarios for processing file descriptors after fork:

1) The parent process waits for the child process to complete. In this case, the parent process does not need to process its descriptor.

2) Parent and Child processes do not execute too many program segments. In this case, after fork, the Parent and Child processes close unnecessary file descriptors, so as not to interfere with the file descriptors used by the other party.

3. Differences between parent and child Processes

1) Return Value of Fork

2) process ID

3) The child process's tms_utime, tms_stime, tms_cutime, and tms_ustime are both set to 0.

4) The filelock set by the parent process is not inherited by the quilt process.

5) The unprocessed alarm clock (Alarm) of the sub-process is cleared.

6) The unprocessed Signal Set of the sub-process is set to an empty set.

4. Reasons for Fork failure

1) the system already has too many processes.

2) the total number of processes with the actual user ID exceeds the system limit.

5. Fork Application

1) A parent process wants to copy itself so that the parent and child processes can execute different code segments at the same time. For example, in a network service, the parent process waits for a request from the client service.

2) A process must execute a different program. In this case, the child process immediately calls the exec function after returning from fork.

6. Differences Between Fork functions and vfork Functions

1) The vfork function also creates a child process, but does not completely copy the address space of the parent process to the child process, because the child process will immediately call exec (or exit ), the address space is not stored. Instead, before a process calls exec (or exit), it runs in the space of the parent process.

2) vfork ensures that the sub-process runs first. Only after the process calls exec (or exit) Can the process be scheduled to run.

7. Interaction between fork and I/O functions

First look at a small program:

# Include <unistd. h> <br/> # include <stdio. h> <br/> # include <string. h> <br/> # include <stdlib. h> <br/> # include <sys/STAT. h> <br/> # include <errno. h> </P> <p>/** <br/> * fork function instance <br/> **/<br/> int global = 6; <br/> char Buf [32] = "the secret that cannot say"; </P> <p> int main () <br/>{< br/> int val = 0; // automatic variable <br/> pid_t PID; </P> <p> val = 99; <br/> If (write (stdout_fileno, Buf, sizeof (BUF)-1 )! = Sizeof (buf-1) <br/>{< br/> printf ("write error"); <br/> exit (1 ); <br/>}</P> <p> // call the standard I/O library function without flushing the cache <br/> printf ("before fork/N "); </P> <p> If (pid = fork () <0) {<br/> printf ("fork error "); <br/> exit (1); <br/>}< br/> else if (pid = 0) <br/>{< br/> // sub-process running <br/> global ++; <br/> Val ++; <br/>}< br/> else <br/> {<br/> // run the parent process <br/> sleep (2 ); <br/>}</P> <p> printf ("pid = % d, global = % d, val = % d/N", getpid (), global, val); <br/> exit (0); <br/>}

Compile the program and execute it as follows:

[Wzhwho @ local ~] $./A. Out

The secret that cannot say

Before fork

PID = 1039, global = 7, val = 100

PID = 1038, global = 6, val = 99

 

[Wzhwho @ local ~] $./A. Out> TMP

[Wzhwho @ local ~] $ Cat TMP

The secret that cannot say

Before fork

PID = 1039, global = 7, val = 100

Before fork

PID = 1038, global = 6, val = 99

 

The Write function is not buffered. Because the fork previously called the write function, the data is only written to the standard output once. However, standard I/O functions are buffered. If the standard output is to the terminal, it is a row buffer, otherwise it is a full buffer. When the program is run interactively, only the row output by the printf is obtained once, because the standard output buffer is flushed by line breaks. When the standard input is redirected to a file, the printf output line is obtained twice because printf is called once before fork, but when fork is called, this row of data is still in the buffer, and then the data space of the parent process is copied to the child process, the buffer is also copied to the child process. When each process is terminated, a copy in its buffer zone is eventually flushed.

 

 

 

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.