Linux inter-process communication - use of the pipeline

In linux using interprocess communication (IPC) method is
1, the duct (pipe) and named pipe (the FIFO)
2, signal (Signal)
. 3, the message queue
4, the shared memory
5, the amount of signal
6, the socket
described below usage pipe:
pipe is unidirectional, and its output connected to the input of another process an FIFO process together, a process (write process) write data to the tail pipe, another process (read process ) read data header pipe.

When the data is read in the pipeline, the pipeline of this data will be deleted when the process reads empty duct obstruction occurs, the same way when the process will be blocked to fill the pipe while writing data.

Pipeline divided into two categories, unnamed pipes (pipe) and named pipe (FIFO), the former is used for communication between the child and the parent process, which is used for communication between different processes.
Created using unnamed conduit pipe is a function of pipe function prototype:
int pipe (int filedis [2]);
filedis is an array comprising two file descriptors, filedis [0] for reading, fileids [1] for writing.
After the pipeline is required to use closed, and shut down the pipeline just use the close function to close the two file descriptors.
When you create unnamed pipe, the pipe must be created before creating the nameless child process, so that the child will inherit the parent process piping.

  1 #include <unistd.h>
  2 #include <sys/wait.h>
  3 #include <sys/types.h>
  4 #include <errno.h>
  5 #include <stdio.h>
  6 #include <stdlib.h>
  7 #include <string.h>
  8 
  9 
 10 int main(int argc, char **argv)
 11 {
 12     int fd;
 13     char buf_r[100];
 14     int acc;
 15 
 16     memset(buf_r,0,sizeof(buf_r)); 
 17     if ((acc=access(FIFO,F_OK))==-1) {  //判断FIFO文件是否已创建
 18         if (mkfifo(FIFO,0777) < 0) {   //创建FIFO文件
 19             printf("fifo can not create\n");
 20             exit(1);
 21         }
 22     }
 23     printf("acc = %d\n",acc);
 24 
 25     printf("read byte......\n");  
 26     fd = open(FIFO,O_RDONLY | O_NONBLOCK,0); //打开FIFO文件
 27     printf("open\n");  //测试
 28     if (fd == -1) {
 29         printf("can not open fifo\n");
 30     }
 31     printf("while\n");
 32     while (1) {
 33         memset(buf_r,0,sizeof(buf_r));
 34         if (read(fd,buf_r,100) == 0) {   //读数据
 35             printf("no data\n");
 36         }
 37 
 38         printf("read %s from fifo\n",buf_r);
 39         sleep(2);
 40     }
 41     pause();
 42     return 0;
 43 }


write.c

  1 #include <sys/types.h>
  2 #include <sys/stat.h>
  3 #include <fcntl.h>
  4 #include <unistd.h>
  5 #include <stdio.h>
  6 #include <stdlib.h>
  7 #include <string.h>
  8 #define FIFO "/home/book/c/fifo/myfifo"
  9 
 10 int main(int argc ,char **argv)
 11 {
 12     int fd;
 13 
 14     fd = open(FIFO,O_WRONLY | O_NONBLOCK,0);
 15     if (fd == -1) {
 16         printf("can not open fifo\n");
 17         exit(1);
 18     }
 19     if (write(fd,"hello linux", 14)<0) {
 20         printf("write error\n");
 21         exit(1);
 22     }
 23     else {
 24         printf("write succse\n");
 25     }
 26 
 27     return 0;
 28 }

Broke two windows were run command function to read and write function

Here Insert Picture Description

Published 15 original articles · won praise 0 · Views 955

Guess you like

Origin blog.csdn.net/yohe12/article/details/103980470