program 4
//WRITER
#include <stdio.h>
#include <fcntl.h>
#include <sys/stat.h>
#include <sys/types.h>
#include <unistd.h>
int main() {
int fd;
char buf[1024] = "Hello World";
char *myfifo = "Absolute path to a folder";
// Create the FIFO (named pipe)
mkfifo(myfifo, 0666);
printf("Run Reader process to read the FIFO File\n");
// Open the FIFO for writing
fd = open(myfifo, O_WRONLY);
if (fd == -1) {
perror("open");
return 1;
}
// Write data to the FIFO
if (write(fd, buf, sizeof(buf)) == -1) {
perror("write");
close(fd);
return 1;
}
// Close the FIFO
close(fd);
return 0;
}
//READER
#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;
char *myfifo = "Absolute path to a folder";
char buf[MAX_BUF];
// Open the FIFO for reading
fd = open(myfifo, O_RDONLY);
if (fd == -1) {
perror("open");
return 1;
}
// Read data from the FIFO
if (read(fd, buf, MAX_BUF) == -1) {
perror("read");
close(fd);
return 1;
}
// Display the message read from the FIFO
printf("Reader process has read: %s\n", buf);
// Close the FIFO
close(fd);
return 0;
}
Comments
Post a Comment