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

Recommended Answers

All 10 Replies

The FIFO will never have input until you write something to it. Since you don't write anything to it, select() will always say that there is no input waiting.

Remember, a FIFO must be open at both ends (which you have done), and to read data you must first write data (which you haven't done).

Hence, the first example works because you write, then read. The second fails because you never write before select()ing to read.

Hope this helps.

Read the man page for select, and concentrate on what the first parameter should be.

but this is the thing i need to do whenever i write somthing to fifo via first program and running both simultaneously the second should respond to it.I am not supposed to write something in second program.

Your 2nd program isn't doing anything until you fix your select call.

@Salem
as u have said i read man of select again and as far as my understanding go i was passing wrong value for nfds and then i changed it to zero but yet no change in output occurs.

The first parameter is
MAX value of the fd's + 1.
Passing 1 means you're just looking for fd0 (aka stdin).

My guess is it should be i+1, but i is a terrible name for a file descriptor.

As u have said i tried with i+1 but nothing changes . Can u please give me a code for this problem .

Salem is right. I almost said something about that myself but it has been a while since I've used select() and I forgot you aren't waiting on stdin (even though your comments say you are).

To wait on the FIFO, your call should be retval = select(i+1, &fd, NULL, NULL, &tv); Typically, file descripter variables are named something like "fd". A file descriptor set variable is typically named "fds".

Hope this helps.

[edit] Oh yeah, also remember that you can't read anything until something is first written.

Another problem (or two) would be.

1. Why are you using O_RDWR on both ends?

2. In the receiver, you should test retval for success/fail before testing with FD_ISSET for any descriptors which may have received data.

Post your latest code.

Thanks Salem and Duoas the problem is solved i think the error was that i was using O_RDWR mode instead of O_RDONLY mode.
A lot of thanks .

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.