In this chapter we shall learn about:
1. Introduction
2. How many ways are there to create a FIFO
3. Creating FIFO directly from the shell.
4. Full example to create a pipe using command line:
5. Creating FIFO by using “mknod()” system call.
6. Example 1: fifoServer.c
7. Example 2: fifoClient.c
Introduction:
1. FIFO or named pipe exist as a device special file in the file system.
2. FIFO can be used to process of different ancestry.
3. They can share data through a named pipe.
4. The named pipe remains in the file system for later use.
5. An “un-named” pipe will last as long as the process. Named pipe or FIFO will be there till you remove the file.
6. Here two or more files can communicate with each other by reading/writing from this file.
How many ways are there to create a FIFO
There are 2 ways to create FIFO.
1. Creating FIFO directly from the shell.
2. Creating FIFO by using “mknod()” system call.
In this chapter we shall see both ways of creating named FIFO.
1. Creating FIFO directly from the shell.
You can use “mknod()” command on command line to create a named pipe.
When you create a named pipe, it is equivalent to creating an ordinary file. It will exist until you delete it.
Syntax for “mknod”:
mknod named-pipe-identifier p
Here:
“named-pipe-identifier” is the pathname of the named pipe you want to create
“p” is the pipe.
For example:
mknod mypipe p
To remove a named pipe, use “rm” command.
Example:
rm mypipe
Full example to create a pipe using command line:
# Named Pipe # # Create named pipe using mknod mknod myPipe p # "cat" command will open "myPipe" to read # "&" is used to run cat process as a background process # here cat command is blocked because myPipe file is not yet opened for writing # It will wait in background cat myPipe & # write message into pipe # as "echo" command opens "myPipe" for writing, once writing is completed, "cat" command that is blocked will display the message echo "This is a message!" > myPipe # Delete pipe... rm myPipe
2. Creating FIFO by using “mknod()” system call.
We will use “mknod()” system call to create FIFO.
The syntax is as below:
int mkfifo(const char *pathname, mode_t mode);
Now we will create 2 programs.
1. “fifoServer.c”
2. “fifoClient.c”
3. we need to use “fp = fopen(file_location, open_mode)” system call to open a physical file.
4. we need to use “fclose(fp)” system call to close a physical file.
1. fifoServer.c
#include <stdio.h> #include <stdlib.h> #include <sys/stat.h> #include <unistd.h> #define FIFO_FILE "myFifo" int main(void) { FILE *fp; char readbuf[80]; /* Create the FIFO if it does not exist */ umask(0); mknod(FIFO_FILE, S_IFIFO|0666, 0); while(1) { fp = fopen(FIFO_FILE, "r"); fgets(readbuf, 80, fp); printf("Received string: %s\n", readbuf); fclose(fp); } return(0); }
As FIFO is a blocking call, we need to run the server in background till it receives a message from client.
So after you compile it, you will have to run as below:
./fifoServer&
2. fifoClient.c
#include <stdio.h> #include <stdlib.h> #define FIFO_FILE "myFifo" int main(int argc, char *argv[]) { FILE *fp; if ( argc != 2 ) { printf("USAGE: fifoclient [string]\n"); exit(1); } if((fp = fopen(FIFO_FILE, "w")) == NULL) { perror("fopen"); exit(1); } fputs(argv[1], fp); fclose(fp); return(0); }
After compiling it, you need to send the data or buffer wile executing the program.
You need to run as:
./fifoClient “hello world”
List Of Tutorials available in this website: