interprocess communication: Pipelines (1)

Source: Internet
Author: User
Tags sprintf

First, the pipeline

When connecting data flows from one process to another, we use the term pipeline (pipe). We usually connect the output of one process to the input of another process through a pipeline. For Shell commands, the connection to a command is done through a pipe character, such as: CMD1 | Cmd2

1. Process Pipeline

Perhaps the simplest way to pass data between two programs is to use the Popen and Pclose functions, which are prototyped as follows:

#include <stdio.h>*popen (constchar *command,constChar *  Open_mode); int pclose (FILE *stream_to_close);

The Popen function allows a program to start another program as a new process and can pass data to it or receive data through it. The command string is the name of the program to run and the corresponding parameter. The Open_mode must be "R" or "W". This means that we cannot invoke another program and read and write to it at the same time.

2. Pipe call

The pipe function is prototyped as follows:

#include <unistd.h>int pipe (int file_descriptor[2]);

The parameter of the pipe function is a pointer to an array of two integer-type file descriptors. The function returns 0 after filling in the array with two new file descriptors, and returns 1 if it fails, and sets errno to indicate the cause of the failure. Two returned file descriptors are connected in a special way. All data written to file_descriptor[1] can be read back from File_descriptor[0. The data is processed based on the FIFO principle, which means that if the bytes are written to file_descriptor[1], the data read from file_descriptor[0] will also be in the form of a. (The file descriptor is used here instead of the file stream, so we have to use the underlying read and write to access the data instead of using the file stream library functions fread and fwrite.)

#include <unistd.h>#include<stdlib.h>#include<stdio.h>#include<string.h>intMain () {intdata_processed; intfile_pipes[2]; Const CharSome_data[] ="123"; Char*data;    pid_t Fork_result; if(pipe (file_pipes) = =0) {Fork_result=Fork (); if(Fork_result = = (pid_t)-1) {fprintf (stderr,"Fork Failure");        Exit (Exit_failure); }        if(Fork_result = = (pid_t)0) {Close (0); DUP (file_pipes[0]); //Close (file_pipes[0]);Close (file_pipes[1]); Read (file_pipes[0],data,8); printf ("%s\n", data); //EXECLP ("od", "OD", "-C", (char *) 0);exit (exit_failure); }        Else{Close (file_pipes[0]); Data_processed= Write (file_pipes[1], Some_data,strlen (some_data)); Close (file_pipes[1]); printf ("%d-wrote%d bytes\n", (int) Getpid (), data_processed); }} exit (Exit_success);}

The following results are obtained after running:

33123

Next, we'll learn how to run another program in a child process that is completely different from its parent process, rather than just running the same program. We use the EXEC call to do the work. One difficulty here is that the process called by exec needs to know which file descriptor should be accessed. In the previous example, this is not a problem because the child process itself has a copy of the file_pipes data. However, after the exec call, the situation is different because the original process has been replaced by the new child process. To solve this problem, we can pass the file descriptor (which is actually just a number) as a parameter to the program that started with exec.

To demonstrate how it works, we need to use two programs, the first of which is the data producer, which is responsible for creating pipelines and initiating child processes, which are data consumers.

The following pipe3.c:

#include <unistd.h>#include<stdlib.h>#include<stdio.h>#include<string.h>intMain () {intdata_processed; intfile_pipes[2]; Const CharSome_data[] ="123"; CharBuffer[bufsiz +1];    pid_t Fork_result; memset (Buffer,' /',sizeof(buffer)); if(pipe (file_pipes) = =0) {Fork_result=Fork (); if(Fork_result = = (pid_t)-1) {fprintf (stderr,"Fork Failure");        Exit (Exit_failure); }        if(Fork_result = =0) {sprintf (buffer,"%d", file_pipes[0]); (void) Execl ("Pipe4","Pipe4", Buffer, (Char*)0);        Exit (Exit_failure); }        Else{data_processed= Write (file_pipes[1], Some_data, strlen (Some_data)); printf ("%d-wrote%d bytes\n", Getpid (), data_processed); }} exit (Exit_success);}

The Data Consumer program PIPE4.C is responsible for reading the data as follows:

#include <unistd.h>#include<stdlib.h>#include<stdio.h>#include<string.h>intMainintargcChar*argv[]) {    intdata_processed; CharBuffer[bufsiz +1]; intFile_descriptor; memset (Buffer,' /',sizeof(buffer)); SSCANF (argv[1],"%d", &file_descriptor); Data_processed=Read (file_descriptor, buffer, bufsiz); printf ("%d-read%d bytes:%s\n", Getpid (), data_processed, buffer); Exit (exit_success);}

Pipe3 call Pipe4 in the program, when running Pipe3, we get the following result:

19174 3 bytes 19175 3 123

The beginning of the PIPE3 program, like the previous example, creates a pipeline with a pipe call and then creates a new process with the fork call. Next, it saves the file descriptor of the read pipeline data to a buffer using sprintf, and the contents of the buffer will form one of the parameters of the PIPE4 program. We start the PIPE4 program with the EXECL call.

3. Use the pipe as standard input and standard output

The main use of the following two functions:

#include <unistd.h>int dup (int  file_descriptor); int dup2 (int file_descriptor_one,int file_descriptor_two);

The purpose of the DUP call is to open a new file descriptor, which is a bit similar to the open call. The difference is that the new file descriptor created by the DUP invocation points to the same file (or pipeline) as the existing file descriptor as its argument. For the DUP function, the new file descriptor always takes the smallest available value. For the DUP2 function, the new file descriptor it creates is either the same as the parameter file_descriptor_two, or the first available value that is greater than the parameter.

The file descriptor is processed with the close and DUP functions, and when we close file descriptor 0 and then invoke the DUP function, the following results are available:

File descriptor Initial value After closing file descriptor 0 After the DUP call
0 Standard input {closed} Pipe file Descriptor
1 Standard output Standard output Standard output
2 Standard error Output Standard error Output Standard error Output
3 Pipe file Descriptor Pipe file Descriptor Pipe file Descriptor
#include <unistd.h>#include<stdlib.h>#include<stdio.h>#include<string.h>intMain () {intdata_processed; intfile_pipes[2]; Const CharSome_data[] ="123";    pid_t Fork_result; if(pipe (file_pipes) = =0) {Fork_result=Fork (); if(Fork_result = = (pid_t)-1) {fprintf (stderr,"Fork Failure");        Exit (Exit_failure); }        if(Fork_result = = (pid_t)0) {Close (0); DUP (file_pipes[0]); Close (file_pipes[0]); Close (file_pipes[1]); EXECLP ("od","od","- C", (Char*)0);        Exit (Exit_failure); }        Else{Close (file_pipes[0]); Data_processed= Write (file_pipes[1], Some_data, strlen (Some_data)); Close (file_pipes[1]); printf ("%d-wrote%d bytes\n", (int) Getpid (), data_processed); }} exit (Exit_success);}

The following results are obtained after running:

19316 3 bytes 0000000   1   2   3
0000003

interprocess communication: Pipelines (1)

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.