i m writing a practice code to implement select with fifo.
i m posting two codes along with :
CODE 1:
Help with Code Tags
(Toggle Plain Text)
#include <sys/types.h>
#include <sys/stat.h>
#include <stdio.h>
#include<fcntl.h>
int main()
{
int fd,rc,i;
/* Making A FIFO names as fifo2*/
char data[100];
char data1[100]="sbfdjgb";
if (mkfifo("/home/ishan/fifo2", S_IRWXU|S_IROTH) != 0)
perror("mkfifo() error");
else
puts("success!");
/* Opening A FIFO*/
if ((fd=open("/home/ishan/fifo2",O_RDWR)) < 0)
{
perror("open()error");
}
else
{
puts("success");
}
/* Writing A FIFO*/
puts("\n enter data to be written");
gets(data);
if (-1 == (rc=write(fd, data, strlen(data))))
{
perror("write failed");
return(0);
}
else
{
puts("\n*\n");
puts(data);
}
/*
if(read(fd,data1,strlen(data1))>0)
puts(data1);
else
printf("error .............. ");*/
/*
/* Close the FIFO*/
if (0 != (rc=close(fd)))
{
perror("close failed");
return(0);
}
*/
return 0;
}
this program works properly as
OUTPUT:
success
!success
enter data to be written
ishaan
*
ishaan
code 2:
to open that fifo and implementing a select on it
Help with Code Tags
(Toggle Plain Text)
#include <stdio.h>
#include <sys/time.h>
#include <sys/types.h>
#include <unistd.h>
#include <fcntl.h>
#include <sys/stat.h>
int main(void)
{
int i;
fd_set fd;
struct timeval tv;
int retval;
/* Opening A FIFO*/
if ((i=open("/home/ishan/fifo2",O_RDWR)) < 0)
{
perror("open()error");
}
else
{
puts("!success");
}
/*Watch stdin (fd 0) to see when it has input. */
FD_ZERO(&fd);
FD_SET(i, &fd);
/* Wait up to five seconds. */
tv.tv_sec = 10;
tv.tv_usec = 0;
retval = select(1, &fd, NULL, NULL, &tv);
/* Don't rely on the value of tv now! */
if(FD_ISSET(i,&fd))
printf("............ data received ");
else
printf("............ data not received");
if (retval == -1)
perror("select()");
else if (retval)
printf("Data is available now.\n");
/* FD_ISSET(0, &rfds) will be true. */
else
printf("No data within ten seconds.\n");
return 0;
}
whenever i write somthin on fifo the control data should be received but its not as
OUTPUT:
!success
............ data not receivedNo data within ten seconds.
PLEASE HELP ME WHERE I M WRONG IF POSSIBLE SEND THE CORRECT CODE