Unknown pipeline famous pipeline continued

Source: Internet
Author: User

1. In an unknown pipeline, it can only be used for parent-child processes with kinship. This is because only child processes have the same pipelines as parent processes, and they share

2. Famous pipelines:

Courseware in the file system

You can use file IO to operate famous pipelines.

Follow advanced instructions

The function for creating a famous Pipeline: mkfifo () is just a creation, and the read end is not opened, which is different from pipe.

3. Notes for using famous pipelines:

Open () function flag to O-RDONLY open, can only open the Reading end of the famous pipeline, to O-WRONLY, can only open the write end of the famous pipeline;

Open () function flag opened in O-RDWR mode, will open the read and write ends of the pipeline, only open the read and write ends of the pipeline at the same time, the famous pipeline is truly opened, opening the open () function on the read or write end only causes blocking.

Create a pipe FIFO


If (mkfifo ("./FIFO", 0666) <0) // the path of the file to be created by the first parameter. The second parameter indicates the full image of the file to be created.
{
Perror ("create wrong ");
Exit (1 );
}

This is the write end of the famous Pipeline opened in the form of a file. The two files run simultaneously on both terminals.

Result: before
Write open
After Write open
Writeno: 6 !!! Hello world !!!

Before
Write open
After Write open
Writeno: 6 !!! Hello world !!!

This means that the read and write operations of the famous pipeline are successfully performed.


/* ============================================================================ Name        : namepipewrite.c Author      :  Version     : Copyright   : Your copyright notice Description : Hello World in C, Ansi-style ============================================================================ */#include <stdio.h>#include <stdlib.h>#include<sys/types.h>#include<sys/stat.h>#include<fcntl.h>#include<string.h>int main(void) {int fd;puts("before write open");fd=open("./fifo",O_RDWR);puts("after write open");if(fd==-1){perror("open wrong!");exit(1);}char buf[10]="hello!";int writeno=write(fd,buf,strlen(buf));if(writeno==-1){perror("write wrong");exit(1);}printf("writeno:%d",writeno);puts("!!!Hello World!!!"); /* prints !!!Hello World!!! */return EXIT_SUCCESS;}


This is the Reading end of a famous Pipeline implemented by opening a file.

/* ===================================================== ============================================================ Name: mkfifo. c Author: Version: Copyright: Your copyright notice Description: Hello world in C, ANSI-style ========================================== ======================================================= * /# include <stdio. h> # include <stdlib. h> # include <unistd. h> # include <fcntl. h> # include <sys/STAT. h> # include <sys/types. h> int main (void) {I Nt fd; char Buf [10]; puts ("before read Open"); If (FD = open (". /FIFO ", 0666) =-1) {perror (" Open error "); exit (0);} puts (" after read Open "); int readno = read (FD, Buf, 10); If (readno =-1) {perror ("read error! "); Exit (1);} printf (" readno: % d ", readno); // If (mkfifo (". /FIFO ", 0666) <0) // path of the file to be created by the first parameter, the second parameter indicates the full image of the created file // {// perror ("create wrong"); // exit (1); //} return exit_success ;}

Famous pipeline experiment:

1. Create an MPS queue

/* ============================================================================ Name        : namepipeconnnect.c Author      :  Version     : Copyright   : Your copyright notice Description : Hello World in C, Ansi-style ============================================================================ */#include <stdio.h>#include <stdlib.h>#include<sys/stat.h>#include<sys/types.h>#include<fcntl.h>int main(int argc,char **argv){if(argc<2){puts("you should enter 2");puts("like :1   2  ");exit(1);}FILE *fp;if(mkfifo(argv[1],0666)<0){perror("create name pipe wrong!");exit(1);}return EXIT_SUCCESS;}

2. Write:

#include <stdio.h>#include <stdlib.h>#include<sys/stat.h>#include<sys/types.h>#include<errno.h>#include<fcntl.h>#include<string.h>void again(int fd,const char *buf,int * writeno){if((*writeno=write(fd,buf,strlen(buf)))<0){if(errno==EINTR){again(fd,buf,writeno);}else{fprintf(stderr,"write failed on fifo:%s",strerror(errno));}}}int main(int argc,char **argv){puts("je");   char buf[1024];   int writeno;if(argc<2){puts("you should enter 2");puts("like:1    2");exit(1);}int fp;perror("caught write signal");puts("je");if((fp=open(argv[1],O_RDWR))==-1){perror("write pipe wrong!");exit(1);}puts("je");while(fgets(buf,1024,stdin)){again(fp,buf,&writeno);}   return 0;}

3. Read:

#include <stdio.h>#include <stdlib.h>#include<sys/stat.h>#include<sys/types.h>#include<errno.h>#include<fcntl.h>#include<string.h>void again(int fd,char *buf,int *readno){if((*readno=read(fd,buf,1024))<0){if(errno==EINTR){again(fd,buf,readno);}else{fprintf(stderr,"read failed on :%s\n",strerror(errno));}}else if(readno==0){fprintf(stderr,"peer closed fifo.\n");exit(1);}else{buf[*readno]='\0';fprintf(stdout,"read %d butes from fifo:%s",readno,buf);}}int main(int argc,char **argv){int fp;int readno;char buf[1024];if(argc<2){puts("you should enter 2");puts("like:1      2");exit(1);}if((fp=open(argv[1],O_RDONLY))==-1){perror("open read pipe wrong");exit(1);}puts("create read Okay");puts("caught read sgnal");while(1){again(fp,buf,&readno);}return 0;}

Compile and create the pipe program on the terminal

Then run write on a terminal

Run the READ command on another terminal. When the READ command is written in the write command, the corresponding output is displayed in the READ command.

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.