The naming pipeline for Linux process communication

Source: Internet
Author: User

The previous section learned the Nameless pipe, which learns named Pipes.

Two Named pipes
Nameless pipes can only be used to communicate between a parent-child process or sibling process, which brings trouble to the exchange of data between unrelated processes, and solving this problem is another pipeline communication for this SectionTo learning: Named pipes.
A named pipe is also known as a FIFO file, and the FIFO differs from the pipeline in that it provides a pathname associated with it and exists in the file system as a FIFO file. Thus, even processes that do not have affinity to the FIFO creation process, as long as they can access the path, can communicate with each other through the FIFO (the process that accesses the path and the creation process of the FIFO), so that processes that are not related to FIFO can also exchange data. It is important to note that FIFO strictly adheres to first-in, FIFO, which reads from the beginning of the pipeline, and is always returning data from the start, and writes the data to the end. They do not support file location operations such as Lseek ().

http://blog.csdn.net/xiaoliangsky/article/details/40121893

1 Mkfifo
int Mkfifo (const char *pathname, mode_t mode);
The Mafifo function creates a fifo,fifo that behaves as a file in the file system.
Pahtname file path
Mode is the same as mode in the system call open function.

return value
If the function call successfully returns non-1;
Function call failed return-1.

2 Named pipe operations
FIFO in the file system is represented as a file, most of the system file calls can be used in FIFO, such as: Read,open,write,close,unlink,stat functions. But a function such as seek cannot be called on a FIFO.

2.1 Opening a named pipe
You can call the open function to turn on the named pipe, but there are two points to note
1) The named pipe FIFO file cannot be opened in O_RDWR mode, otherwise its behavior is undefined, the pipeline is unidirectional and cannot be read and written at the same time;
2) is the path name of the FIFO that is passed to the open call, not the normal file

There are typically four ways to open a FIFO file:
Open (pathname, o_rdonly);//1 read-only, blocking mode
Open (Pathname, o_rdonly | O_nonblock);//2 read-only, non-blocking mode
Open (pathname, o_wronly);//3 write-only, blocking mode
Open (Pathname, o_wronly | O_nonblock);//write-only, non-blocking mode

Note the blocking mode open FIFO:
1) When the FIFO file is opened in blocking, read-only mode, it will block until other processes open the Access file in the write mode;
2) when the FIFO file is opened in block, write-only mode, it will block until the other process opens the file in read mode;
3) returns immediately when the FIFO is opened in a non-blocking mode (specified O_nonblock). When only the Open is written, if no process has opened the FIFO for read, then 1 is returned, and its errno is Enxio.
Here is an example of a named pipe:

Using Named pipes to implement communication between multiple processes, one server process is responsible for receiving messages from multiple cilent processes

SERVER.C Code:

#include <stdio.h> #include <string.h> #include <stdlib.h> #include <unistd.h> #include < limits.h> #include <fcntl.h> #include <sys/stat.h>typedef struct {pid_t     Child_pid;char      MESSAGE[PIPE_BUF+1];} Fifo_message;int Main () {int                     fd;const char             *fifoname;fifo_message            msgbuf;fifoname = "/tmp/serverfifo"; if (Access (fifoname, F_OK) = =-1) {if (Mkfifo (fifoname, 0666) = =-1) {perror ("Mkfifo error\n"); exit (-1);}} if (fd = open (Fifoname, o_rdonly)) = =-1)//Open in read-only, blocking mode {fprintf (stdout, "open%s failed\n", fifoname); exit (-1);} while (1) {if (read (FD, &msgbuf, sizeof (MSGBUF)) < 0) {close (FD);p error ("read error\n"); exit (-1);} fprintf (stdout, "message from Child:%d, message:%s\n", Msgbuf.child_pid, Msgbuf.message); sleep (1);}    return 0;}

CLIENT.C's Code:

#include <stdio.h> #include <stdlib.h> #include <limits.h> #include <unistd.h> #include < fcntl.h> #include <sys/stat.h>struct fifo_message{pid_t      child_pid;char       message[pipe_buf+1];}; int main () {int                     fd;const char             *fifoname;struct fifo_message     msgbuf;fifoname = "/tmp/serverfifo"; Access (fifoname, F_OK) = =-1) {perror ("Access error\n"); exit (-1);} if (fd = open (Fifoname, o_wronly)) < 0//Opens {perror ("open error\n") in write-only, blocking mode;} Msgbuf.child_pid = Getpid (); while (1) {printf ("Input the Message:"); if (fgets (msgbuf.message, sizeof (Msgbuf.message), stdin) = = NULL) {perror ("Fgets error or end\n"); Msgbuf.message[strlen (msgbuf.message)] = ' perror '; if (write (fd, &msgbuf, sizeof (msgbuf)) = =-1) ("Write error\n" ); Close (FD); exit (-1);}} Close (FD); return 0;}

Operation Result:



The naming pipeline for Linux process communication

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.