Operating system third experiment report: pipeline

  • Name: Zhu Duxin
  • Student ID: 201821121021
  • Class: Calculate 1811

1. Write a program

 1. Pipe naming (because the read and write later specify the pipe name, so the creation process here also uses the method specified in the program.)
  1 #include <stdio.h>
   2 #include <sys / types.h>
   3 #include <sys / stat.h>
   4  int main () {
   5      int temp = mkfifo ( " fifo " , 0777 ); // Create Pipeline fifo
   6      if (temp ==- 1 ) {// Return 0 if successful, -1
   7          perror ( " defeat " );
   8          return - 1 ;
   9      }
  10      return  0 ;
  11 }

 

Since the parameter mode in int mkfifo ( const char * pathname, mode_t mode); refers to the permissions of the pipeline file, I will simply organize the permissions, the following are the contents represented by each permission:

 Authority  Explanation  Authority  Explanation
 r--  Read only  -w-  Write only
 --x Executable only   rw-  Readable and writable
 -wx  Writable executable  r-x  Readable and executable
 rwx  Readable, writable and executable  ---  No permission

 And these permissions are usually expressed in octal: (Binary can also be used, and the specific binary can be converted by itself)

Authority Octal
--- 0
x 1
w 2
wx 3
r 4
rx 5
rw 6
rwx 7

Since this is an experiment, mode is selected as 777, which is convenient for the experiment.

And create a famous pipe can be directly created using the command mkfifo name.

2.fifo_read.c
  1 #include<unistd.h>
  2 #include<stdlib.h>
  3 #include<errno.h>
  4 #include<string.h>
  5 #include<fcntl.h>
  6 #include<stdio.h>
  7 
  8 int main(){
  9     int fifo_read=open("fifo",O_RDONLY);
 10     if(fifo_read==-1){
 11         perror("open");
 12         return -1;
 13     }
 14
 15     while(1){
 16         char buffer[128]={0};
 17         int flag=read(fifo_read,buffer,128);
 18         if(flag==0){
 19             printf("it is closed");
 20             break;
 21         }
 22         printf("read:%s from fifo\n",buffer);
 23     }
 24
 25     close(fifo_read);
 26     return 0;
 27 }

 

3.fifo_write.c
  1 #include<stdio.h>
  2 #include<stdlib.h>
  3 #include<signal.h>
  4 #include<unistd.h>
  5 #include<string.h>
  6 #include<fcntl.h>
  7 
  8 int main(){
  9     int fifo_write=open("fifo",O_WRONLY);
 10     if(fifo_write==-1){
 11         perror("open");
 12         return -1;
 13     }
 14
 15     while(1){
 16         char buffer[128]={0};
 17         char* find;
 18         fgets(buffer,128,stdin);
 19         find=strchr(buffer,'\n');
 20         if(find){
 21             *find='\0';
 22         }
 23         if(strcmp(buffer,"end")==0){
 24             break;
 25         }
 26         write(fifo_write,buffer,strlen(buffer));
 27     }
 28     close(fifo_write);
 29     return 0;
 30 }

 

2. Analyze the operation results

 fifo_write write:

 

 fifo_read read:

3. Generate new questions and answers through this experiment

 During the first test, it was found that the reader had received the information, but the display result could not be output in a full line, and the content after the information was received could not be displayed, and it would not be displayed until the next time the message was received:

write:

read:

 

 Analyze the code and find that the problem is that when fgets () reads the input content, it will write the newline character together, which causes the above figure to not display after hello, and the original content after hello goes to the next line, by looking in the code The newline character and replace it can effectively solve the problem.

 

Guess you like

Origin www.cnblogs.com/duxinZhu/p/12708207.html