Operating system third experiment report: pipeline

0. Personal information

  • Name Lei Tanchun
  • Student ID 201821121030
  • Class calculation 1811

1. Write a program

Create a named pipe and create two processes to read and write to the pipe:

//fifo_write

#include <fcntl.h> #include <unistd.h> #include <stdio.h> #include <stdlib.h> #include <errno.h> #include <signal.h> #include <string.h> #include <sys/types.h> #include <sys/stat.h> void handle_sig(int sig) { printf("signal pipe\n"); exit(-1); } int main(int argc, {argv)**char int fd; int ret; char buf [ 128 ]; int i = 0 ; / * Process pipeline signal * / signal (SIGPIPE, handle_sig); if (mkfifo ( " ./fifo " , 0640 ) ==- 1 ) { if (errno! = EEXIST) { perror ( " mkfifo " ); return - 1 ; } } / * open the pipeline in write-only mode * / fd= open("./fifo", O_WRONLY); if(fd == -1) { perror("open"); return -1; } while(1) { scanf("%s",buf); //sprintf(buf, "data %d", i++); /*往管道写数据*/ ret = write(fd, buf, strlen(buf)); printf("write fifo [%d] %s\n", ret, buf); sleep(1); } return 0; }
//fifo_read

#include <fcntl.h> #include <unistd.h> #include <stdio.h> #include <stdlib.h> #include <errno.h> #include <signal.h> #include <string.h> #include <sys/types.h> #include <sys/stat.h> int main(int argc, char** argv) { int fd; int ret; char buf[128]; if(mkfifo("./fifo", 0640) ==- 1 ) { if (errno! = EEXIST) / * If the error type is that a fifo file already exists, continue to execute * / { perror ( " mkfifo " ); return - 1 ; } } / * In read-only mode Open the pipeline * / fd = open ( " ./fifo " , O_RDONLY); if (fd ==- 1 ) { perror ( " open " ); return - 1 ; } while(1) { memset(buf, 0, sizeof(buf)); /*读管道*/ if((sizeof(buf)-1)) { ret = read(fd, buf, sizeof(buf) - 1); printf("read fifo [%d] : %s\n", ret, buf); } } return 0; }

 

2. Operation result

1. Input

 

 

2. Output

 

 

3. Questions and answers generated through this experiment

Guess you like

Origin www.cnblogs.com/BarnettOne/p/12704208.html