Hi
I am trying to write a named pipe and read from other end. The child process creates the pipe and parent tries to read. But it does not give any output! Here is the code
Code:
#define BUFSIZE 256
int main()
{
pid_t pid;
char buf[BUFSIZE];
int fd;
pid=fork();
if (pid == 0)
{
int fd1;
if (mkfifo("/tmp/fifo_file", 0666)<0)
{
perror("mkfifo");
exit(1);
}
if ((fd=open("/tmp/fifo_file",O_WRONLY))<0)
{
perror("open(1)");
exit(1);
}
if ((fd1=open("./source.txt",O_RDONLY))<0)
{
perror("open(2)");
exit(1);
}
while(read(fd1,buf,BUFSIZE)>0)
{
if (write(fd,buf,strlen(buf))<0)
{
perror("write");
exit(1);
}
usleep(rand()%100000);
}
} else if (pid > 0)
{
int ret;
if ((fd=open("/tmp/fifo_file",O_RDONLY|O_NONBLOCK))<0)
{
perror("open(3)");
exit(1);
}
while(read(fd,buf,BUFSIZE)>0)
{
fputs(buf,stdout);
usleep(rand()%100000);
}
waitpid(pid,&ret,0);
} else
{
perror("fork");
exit(1);
}
return 0;
}
The code runs with blocking IO, but what is the problem with nonblockin mode here?
thanks in advice....