13.4 Pipe call After looking at the advanced popen function, take a look at the bottom pipe function. Passing data between two programs through this function does not require a shell to be started to interpret the requested command. It also provides more control over read and write data.
the prototype of the pipe functionAs shown below:
#include <unistd.h>int pipe (int file_descriptor[2]);
Parameters: is an array of file descriptors of two integer types.
return value: This function fills in the array with two new file descriptors, returns 0 if successful, returns 1 if unsuccessful, and sets errno to indicate the cause of the failure.
Error Description:
Emfile: Too many file descriptors used by the process
Enfile: The system's File table is full
Efault: Invalid file descriptor
after the pipe is called, two file descriptors are connected in a special way. all data written to file_descrpter[1] can be read back from file_descripter[0] .
data is processed based on the FIFO principle, which means that if the bytes are written to file_descripter[1], the data read from file_descripter[0] will also be in the form of a.
It is important to note that the file descriptor is used instead of the file stream, so you must use the underlying read and write notes to access the data, not the file stream library functions fread and fwrite.
Write the program pipe1.c, which creates a pipe with the pipe function.
<pre name= "code" class= "CPP" >/************************************************************************* > File name:pipe1.c > Description:pipe1.c program creates a pipeline with the pipe function > author:liubingbing > Created time:201 5 July 10 Friday 10:54 58 seconds > OTHER:PIPE1.C program creates a pipeline with two file descriptors for array files_pipes[]. Then write the data to the pipeline with the file descriptor File_pipes[1], and read back to the data pipeline with the file descriptor File_pipes[0] There are built-in buffers that save data between write and read calls ************************** /#include <stdio.h> #include <stdlib.h> #include < Unistd.h> #include <string.h>int main () {/* data_processed stores return values for write and read calls */int data_processed;/* An array of file descriptors, File_pipes[0] for reading, file_pipes[1] for writing */int file_pipes[2];const char some_data[] = "123"; char Buffer[bufsiz + 1] memset (buffer, ' n ', sizeof (buffer));/* Pipe function creates a pipe */if (pipe (file_pipes) = = 0) in file_pipes[0] and file_pipes[1] {/* The Write function writes strlen (some_data) bytes from the memory referred to by the pointer some_data to the file referred to in file_pipes[1] * If successful, the number of bytes actually written is returned, or 1 if it fails, and the error isThe code is saved in errno, and returning 0 means that no data is written */data_processed = Write (file_pipes[1], Some_data, strlen (some_data));p rintf ("wrote%d Bytes\n ", data_processed);/* Read function reads Bufsiz bytes from file descriptor File_pipes[0] to the memory pointed to in buffer * If successful, the number of bytes actually read is returned, or 1 if it fails , and the error code is saved in errno, and the return 0 means that no data has been read, the end of the file has been reached */data_processed = Read (File_pipes[0], buffer, bufsiz);p rintf ("read%d bytes: %s\n ", data_processed, buffer); exit (exit_success);} Exit (exit_failure);}
The results of the program run are as follows:
This program creates a pipeline with the two file descriptors of the array file_pipes[]. Then write the data to the pipeline with File_pipes[1] and then read back the data from the pipe with file_pipes[0].
Note: The pipeline has some built-in buffers that save data between write and read calls
This example seems useless because in this program you can copy data directly from Some_data to buffer, without having to use pipe to create a pipeline.
the real advantage of the pipeline is that , if you want to pass data in two processes, the original open file descriptor remains open when the program creates a new process with a fork call. If you create a pipeline in the original process and then call Fork to create a new process, you can pass data between two processes through a pipeline.
Communication between two processes
Writes the program pipe2.c, uses the fork call on the basis of the pipe1.c, realizes the communication between two processes
/************************************************************************* > File name:pipe2.c > Description: The PIPE2.C program creates a pipeline in the parent process, and then calls Fork to create the child process, passing data between the parent process and the child process through the pipeline > Author:liubingbing > Created time:2015 July 10 Friday 11 41 minutes 09 Seconds > OTHER:PIPE2.C Program parent process----FILE_PIPES[1] (write data to pipe)----file_pipes[0] (read back data from pipeline)----child process ********************* /#include <stdio.h> #include <stdlib.h> #include <unistd.h> #include <string.h>int main () {int data_processed;int file_pipes[2];const char some_data[] = "123 "; char Buffer[bufsiz + 1];p id_t fork_result;memset (buffer, ' \ n ', sizeof (buffer)); */* Pipe function creates a pipeline with a file_pipes file descriptor array * Write data to pipeline with file descriptor File_pipes[1] read back data from the pipeline with file descriptor File_pipes[0] */if (pipe (file_pipes) = = 0) {/* Fork Create a child process * If creation fails, return 1 * If successful , return 0 indicates the child process PID * Other for parent process */fork_result = fork (), if (Fork_result = =-1) {fprintf (stderr, "fork Failure"); exit (exit_failure);} if (Fork_result = = 0) {/* child process using the read system call from FILE_PIPEs[0] points to a file that reads Bufsiz bytes of data to buffer pointing to the memory * If the number of bytes actually read data is returned *///sleep (2);d ata_processed = Read (File_pipes[0], buffer, Bufsiz);p rintf ("Read%d bytes:%s\n", data_processed, buffer); exit (exit_success);} else {/* The parent process uses the write system call to read the strlen (some_data) bytes of data from Some_data to the file_pipes[1] point to the file * If the number of bytes actually read into the data is successfully returned */data_ Processed = Write (file_pipes[1], Some_data, strlen (some_data));p rintf ("wrote%d bytes\n", data_processed);}} Exit (exit_success);}This one
The program first creates a pipe with the pipe call and then creates a new process with the fork call. If the fork call succeeds, the parent process writes the data to the pipeline, and the child process reads the data from the pipelineThe. Parent-child process exits after only one write or read has been called. If the parent process exits before the child process, a shell prompt is seen between the two parts of the output. The following (./a.out adds sleep (2) to the child process):
Such
combine pipe and fork to read and write data between different processes.
Copyright NOTICE: This article for Bo Master original article, without Bo Master permission not reproduced.
Linux programming--pipe calls between two processes (chapter 13th)