Linux process Communication--pipelines

Source: Internet
Author: User

interprocess communication (Ipc:inner proceeding communication)

Process is the operating system implementation of the program exclusive system operation of the illusion, is the processor, main memory, I/O device abstract representation. Each process is an independent resource management unit, each process sees itself as an illusion of exclusive use of the system, so that each process is not able to directly access the resources of the other process, the information interaction between different processes requires the use of special process communication mechanism provided by the operating system.

The communication between processes, physically, can be divided into the communication between the processes of the host and the processes between the different hosts. From the way of communication content, it can be divided into data interaction, synchronous communication and asynchronous communication.

The mode of communication between Linux system processes is roughly as shown


The combination of the two types of physical and content partitioning can be understood as follows:

(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 also be divided into nameless pipes and Named Pipes , the use of which is not the same.

Nameless Pipes pipe: Mainly used for communication between affinity processes, nameless pipe communication is unidirectional, only from one paragraph to another; The nameless pipe is temporary and disappears automatically when the communication is complete. It is generally used to create a nameless pipe, and then create a child process, so that the child process inherits the parent process's pipe file descriptor, so as to achieve communication between parent-child processes, and between non-affinity pipelines, if you want to use the nameless pipe to communicate, you need to rely on a separate file descriptor delivery mechanism.

Well-known pipeline FIFO: A well-known pipeline is a real special file, using a well-known pipeline can be implemented with the host of arbitrary processes between the data interaction.


Nameless pipes in the shell

Using a nameless pipe in the shell command line is a pipe-to-tube character "| "implemented, such as the following Shell command, passes the output of the cat command through the pipeline to the input of the grep command.


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 corresponds 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 can only be used by the system to access it. in this memory space, a cyclic queue is temporarily stored for the information that a process sends to another process, and the corresponding space is automatically freed when the communication is complete.


Nameless pipes in Linux C programming

1. Create a nameless pipe

Creating a nameless pipe requires the use of the pipe (int _pipedes[2]) function, which is an integer array with two elements, and if successful, this array will store the file descriptor and the write end file descriptor of the nameless pipe read-only, using both read and write file descriptors, We can 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 program is done 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 a pipeline, but there is no data in the pipeline, and there is no process to write data to the writing end, the reading operation will be blocked; If you write data to the pipeline, But then the pipe is full (the pipeline is a special memory space file, the default size is pipe_buf, typically 4096 bytes), and the write operation will be blocked. In addition, if the read end is closed when the pipe is written, then the write operation will receive the sigpipe signal, and write function returns-1.


Named pipes

Named pipes are actually a kind of file, in the shell, we can use the Mknod command to create a named pipe, the following is the use of Mknod, and the use of Mknod created a p_file file, you can find that the file type 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, Mkfifo has two parameters, the first is the name of the named pipe to be created, and the second is the pattern of the generated named pipe file.


2. Read and write Named pipes

Like 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 is as follows

#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;}

The source code for the read pipeline program is as follows

#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 run first because the pipeline file needs to be created, and the Write method will be blocked because there is no read associated to the pipeline; The read process can read the data sent by the write process to the other terminal, and the read process will be unblocked.



Summarize:

Nameless pipes are primarily used for communication between parent-child processes that have affinity, are temporary, need to create pipelines first, then create child processes, pipelines are unidirectional, and for two-way communication, two pipelines are required. Named Pipes are actual files that need to be opened before they are used, and pipeline default read and write operations are blocked.



Linux process Communication--pipelines

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.