0

I have two pipes and I need to verify which one is ready to be read. The problem is with the nfds parameter. If i set the parameter like:

pipeStore[0] + 1

I can only use pipeStore and the same with the other pipe

    int max_fd;
    fd_set rpipe_fds;
    struct timeval tv;
    int retval;
    int store_index = 0;
    int vector[100];

    FD_ZERO(&rpipe_fds); 
    FD_SET(pipeRead[0], &rpipe_fds); 
    FD_SET(pipeStore[0], &rpipe_fds);


    tv.tv_sec= 100 ; 
    tv.tv_usec= 0 ;
    int val_max;
    if(pipeRead[0] >= pipeStore[0])
        val_max = pipeRead[0] + 1;
    else 
        val_max = pipeStore[0] + 1;


        retval = select(val_max,&rpipe_fds,NULL,NULL,&tv);
        if(retval)
        {

            if(FD_ISSET(pipeStore[0],&rpipe_fds))
            {

                read(pipeStore[0], store_vector, sizeof(store_vector));

                vector[store_vector[0]] = store_vector[1];
                printf("Stored %d at position %d\n",vector[store_vector[0]],store_vector[1]);
            }

            if(FD_ISSET(pipeRead[0],&rpipe_fds) )
            {
                read(pipeRead[0], &store_index, sizeof(int));
                int val_to_return = vector[store_index];
                write(pipeStore[1], &val_to_return, sizeof(int));            
            }  
        `}

So I tried with the code above choosing the max with that simple alghoritm but nothing: I can only verify the change in one pipe at the same time. Can you help me?

  • 1
    Please present a [mcve]. What you have described in terms of setting `nfds` is correct and should work. The problem is more likely that you are not resetting `rpipe_fds` after each`select`. – kaylum Jul 02 '17 at 00:26
  • 1
    "I can only verify the change in one pipe at the same time" - what does this mean? What is your expected output, and your actual output? Your question doesn't contain enough information to answer it. – Crowman Jul 02 '17 at 00:26
  • See [Are there any platforms where using structure copy on an `fd_set` (for `select()` or `pselect()`) causes problems?](https://stackoverflow.com/questions/2421672/) Also [What is the `nfds` from `select()` used for?](https://stackoverflow.com/questions/8695678/) – Jonathan Leffler Jul 02 '17 at 00:31
  • Also [What are the differences between `poll()` and `select()`?](https://stackoverflow.com/questions/970979) and [Increasing the limit of `FD_SETSIZE` and `select()`](https://stackoverflow.com/questions/7976388). – Jonathan Leffler Jul 02 '17 at 00:41

0 Answers0