Knowledge overview
Create a file in the kernel through the pipe, and then you can implement two process communication
Piping is the most basic IPC mechanism, created by the pipe function:
1 #include <unistd.h>2int pipe (int filedes[2]);
Call the pipe function in the kernel to open a buffer (called a pipe) for communication, it has a read end of a write end, and then passed through the Filedes parameter to the user program two file descriptors,
Filedes[0] point to the read end of the pipe, filedes[1] points to the write end of the pipe (very well, as if 0 is standard input 1 is the standard output). So the pipeline in the user program looks like an open file,
Via read (Filedes[0]); or write (filedes[1]); Reading and writing data to this file is actually reading and writing the kernel buffer. The pipe function call successfully returned 0, and the call failed to return-1.
How can communication between two processes be realized after the pipeline is opened? For example, you can follow the steps below to communicate.
1. The parent process calls pipe to open a pipeline and gets two file descriptors pointing at both ends of the pipeline.
2. The parent process calls fork to create a child process, and the child process also has two file descriptors pointing to the same pipe.
3. The parent process closes the pipe read end, and the child process closes the pipe write end. The parent process can write to the pipeline, and the child process can be read from the pipe,
The pipeline is implemented by a ring queue, and the data flows from the writing end to the reading end, thus realizing interprocess communication.
#include <stdlib.h>#include<unistd.h>#defineMAXLINE 80intMainvoid){ intN; intfd[2]; pid_t pid; CharLine[maxline]; if(Pipe (FD) <0) {perror ("Pipe"); Exit (1); } if(PID = fork ()) <0) {perror ("Fork"); Exit (1); } if(PID >0) {/*Parent*/Close (fd[0]); Write (fd[1],"Hello world\n", A); Wait (NULL); } Else{/* Child*/Close (fd[1]); N= Read (fd[0], line, MAXLINE); Write (Stdout_fileno, line, N); } return 0;}
Problem
The parent process only uses the write end, so the read end, the child process is only used to read the end, so the write end is closed, and then communicate with each other, do not use the read end or write end must be closed, readers think of if not close what will be the problem.
Thinking
1. If all file descriptors pointing to the pipe write end are closed (the reference count for the pipe write is equal to 0),
While there are still processes reading the data from the read-side of the pipeline, the remaining data in the pipeline is read,
Read again will return 0, just like the end of the file.
2. If there is a file descriptor pointing to the pipe write end is not closed (the reference count of the pipe write end is greater than 0),
And the process that holds the end of the pipeline does not write data to the pipeline, when the process reads the data from the pipe,
Then the remaining data in the pipeline is read, again read will block,
The data is read and returned until the data in the pipeline is readable.
Consider the following code
#include <stdio.h>#include<stdlib.h>#include<unistd.h>#include<string.h>intMainvoid){ intN; Charbuff[ -]; pid_t pid; intfd[2]; if(Pipe (FD) <0) {perror ("Pipe"); Exit (0); } if((Pid=fork ()) <0) {perror ("Fork"); Exit (0); } if(pid>0) { /*Parent*/printf ("+++++++++++++\n"); Close (fd[0]); Write (fd[1],"Hello World", One); Sleep (5); //write (fd[1], "I am a Student", +);printf"+++++++++++++\n"); } Else{printf ("--------------\ n"); //Close (fd[1]);memset (Buff,0, -); N= Read (fd[0],buff, -); printf ("buff=%s\n", Buff); memset (Buff,0, -); printf ("Read twice\n"); N= Read (fd[0],buff, -); printf ("buff=%s\n", Buff); printf ("--------------\ n"); } return 0;}
The parent process closes the read port and writes Hello world to the pipe through the write port. Then the parent process ends. Close related file (read-write) descriptor
When the child process closes the write port, the write file descriptor reference count is 0 when the parent process ends. So the child process returns 0 after reading again. The child process ends the exit.
When the child process does not close the write port, the parent process ends with a write file descriptor reference count of 1 (own not closed). So when the child process reads again, it falls into a blocking state.
Because the parent process is executed under the shell. So when the parent process ends, the shell process thinks the command execution is over, prints the shell prompt, and the child process waits for the input to be read.
The parent process is finished and does not enter data for him, and the child process itself is simply reading instead of writing the data to the pipe. So the child process has been running in the background, with the PS command to view the child process information.
Therefore, the sub-process is only used to read the end, so the write end is closed . Prevent the sub-process from doing a useless job ...
On the read-write end closure of pipe pipelines