Skip to content

Creation Of an InterProcess Pipe in C Unix/Linux

20090623 (2009 June 23 Tuesday)

The most simple way is to use the shell to create a pipeline. But if you want to create any other shape you will have to do some programming. Some languages such as Python, TCL, purl … have built in functions to create task harnesses. Here is how to do it in C.

Creation Of InterProcess Pipes in C on Unix/Linux

  1. Create a Task.
    A Task

    int main (void) {
    
    } 

  2. Open Pipe
    A Task with a Pipe

    #include <unistd.h>
    int main (void) {
       
        int MyPipe[2];
        if ( pipe( MyPipe) NEQU 0 ){
            Error("Could not open pipe");
        } 
        
    }

  3. Fork
    Forked Tasks with untrimed pipe

    #include <unistd.h>
    int main (void) {
        int MyPipe[2];
    
        int Pid;
    
        if ( pipe( MyPipe) NEQU 0 ){
            Error("Could not open pipe");
        }
    
        Pid=fork();
        if (-1 EQU Pid) // Error
        {
            Error("Could not Fork");
        } 
    }

  4. Trim Pipe
    Forked Tasks with trimed pipe

    #include <unistd.h>
    
    enum  PipeDirection_i {PipeRead=0,PipeWrite=1};
    
    int main (void) {
        int MyPipe[2];
    
        int Pid;
    
        if ( pipe( MyPipe) NEQU 0 ){
            Error("Could not open pipe");
        }
    
        Pid=fork();
        if (-1 EQU Pid) // Error
        {
            Error("Could not Fork");
        }
    
        else if (0 EQU Pid) // Child Process....
        {
            /*Close parent ends*/
            CHK( close( My[PipeWrite]   ) ); //Close parent ends
        }
    
        // Parent Process....
        CHK( close( MyPipe[PipeRead] ) );    //Close child ends
     }

  5. Plumb Pipe
    Forked Task with pipe re plumbed

    #include <unistd.h>
    
    enum  PipeDirection_i {PipeRead=0,PipeWrite=1};
    
    FILE* PipeOut;
    
    int main (void) {
        int MyPipe[2];
    
        int Pid;
    
        if ( pipe( MyPipe) NEQU 0 ){
            Error("Could not open pipe");
        }
    
        Pid=fork();
        if (-1 EQU Pid) // Error
        {
            Error("Could not Fork");
        } 
    
        else if (0 EQU Pid) // Child Process....
        {
            /*Close parent ends*/
            CHK( close( My[PipeWrite]   ) ); //Close parent ends
    
            /**************************/
            /*Plumbing of Destination */
            CHK( dup2(  MyPipe[PipeRead],0 ) ); //attach stdin to pipe
            CHK( close( MyPipe[PipeRead] ) ); //close old descriptors in child 
        }
    
        // Parent Process....
        CHK( close( MyPipe[PipeRead] ) );    //Close child ends
    
        /*Plumbing of Parent */
        PipeOut = fdopen ( MyPipe[PipeWrite] ,"w"); //attach parent ends
    
        if ( NULL EQU PipeOut ) {
          Error("error in fdopen opening pipe");
        }
    }

  6. Exec
    Process B now running another program

    #include <unistd.h>
    
    enum  PipeDirection_i {PipeRead=0,PipeWrite=1};
    
    FILE* PipeOut;
    
    int main (void) {
        int MyPipe[2];
    
        int Pid;
    
        if ( pipe( MyPipe) NEQU 0 ){
            Error("Could not open pipe");
        }
    
        Pid=fork();
        if (-1 EQU Pid) // Error
        {
            Error("Could not Fork");
        } 
    
        else if (0 EQU Pid) // Child Process....
        {
            /*Close parent ends*/
            CHK( close( My[PipeWrite]   ) ); //Close parent ends 
    
            /**************************/
            /*Plumbing of Destination */
            CHK( dup2(  MyPipe[PipeRead],0 ) ); //attach stdin to pipe
            CHK( close( MyPipe[PipeRead] ) ); //close old descriptors in child
    
    	execl( "/bin/bash","--verbose","-c",ExecName,NULL );
    	//or: execl( ExecName,NULL );
    	//or: execl( ExecName,...,NULL ); 
    
    	//Cant get here unless exec fails.
    	Error("Did not exec");
        }
    
        // Parent Process....
        CHK( close( MyPipe[PipeRead] ) );    //Close child ends  
    
        /*Plumbing of Parent */
        PipeOut = fdopen ( MyPipe[PipeWrite] ,"w"); //attach parent ends
    
        if ( NULL EQU PipeOut ) {
          Error("error in fdopen opening pipe");
        }
    }
Advertisements
One Comment leave one →
  1. 20110301 (2011 March 01 Tuesday) 14:16

    hi, good site very much appreciatted

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s

%d bloggers like this: