About Linux IPC (vi): Pipe and FIFO

Source: Internet
Author: User
Tags strlen

The earliest IPC form on UNIX systems is pipeline, and pipeline creation uses the pipe function:
    1. #include <unistd.h>
    2. int pipe (int pipefd[2]);

The function creates a one-way pipeline that returns two descriptors pipefd[0], and pipefd[1],pipefd[0] for read operations, Pipefd[1] for write operations. This function generally applies communication between a parent-child process (a relational process), first a process creates a pipeline, then a subprocess, and the parent-child process can communicate through the pipeline.
Piping has the following characteristics:
The pipe is half-duplex, the data can only flow in one direction, when two sides need to communicate, need to establish two pipelines;
Can only be used between parent-child processes or sibling processes (affinity processes);
Separate form a separate file system: A pipe is a file for the process at both ends of the pipe, but it is not an ordinary file, it does not belong to a file system, but is a separate file system, and only exists in memory.
Read and write data: a process that writes to the pipeline is read out by the process at the other end of the pipeline. The content that is written is added to the end of the pipe buffer every time, and the data is read from the head of the buffer each time.


The function pipe is generally used in the following steps:
1.pipe creating pipelines;
2.fork creating sub-processes;
3. The parent-child process shuts down read and write (or write and read) descriptors, respectively;
4. Read the end on the read descriptor to start reading (or block on the read to wait for the writing end to write), write end to write, complete the parent-child process communication process.
A simple communication implementation (modified from the Linux Man Manual)
  1. #include <sys/wait.h>
  2. #include <stdio.h>
  3. #include <stdlib.h>
  4. #include <unistd.h>
  5. #include <string.h>
  6. #include <errno.h>
  7. int main (int argc, char *argv[])
  8. {
  9. int pipefd[2];
  10. pid_t Cpid;
  11. Char buf[128];
  12. int Readlen;
  13. if (argc! = 2) {
  14. fprintf (stderr, "Usage:%s <string>\n", argv[0]);
  15. return-1;
  16. }
  17. if (pipe (PIPEFD) < 0) {
  18. fprintf (stderr, "pipe:%s\n", Strerror (errno));
  19. return-1;
  20. }
  21. Cpid = fork ();
  22. if (Cpid < 0) {
  23. fprintf (stderr, "fork:%s\n", Strerror (errno));
  24. return-1;
  25. }
  26. if (0 = = cpid) {/ * child process * /
  27. Close (pipefd[1]); / * Child process Close Write end * /
  28. Readlen = Read (Pipefd[0], buf, 128); //child process blocking on read, waiting for parent process to write
  29. if (Readlen < 0) {
  30. fprintf (stderr, "read:%s\n", Strerror (errno));
  31. return-1;
  32. }
  33. Write (Stdout_fileno, buf, Readlen);
  34. Write (Stdout_fileno, "\ n", 1);
  35. Close (pipefd[0]); //Close read descriptor after reading
  36. return 0;
  37. } Else {/ * parent Process * /
  38. Close (pipefd[0]); / * The parent process closes the useless read-
  39. Sleep (2);
  40. Write (Pipefd[1], argv[1], strlen (argv[1])); //Parent process starts writing
  41. Close (pipefd[1]); / * Parent Process Close Write descriptor * /
  42. Wait (NULL); / * Parent process waits for child process to exit, recycle child process resource * /
  43. return 0;
  44. }
  45. }

The runtime prints the command-line input parameters, and after the parent process sleeps for 2 seconds, the child process is blocked from reading until the parent process finishes writing the data, the visible pipeline has a synchronization mechanism and does not need to add the synchronization mechanism itself. If you want two processes to transmit data in two directions, you need to build two pipelines to implement them.
The biggest disadvantage of a pipeline is that it can only communicate between processes that have a common ancestor process, and there is no way to use it between two unrelated processes, but a well-known pipeline FIFO solves this problem. FIFO is similar to pipe, but also only one-way transmission of data, but unlike the pipe, he can communicate between unrelated processes, it provides a path associated with it, so long as the process can access the path can establish communication, similar to the previous shared memory, provides a path associated with it.
    1. #include <sys/types.h>
    2. #include <sys/stat.h>
    3. int Mkfifo (const char *pathname, mode_t mode);
Pathname is the system pathname and mode is the file permission bit, similar to the second parameter of the Open function.
Opening or creating a new FIFO is called Mkfifo First, and when the specified pathname already exists, MKFIFO returns a eexist error, and the Open function is called again.
The following is the use of MKFIFO to achieve two-way communication between unrelated processes, where two FIFO is required to be used for both reading and writing. The service process loops the read and waits for the client process to write, then prints the data from the client process and returns the data to the client process; The client process writes data to the server and waits for the data returned by the service process to be read.
Server process:
  1. #include <stdio.h>
  2. #include <string.h>
  3. #include <errno.h>
  4. #include <sys/stat.h>
  5. #include <sys/types.h>
  6. #include <fcntl.h>
  7. #include "slnipc.h"
  8. int main (int argc, const Char *argv[])
  9. {
  10. int RC;
  11. int wr_fd, RD_FD;
  12. Char sendbuf[128];
  13. Char recvbuf[128];
  14. rc = Mkfifo (Sln_ipc_2ser_path, O_creat | O_EXCL); //Set up FIFO for service process read
  15. if (RC < 0) && (errno! = eexist)) {
  16. fprintf (stderr, "Mkfifo:%s\n", Strerror (errno));
  17. return-1;
  18. }
  19. rc = Mkfifo (Sln_ipc_2clt_path, O_creat | O_EXCL); //Establish FIFO for service process write
  20. if (RC < 0) && (errno! = eexist)) {
  21. fprintf (stderr, "Mkfifo:%s\n", Strerror (errno));
  22. return-1;
  23. }
  24. WR_FD = open (Sln_ipc_2clt_path, O_RDWR, 0);
  25. if (wr_fd < 0) {
  26. fprintf (stderr, "Open:%s\n", Strerror (errno));
  27. return-1;
  28. }
  29. RD_FD = open (Sln_ipc_2ser_path, O_RDWR, 0);
  30. if (rd_fd < 0) {
  31. fprintf (stderr, "Open:%s\n", Strerror (errno));
  32. return-1;
  33. }
  34. for (;;) {  
  35. rc = Read (RD_FD, recvbuf, sizeof (RECVBUF)); //Loop waiting for customer process data to be accepted
  36. if (RC < 0) {
  37. fprintf (stderr, "read:%s\n", Strerror (errno));
  38. continue;
  39. }
  40. printf ("Server recv:%s\n", recvbuf);
  41. snprintf (SendBuf, sizeof (SENDBUF), "Hello, this is server!\n");
  42. rc = Write (WR_FD, SendBuf, strlen (SENDBUF));
  43. if (RC < 0) {
  44. fprintf (stderr, "write:%s\n", Strerror (errno));
  45. continue;
  46. }
  47. }
  48. Close (WR_FD);
  49. Close (RD_FD);
  50. return 0;
  51. }

Client process
  1. #include <stdio.h>
  2. #include <sys/stat.h>
  3. #include <sys/types.h>
  4. #include <fcntl.h>
  5. #include <string.h>
  6. #include <errno.h>
  7. #include "slnipc.h"
  8. int main (int argc, const Char *argv[])
  9. {
  10. int RC;
  11. int rd_fd, WR_FD;
  12. Char recvbuf[128];
  13. Char sendbuf[128];
  14. if (argc! = 2) {
  15. fprintf (stderr, "Usage:%s <string>\n", argv[0]);
  16. return-1;
  17. }
  18. snprintf (SendBuf, sizeof (SENDBUF), "%s", argv[1]);
  19. WR_FD = open (Sln_ipc_2ser_path, O_RDWR, 0);
  20. if (wr_fd < 0) {
  21. fprintf (stderr, "Open:%s\n", Strerror (errno));
  22. return-1;
  23. }
  24. RD_FD = open (Sln_ipc_2clt_path, O_RDWR, 0);
  25. if (rd_fd < 0) {
  26. fprintf (stderr, "Open:%s\n", Strerror (errno));
  27. return-1;
  28. }
  29. rc = Write (WR_FD, SendBuf, strlen (SENDBUF));
  30. if (RC < 0) {
  31. fprintf (stderr, "write:%s\n", Strerror (errno));
  32. return-1;
  33. }
  34. rc = Read (RD_FD, recvbuf, sizeof (RECVBUF));
  35. if (RC < 0) {
  36. fprintf (stderr, "write:%s\n", Strerror (errno));
  37. return-1;
  38. }
  39. printf ("client read:%s\n", recvbuf);
  40. Close (WR_FD);
  41. Close (RD_FD);
  42. return 0;
  43. }

The server starts running and then runs the client, running the results
    1. #./server
    2. Server Recv:hi, this isFIFO client
    3. #./client "Hi,this is FIFO client"
    4. Client Read:hello, this is server!


Here are some similar to the socket implementation interprocess communication process, only the FIFO read-write descriptor is two, the socket read and write using the same descriptor. The emergence of FIFO overcomes the communication of pipelines only between the processes that are related to each other. and other inter-process communication has been, FIFO transmission of data is also a byte stream, you need to define the protocol format to resolve the communication data, you can use the socket section described in the way to achieve the communication protocol.

Download the source code in this section:

http://download.csdn.net/detail/gentleliu/8183027
    • Related articles recommended:
    • About Linux IPC (i): socket-based interprocess communication (I.)
    • About Linux IPC (ii): socket-based interprocess communication (bottom)
    • About Linux IPC (iii): MMAP system calls shared memory
    • This article from: Hobby Linux Technology Network
    • This article link: http://www.ahlinux.com/c/9591.html

About Linux IPC (vi): Pipe and 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.