Linux process communication (SystemV) Section 1 ------> Pipeline pipe
Source: Internet
Author: User
Introduction: I. # include & lt; unistd. h & gt; intpipe (intfd [2]); //! & Gt; note that the parameter fd [0] is the read file descriptor, and fd [1] is the file descriptor used for writing. it is generally used for communication between parent and child processes! Because pipe is unsigned, it can only work in one process !... Introduction: I. # include Int pipe (int fd [2]); //!> Note that the fd [0] parameter is the read file descriptor, and fd [1] is the file descriptor used for writing. it is generally used for communication between parent and child processes! Because pipe is unsigned, it can only work in one process! "One-way pipe": after the parent process has created pipe, it also creates a sub-process through fork (). then the parent process can close its own "read process ", because the parent process writes data to the child process, there is no need to "read", and then the child process closes its own "write", thus forming a "one-way" pipe. "Two-way pie": (it can be used for CS) to create two pipe, which is equivalent to adding a "one-way" pipe II. about the return value of the fork function for the fork: for the parent process, the ID number of the child process is returned (it must be greater than 0 ), therefore, if (pid = fork ()> 0) and if (pid = 0) can be used to determine whether the sub-process is executing or not. for pipe, after an OK is created, both the child process and the parent process have a read and write interface for this pipeline (pipe! The operation value is the same for fd [0] and fd [1] for test. for example, for multi-process programming. in c code, there should be "sub-processes" and "parent processes" that can be executed! In other words, after fork, the program is divided into two parts: the main process and sub-process. We can test: if it is printf ("on"); then we can output on twice, which is output by the parent process and sub-process respectively, however, if it is printf ("on \ n"); then it is output only once. Cause: The mechanism of printf is that "\ n" is output directly. if no, it is stored in the cache and then output together. Therefore, if "\ n" exists, the parent process outputs the data first. if the space of the parent process is empty, the cached data in printf is not saved !!! So the sub-process is not inherited, so it will not output !!! That is to say, the printf space cache area of the parent process is also inherited !!!!!!!!!!!!!!!!!!!!! Getpid (): get the ID of the current process getppid (): get the ID of the parent process 4. Reference Code: //!> //!> One-way pipe instance //!> # Include # Include # Include # Include Int g_num = 0; //!> Global variable: used to test the independent space of the parent and child processes. //!> We can know that global variables have their own independent space in sub-processes and parent processes! Int main () {int n, fd [2]; //!> Fd is the descriptor pid_tpid; //!> The ID charline [100] of the created process is saved; //!> Equivalent to a cache int num = 0; //!> It is also independent ~~~ Printf ("on"); //!> ATTENTION printf ("on \ n"); if (pipe (fd) <0) //!> Here, pipe is created, and 0 is returned if it succeeds. if it fails,-1 {exit (0);} if (pid = fork () <0) //!> Create a sub-process {exit (0);} else if (pid> 0) {close (fd [0]); write (fd [1], "I am your father... \ n ", 19); g_num ++; num ++; printf (" \ nFather g_num: % d num: % d \ n ", g_num, num ); printf ("\ nMYID: % d Parent ID: % d \ n", getpid (), getcpid ();} else //!> = 0 current process (that is, the created sub-process) {close (fd [1]); n = read (fd [0], line, 100 ); write (STDOUT_FILENO, line, n); // g_num ++; num ++; printf ("\ nChild g_num: % d num: % d \ n", g_num, num); printf ("\ nMYID: % d Parent ID: % d \ n", getpid (), getppid ();} printf ("OK"); return0 ;} //!> //!> Bidirectional pipe instance //!> # Include # Include # Include # Include # Include Int main () {int fd_1 [2], fd_2 [2]; //!> Values, charcData [100], pid_tpid, and if (pipe (fd_1) <0) of bidirectional pipe. //!> Create the pipe_1 {printf ("\ n failed to create the first pipe! \ N "); exit (0);} if (pipe (fd_2) <0) //!> Create the pipe_2 {printf ("\ n failed to create the second pipe! \ N "); exit (0);} if (pid = fork () <0) //!> False to create a new process {printf ("\ n failed to create the process! \ N "); exit (0);} else if (pid = 0) //!> That is, the subprocess returned by fork... {//!> The child process also needs to send data to pipe close (fd_2 [0]); char str [30] = "I am your child! "; Write (fd_2 [1], str, strlen (str); //!> The second pipe is sent by the child process, and the parent process accepts data //!> The child process also needs to accept the data close (fd_1 [1]) of the parent process; int n = read (fd_1 [0], cData, 100); //!> The first pipe is sent by the parent process, and the child process accepts data write (STDOUT_FILENO, cData, n);} else //!> Fork returns the child process ID, which must be greater than 0. Therefore, the code of the parent process is executed here {//!> The parent process also needs to send data to pipe close (fd_1 [0]); char str [30] = "I am your father! "; Write (fd_1 [1], str, strlen (str); //!> The second pipe is sent by the child process, and the parent process accepts data //!> The parent process also needs to accept the data 'close (fd_2 [1]) of the parent process; int n = read (fd_2 [0], cData, 100); //!> The first pipe is that the parent process sends data, and the child process receives data write (STDOUT_FILENO, cData, n) ;}return0 ;}from the column in shanshanpt
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