Inter-process communication IPC-Named Pipe FIFO

Source: Internet
Author: User

FIFO is also known as a named pipeline. unnamed pipelines can only be used between two related processes. The two related processes must have one of them to create their ancestor processes, but FIFO, data can also be exchanged between unrelated processes.

FIFO is a file type. The st_mode member code in the stat structure shows whether the file is of the FIFO type. in Linux, check the FIFO file you created:

Creating a FIFO is similar to creating a file and also exists in the file system. Definition:

#include <sys/stat.h>int mkfifo(const char* path, mode_t mode);int mkfifoat(int fd, const char* path, mode_t mode);

Return values of the two functions: If the return value is 0, the return value is-1 if the return value is 0. For details about how to use this function, see open function.

Write your own background FIFO Reader Program:

#include <stdio.h>#include <sys/stat.h>#include <errno.h>#include <fcntl.h>int main(int argc, char* argv[]){  int fd;  int nRead;  char szBuff[128];  const char* szPath = "/tmp/fifo";         //临时目录的一个fifo,可以在程序里创建也可以在shell里创建  fd = open(szPath, O_RDONLY, 0);  if (-1 == fd)    {      printf("open fifo error\n");      goto exit;    }  while(1)    {      if((nRead = read(fd, szBuff, sizeof(szBuff))) == -1)        {          if (errno == EAGAIN)            printf("no data\n");        }      if (szBuff[0] == ‘Q‘)        break;      szBuff[nRead] = ‘\0‘;      printf("data:%s\n", szBuff);      sleep(1);    }exit:  return 0;}

After cc fifo. C is compiled successfully, A. Out is obtained. at the command prompt, enter:

$ ./a.out &[1] 4768          //这里是进程ID回现

Run the. Out Program as a background process.

Create a FIFO in the terminal (or in the program ):

$ mkfifo /tmp/fifo$ ls -ln /tmp/fifo prw-rw-r-- 1 1001 1001 0 10月  9 22:04 /tmp/fifo

We use the tee cashback program that comes with Linux to communicate with A. Out.

$ tee /tmp/fifo    //标准输出到fifohello fifo!                            //    这里是我输入的hello fifo!                            //    这里是tee回现功能data:hello fifo!                    //    这里是a.out回应qqdata:q                                //    这里是a.out回应QQhello fifo?hello fifo?[1]+  完成                  ./a.out

So far, the communication between process A. Out and process tee has been completed.

inter-process communication IPC-Named Pipe FIFO

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.