In the Linux operating system, each process has its own running space, the space is stored with data and execution code, then the different processes between each other is the exchange of data and information? Linux provides one of the most basic mechanisms for interprocess communication (Ipc-inter process communication)-pipelines.
In the Linux system all files, pipeline is a special kind of file, it is in the kernel opened a buffer, the buffer size is often fixed, Ubuntu 65536 is 2^16 bytes is 32K. It is worth proposing that the pipeline communication is unidirectional , it provides a read end and a write end, with a function pipe to create, its return value call successfully returned 0, if the failure returned-1:
650) this.width=650; "src=" Http://s3.51cto.com/wyfs02/M02/7E/D1/wKioL1cJvSWgmpsTAAARVlInjY8002.png "title=" QQ picture 20160410104348.png "alt=" Wkiol1cjvswgmpstaaarvlinjy8002.png "/>
Once the pipeline has been opened, its mechanism for interprocess communication can be as follows:
650) this.width=650; "src=" Http://s2.51cto.com/wyfs02/M02/7E/D1/wKioL1cJwxHzXSP2AAANu7cgMM4892.png "title=" QQ picture 20160410111259.png "alt=" Wkiol1cjwxhzxsp2aaanu7cgmm4892.png "/>
First, a parent process pipe out a pipeline and get its read and write end of the file descriptor;
Second, when the parent process fork out a child process, the child process gets two file descriptors from the parent process pointing to the same pipe
The parent process closes the write end, the child process closes the read end, and the child process writes the data to the pipeline, which the parent process can read from, thus enabling interprocess communication.
Demo Code:
650) this.width=650; "src=" Http://s4.51cto.com/wyfs02/M01/7E/D5/wKiom1cJzsexoNl3AAAWHyYy_qY807.png "style=" float: none; "title=" QQ picture 20160410120428.png "alt=" Wkiom1cjzsexonl3aaawhyyy_qy807.png "/>
650) this.width=650; "src=" Http://s1.51cto.com/wyfs02/M01/7E/D2/wKioL1cJ5LChhWwjAACJVhRp7mw864.png "title=" QQ picture 20160410133616.png "alt=" Wkiol1cj5lchhwwjaacjvhrp7mw864.png "/>
The code sub-process writes data to the pipeline every 1 seconds, and the parent process reads and outputs from the read side of the pipeline, and the entire code is output only by the parent process, and the child processes are written, thus enabling interprocess communication, but there are four things to note when using pipelines:
When the write end of the pipeline is closed, that is, the reference count of the write end is 0, and the read end is reading the data from the pipeline, when all the data in the pipeline is read, read again will return 0, as if the end of the file is read. Example: Change the sub-process in the above program to the following:
650) this.width=650; "src=" Http://s2.51cto.com/wyfs02/M02/7E/D2/wKioL1cJ4bqCSNJ8AAAXywnF09U272.png "title=" QQ picture 20160410132338.png "alt=" Wkiol1cj4bqcsnj8aaaxywnf09u272.png "/>
When Count is less than 5 o'clock to close the write end of the child process, the result is:
650) this.width=650; "src=" Http://s4.51cto.com/wyfs02/M00/7E/D3/wKioL1cJ7gWRn_8tAAAcMY82NR0620.png "title=" QQ picture 20160410132208.png "alt=" Wkiol1cj7gwrn_8taaacmy82nr0620.png "/>
2. When the write end of the pipeline does not close, but also does not write data to the pipeline, there is a process to read the data from within the pipeline, when the process will read the data inside the pipeline, read again will block until there is data written to read again. The previous example of the neutron process is changed to read as follows:
650) this.width=650; "src=" Http://s2.51cto.com/wyfs02/M02/7E/D3/wKioL1cJ723TwkIpAAAa7vgFhMA914.png "title=" QQ picture 20160410142151.png "alt=" Wkiol1cj723twkipaaaa7vgfhma914.png "/>
When Count equals 5 o'clock, the parent process has read to 5 data, then does not close the child process's write end, but lets the child process sleep on 5 seconds, then the parent process reads the data from the pipeline to block, 5 seconds after the child process again writes the data in the pipeline, the parent process can obtain the data from the pipeline normally.
3. When a file descriptor that points to a pipe read end is closed, that is, the read-side reference count is 0, but there is still a process writing data to the pipeline, the process receives a sigpipe signal, which usually causes the process to terminate unexpectedly. Change the parent process in the previous example to the following:
650) this.width=650; "src=" Http://s5.51cto.com/wyfs02/M00/7E/D6/wKiom1cKASHSELtQAAAo1g0HcKI712.png "style=" float: none; "title=" QQ picture 20160410154007.png "alt=" Wkiom1ckashseltqaaao1g0hcki712.png "/>
When Count is less than 5 o'clock in the parent process, the read end of the pipeline is closed, and the child process still writes data to the pipeline, calling the Waitpid function within the parent process to get the child process exit code, because the parameter status is an integral type, and its low eight bits are the status codes when the child process exits abnormally. The second eighth bit is the normal termination of the status code, with status bitwise AND 0XFF can get its low eight bits, run the program can get the following results:
650) this.width=650; "src=" Http://s5.51cto.com/wyfs02/M00/7E/D3/wKioL1cKAdPSDzSDAAAdGb9OPIg817.png "style=" float: none; "title=" QQ picture 20160410153938.png "alt=" Wkiol1ckadpsdzsdaaadgb9opig817.png "/>
The child process abnormally terminates, its exit status code is 13, its representative is sigpipe.
4. When the read end of the pipeline is not closed, but no process is reading the data from the pipeline, there is a continuous process of writing data to the pipeline, and when the pipeline is full, the write is blocked again until there is a space in the read pipeline. Change the parent-child process in the example above to read as follows:
650) this.width=650; "src=" Http://s4.51cto.com/wyfs02/M00/7E/D4/wKioL1cKGxvADdK6AAAtwcchkG8802.png "style=" float: none; "title=" QQ picture 20160410172524.png "alt=" Wkiol1ckgxvaddk6aaatwcchkg8802.png "/>
To change the child process to a dead loop, record the number of writes, write 1 bytes to the pipe at a time, and let the parent process read not read the data, run the program results as follows:
650) this.width=650; "src=" Http://s2.51cto.com/wyfs02/M02/7E/D8/wKiom1cKGnLyU2-TAAALO80x014405.png "style=" float: none; "title=" QQ picture 20160410172701.png "alt=" Wkiom1ckgnlyu2-taaalo80x014405.png "/>
When Count is added to 65535, it indicates that the pipeline is no longer written, waits for the read end to read data from the pipeline, and then continues writing, so you know that the pipe size is 65,536 bytes, which is 32K.
In the various analyses above, although the data exchange between processes is realized, it can be noted that this communication mechanism is only applicable between parent-child or blood-related processes, because it is necessary to inherit from the parent process the same file descriptor to read and write to the same pipe, although it can communicate, but inevitably there are some limitations.
To address this limitation, to allow different processes to access the same pipeline regardless of whether they are related to each other, there is another pipe ——— named pipe (FIFO). Named pipes are different from the anonymous pipeline described above, which is a file that exists in the system and can communicate with each other as long as the path to the file is accessible to any process. It is proposed here that FIFO always follows the first-in-first-out principle, that is, the first data written is always read out.
Create a named pipe with the MKFIFO function:
650) this.width=650; "src=" Http://s4.51cto.com/wyfs02/M00/7E/E5/wKioL1cMjfGA1rO4AAAIg5ujAbo659.png "style=" float: none; "title=" QQ picture 20160412140205.png "alt=" Wkiol1cmjfga1ro4aaaig5ujabo659.png "/>
In the function parameter, the first is the relative or absolute path of the file, indicating that a pipe file is created in the system, and the second parameter is the permission information for the file.
After creating a pipeline file, you can use it, because it is a file, so you can open it, open it in a program, if it is open in read-write mode will not block, if opened in a reading-only block until there is a write open this file, similarly, If you open it in write-only mode, it will block until you open the file in a read-only manner.
Examples of programs:
650) this.width=650; "src=" Http://s5.51cto.com/wyfs02/M01/7E/E8/wKiom1cMjT3hHX6NAAEmBoU1SIw167.png "style=" float: none; "title=" QQ picture 20160412140005.png "alt=" Wkiom1cmjt3hhx6naaembou1siw167.png "/>
Two files, client.c and Server.c, were created with the Mkfifo function in client.c to create a hidden file in the current directory. tmp, and then open the file as write-only, and write the data to the file with the Write function, and then open it again in the SERVER.C program. TM P file, because the file is already created in client.c, it can be opened as read-only, and the Read function gets the written data, so that communication between the two processes can be achieved without having to consider whether there is a relationship between the processes.
It is necessary to consider the impact of Umask when setting permissions on a pipeline file.
Run the client program, and then open a terminal to run the server, enter what you want to enter in the client terminal, you can see the server terminal received, so that two interprocess communication.
Finish
This article is from the "Knock Code good Sleep zzz" blog, please be sure to keep this source http://2627lounuo.blog.51cto.com/10696599/1762949
interprocess communication (IPC) ———— pipeline