/MY PROGRAM ARE COMPILED IN FEDORA15 ..WITH 4.6 gcc compiler...../
/* 1ST WRITER */
#include<stdio.h>
#include<stdlib.h>
#include<fcntl.h>
#include<sys/ipc.h>
#include<unistd.h>
#include<sys/types.h>
int main()
{
int ret,wfd,count=0;
int buff=5;
int a=10;
int b=15;
ret=mkfifo("anu",777|IPC_CREAT);
if(ret==0)
printf("fifo created\n");
wfd=open("anu",O_WRONLY);//after opening ...if it finds a reader...it will write data to fifo untill its limit exceeds ...when limit came...it will allow reader to read ...then again starts wriiting to fifo..
printf("wfd=%d\n",wfd);
count=write(wfd,&buff,4);
printf("count=%d buff=%d\n",count,buff);
//fd=open("anu",O_WRONLY);
//printf("fd=%d\n",fd);
close(wfd);
wfd=open("anu",O_WRONLY);//same reader will works for no. of writers
printf("wfd=%d\n",wfd);
count=write(wfd,&a,4);
printf("count=%d a=%d\n",count,a);
close(wfd);//it will make no diffrence if i opened same fifo after closing....data will remain in that fifo......
wfd=open("anu",O_WRONLY);
printf("wfd=%d\n",wfd);
count=write(wfd,&b,4);
printf("count=%d b=%d\n",count,b);
close(wfd);
sleep(4);
return 0;//once all writers process is terminated..so no writer is there ...means reader will not be able to read..data from fifo will be erased ....& then again it opens demands a reader to read to come out of block on open position
wfd=open("anu",O_WRONLY);//after opening ...if it finds a reader...it will write data to fifo untill its limit exceeds ...when limit came...it will allow reader to read ...then again starts wriiting to fifo..
printf("wfd=%d\n",wfd);
count=write(wfd,&buff,4);
printf("count=%d\nbuff=%d\n",count,buff);
//fd=open("anu",O_WRONLY);
//printf("fd=%d\n",fd);
close(wfd);
wfd=open("anu",O_WRONLY);//same reader will works for no. of writers
printf("wfd=%d\n",wfd);
count=write(wfd,&a,4);
printf("count=%d\na=%d\n",count,a);
close(wfd);//it will make no diffrence if i opened same fifo after closing....data will remain in that fifo......
wfd=open("anu",O_WRONLY);
printf("wfd=%d\n",wfd);
count=write(wfd,&b,4);
printf("count=%d\nb=%d\n",count,b);
close(wfd);
sleep(1);
return 0;//once writer process is terminated & means reader will not be able to read...data from fifo will be erased ....& then again it opens demands a reader to read to come out of block on open position
wfd=open("anu",O_WRONLY);//after opening ...if it finds a reader...it will write data to fifo untill its limit exceeds ...when limit came...it will allow reader to read ...then again starts wriiting to fifo..
printf("wfd=%d\n",wfd);
count=write(wfd,&buff,4);
printf("count=%d\nbuff=%d\n",count,buff);
//fd=open("anu",O_WRONLY);
//printf("fd=%d\n",fd);
close(wfd);
wfd=open("anu",O_WRONLY);//same reader will works for no. of writers
printf("wfd=%d\n",wfd);
count=write(wfd,&a,4);
printf("count=%d\na=%d\n",count,a);
close(wfd);//it will make no diffrence if i opened same fifo after closing....data will remain in that fifo......
wfd=open("anu",O_WRONLY);
printf("wfd=%d\n",wfd);
count=write(wfd,&b,4);
printf("count=%d\nb=%d\n",count,b);
close(wfd);
sleep(1);
return 0;//once writer process is terminated & means reader will not be able to read...data from fifo will be erased ....& then again it opens demands a reader to read to come out of block on open position
/* LAST & WONDERFUL.....READING IN FOR LOOP TO ALL 3 THREE WRITERS IN SYNCHRONISED WAY */
/Every new process reader is for new process writer ......it does not matters either i m opening fifo..... again & again after closing wfd....in one process ...to read data by executing another program no. of times....means every time new reader of process will demand a new writer of process ..
#include<stdio.h>
#include<stdlib.h>
#include<fcntl.h>
#include<unistd.h>
int main()
{
//sleep(2);
int i=0,fd;
int count,buf,a,b;
for(i=1;i<4;i++)
{
printf("**********i=%d**********\n",i);
fd=open("anu",O_RDONLY);//when both writer & reader r executing ...untill then ...reader can read the written data......otherwise again operating same reader will demand new writer ...that shold open now..& start wriiting data freshly...
printf("fd=%d\n",fd);
read(fd,&buf,4);//reader is able to read only upto the time in millisecond...untill there is writer..& no writting program or process is finished.....if i had made delay of 1 sec at staring of the this reader....untill then writer completes its work..& finished...no wait by writer untill fifo is full...then after that data will not be read....writer should be there if u want to read data from fifo....
If writing is one by one ...LIKE....writer1 && writer2 && writer3...........then reader will read all 3 writers in for loop..... If writing is at same instance & semaphore is not used.....LIKE....writer1 & writer2 & writer3.....reader will be able to read only one of the writer