Linux process Communication--pipelines

Source: Internet
Author: User

interprocess communication (Ipc:inner proceeding communication)

A process is a way for an operating system to implement an illusion of exclusive system execution. is an abstract representation of the processor, main memory, I/O devices. Each process is an independent resource management unit, and each process is shown to be an illusion of its own exclusive use of the system. Therefore, there is no direct access to the resources of the other process between the processes, and the information interaction between different processes requires the special process communication mechanism provided by the operating system.

Communication between processes. Physically, it can be divided into the communication between the process of the same host and the process between different hosts. From the way of communication content. Can be divided into data interaction, synchronous communication, asynchronous communication.

The communication between Linux system processes is, for example, seen


The combination of the above two types of physical and content-based partitioning can be understood as:

(1) Data interaction mechanism with host processes: Nameless pipe (pipe), well-known pipeline (FIFO), Message Queuing (Messages queue), and shared memory.

(2) synchronous communication mechanism with host process: signal volume (Semaphore).

(3) asynchronous communication mechanism with host process: Signal (Signal).

(4) The process data interaction mechanism between different hosts: socket (socket), Remote call RPC (remotes Procedure calls).


Pipeline communication

Pipelines can be divided into nameless pipes and Named Pipes , and the use of the two is different.

Nameless Pipes pipe: Used primarily for communication between affinity processes, the communication of nameless pipes is unidirectional. Can only be from one paragraph to another; The nameless pipe is temporary. After the communication, they will voluntarily disappear.

It is generally used to create a nameless pipeline and then create a child process that inherits the pipe file descriptor of the parent process, thus enabling communication between parent and child processes. Between unrelated pipelines, false assumptions are made to communicate using nameless pipes. It is necessary to use another file description of the narrative transfer mechanism.

Well-known pipeline FIFO: A well-known pipeline is a real special file, using a well-known pipeline can realize the interaction between the host and the arbitrary process.


Nameless pipes in the shell

Using a nameless pipe in the shell command line is a pipe-to-tube character "| "Implemented. The following shell command passes the output of the cat command through the pipeline to the input of the grep command.

watermark/2/text/ahr0cdovl2jsb2cuy3nkbi5uzxqvzglhb3jlbnhpyw5n/font/5a6l5l2t/fontsize/400/fill/i0jbqkfcma==/ Dissolve/70/gravity/southeast ">

The nameless pipe is a special kind of file, which means that you can operate the nameless pipe as the operation file, the nameless pipe in the kernel corresponding to a special memory space, this memory space is managed by the operating system, is not visible to the user, in the user space of the application only through the system to access it. in this memory space in a circular queue to temporarily store a process sent to another process of information, and after the completion of the communication will voluntarily release the corresponding space.


Nameless pipes in Linux C programming

1. Create a nameless pipe

To create a nameless pipe, you need to use the pipe (int _pipedes[2]) function, which is an integer array with two elements, assuming that the operation succeeds, this array will store the file description descriptor and the write end file descriptor of the nameless pipe reading, using these two read, Writing a descriptive descriptor, we are able to manipulate the read and write of nameless pipes as well as read and write files. If the pipe () function call fails, it returns-1.


2. Read and write nameless pipes

The following procedure is completed in a program where a process acts as both a read process and a write process.

#include <stdio.h> #include <unistd.h> #include <string.h>int main () {        int p[2];        Char *str = "HelloWorld";  String to be written to nameless pipe        char buf[128];//buffer memset when reading nameless pipe        (buf, ' 128 ');//Empty buffer        if (pipe (p) = = 1)        {                printf ("function pipe () calls failed.");                return-1;        }        Write (P[1],str,strlen (str));  Write nameless pipe        read (P[0],buf,strlen (str));  Read the Nameless pipe        printf ("%s\n", buf);//print Read the contents of        return 0;}

The following program simulates communication between parent-child processes that have affinity, and the child process sends the string STR to the parents process, following the first creation of the pipeline and then creating the child process, which will replicate the parent process's pipe file descriptor.

#include <stdio.h> #include <unistd.h> #include <string.h>int main () {        int p[2];        int pid;        Char *str = "HelloWorld";        Char buf[128];        memset (buf, ' + ');        if (pipe (p) = =-1)        {                printf ("function pipe () calls failed.");                return-1;        }        if ((Pid=fork ()) = =-1)  //Create a child process        {                printf ("function fork () calls failed.\n");                return-1;        }        else if (PID = = 0)  //In the sub-process        {                printf ("In sub:pid=%d\n", Getpid ());                Write (P[1],str,strlen (str)); Writes Str        }else {  //In the parent process to                printf ("In father:pid=%d\n", Getpid ())                to the nameless pipe; Read (P[0],buf,strlen (str));  Read the Nameless pipe                printf ("In father:buf=%s\n", buf);}        }

The output of the above program:



Note: Read and write methods that read and write nameless pipes are blocked by default when reading the pipe, but there is no data in the pipeline. There is no process to write data to the writing end of the time, the read operation will be blocked. In the same vein, write the data to the pipeline, but the pipeline is full (the pipeline is a special memory space file, the default size is pipe_buf, usually 4096 bytes). The write operation will be blocked. In addition assume. When the pipe is written, the read end is closed, then the write operation will receive the sigpipe signal, and the Write function returns-1.


Named pipes

A named pipe is a file that actually exists, and in the shell, we can create a named pipe using the Mknod command. Here's how to use Mknod. and using Mknod to create a p_file file, you can find the file type of this file is P


Named pipes in Linux C programming

1. Create a named pipe

In programming, you can use the Mkfifo (char *path, _mode_t _mode) function to create a named pipe with a mkfifo of two parameters. The first is to specify the name of the named pipe to be created. The second is the pattern of the generated named pipe file.


2. Read and write Named pipes

Like the nameless pipes. The essence of a named pipe is still a kernel-space-managed memory, but you need to use the Open function to open the named pipe file before using write and read

The following program is used to communicate between two unrelated processes using a named pipe

Write process source code such as the following

#include <stdio.h> #include <unistd.h> #include <string.h> #include <fcntl.h>int main () {int fd; int Res;char *str = "HelloWorld"; res = Mkfifo ("Myfifo", 0766), if (res! = 0) {printf ("function Nkfifo () calls failed."); return-1;} FD = open ("Myfifo", o_wronly); Create FIFO file named "Myfifo" if (fd = =-1) {printf ("function open () calls failed."); return-1;} printf ("This Proceeding ID =%d, FIFO file File id=%d\n", Getpid (), FD); res = write (Fd,str,strlen (str));  Write to FIFO file, this would be blocking if no readif (res = =-1) {printf ("function write () calls failed."); return-1;} printf ("Write ok.\n"); Close (FD); return 0;}

Read the source code for the pipeline program such as the following

#include <stdio.h> #include <unistd.h> #include <string.h> #include <fcntl.h>int main () {int fd; int Res;char buf[4096];memset (buf, ' n ', 4096); fd = open ("Myfifo", o_rdonly);  Open FIFO Fileif (fd = =-1) {printf ("function open () calls failed."); return-1;} printf ("This proceeding id=%d, fd=%d\n", Getpid (), FD); Read (fd,buf,sizeof (BUF)); Read FIFO into buffer, which is a blocking methodprintf ("read:buf=%s\n", buf); close (FD); return 0;}

The write process needs to be executed first, and because the pipeline file needs to be created, the write method will be blocked because there is no read associated to the pipeline. Continue to execute the read process at another terminal, the read process can read the data sent to the write process. And the blocking of the write process will be lifted.

watermark/2/text/ahr0cdovl2jsb2cuy3nkbi5uzxqvzglhb3jlbnhpyw5n/font/5a6l5l2t/fontsize/400/fill/i0jbqkfcma==/ Dissolve/70/gravity/southeast ">


Summarize:

Nameless pipes are primarily used for communication between parent-child processes that have affinity relationships. is temporary. You need to create a pipeline before you create a child process, and the pipelines are unidirectional. To achieve two-way communication, you need two pipelines.

Named Pipes are actual files that need to be opened before they are used, and the default read and write operations of the pipeline are blocked.



Linux process Communication--pipelines

Related Article

E-Commerce Solutions

Leverage the same tools powering the Alibaba Ecosystem

Learn more >

Apsara Conference 2019

The Rise of Data Intelligence, September 25th - 27th, Hangzhou, China

Learn more >

Alibaba Cloud Free Trial

Learn and experience the power of Alibaba Cloud with a free trial worth $300-1200 USD

Learn more >

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.