Description of the function for creating a child process in Linux

Source: Internet
Author: User

1. Fork function, create a child process

pid_t fork (void); //pid_t is a type of shaping

return value:

In case of success, the parent process returns the ID of the child process (non-negative integer), and the child process returns 0;

In the case of a failure, the parent process returns-1, creating the child process failed, that is, the child process was not created.

To illustrate:

int main (int argc,char* argv[]) {

pid_t pid;

PID = fork (); //Create a sub-process, starting with this code (including this code), there are two processes executing,

However, which process performs the indeterminate first to see which process preempted to the CPU.

if (PID = =-1) {//-1 Description is parent process, 1 indicates the creation process failed

Perror ("fork fail"); Exit (1);

} else if (PID > 0) {//pid greater than 0 description is the parent process, >0 indicates that the child process was created successfully

Sleep (1);//Here the parent process sleeps for 1 seconds to let the child process exit first, otherwise the child process becomes an orphan process (that is, the parent process of the child process becomes the init process)

printf ("Parent_pid =%d, ParentID =%d\n", Getpid (), Getppid ());

The else if (PID = = 0) {//pid equals 0 description is a child process, at which point the execution of the fork function succeeds, not the child process is created.

printf ("Child_pid =%d, parentid=%d\n", Getpid (), Getppid ());

}

printf ("Parent-child process will execute to this sentence \ n");

return 0;

}

2. Fork loops Create n sub-processes

Loop to create the key of the child process: the child process using break out of the loop, only let the parent process to create a child process, not to let the child process to create a child process, or the end is to create a 2^n-1 child process, and this logic a bit messy feeling, bad control.

To illustrate:

int main (int argc, char *argv[]) {

int i, n = 3;//creates 3 sub-processes by default

if (argc = = 2) {n = atoi (argv[1]);}//Can be passed through command line arguments to create the number of

for (i = 0; i < n; i++) {//parent process loops n times, executes the conditions in the following if fork to create a child process, I equals n when exiting the loop, creating a total of n sub-processes.

if (fork () = = 0) break;//fork equals 0 Description is a child process that jumps directly out of the loop

}

Here is the code that each child process and parent process will execute to

Sleep (i);Let the I process sleep I seconds, so that you can see each process in sequence exit, easy to see the results

if (n = = i) printf ("I am parent, pid =%d\n", Getpid ());

elseprintf ("I ' m%dth child, PID =%d\n", i+1, Getpid ());

return 0;

}

3. Getpid function

//todo

4. Getppid function

5. Getuid function

6. Getgid function

7. Kill function

8. Exit function

Description of the function for creating a child process in Linux

Related Article

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.