Search code examples
clinuxpipeposix-select

Select loop in linux - how to make it better?


I have select with N pipe, my problem: After catch ,,one" pipe - select don't work. Probably I don't remove i descriptor. And here is my questions:
1) Do I really have to make dynamic array like vector in C and insert and remove i
2) How to make my code better? How Can I solve problem with select?

My code:

while(1)
        {
                sel = select(val+1,&set,NULL,NULL,&timeout);

                if(sel < 0)
                        perror("Blad funkcji select");
                else if(sel == 0)
                        printf("Brak komunikatow \n");
                else{

                        for(i = 0; i < val; i++)
                        {
                                if(FD_ISSET(fd[i][0],&set))
                                {
                                   while(read(fd[i][0],&buf,rozmiar) > 0)
                                   write(1,&buf,rozmiar);


                                } // check if exist and write to stdout


                } // end SELECT
                for(i = 0, j =0; i< val; i++)
                {
                        FD_SET(fd[i][0], &set);

                }

                timeout.tv_sec = 4;
                timeout.tv_usec = 0;
        }

Solution

  • RETURN VALUE top

       On success, select() and pselect() return the number of file
       descriptors contained in the three returned descriptor sets (that is,
       the total number of bits that are set in readfds, writefds,
       exceptfds) which may be zero if the timeout expires before anything
       interesting happens.  On error, -1 is returned, and errno is set to
       indicate the error; the file descriptor sets are unmodified, and
       timeout becomes undefined.
    
     if(sel ==-1)
                    perror("Blad funkcji select");