Jan 21 2014
Named Pipe FIFO Sender Receiver
A named pipe is really just a special kind of file (a FIFO file) on the local hard drive. Unlike a regular file, a FIFO file does not contain any user information. Instead, it allows two or more processes to communicate with each other by reading/writing to/from this file.
Creating FIFO File
You can create a FIFO with the mkfifo(1) command or the mkfifo(3) function call. From the command line, issue
mkfifo [-m mode] fifoname The mode flags can be specified just as in chmod(1), or they will be set as 0666 (octal) or a=rw less your umask value. The mkfifo(3) function call is similar to chmod(2), and uses the include files <sys/stat.h> and <sys/types.h>:
int mkfifo(const char *name, mode_t mode) It returns 0 unless there is an error, in which case it returns –1 and sets errno.
Using a FIFO File
Since this named pipe looks like a file, you can use all the system calls associated with files to interact with it. In particular, you can use the open, read, write, and close system calls. The following are prototypes for each of these calls as you will need to use them.
- int open(const char *pathname, int flags);
- int read(int fd, void *buf, size_t count);
- int write(int fd, const void *buf, size_t count);
- int close(fd);
Named Pipe FIFO Sender Receiver Example
sender.c
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 | //Named Piped Sender Example #include<stdio.h> //Standard Input Functions #include<sys/types.h> //Named Pipes (FIFO) Types #include<sys/stat.h> // Named Pipes (FIFO) Functions #include<fcntl.h> //File Control Unix Functions #include<unistd.h> //Unix Standard Functions #define MAX 100 //Buffer Size for Writing Message int main() { char *myfifo = "/tmp/myfifo"; //FIFO File Path int fp; //Open Pipe Return Value char msg[MAX]; //Original Message to be sent printf("Creating Pipe...Waiting for receiver for Process...\n"); //Creating Named Pipe //mkfifo(<filepath>,<permission>) if(mkfifo(myfifo,0666) < 0) { perror("FIFO (Named Pipe) could not be created.\n"); exit(-1); } else printf("FIFO (Named Pipe) Created...\n"); //Opening Named Pipe //open(<filepath>,<openingmode>); fp = open(myfifo,O_WRONLY); if(fp < 0) { perror("FIFO (Named Pipe) could not be opened.\n"); exit(-2); } else printf("FIFO (Named Pipe) Opened...\n"); printf("Enter Message : "); gets(msg); //Writing to Named Pipe //write(<open_pipe_return_value>,<message>,<msgsize>); if(write(fp,msg,sizeof(msg)) < 0) { perror("Error writing to FIFO (Named Pipe)\n"); exit(-3); } else printf("Message has been sent to FIFO (Named Pipe)\n"); //Closing Named Pipe //close(<open_pipe_return_value>); if(close(fp) < 0) { perror("Error closing FIFO (Named Pipe)\n"); exit(-4); } return 0; } |
receiver.c
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 | //Named Piped Receiver Example #include<stdio.h> //Standard Input Functions #include<sys/types.h> //Named Pipes (FIFO) Types #include<sys/stat.h> // Named Pipes (FIFO) Functions #include<fcntl.h> //File Control Unix Functions #include<unistd.h> //Unix Standard Functions #define MAX 100 //Buffer Size for Writing Message int main() { char *myfifo = "/tmp/myfifo"; //FIFO File Path int fp; //Open Pipe Return Value char msg[MAX]; //Original Message to be sent //Opening Named Pipe //open(<filepath>,<openingmode>); fp = open(myfifo,O_RDONLY); if(fp < 0) { perror("FIFO (Named Pipe) could not be opened for reading.\n"); exit(-1); } else printf("FIFO (Named Pipe) Opened... Trying to read from\n"); //Reading From Named Pipe //read(<open_pipe_return_value>,<message>,<msgsize>); if(read(fp,msg,sizeof(msg)) < 0) { perror("Error reading from FIFO (Named Pipe)\n"); exit(-2); } else printf("Message Received from FIFO (Named Pipe)\n"); printf("\nMessage : "); puts(msg); //Closing Named Pipe //close(<open_pipe_return_value>); if(close(fp) < 0) { perror("Error closing FIFO (Named Pipe)\b"); exit(-3); } if(unlink(myfifo) < 0) { perror("Error deleting FIFO (Named Pipe)\b"); exit(-4); } return 0; } |