I am trying to implement a IPC using FIFO. The code for sender is as follows..
#include <stdio.h>
#include <errno.h>
#include <stdlib.h>
#include <sys/stat.h>
#include <unistd.h>
#include <string.h>
#include <fcntl.h>
#include <sys/types.h>
#define BUFFER_SIZE 100
#define LISTENER1 "listener1"
char news[BUFFER_SIZE];
void broadcast()
{
int fd1;
mkfifo(LISTENER1,0644);
fd1=open(LISTENER1,O_WRONLY);
printf("\nReady to broadcast\n");
do {
printf("\nEnter message : ");
scanf("%s",news);
write(fd1,news,strlen(news));
} while (1);
return;
}
int main()
{
broadcast();
return 0;
}
The code for receiver is as follows..
#include <stdio.h>
#include <errno.h>
#include <stdlib.h>
#include <sys/stat.h>
#include <unistd.h>
#include <string.h>
#include <fcntl.h>
#include <sys/types.h>
#define BUFFER_SIZE 100
#define LISTENER "listener1"
char news[BUFFER_SIZE];
void receive()
{
int fd1,num;
mkfifo(LISTENER,0644);
fd1=open(LISTENER,O_RDONLY);
while((num=read(fd1,news,BUFFER_SIZE))>0)
{
news[num]='\0';
printf("\n Received : %s",news);
}
return;
}
int main()
{
receive();
return 0;
}
I run the two programs in two terminal window. The problem i am facing is that the messages are not getting delivered intently. Suppose i type a message in the window of the sender, it does not appear in the receiver's window until and unless i type the next message in the sender's window. I want that the messages be delivered as soon as the return key is pressed. Please help!