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

Program 4

Uploaded by

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

Program 4

Uploaded by

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

/*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./

A). Write first

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

int main() {
int fd;
char *myfifo = "/tmp/myfifo";
mkfifo(myfifo, 0666);
char arr1[80], arr2[80];

while (1) {
fd = open(myfifo, O_WRONLY);
fgets(arr2, 80, stdin);
write(fd, arr2, strlen(arr2)+1);
close(fd);

fd = open(myfifo, O_RDONLY);
read(fd, arr1, sizeof(arr1));
printf("User2: %s\n", arr1);
close(fd);
}
return 0;
}

B). Read First

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

int main() {
int fd1;
char *myfifo = "/tmp/myfifo";
mkfifo(myfifo, 0666);
char str1[80], str2[80];

while (1) {
fd1 = open(myfifo, O_RDONLY);
read(fd1, str1, 80);
printf("User1: %s\n", str1);
close(fd1);

fd1 = open(myfifo, O_WRONLY);


fgets(str2, 80, stdin);
write(fd1, str2, strlen(str2)+1);
close(fd1);
}
return 0;
}

You might also like