Linux system programming: Process Control (fork), linuxfork

Source: Internet
Author: User

Linux system programming: Process Control (fork), linuxfork

In linux, fork is used to create a sub-process. This function has the following features:

1) execute once and return twice. the return value in the parent process is the PID of the child process, and the return value in the child process is 0. To obtain the PID of the parent process, the child process must call the getppid function.

2) The generated sub-process will copy the code after the fork Function

3) The global and local variables of the Parent and Child processes are not shared.

4) The child process inherits the environment status of the parent process.

/* ===================================================== ========================================= * Copyright (C) 2018. all rights reserved. ** file name: fork. c * Creator: ghostwu (Wu Hua) * creation Date: July 15, January 12, 2018 * description: fork basic use * =============================================== ===========================*/# include <stdio. h> # include <stdlib. h> # include <unistd. h> int count = 10; int main (int argc, char * argv []) {pid_t pid; int var = 5; printf ("---- before fork ------, current process id = % d \ n ", getpid (); pid = fork (); printf (" ---- after fork ------ \ n "); if (pid <0) {perror ("fork"); exit (-1) ;}else if (pid = 0) {// sub-process var ++; count ++ ;} else {sleep (3);} printf ("fork return value pid = % d, current process pid = % d, count = % d, var = % d \ n", pid, getpid (), count, var); return 0 ;}

In the preceding example, we can see that:

1) The sub-process does not output the "before fork" code, because the sub-process copies the code after fork.

2) The sub-process has no effect on the variables of the parent process after the variable operation. They are two different copies.

3) The standard output is the row buffer mode: When a line break is refreshed, when the buffer is full, refresh and force refresh (fflush); while the standard output (stdout) is the row buffer, because it involves terminal devices;

 

Modify the output method of this example:

Here, you will find that there are two more outputs ("before fork"), because the IO operation becomes the full buffer mode after pipeline redirection output is adopted, when a child process is generated, the data in the buffer zone of the parent process will be copied. Therefore, when the child process refreshes the buffer zone, the child process will also refresh the copied content from the parent process buffer zone. Therefore,Use fflush (NULL) to refresh all the buffers before using fork to generate sub-processes!

/* ===================================================== ========================================= * Copyright (C) 2018. all rights reserved. ** file name: fork. c * Creator: ghostwu (Wu Hua) * creation Date: July 15, January 12, 2018 * description: fork basic use * =============================================== ===========================*/# include <stdio. h> # include <stdlib. h> # include <unistd. h> int count = 10; int main (int argc, char * argv []) {pid_t pid; int var = 5; printf ("---- before fork ------, current process id = % d \ n ", getpid (); fflush (NULL); pid = fork (); printf (" ---- fork ------ \ n "); if (pid <0) {perror ("fork"); exit (-1);} else if (pid = 0) {// sub-process var ++; count ++;} else {sleep (3);} printf ("fork return value pid = % d, current process pid = % d, count = % d, var = % d \ n ", pid, getpid (), count, var); return 0 ;}View Code

At this time, there will be no two ("before fork"), because the data in the parent process buffer has been refreshed to the kernel buffer before fork generates the child process, not in the standard IO buffer.

If the child process ends before the parent process, the child process will become a zombie process.

Because: they must wait for the parent process to "zombie" to be completely released. If the parent process ends first, the parent process of these child processes will become the init process No. 1, when these sub-processes are completed, they will become zombie processes, and the init process on the first day will receive them as soon as possible.

/* ===================================================== ========================================= * Copyright (C) 2018. all rights reserved. ** file name: fork3.c * Creator: ghostwu (Wu Hua) * creation Date: July 15, January 12, 2018 * description: * ===================================================== =========================================*/# include <stdio. h> # include <stdlib. h> # include <unistd. h> int main (int argc, char * argv []) {pid_t pid; int I = 0; for (I = 0; I <10; I ++) {pid = fork (); if (pid <0) {perror ("fork ()"); exit (-1);} else if (pid = 0) {printf ("[pid] = % d \ n", getpid (); exit (0) ;}} sleep (10); return 0 ;}View Code

 

If the parent process ends before the child process, the child process will become an orphan process.

The parent process of all sub-processes is changed to the init process no. 1.

# Include <stdio. h> # include <stdlib. h> # include <unistd. h> int main (int argc, char * argv []) {pid_t pid; int I = 0; for (I = 0; I <10; I ++) {pid = fork (); if (pid <0) {perror ("fork ()"); exit (-1);} else if (pid = 0) {sleep (10); printf ("[pid] = % d \ n", getpid (); exit (0) ;}} return 0 ;}View Code

 

 

 

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.