7 minutes to learn how to use anonymous pipe() (complete code test attached)

Posted by casty on Fri, 04 Mar 2022 20:09:05 +0100

1, To create an anonymous pipe:

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

Function: create an anonymous pipeline for inter process communication;

Parameter: int pipefd[2] this array is an outgoing parameter;

pipefd[0] corresponds to the reading end of the pipeline;

pipefd[1] corresponds to the write end of the pipeline;

Return value:

Success 0;

Failed - 1;

Note: anonymous pipes can only be used in processes with common ancestors (parent and child processes, two brother processes, and relatives)

2, Command to view pipeline buffer size: ulimit -a

3, Function to view pipeline buffer size:

#include <unistd.h>
long fpathconf(int fd,int name);

The code is as follows:

#include <stdio.h>
#include <stdlib.h>
#include <unistd.h>
#include <sys/stat.h>
#include <sys/types.h>
int main()
{
    int pipefd[2];
    int ret=pipe(pipefd);
    long size=fpanthconf(pipefd[0],_PC_PIPE_BUF)//Memory dedicated to reading pipe
    printf("pipe size:%ld",size);
    return 0;
    
}

The operation results are as follows:

4096 bytes = 4K

4, Its features are:

1. A pipeline is a buffer maintained in kernel memory;

2. Pipelines have the characteristics of files: read and write operations. Anonymous pipelines have no file entities, while famous pipelines have file entities;

3. The data transmitted through the pipeline is in order, and the reading order is consistent with the writing order;

4. The data transmission direction in the pipeline is unidirectional, one end is used for writing and the other end is used for reading, Half duplex (simplex: it can only be transmitted in one direction, for example, the remote control sends signals to the TV, but the TV cannot send signals to the remote control; duplex: it can transmit signals in both directions at the same time, for example, when two people are talking on the phone, one does not affect the other party's speech, and both parties can receive the other party's information at the same time; half duplex: it can only be transmitted in one direction for a period of time, if the transmission in this direction is completed The other end will also be transmitted in reverse, such as walkie talkie);

5. Reading data from the pipeline is one-time. Once the data is read, the data will be discarded to free up space for storing new data;

6. Anonymous pipes can only be used in processes with common ancestors (parent and child processes, two sibling processes, relatives).

5, Data structure of pipeline: Ring queue.

6, Code implementation:

1. First, make a simple one-way transmission signal

  1 #include <unistd.h>
  2 #include <string.h>
  3 #include <sys/types.h>
  4 #include <sys/stat.h>
  5 #include <stdio.h>
  6 #include <stdlib.h>
  7 int main()
  8 {
  9         //Create pipe before fork
 10         int pipefd[2];//There are 0 and 1 inside
 11         int ret=pipe(pipefd);
 12         if(ret==-1)
 13         {
 14                 perror("pipe:");
 15                 exit(1);
 16         }
 17         pid_t pid=fork();
 18         if(pid>0)
 19         {
 20                 //Parent process
 21                 printf("i am parent process,pid:%d\n",getpid());
 25                 char buf[1024]={0};
 26                 while(1)
 27                 {
 28                     int len=read(pipefd[0],buf,sizeof(buf));//Read the data sent by the subprocess
 29                     printf("parent recv:%s,pid:%d\n",buf,getpid());
 34                 }
 35         }else if(pid==0)
 36         {       //Subprocess
 37                 printf("i am child process,pid:%d\n",getpid());
 38                 //Send data to parent process
 39                 char * str="hello,i am child";
 41                 while(1)
 42                 {
 43                 write(pipefd[1],str,strlen(str));
 44                 sleep(1);
 48                 }
 49         }
 52         return 0;
 53 }

The output result is:

2. Make a two-way transmission code

During two-way communication, it must be noted that the reading and writing order of the child process and the parent process is different, because the child process must send data to the parent process before it can receive the data sent by the parent process, so for the child process, write() and then read(). The parent process receives data first and then sends data to the child process. Therefore, for the parent process, read () and then write (), otherwise blocking will occur.

  1 #include <unistd.h>
  2 #include <string.h>
  3 #include <sys/types.h>
  4 #include <sys/stat.h>
  5 #include <stdio.h>
  6 #include <stdlib.h>
  7 int main()
  8 {
  9         //Create pipe before fork
 10         int pipefd[2];//There are 0 and 1 inside
 11         int ret=pipe(pipefd);
 12         if(ret==-1)
 13         {
 14                 perror("pipe:");
 15                 exit(1);
 16         }
 17         pid_t pid=fork();
 18         if(pid>0)
 19         {
 20                 //Parent process
 21                 printf("i am parent process,pid:%d\n",getpid());
 22
 23                 char *strs="i am parent process!";
 25                 char buf[1024]={0};
 26                 while(1)
 27                 {
 28                     int len=read(pipefd[0],buf,sizeof(buf));//Read data from child processes
 29                     printf("parent recv:%s,pid:%d\n",buf,getpid());
 30                    //Send data to child processes
 31                     write(pipefd[1],strs,strlen(strs));
 32                     sleep(1);
 34                 }
 35         }else if(pid==0)
 36         {       //Subprocess
 37                 printf("i am child process,pid:%d\n",getpid());
                 
 39                 char * str="hello,i am child";
 40                 char buff[1024]={0};
 41                 while(1)
 42                 {
 38                      //Send data to parent process
 43                     write(pipefd[1],str,strlen(str));
 44                     sleep(1);
 45                     //Read the data sent by the parent process
 46                     int fd=read(pipefd[0],buff,sizeof(buff));
 47                     printf("child recv:%s,pid:%d\n",buff,getpid());
 48                 }
 49         }
 52         return 0;
 53 }

The operation results are as follows:

If you think it looks good, give it a compliment, refill.

 

Topics: C++ Linux Operation & Maintenance vim