0% found this document useful (0 votes)
10 views2 pages

OS Program 4

The document provides a C program that demonstrates interprocess communication using a named pipe (FIFO) between a writer and a reader process. The writer process takes user input and sends it through the FIFO, while the reader process reads and displays the data. The program utilizes mkfifo, open, read, write, and close system calls to facilitate this communication.

Uploaded by

karthikrm2468
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
10 views2 pages

OS Program 4

The document provides a C program that demonstrates interprocess communication using a named pipe (FIFO) between a writer and a reader process. The writer process takes user input and sends it through the FIFO, while the reader process reads and displays the data. The program utilizes mkfifo, open, read, write, and close system calls to facilitate this communication.

Uploaded by

karthikrm2468
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 2

Program 4.

Develop a 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.

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

#define FIFO_NAME "myfifo"

int main() {
int fd;
char buffer[BUFSIZ];

// Create the FIFO (named pipe) if it doesn't already exist


if (mkfifo(FIFO_NAME, 0666) == -1) {
perror("mkfifo");
exit(EXIT_FAILURE);
}

// Writer process
if (fork() == 0) {
printf("Writer process is running. Enter data to write (type 'exit' to quit):\n");

fd = open(FIFO_NAME, O_WRONLY);
if (fd == -1) {
perror("open");
exit(EXIT_FAILURE);
}

while (1) {
fgets(buffer, BUFSIZ, stdin);

if (strcmp(buffer, "exit\n") == 0) {
break;
}

write(fd, buffer, strlen(buffer) + 1);


}

close(fd);
}
// Reader process
else {
printf("Reader process is running. Reading data from the FIFO:\n");

fd = open(FIFO_NAME, O_RDONLY);
if (fd == -1) {
perror("open");
exit(EXIT_FAILURE);
}

while (1) {
if (read(fd, buffer, BUFSIZ) == 0) {
break;
}

printf("Received: %s", buffer);


}

close(fd);
}

return 0;
}

You might also like

pFad - Phonifier reborn

Pfad - The Proxy pFad of © 2024 Garber Painting. All rights reserved.

Note: This service is not intended for secure transactions such as banking, social media, email, or purchasing. Use at your own risk. We assume no liability whatsoever for broken pages.


Alternative Proxies:

Alternative Proxy

pFad Proxy

pFad v3 Proxy

pFad v4 Proxy