β½Named pipe FIFO
In this section, we shall see what are FIFOs and how they work
mkfifo filename#include <stdio.h>
#include <unistd.h>
#include <sys/stat.h>
#include <sys/types.h>
#include <errno.h>
#include <fcntl.h> // for open() function
int main(){
// 0777 are the permission bits in octal which allows any user/process to read/write from/to the FIFO
if(mkfifo("myfifo", 0777) == -1){
if(errno == EEXIST)
printf("FIFO already exists\n");
else
printf("There was some unexpected error\n");
}
printf("Opening FIFO...\n");
int fd = open("myfifo", O_WRONLY);
printf("FIFO opened\n");
char str[15] = "Saptarshi Dey\n";
if(write(fd, str, sizeof(str)) == -1)
return errno;
printf("Data written to FIFO\n");
close(fd); // close the file descriptor
printf("Closed\n");
return 0;
}Output
Output after reading from FIFO
Last updated