Process Communication--a well-known pipeline

Source: Internet
Author: User

In the previous article, I learned a simple way of interprocess communication: Anonymous pipelines. However, it can only be used for communication between processes that have affinity. FIFOthe communication mechanism is similar, but it can communicate between any two processes.

FIFO file operation

Creating FIFO A file that is similar to creating, indeed, FIFO can exist in the file system. The following are FIFO the created functions:

#include <sys/stat.h>int mkfifi(const char *pathname, mode_t mode);                                返回值:若成功返回0,若出错返回-1

Parameter description:

pathname: FIFO the path to the file
mode: Sets FIFO the pattern Word for a file property, usually set to0666
Return value: If 0 is returned successfully, return-1 if error

Here is an example of a simple use of a mkfifo function to create a FIFO file, and use the LS command to view its properties:

int0666; if (ret = Mkfifo ("./fifo",  0) {    printf ("  Mkfifo Error The return code is%s\n", Strerror (errno));     return -1;}

After you create the FIFO file, you need to call the open function to open FIFO the file, and then perform the operation on that basis read write . Here are FIFO some points of note about file operations.

Note the point:

  

    • Similar to pipelines, if a read operation is performed on a FIFO that does not exist for the write process, it always returns 0, which means that if the write process closes the FIFO file prematurely and no data exists in the FIFO, the read operation returns 0 without blocking. If a write operation is performed on a FIFO that does not exist for the process, an error returns 1, generating a sigpipe signal.
    • If it is open not set O_NONBLOCK , open the file as read, FIFO there must be a process to open the file in writing, FIFO otherwise it will be blocked, and open in writing FIFO , if there is no read FIFO -open process, it is blocked

    • If set O_NONBLOCK , open as read or write FIFO will return immediately. The return value is based on whether there is a corresponding write or read process to make an error.

Here is a test code:

#include <sys/stat.h>#include<stdio.h>#include<unistd.h>#include<fcntl.h>#include<errno.h>#include<string.h>intMain () {intret; intFD; mode_t Mode=0666; if(ret = Mkfifo ("./fifo", mode)) <0) {printf ("Mkfifo Error The return code is%s\n", Strerror (errno)); return-1; }    //the test does not set the O_nonblock identity and the write process does not exist, and this operation is blocked    if(FD = open ("./fifo", o_rdonly)) <0) printf ("Open Error The return code is%s\n", Strerror (errno)); //the test does not set the O_nonblock identity and the read process does not exist, and this operation is blocked    if(FD = open ("./fifo", o_wronly)) <0) printf ("Open Error The return code is%s\n", Strerror (errno)); return 0;}

Instance

Here is an example of using FIFO to implement server-to-client communication:

/** FILE:FIFOREAD.C **/#include<sys/stat.h>#include<stdio.h>#include<unistd.h>#include<fcntl.h>#include<errno.h>#include<string.h>intMain () {intret; intFD; mode_t Mode=0666; if(ret = Mkfifo ("./fifo", mode)) <0) {printf ("Mkfifo Error The return code is%s\n", Strerror (errno)); return-1; }    //Open the FIFO file and Papare to read    if(FD = open ("./fifo", o_rdonly)) <0) {printf ("Open Error The return code is%s\n", Strerror (errno)); return-1; }    //read the data from client and print    intnread; Charline[1024x768];  while(nread = read (FD, line,1024x768)) >0) {printf ("The recv bytes is%d\n", nread); printf ("The recv msg is%s\n", line); } printf ("The return nread is%d\n", nread);    Close (FD); return 0;}

#include <sys/stat.h>#include<stdio.h>#include<unistd.h>#include<fcntl.h>#include<errno.h>#include<string.h>intMain () {intFD; //Open test without O_noblock flag    if(FD = open ("./fifo", o_wronly)) <0) {printf ("Open Error The return code is%s\n", Strerror (errno)); return-1; }    inti; intNwrite; Charline[ -] ="Hello fifo\n";  for(i =0; I <Ten; i++)    {        if(Nwrite = write (fd, line,sizeof(line))) <0) {printf ("Wirte error\n"); return-1; } Sleep (1);    } close (FD); return 0;}

Process Communication--a well-known pipeline

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.