OS Program 4
OS Program 4
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <unistd.h>
#include <fcntl.h>
#include <sys/types.h>
#include <sys/stat.h>
int main() {
int fd;
char buffer[BUFSIZ];
// 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;
}
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;
}
close(fd);
}
return 0;
}