50% found this document useful (2 votes)
83 views

Mkfifo

The document describes a C/C++ program that demonstrates interprocess communication between a reader process and a writer process using named pipes. The writer process creates a FIFO, writes the string "Hi" to it, and then closes and removes the FIFO. The reader process opens the FIFO for reading, reads the string from it, and prints it out.
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
50% found this document useful (2 votes)
83 views

Mkfifo

The document describes a C/C++ program that demonstrates interprocess communication between a reader process and a writer process using named pipes. The writer process creates a FIFO, writes the string "Hi" to it, and then closes and removes the FIFO. The reader process opens the FIFO for reading, reads the string from it, and prints it out.
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
You are on page 1/ 2

4.

Write a C/C++ program which demonstrates interprocess communication between a reader process
and a writer process. Use mkfifo, open, read, write and close APIs in your program.

/*Writer Process*/

#include <stdio.h>
#include <fcntl.h>
#include <sys/stat.h>
#include <sys/types.h>
#include <unistd.h>

int main()
{
int fd;
char buf[1024];

/* create the FIFO (named pipe) */


char * myfifo = "/tmp/myfifo";
mkfifo(myfifo, 0666);
printf("Run Reader process to read the FIFO File\n");
fd = open(myfifo, O_WRONLY);
write(fd,"Hi", sizeof("Hi"));

/* write "Hi" to the FIFO */


close(fd);
unlink(myfifo); /* remove the FIFO */
return 0;
}
/* Reader Process */

#include <fcntl.h>
#include <sys/stat.h>
#include <sys/types.h>
#include <unistd.h>
#include <stdio.h>

#define MAX_BUF 1024

int main()
{
int fd;

/* A temp FIFO file is not created in reader */


char *myfifo = "/tmp/myfifo";
char buf[MAX_BUF];

/* open, read, and display the message from the FIFO */


fd = open(myfifo, O_RDONLY);
read(fd, buf, MAX_BUF);
printf("Writer: %s\n", buf);
close(fd);
return 0;
}

You might also like