Process Communication between Linux instances
Prerequisites: fork () creates a process that is exactly the same as before. The execution of these two processes does not have a fixed sequence. Which process must be executed first depends on the system's process scheduling policy.
After a process calls the fork () function, the system allocates resources for new processes, such as space for storing data and code. Then, copy all values of the original process to the new process. Only a few values are different from those of the original process. It is equivalent to cloning a self.
// Fork () study example 1
# Include <unistd. h> # include <stdio. h> int main () {pid_t fpid; // fpid indicates the value int count = 0 returned by the fork function; // fork stores the variable in two different memories, therefore, the value of count is 1 rather than 1 or 2. Fpid = fork (); if (fpid <0) printf ("error in fork! "); Else if (fpid = 0) {printf (" I am the child process, my process id is % d, n ", getpid ()); printf ("I am Dad's son \ n"); // Chinese looks more straightforward for some people. Count ++;} else {printf ("I am the parent process, my process id is % d \ n", getpid ()); printf ("My child dad \ n"); count ++;} printf ("statistical result: % d \ n", count); return 0 ;}
Running result:
Only one process executes the code before for, but after fork, another process is created to execute the code at the same time.
The program uses the fpid returned by fork to determine whether it is a child process, a parent process, or a process creation failure.
One of the wonders of fork calling is that it is called only once, but can return twice. It may have three different return values:
1) in the parent process, fork returns the process ID of the newly created child process; // It is equivalent to the parent process pointing to its own child process, and the child process has no child process to point.
2) in the sub-process, fork returns 0;
3) if an error occurs, fork returns a negative value;
There are two possible reasons for fork errors:
1) the current number of processes has reached the limit set by the system, and the errno value is set to EAGAIN.
2) If the system memory is insufficient, the errno value is set to ENOMEM.
# Include <unistd. h> # include <stdio. h> int main (void) {int I = 0; printf ("I son/pa ppid pid fpid \ n "); // ppid refers to the parent process pid of the current process // pid refers to the pid of the current process, // fpid refers to the value that fork returns to the current process for (I = 0; I <2; I ++) {pid_t fpid = fork (); if (fpid = 0) printf ("% d child % 4d % 4d % 4d \ n", I, getppid (), getpid (), fpid); else printf ("% d parent % 4d % 4d % 4d \ n", I, getppid (), getpid (), fpid);} return 0 ;}
1. When executing the first loop:
Pid = 5944 process, created a sub-process 5945
2. In the second loop:
5944 of processes wear sub-processes with pid = 5946
5945 of the processes as the parent process created a sub-process with pid = 5947
The parent process of p5947 should be 5945, but the 5945 process may have died. (I have not figured out the specific cause. For more information, see references)
Inter-process communication: pipelines and nameless Pipelines
1. Introduction to pipe 1.1 pipelines. the pipeline is half-duplex, and data can only flow in one direction. when both parties need to communicate, two pipelines B must be established. it can only be used between parent and child processes or brothers (kinship processes); C. an independent file system is formed: An MPs queue is a file for processes at both ends of the MPs queue, but it is not a common file. It does not belong to a file system, but a self-built portal, A file system exists only in memory. D. Data Reading and Writing: The content written by a process in the pipeline is read by the process at the other end of the pipeline. The written content is added at the end of the MPs queue buffer, and data is read from the buffer header each time. 1.2 create an MPS queue this article permanently updates the link address: