Reputation: 389
I have two process.
One will first Read then write.
Other one will first write then read.
And I want to implement it using two pipes.
Here are my implementations
/***READ BEFORE WRITE*****/
#include <fcntl.h>
#include <stdio.h>
#include <sys/stat.h>
#include <unistd.h>
#define MAX_BUF 1024
int main()
{
int fd,fd1;
char buf[MAX_BUF];
char * myfifo = "/home/aditya/Desktop/myfifo";
char * mynewfifo = "/home/aditya/Desktop/mynewfifo";
mkfifo(mynewfifo, 0666);
printf("Read before writing\n");
printf("Before opening\n");
fd = open(myfifo, O_RDONLY);
fd1 = open(mynewfifo, O_WRONLY);
printf("After opening\n");
read(fd, buf, MAX_BUF);
printf("Received: %s\n", buf);
close(fd);
printf("reader is writing\n");
write(fd1, "Hi", sizeof("Hi"));
close(fd1);
unlink(mynewfifo);
return 0;
}
/**** Wriiting after Reading ******/
#include <fcntl.h>
#include <stdio.h>
#include <sys/stat.h>
#include <sys/types.h>
#include <unistd.h>
int main()
{
int fd,fd1;
char * myfifo = "/home/aditya/Desktop/myfifo";
char * mynewfifo = "/home/aditya/Desktop/mynewfifo";
char buf[1024];
mkfifo(myfifo, 0666);
printf("writing before reading\n");
printf("Before opening\n");
fd = open(myfifo, O_WRONLY);
fd1=open(mynewfifo,O_RDONLY);
printf("After opening\n");
write(fd, "Hi", sizeof("Hi"));
close(fd);
printf("Writer is reading\n");
read(fd1, buf, 1024);
printf("Received: %s\n", buf);
close(fd1);
/* remove the FIFO */
unlink(myfifo);
return 0;
}
When i run the "Write after read" first and then "Read after write",it seems to work.
But when i do the vice versa(that is run "Read after Write") they print "Before opening" and continue to run.
Please explain what am i doing wrong.
Upvotes: 0
Views: 530
Reputation: 169
The problem is that your READ AFTER WRITE program reads from a named pipe myfifo which has not been yet created. When you try to execute your READ AFTER WRITE program at first place. I added the following two conditions(see the code below) in your program to check if there is a problem in your name pipes and a problem did pop out that your myfifo pipe was not created when you first execute your READ AFTER WRITE program.
Now what happens is that since the pipe was not created, so your READ AFTER WRITE keeps waiting for some other process to write so that is can read something. Plus, on the other hand , your second program also keeps standing on "opening..." because there is no reader on the other hand to read what it is supposed to write.
Now when you execute your second program first, this problem gets solved since the myfifo pipe is already created for READ AFTER WRITE program to read.This is truly logical since you are trying to read from a named pipe myfifo which is created in your second program.
Thus, always do check in your program that whether the pipe you are using is created or not.
/***READ AFTER WRITE*****/
#include <fcntl.h>
#include <stdio.h>
#include <sys/stat.h>
#include <unistd.h>
#define MAX_BUF 1024
int main()
{
int fd,fd1;
char buf[MAX_BUF];
char * myfifo = "/home/aisha/Desktop/myfifo";
char * mynewfifo = "/home/aisha/Desktop/mynewfifo";
mkfifo(mynewfifo, 0666);
printf("Read before writing\n");
printf("Before opening\n");
fd = open(myfifo, O_RDONLY);
if(fd<0)
{
printf("MYFIFO NOT MADE\n");
}
fd1 = open(mynewfifo, O_WRONLY);
if(fd1<0)
{
printf("MYNEWFIFO NOT MADE\n");
}
sleep (2);
printf("After opening\n");
read(fd, buf, MAX_BUF);
printf("Received: %s\n", buf);
close(fd);
printf("reader is writing\n");
write(fd1, "Hi", sizeof("Hi"));
close(fd1);
unlink(mynewfifo);
return 0;
}
Upvotes: 1