Linux inter-process communication (IPC) 2-Named Pipe (FIFO)

Source: Internet
Author: User
1. Named Pipe (FIFO)

The pipeline is described in the previous article "one of Linux processes communication (IPC)-Pipeline". However, a major defect in pipeline application is that there is no name, so it can only be used for communication between kinship processes. Later, the concept of named pipeline (named pipe, FIFO) was proposed based on the pipeline, and the restriction was overcome. FIFO is different from pipelines in that it provides a path name associated with it and exists in the file system as a FIFO file. In this way, even if there is no kinship with the FIFO creation process, as long as the path can be accessed, it can communicate with each other through FIFO (the process that can access this path and the process that creates the FIFO). Therefore, data can be exchanged through non-FIFO processes. It is worth noting that the first in first out (FIFO) is strictly followed, and data is always returned from the beginning for the read of the pipeline and the first in first out (FIFO, write to them to add the data to the end. They do not support file location operations such as lseek.

2. Create a named pipe
#include <sys/types.h>#include <sys/stat.h>int mkfifo(const char *pathname, mode_t mode);
This function is used to create a first-in-first-out (FIFO) file. The file path is specified by the pathname parameter; the second parameter mode is the same as the mode in the System Call OPEN function. If the file in the pathname path already exists, mkfifo returns-1 and errono returns eexist. 3. Name MPs queue operations

FIFO is a file in the file system. Most system file calls can be used in FIFO, such as read, open, write, close, unlink, stat, and other functions. However, functions such as seek cannot be called for FIFO.

You can call open to open FIFO. Note the following:

1. When a read-only FIFO is enabled in the blocking (o_nonblock is not specified) mode, the FIFO is blocked. Other processes can open the FIFO in write mode.

2. Similarly, when a read-only FIFO is enabled in the blocking (o_nonblock is not specified) mode, the read-only FIFO is blocked.

3. When the read-only FIFO is enabled in non-blocking mode (o_nonblock is specified),-1 is returned immediately, and its errno is enxio.

As mentioned in "one of Linux inter-process communication (IPC)-Pipeline", if the data volume written to the pipeline is less than or equal to pipe_buf, the system ensures that write is an atomic operation, and multiple processes write to the pipeline at the same time, there will be no interspersed data. If the write data volume is greater than pipe_buf, the system will not guarantee that write is an atomic operation. If multiple processes write pipelines at the same time, the data may be interspersed and written. This rule continues to apply in the named pipeline. 4. Application Instances

This example is a client-server mode. The server creates a well-known FIFO file ("/tmp/Server" in this example) and reads the FIFO file. The client opens the FIFO file and writes its request to the FIFO file. The server reads and executes the command.

The server code is:

#include <stdio.h>#include <errno.h>#include <stdlib.h>#include <sys/types.h>#include <sys/stat.h>#include <fcntl.h>#include <unistd.h>struct fifo_cmd{pid_t child_pid;char cmd[100];};int main(void){int fd;struct fifo_cmd cmd;int err;int n;if((err = mkfifo("/tmp/server", 0777)) < 0){if(errno != EEXIST){perror("mkfido fail: ");exit(-1);}}if((fd = open("/tmp/server", O_RDONLY)) < 0){perror("open fail: ");exit(-1);}while(1){if((n = read(fd, &cmd, sizeof(cmd))) < 0){perror("read fail: ");exit(-1);}if(n > 0){printf("command from %d: %s/n", cmd.child_pid, cmd.cmd);}sleep(1);}}

Compile this code into a server.

The client code is as follows:

#include <stdio.h>#include <errno.h>#include <stdlib.h>#include <sys/types.h>#include <sys/stat.h>#include <unistd.h>#include <fcntl.h>#include <string.h>struct fifo_cmd{pid_t pid;char cmd[100];};int main(int argc, char* argv[]){int fd;struct fifo_cmd cmd;if((fd = open("/tmp/server", O_WRONLY)) < 0){perror("open fail: ");exit(-1);}cmd.pid = getpid();while(1){printf("%%: ");fgets(cmd.cmd, sizeof(cmd.cmd), stdin);cmd.cmd[strlen(cmd.cmd) - 1] = 0;if(write(fd, &cmd, sizeof(cmd)) < 0){perror("write fail: ");exit(-1);}}}

Compile the client code into a client.

During the test, we can start multiple clients. We will see that commands of the client will be executed on the server.

5. Summary

Compared with pipelines, a FIFO file exists in the file system, which enables inter-process communication.

Related Article

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.