Process source language --- fork

1, Process source language
The process related API provided by the system completes multi process development.
fork process creation, exec process function overloading, wait process recycling, waitpid process recycling. (upgraded version of wait function)
2, fork
1. Fork originally means fork, which represents the process relationship with the shape of fork.

The parent process calls fork and tells the kernel to help me create a process.
2. fork function
The core functions of fork:
1)_ Creat (process creation) is just an empty shell
2)_ Clone (clone, process initialization) copies the resource inheritance of the parent process to the child process

pid_t pid;  //Process pid type, user stored process id
pid_t fork(void);  //The fork function can create a child process

1) The fork function call returns twice at a time
2) Returns the PID of the child process in the parent process (PID > 0)
3) Return 0 in child process (PID = = 0)
4) Failure returned - 1
It doesn't matter if you don't understand these four sentences. You will understand them later.

#include<stdio.h>
#include<unistd.h>
int main()
{
	pid_t pid;
	pid = fork();
	printf("process starting ... \n");
	while(1)
		sleep(1); //sleep under Linux, unit: s	
	return 0;
}



(ps aux view system process information)
3. By default, the work and data of the parent process will be inherited to the child process, which can be used directly. Partial copy of kernel space, one copy of which can be inherited to the child process, and the others can be regenerated. Full copy of user space. The child process copies the resources of the parent process, and the modification of resources by the child process has nothing to do with the parent process.
That will cause a problem. The code segment will be completely copied to the child process in the user area, which means that the fork code will also be copied to the child process, which will always create the process? Parents create children and children create grandchildren... But in fact, we found that this situation did not occur through the program we just executed.
On the code execution of parent-child processes: the parent process executes downward from the starting position to the end, and the child process executes from fork to the end. (it can be understood in this way for the time being, but it is actually inaccurate. We will explain it in detail below)
4. Distinguish parent-child process workspace
Let each member in the parent-child process model have independent tasks: the FORK function call returns twice at a time, and the parent-child process can be distinguished by the return value.

#include<stdio.h>
#include<unistd.h>
#include<stdlib.h>

int main()
{
	pid_t pid;
	pid = fork();
	if(pid > 0){
		printf("parent process running ..\n");
                while(1)
                       sleep(1);
	}else if(pid == 0){
		printf("child process running..\n");
		while(1)
                       sleep(1);
	}else{
		perror("fork call fialed");
		exit(0);
	}
	return 0;
	
}


5. fork is executed by parent-child processes, and each process executes a part.
In fact, both the parent process and the child process will execute the fork statement, but the contents of the fork statement are different. Under linux, normal programs will return 0 at the end. We mentioned above that the fork function consists of two main modules. First_ CREATE, CREATE a new process, just an empty shell, through the second process_ After CLONE has resources, it will return the pid of the process, that is, the parent process will call fork and get the result after these two modules_ The pid returned by CLONE is the pid of the child process, and the child process calls fork without going through the above two parts, because the above two parts are not under his control, and it has nothing to go. Return 0 directly to get the return value of 0. Now look at the sentence that the fork function is called once but returned twice.
6. One parent multi child process model (multi process concurrency model)

#include<stdio.h>
#include<unistd.h>
#include<stdlib.h>
int main()
{   
	int i;
	pid_t pid;
	for(i = 0;i < 10;i++){
		pid = fork();
		if(pid == 0)
			break;
	}
	if(pid > 0){
		printf("parent process running ...\n");
		while(1)
			sleep(1);
	}else if(pid == 0){
		printf("child code [%d] process running ...\n",i);
		while(1)
			sleep(1);
	}else{
		perror("fork call failed");
		exit(0);
	}
	return 0;
}

Let's analyze the code of the for loop, because there may be nothing difficult to understand except here. Our original intention is to let a parent process create 10 child processes. When i is equal to 0, a child process No. 0 is created. The inherited i of the child process No. 0 is also equal to 0. It will not go out in the for loop, but will continue to go through the for loop, That is, the child process 0 will fork out the child process. When i is equal to 1, the parent process will create a child process 1. The inherited i of child process 1 is equal to 1. In the for loop, the child process will also fork out the child process, and the child process of the child process will continue to create the child process according to the inherited i value... Therefore, We don't want the child process created by the parent process to execute the for loop. We need to add judgment to find that it is a child process and end the loop directly.

It is normal that the output sequence is not in accordance with 0 ~ 9.

Count the parent process, a total of 11.

Keywords: Linux fork

Added by BioBob on Sun, 16 Jan 2022 08:18:54 +0200