4th. Pipeline and FIFO

Source: Internet
Author: User

4.1 Piping

Pipelines are created by the pipe function and provide a one-way data stream.

Header file #include <unistd.h>
Function prototypes int pipe (int fd[2]);
return value The success is 0, the error is-1
function function The function returns two file descriptors: Fd[0] and fd[1]. FD[0] used for read operations, Fd[1] to write operations
Description Pipelines can only be used for affinity interprocess communication. To achieve non-affinity interprocess communication with a well-known pipeline FIFO

4.2 Pipe for half-duplex communication

Steps to implement:

(1) Creating pipelines (Fd[0] and fd[1])

(2) Fork

(3) The parent process closes the read end of the pipe (fd[0])

(4) Sub-process close the write end of the pipeline (Fd[1])

(5) Parent process writes data to the write end of the pipeline (Fd[1])

(6) The sub-process reads the data from the read end of the pipe (fd[0])

1#include <stdio.h>2#include <stdlib.h>3#include <unistd.h>4 5 #defineRead_max_len 106 7 intMain ()8 {9pid_t Childpid =0;Ten     intpipefd[2] = {0}; One     CharBuf[read_max_len +1] = {0}; A  -     if(Pipe (PIPEFD) <0) -     { theprintf"Pipe Error");  -         return-1; -     } -  +Childpid =fork (); -  +     if(Childpid <0) A     { atprintf"Fork error\n"); -         return-1; -     } -  -     //Child Process -     if(Childpid = =0) in     { -Close (pipefd[1]);  to  +Read (pipefd[0], buf, Read_max_len); -printf"child:%s\n", buf); the  *Exit0); $     }Panax Notoginseng      -     //Parent Process theClose (pipefd[0]); +  AWrite (pipefd[1],"Hello",sizeof("Hello")); theprintf"parent:%s\n","Hello"); +  -Waitpid (Childpid, NULL,0); $  $     return 0; -}
View Code

4.3 Pipeline for full duplex communication

Steps to implement:

(1) Create Pipelines 1 (fd1[0] and fd1[1]), Pipe 2 (fd2[0] and fd2[1])

(2) Fork

(3) Parent process closes the read end of Pipe 1 (fd1[0]), closes the write end of Pipe 2 (fd2[1])

(4) Sub-process closes the write end of pipe 1 (fd1[1]), closes the read end of Pipe 2 (fd2[0])

(5) Communication between parent and child processes

4.4 Popen and Pclose functions
Header file #include <stdio.h>
Function prototypes FILE *popen (const char *command, const char *type);
int Pclose (FILE *stream);
return value Successfully returned file descriptor, failed to return null
Parameters Command Shell command, this line of command will be uploaded to Bin/sh and use the-C flag
Type R: Read the standard output of the Commond
W: standard input written to command
Description The file descriptor created by Popen must be closed by Pclose.
1#include <stdio.h>2#include <string.h>3 4 #defineBuf_max_len 1005 6 intMain ()7 {8FILE *FP =NULL;9     CharBuf[buf_max_len +1] = {0};Ten  Onefp = Popen ("ls","R"); A     if(fp = =NULL) -     { -printf"Popen error\n");  the         return-1; -     } -  -      while(Fgets (buf, Buf_max_len, fp)! =NULL) +     { - fputs (buf, stdout); +     } A  at Pclose (FP); -  -     return 0; -}
View Code

4.5 FIFO

4th. Pipeline and FIFO

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.