Linux- pipe (day09)

 table of Contents

First, using a C program to access environment variables

Second, the file input redirection

Third, the pipeline

Fourth, the signal


 

 

Third, the pipeline

Pipeline is divided into: anonymous pipes and named pipes.

1, unnamed pipes

  Use pipe (2) to create anonymous pipe

#include<unistd.h>

int pipe(int pipefd[2]);

Features:

  Create a one-way conduit for inter-process communication

parameter:

  pipefd [2]:

    pipefd [0]: the read end

    pipefd [1]: Write end

return value:

  Success: 0

  Error: -1, errno is provided

Ideas:

  (1) the parent process to create a pipe

  (2) fork (2) create a child process

  (3) the parent process close the write end, the child process close the read end

  (4) Write a child process, the parent process read

note:

  Unnamed pipes need to use the file descriptor, so the pipe is applied to a nameless kinship (father and son, brother) process communication

 

 

#include<stdio.h>
#inlcude<unistd.h>
#include<sys/typs.h>
#include<sys/wait.h>
#include<string.h>
#include<stdlib.h>
int main(void){
    pid_t pid;
    char MSG [] = " the this IS A Test " ;
     char buf [ 128 ];
     int FD [ 2 ];
     // create a child process 
    PID = the fork ();
     // Create Pipeline 
    int R & lt = pipe (FD);
     IF (R & lt == - . 1 ) {
        perror("pipe");
        return 2;
    }
    if(pid==-1){
        perror("fork");
        return 1;
    }
    if(pid==0){
        Close (FD [ 0 ]); // Close the read end of 
        Write (FD [ . 1 ], MSG, strlen (MSG)); // write pipe 
        Exit ( 0 );
    }else{
       close(fd[1]);
        int rt=read(fd[0],buf,128);
        write(1,buf,rt);
        wait(NULL);
    }
   
    return 0;  
}

 

 2, named pipe

  The essence is to create a named pipe pipeline price you ask, write data to a file, read data to a file.

  Create a named pipe can use the function mkfifo (3)

#include<sys/types.h>

#include<sys/stat.h>

int mkfifo(const char *pathname,mode_t mode);

Features:

  Create a special FIFO file, the file named pathname, mode specifies the file permissions (mask & ~ umask)

parameter:

  pathname: Name of the pipe file

  mode: Pipeline file permissions

return value:

  Success: 0

  Failed: -1, errno is provided

Code named pipe communications, for the following process A

#include<stdio.h>
#include<sys/types.h>
#include<sys/stat.h>
#include<string.h>
#include<fcntl.h>
#include<unistd.h>
int main(int argc,char *argv){
    char *msg[]="hello world\n";
    //创建管道文件
    int m=mkfifo(argv[1],0664);
    if(m==-1){
        perror("mkfifo");
        return 1;
    }
    // open conduit file 
    int FD = Open (the argv [ . 1 ], O_RDWR);  
     IF (FD == - . 1 ) {
        perror("open");
        return 2;
    }
    write(fd,msg,strlen(msg)+1 );
    close(fd);
    return 0;
}

The following code to process B

 

#include<stdio.h>
#include<sys/types.h>
#include<sys/stat.h>
#include<string.h>
#include<fcntl.h>
#include<unistd.h>
int main(int argc,char *argv){
    char buf[128];
    int fd= open(argv[1],O_RDONLY);
    if(fd==-1){
        perror("open");
        return 1;
    }
    // read the data file from the pipe 
    int R & lt = Read (FD, buf, 128 );
    write(1,buf,r)
    
    close(fd);
    return 0;
}

 

 

First, using a C program to access environment variables

Second, the file input redirection

Third, the pipeline

Fourth, the signal


 

 

Third, the pipeline

Pipeline is divided into: anonymous pipes and named pipes.

1, unnamed pipes

  Use pipe (2) to create anonymous pipe

#include<unistd.h>

int pipe(int pipefd[2]);

Features:

  Create a one-way conduit for inter-process communication

parameter:

  pipefd [2]:

    pipefd [0]: the read end

    pipefd [1]: Write end

return value:

  Success: 0

  Error: -1, errno is provided

Ideas:

  (1) the parent process to create a pipe

  (2) fork (2) create a child process

  (3) the parent process close the write end, the child process close the read end

  (4) Write a child process, the parent process read

note:

  Unnamed pipes need to use the file descriptor, so the pipe is applied to a nameless kinship (father and son, brother) process communication

 

 

#include<stdio.h>
#inlcude<unistd.h>
#include<sys/typs.h>
#include<sys/wait.h>
#include<string.h>
#include<stdlib.h>
int main(void){
    pid_t pid;
    char MSG [] = " the this IS A Test " ;
     char buf [ 128 ];
     int FD [ 2 ];
     // create a child process 
    PID = the fork ();
     // Create Pipeline 
    int R & lt = pipe (FD);
     IF (R & lt == - . 1 ) {
        perror("pipe");
        return 2;
    }
    if(pid==-1){
        perror("fork");
        return 1;
    }
    if(pid==0){
        Close (FD [ 0 ]); // Close the read end of 
        Write (FD [ . 1 ], MSG, strlen (MSG)); // write pipe 
        Exit ( 0 );
    }else{
       close(fd[1]);
        int rt=read(fd[0],buf,128);
        write(1,buf,rt);
        wait(NULL);
    }
   
    return 0;  
}

 

 2, named pipe

  The essence is to create a named pipe pipeline price you ask, write data to a file, read data to a file.

  Create a named pipe can use the function mkfifo (3)

#include<sys/types.h>

#include<sys/stat.h>

int mkfifo(const char *pathname,mode_t mode);

Features:

  Create a special FIFO file, the file named pathname, mode specifies the file permissions (mask & ~ umask)

parameter:

  pathname: Name of the pipe file

  mode: Pipeline file permissions

return value:

  Success: 0

  Failed: -1, errno is provided

Code named pipe communications, for the following process A

#include<stdio.h>
#include<sys/types.h>
#include<sys/stat.h>
#include<string.h>
#include<fcntl.h>
#include<unistd.h>
int main(int argc,char *argv){
    char *msg[]="hello world\n";
    //创建管道文件
    int m=mkfifo(argv[1],0664);
    if(m==-1){
        perror("mkfifo");
        return 1;
    }
    // open conduit file 
    int FD = Open (the argv [ . 1 ], O_RDWR);  
     IF (FD == - . 1 ) {
        perror("open");
        return 2;
    }
    write(fd,msg,strlen(msg)+1 );
    close(fd);
    return 0;
}

The following code to process B

 

#include<stdio.h>
#include<sys/types.h>
#include<sys/stat.h>
#include<string.h>
#include<fcntl.h>
#include<unistd.h>
int main(int argc,char *argv){
    char buf[128];
    int fd= open(argv[1],O_RDONLY);
    if(fd==-1){
        perror("open");
        return 1;
    }
    // read the data file from the pipe 
    int R & lt = Read (FD, buf, 128 );
    write(1,buf,r)
    
    close(fd);
    return 0;
}

 

 

Guess you like

Origin www.cnblogs.com/ptfe/p/11011343.html