Linux Process Communication Pipeline and linux Communication Pipeline

Source: Internet
Author: User

Linux Process Communication Pipeline and linux Communication Pipeline

A pipe is unidirectional, FIFO, and connects the output of a process with the input of another process. One process (write process) writes data at the end of the pipeline, and the other process (read process) reads data from the header of the pipeline. After the data is read by a process, it will be deleted from the pipeline, and other read processes will no longer be able to read the data. MPs queue provides a simple flow control mechanism. When a process attempts to read an empty MPs queue, the process will be blocked. Similarly, when the pipeline is full, the process attempts to write data to the pipeline, and the process will be blocked.
There are two types of pipelines: an unknown pipeline and a named pipeline. An unknown pipeline can only be used for communication between parent and child processes. A famous pipeline can be used for communication between any two processes in the same system.

  • The unknown pipeline is created by the pipe () function.
Int pipe (int pipefd [2]); # When an unknown pipeline is created, it creates two file descriptors: # pipefd [0] for reading the pipeline, # pipefd [1] is used to write pipelines. # The pipeline created by the pipe () function is open by default.

Example program:

# Include <stdio. h> # include <unistd. h> # include <sys/types. h> # include <sys/stat. h> # include <fcntl. h> # include <string. h> # include <errno. h> # include <stdlib. h> int main (int argc, char ** argv) {int pipe_fd [2]; pid_t pid; if (pipe (pipe_fd) =-1) {fprintf (stderr, "% s \ t % d \ n", strerror (errno), _ FILE __,__ LINE _); exit (-1 );} if (pid = fork () = 0) {close (pipe_fd [0]); write (pipe_fd [1], "hello", 6); write ( Pipe_fd [1], "world", 7); printf ("Child process write done! \ N "); close (pipe_fd [1]); exit (0);} else if (pid> 0) {close (pipe_fd [1]); sleep (3 ); char buf [20]; memset (buf, 0, 20); read (pipe_fd [0], buf, 20); printf ("Parent process in: % s \ n ", buf); close (pipe_fd [0]);} return 0 ;}# read, write, and close operations on an unknown pipeline are the same as operations on common files.
  • You can create a famous Pipeline in either of the following ways:
    1. Use shell commands in the following format:
mkfifo [OPTION] FILENAME

2. Call the mkfifo () function in the program

int mkfifo(const char *pathname, mode_t mode);

The mkfifo () function creates a special FIFO file (famous pipe) based on the pathname parameter. The file must not exist, and the mode parameter is the permission of the file. Other processes of the FIFO file created by mkfifo () can read and write files in normal mode. The FIFO file created by mkfifo () is disabled by default. When you use the open () function to open a file, you must note the differences with normal files:
1. You cannot open a FIFO file in O_RDWR mode for read/write operations. This action is undefined. Because pipelines are unidirectional, if you need to transmit data between programs in two directions, you can use a pair of famous pipelines.
2. usage of the O_NONBLOCK flag of the flag.
There are four valid combinations of O_RDONLY, O_WRONLY, and O_NONBLOCK logos:
Flags = O_RDONLY: open will call blocking, unless another process opens the same FIFO in write mode, it will wait.
Flags = O_WRONLY: open will call blocking, unless another process opens the same FIFO in Read mode, it will wait.
Flags = O_RDONLY | O_NONBLOCK.
Flags = O_WRONLY | O_NONBLOCK: returns immediately. If no other process is opened in Read mode, open will fail. In this case, the FIFO is not opened and-1 is returned.

Example program:

#fifo_read.c#include <sys/types.h>#include <sys/stat.h>#include <fcntl.h>#include <stdio.h>#include <stdlib.h>#include <string.h>#include <errno.h>#define FIFO_FILE   "/tmp/myfifo"int main(int argc, char *argv[]){    int fd ;    char buf[128];    if(access(FIFO_FILE,F_OK) != 0){        if(mkfifo(FIFO_FILE, 0644) == -1){            fprintf(stderr,"%s\t%s\t%d\n",strerror(errno), __FILE__,__LINE__);            return -1;        }    }     if( (fd = open(FIFO_FILE,O_RDONLY | O_NONBLOCK)) == -1){            fprintf(stderr,"%s\t%s\t%d\n",strerror(errno), __FILE__,__LINE__);            return -1;    }    while(1){        memset(buf,0,sizeof(buf));        if(read(fd,buf,sizeof(buf)) == -1){            fprintf(stderr,"%s\t%s\t%d\n",strerror(errno), __FILE__,__LINE__);            return -1;        }        sleep(1);        printf("read is %s !\n",buf);    }    unlink(FIFO_FILE);    return 0;}
# fifo_write.c#include <sys/types.h>#include <sys/stat.h>#include <fcntl.h>#include <stdio.h>#include <stdlib.h>#include <string.h>#include <errno.h>#define FIFO_FILE   "/tmp/myfifo"int main(int argc, char *argv[]){    int fd ;     if( (fd = open(FIFO_FILE,O_WRONLY | O_NONBLOCK)) == -1){            fprintf(stderr,"%s\t%s\t%d\n",strerror(errno), __FILE__,__LINE__);            return -1;    }    if (write(fd,"Hello world",12) == -1){         fprintf(stderr,"%s\t%s\t%d\n",strerror(errno), __FILE__,__LINE__);            return -1;    }    return 0;}

Copyright Disclaimer: This article is an original article by the blogger and cannot be reproduced without the permission of the blogger.

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.