Getting started with the system call function fork()

First understand the process concept

1. Process: 

A process is a running activity of a program with independent functions on a certain set of data. It can apply for and own system resources. It is a dynamic concept and an active entity. It is not only the code of the program , but also the current activity, which is represented by the value of the program counter and the content of the processing register . [From  Baidu Encyclopedia ]

 

The main function of the fork() function is to create a child process based on the call of the parent process. The child process is a relative concept. It will copy the data code and other parts of the parent process. It is a complete copy instead of sharing the same variables, which is equivalent to cloning. One feature of the fork() function is that it is only called once but returns twice. Once the value returned by the parent process is a number greater than 0 (that is, the PID of the child process he created, PID is the unique identifier of the process in the operating system), The other time, the value returned by the child process is 0. There is also a case that if the parent process calls this function, if it returns a number <0, it means that the process creation failed (there are many reasons for the failure). Also noteworthy is the call to fork () function when the child process is followed by the code after the call continues, if there is subsequent call to fork () function sub-process can also create its children as a parent.

 

For the specific process, let's look at the following code:

   

#include <stdio.h>
#include <unistd.h>
#include <sys/types.h>
#include <stdlib.h>

int main(int argc, char *argv[])
{
	pid_t pid; 
	int i = 0;
	 
   	for(i =0 ; i < 2 ; i++)
      	{
	   pid = fork();
	 
	   if (pid == 0) {
		printf("PID: %d\nreturned: %d\n",
		getpid(), pid);
	    } else {
		printf("PID: %d\nreturned: %d\n",
		getpid(), pid);
	    }
	    
      	}
	 return 0;
}

\\代码我在 VS2017 上运行不了 找不到 unistd.h 该头文件的意思是 unix stdio 的意思 . (没有解决 哈哈 )所以我直接在 linux系统上编译执行的 . 

The result of the operation is probably like this

1. First, I compile the source file forkT.c through gcc and command it as FT, and then execute the FT file to get the printed result.

2. The first printed PID 4807 and return 4808 are a child process (PID = 4808) created by the parent process (PID = 4807) by calling the fork() function

3. The second printed PID 4808 and return 0 are the variable pid copied by the child process (PID = 4808), and the variable pid is assigned to 0. As we mentioned earlier, a feature of the fork() function is that the parent process is called once and returns Two times, once is the pid of the child process created by the parent process, and the other time is the return value of the child process is 0, which is directly assigned to the pid in the child process.

4. The PID 4807 and return 4809 printed for the third time indicate that a parent process (PID 4807) enters the for loop and the second loop i=1. At this time, the fork() function is called again and another new process (PID = 4809).

5. The PID 4808 and return 4810 printed for the fourth time indicate that the child process (PID 4808) of the process (PID 4807) enters the second loop i =1 of the for loop. At this time, the fork() function is called to create a new process (PID 4810). At this time, the process (PID 4808) is both a child process and a parent process.

6. The PID 4809 and return 0 printed for the fifth time are the results of the execution of another child process (PID 4809) created by the process (PID 4807). At this time, the process (PID 4809) is in the second loop of the for loop. Where i=1 and pid=0.

7. The PID 4810 and return 0 printed for the sixth time are child processes of the process (PID 4808).

 

If you look a little awkward, let's take a look at the picture below, and maybe you will understand.

Guess you like

Origin blog.csdn.net/weixin_41237676/article/details/98069136