Process Communication between linux and linux

Source: Internet
Author: User

Process Communication between linux and linux

Prerequisites:Fork () creates a process that is exactly the same as before. The execution of these two processes is not in a fixed sequence. Which process is 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, all the values of the original process are

To the new process, only a small number is different from the original process value. 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.

FOne of the wonders of an ork call 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

References:

Http://blog.csdn.net/w616589292/article/details/50957456

Http://www.cnblogs.com/bastard/archive/2012/08/31/2664896.html#3311781

 

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.