Operating system process communication-pipeline

1. Familiar with the wc command

  The wc command is used to calculate the number of bytes, words, or columns of a file. If the file name is not specified or the file name given is "-", the wc command will read data from the standard input device.

 parameter:

  c or --bytes or --chars only display the number of Bytes.

  -l or --lines only display the number of lines.

  -w or --words only display the number of words.

  --help Online help.

  --version display version information

 

2. Process communication

 Enter "su" and enter the password for one-time operation.

 

( 1 ) Reading pipeline

gedit pip_read.c

 

  After opening the file, enter:

#include <unistd.h> 
#include <stdlib. h> 
#include <stdio.h> 
#include <str ing.h>
 int main () { 
  FILE * read_ fp;        // file stream pointer 
  char buffer [BUFSIZ + 1 ] ;  // Store the read content 
  int chars_ read; 
  nenset (buffer, ' \ 0 ' , sizeof (buffer));    // Initialize buf to avoid garbled characters in the file 
  read_ _fp = popen ( " unane -a " , " r " );      // Open the pipe connected to the uname command in read-only mode, Read only way of illustration: the library can be read by an output function fread stdio callee 
  IF (READ_ _fp =! NULL) { 
    chars_ Read = fread (Buffer, the sizeof ( char ), BUFSIZ, READ_ FP);
    IF (chars_ read> 0 ) 
      printf ( " output was:-\ n% s \ n " , buffer); 
    pclose (read_ fp);          // close the pipeline 
    exit (EXIT_ SUCCESS);      // normal exit 
  }
    exit (EXIT_ FAILURE);       / / Read failed
}

  Run screenshot:

 

    

(2) Write pipeline

  Enter the command "gedit pip_write.c" to open the file and enter:

#include <unistd.h>
#include <stdlib.h>
#include <stdio.h>
#include <string.h>
int main()[
  *write_fp;
  char buffer [BUFSIZ+1];
  int chars_write;
  memset(buffer, '\0' ,sizeof(buffer)); 
  sprintf(buffer ,"Once upon a time, there was...\n");
  write_ fp=popen("wc -w" ,"w");  //Write to FILE * stream via pipeline ("w" parameter) 
  if (write_ fp! = NULL) [ 
    fwrite (buffer, sizeof ( char ), strlen (buffer), write_fp);  // Data stream of FILE * write_ fp Write to buf 
  pclose (write_fp); 
  exit (EXIT_SUCCESS); 
  } 
  exit (EXIT_FAILURE); 
}

   Run screenshot:

 

   

3. Anonymous pipeline

 (1) Producer and consumer model

  Enter the command "gedit pipe.c" to open the file and enter:

#include <unistd.h> 
#include <stdlib. h> 
#include <stdio. h> 
#include < string .h>
 int main () [
  int data_ processed;
  int file_ pipes [ 2 ]; // Store two files Descriptor 0 1 
  const  char some_data [] = " 123 " ;
  char buffer [BUFSIZ + 1 ]; 
  memset (buffer, ' \ 0 ' , sizeof (buffer));
  / * The pipe creates a pipe and writes 2 files to the array Descriptor, these two file descriptions
  The descriptors are closely related (father and child processes), use the file descriptor file_pipes [1] to write data to the pipeline, and the file descriptor file_pipe [0] to read data from the pipeline. * / 
  if (pipe (file_pipes) == 0 ) {
    // Use the file_ pipes [1] file descriptor to write data to the pipeline 
    data_ processed = write (file_pipes [ 1 ], some_data, strlen (some_data)); 
    printf ( " Wrote% d bytes \ n " , data_processed); 
    data_ processed = read (file_pipes [ 0 ], buffer, BUFSIZ); 
    printf ( " Read% d bytes:% s \ n " , data_ processed, buffer); 
    exit (EXIT_SUCCESS) ; 
  }   exit (EXIT_FAILURE);
}

   Run screenshot:

     

 (2) Communication between father and son process

 

  Enter the command "gedit pipe_fork.c" to open the file and enter:

#include <unistd .h>
#include <stdlib.h>
#include <stdio. h>
#include <string.h>
int main( ){
  int data_processed;
  int file_pipes[2];
  const char some_data[]="123";
  char buffer [BUFSIZ+1];
  pid_t pid;
  memset(buffer , '\0',sizeof(buffer));
  if(pipe(file_pipes)==0){
    pid=fork();
    if (pid ==- 1 ) {          // failure to create subprocess 
      fprintf (stderr, " Fork failure " ); 
    exit (EXIT_FAILURE); 
    if (pid == 0 ) {            // subprocess 
      sleep ( 1 ); 
      data_processed = read (file_ pipes [0], buffer, BUFSIZ); 
      printf ( " Read% d bytes:% s \ n " , data_processed, buffer);       close (file_pipes [ 1 ]);    // Close the read       exit (EXIT_SUCCESS) ;     }     else { / / father's journey       data_processed=write(file_pipes[1],some_data, strlen(some_data));       printf("Wrote %d bytes\n" ,data_processed);       close(file_pipes[0]);     //写入成功,关闭写端     }
  }   exit(EXIT_ SUCCESS);
}

   Run screenshot:

 

    

 4. Named Pipes

(1) Shell command creation

  

  For the explanation of the mkfifo command, see the code comments below

 (2) Program creation

#include <unistd. h> 
#include <stdlib.h> 
#include <stdio.h> 
#include <sys / types.h> 
#include <sys / stat.h>
 int main ( int argc, char * argv [] ) {
 / * int mkfifo (const char * pathname, mode_t mode); 
nkfifo () will create a special FIFo file according to the parameter pathnane (path), the file must not exist, and the parameter mode is the permission of the file (mode% ~ umask) Therefore, the umask value will also affect the permissions of the FIFo file. * / 
int res = mkfifo ( " / my_fifo " , 0777 );
 if (res == 0 ) 
printf ( " FIFO createdn "  );
exit (EXIT_SUCCESS);
}

 (3) Reading empty 

cat </ fifo   // The path of the output file after compilation

(4) Input (need to use another terminal)

echo "hello,word" > /fifo

  operation result:

  

Guess you like

Origin www.cnblogs.com/Monster-su/p/12737092.html