A named pipe (also known as a FIFO) is one of the methods for inter-process communication. Usually a named pipe appears

Business, Finance, Economics, Accounting, Operations Management, Computer Science, Electrical Engineering, Mechanical Engineering, Civil Engineering, Chemical Engineering, Algebra, Precalculus, Statistics and Probabilty, Advanced Math, Physics, Chemistry, Biology, Nursing, Psychology, Certifications, Tests, Prep, and more.
Post Reply
answerhappygod
Site Admin
Posts: 899604
Joined: Mon Aug 02, 2021 8:13 am

A named pipe (also known as a FIFO) is one of the methods for inter-process communication. Usually a named pipe appears

Post by answerhappygod »

A named pipe (also known as a FIFO) is
one of the methods for inter-process communication. Usually a named
pipe appears as a file and generally processes attach to it for
inter-process communication. A FIFO file is a special kind of file
on the local storage which allows two or more processes to
communicate with each other by reading/writing to/from this file. A
FIFO special file is entered into the file system by calling
mkfifo() in C. Once we have created a FIFO special file in this
way, any process can open it for reading or writing, in the same
way as an ordinary file.
The following code is for the code
file namedpipe_p1.c.
// This process writes first, then
reads
#include <stdio.h>
#include <string.h>
#include <fcntl.h>
#include <sys/stat.h>
#include <sys/types.h>
#include <unistd.h>
int main()
{
int fd;
// FIFO file
path
char * myfifo =
"/tmp/myfifo";
// Creating the
named file(FIFO)
//
mkfifo(<pathname>, <permission>)
mkfifo(myfifo,
0666);
char arr1[80], arr2[80];
// Open FIFO for
write only
fd = open(myfifo,
O_WRONLY);
// Take an input
arr2ing from user.
// 80 is maximum
length
printf("your
message: ");
fgets(arr2, 80,
stdin);
// Write the input
arr2ing on FIFO
// and close it
write(fd, arr2,
strlen(arr2)+1);
close(fd);
// Open FIFO for
Read only
fd = open(myfifo,
O_RDONLY);
// Read from
FIFO
read(fd, arr1,
sizeof(arr1));
// Print the read
message
printf("Received:
%s\n", arr1);
close(fd);
return 0;
}
The following code is for another code
file namedpipe_p2.c.
// This program reads first, then writes
#include <stdio.h>
#include <string.h>
#include <fcntl.h>
#include <sys/stat.h>
#include <sys/types.h>
#include <unistd.h>
int main()
{
int fd1;
// FIFO file path
char * myfifo = "/tmp/myfifo";
// Creating the named file(FIFO)
//
mkfifo(<pathname>,<permission>)
mkfifo(myfifo, 0666);
char str1[80], str2[80];
// First open in read only and read
fd1 = open(myfifo,O_RDONLY);
read(fd1, str1, 80);
// Print the read string and close
printf("Received: %s\n", str1);
close(fd1);
// Now open in write mode and write
// string taken from user.
fd1 = open(myfifo,O_WRONLY);
printf("your message: ");
fgets(str2, 80, stdin);
write(fd1, str2, strlen(str2)+1);
close(fd1);
return 0;
}
Join a community of subject matter experts. Register for FREE to view solutions, replies, and use search function. Request answer by replying!
Post Reply