Named Pipes in Linux C

The mkfifo() function is used to create a named piped in the filesystem. The pipe() system call creates an anonymous pipe that can only be used by related processes. A named pipe, in contrast, can be used by any process, since the pipes are visible in the filesystem.

The mkfifo() call takes two arguments, the first being the pathname of the named pipe that we wish to create, and the second represents the read/write permissions for this pipe. The mkfifo() call returns 0 on success or -1 on error.

#include <stdio.h>
#include <sys/stat.h>

int main(void){

    //read, write, execute permissions for owner.
    printf("S_IRWXU = %d\n", S_IRWXU);

    int returnValue;

    //should return 0, if we are running this program
    //for the first time.
    returnValue = mkfifo("FIFOpipe", S_IRWXU);

    printf("The mkfifo() call returned %d\n", returnValue);

    return 0;


}

If we run this program twice (or more) the return value will be -1, as the named pipe will have already been created. We can see the error mkfifo() has encountered via errno, which is defined in the errno.h header file.

#include <stdio.h>
#include <sys/stat.h>
#include <errno.h>

int main(void){

    mode_t theMode = S_IRWXU;

    int returnValue = mkfifo("FIFOpipe", theMode);

    if(returnValue < 0){
        printf("mkfifo() failed.\n");
        printf("errno = %d\n", errno);
        if(errno==EEXIST){
            printf("That file already exists.\n");
            printf("(or we passed in a symbolic link, which we did not.)\n");
        }
    }

    return 0;

}

The mode_t permissions are delineated in the sys/stat.h header file.

To remove a FIFO, we can use the unlink() function which is included in the unistd.h header file.

#include <stdio.h>
#include <sys/stat.h>
#include <unistd.h>
//note: unistd.h included
//so we can call unlink() to delete the FIFO

int main(void){

    //S_IRWXU is the same value as 
    //'OR'-ing S_IRUSR, S_IWUSR, S_IXUSR
    //(i.e. all permissions for file creator)
    mode_t theMode = S_IRWXU;
    int returnValue;

    printf("S_IRUSR = %d\n", S_IRUSR);
    printf("S_IWUSR = %d\n", S_IWUSR);
    printf("S_IXUSR = %d\n", S_IXUSR);

    printf("S_IRUSR | S_IWUSR | S_IXUSR = %d\n", S_IRUSR | S_IWUSR | S_IXUSR);
    printf("S_IRWXU = %d\n", S_IRWXU);

    returnValue = mkfifo("AnotherPipe", S_IRWXU);
    if(returnValue==0){
        printf("FIFO created successfully.\n");
        printf("Now let's delete it!\n");
        returnValue = unlink("AnotherPipe");
        if(returnValue==0){
            printf("FIFO deleted.\n");
        }
    }


    return 0;
}

Once a named pipe has been created, we can read and write to it just as with any other file.

Advertisements

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