Linux IPC Summary-pipeline

Source: Internet
Author: User

Pipeline

Pipelines are the oldest form of Unix IPC, a special file in memory that can only be used between processes that have a common ancestor (that is, parent-child processes, sibling processes).

Pipelines are created by the pipe function

#include <unistd.h>int pipe (int fd[2])

Fd[1] Write, fd[0] read.

The pipeline for a single process is almost useless, and the process that calls the pipe then calls Fork, creating a pipeline between the parent and child processes.

#include <unistd.h>#include<stdio.h>#include<sys/types.h>#include<sys/wait.h>intMain () {intfd[2]; Charbuf[ the];    pid_t pid;    Pipe (FD); PID=Fork (); if(pid>0)    {//Parent Processprintf"Father thread\n"); Chars[]="hello\n"; Write (fd[1],s,sizeof(s)); Close (fd[0]); Close (fd[1]); }    Else if(pid==0) {printf ("Child thread\n"); Read (fd[0],buf,sizeof(BUF)); printf ("%s\n", BUF); Close (fd[0]); Close (fd[1]); } waitpid (Pid,null,0);//wait for the child process to end    return 0;}

Output Result:

Father thread
Child Thread
Hello

When one end of the pipe is closed:

When reading a closed pipeline, it is considered that the end of the data has been read, the Read function returns the number of read bytes is 0;

When writing a pipe that reads closed, the process that writes data to the pipeline receives the sifpipe signal from the kernel, which the application can process or ignore (the default action is the application terminates).

To read data from the pipeline:

When the write side of the pipeline exists, if the number of bytes requested is greater than pipe_buf, the number of existing data bytes in the pipeline is returned, and if the requested number of bytes is not greater than pipe_buf, the number of existing data bytes in the pipeline is returned (in this case, the amount of data in the pipeline is less than the requested amount of data) or return the requested number of bytes (in this case, the amount of data in the pipeline is not less than the requested amount of data). Note: PIPE_BUF is defined in Include/linux/limits.h.

To write data to the pipeline:

When writing data to a pipeline, Linux does not guarantee the atomicity of writes, and the pipeline buffer has an idle area, and the write process attempts to write data to the pipeline. If the read process does not read the data in the pipeline buffer, the write operation will be blocked.

Pipelines can only be used for affinity processes because they do not have a name, and a well-known pipeline (FIFO) overcomes this limitation.

Fifo

Create the function as follows

#include <sys/types.h>int mkfifo (constchar *pathname, mode_t mode);

The first parameter is a generic pathname, which is the FIFO name. The second parameter sets permissions, just as you would create a normal file.

FIFO read and write like ordinary files, but need to read and write the end are open, the following specific rules:

When opening (open):

If the O_nonblock is not set, the read-only open is blocked to the other process opening FIFO for write. Similarly, write only open to block the FIFO from being opened for read by other processes.

If O_nonblock is set, read-only open returns immediately and returns 1 if no other process opens the FIFO for write.

Simulating producer consumer Problems with FIFO:

Fifo2.cpp:

#include <iostream>#include<unistd.h>#include<sys/types.h>#include<sys/stat.h>#include<limits.h>#include<stdio.h>#include<stdlib.h>#include<sys/types.h>#include<sys/stat.h>#include<fcntl.h>#defineFIFO "/tmp/myfifo"#defineBuf_size Pipe_buf#defineSend_max (1024*1024*10)using namespacestd;intMain () {intpid,fifo_fd; intSend_num; Char*buf[buf_size+1]; if(-1==access (FIFO,F_OK)) {        intres = Mkfifo (FIFO,0777); if(Res! =0) {fprintf (stderr,"can ' t create FIFO in%s", FIFO);        Exit (Exit_failure); }} fifo_fd=open (fifo,o_wronly); printf ("process%d open FIFO%d\r\n", Getpid (), FIFO_FD); if(FIFO_FD = =-1) exit (exit_failure); intRes;  while(send_num<Send_max) {Res=write (fifo_fd,buf,buf_size); if(res = =-1) {cout<<"Write FIFO error"<<Endl;        Exit (Exit_failure); } send_num+=Res; }    return 0;}

Fifo3.cpp

#include <iostream>#include<unistd.h>#include<sys/types.h>#include<sys/stat.h>#include<limits.h>#include<stdio.h>#include<stdlib.h>#include<sys/types.h>#include<sys/stat.h>#include<fcntl.h>#defineFIFO "/tmp/myfifo"#defineBuf_size Pipe_buf#defineSend_max (1024*1024*10)using namespacestd;intMain () {intfifo_fd; intRes; CharBuffer[buf_size+1]; intRead_num =0; FIFO_FD=open (fifo,o_rdonly); printf ("Process%d open FIFO%d\r\n", Getpid (), FIFO_FD); if(FIFO_FD = =-1) exit (exit_failure);  Do{res=read (fifo_fd,buffer,buf_size); Read_num+=Res; } while(res>0);    Close (FIFO_FD); return 0;}

The results are as follows:

Visible read process run 0.013s studied 10m of data, FIFO efficiency is very high.

Linux IPC Summary-pipeline

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.