C 选择linux中的循环-如何使其更好?

C 选择linux中的循环-如何使其更好?,c,linux,select,pipe,C,Linux,Select,Pipe,我选择了N管道,我的问题是: 捕获后,一个“管道”-选择“不工作”。可能我没有删除I 描述符。下面是我的问题: 1) 我真的需要像C中的vector那样制作动态数组,并插入和删除I 2) 如何使我的代码更好?如何解决select的问题 我的代码: while(1) { sel = select(val+1,&set,NULL,NULL,&timeout); if(sel < 0)

我选择了N管道,我的问题是: 捕获后,一个“
管道”
-选择“不工作”。可能我没有删除
I
描述符。下面是我的问题:
1) 我真的需要像C中的
vector
那样制作动态数组,并插入和删除
I

2) 如何使我的代码更好?如何解决select的问题

我的代码:

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;
        }
while(1)
{
sel=select(val+1,&set,NULL,NULL,&timeout);
如果(sel<0)
佩罗尔(“Blad funkcji select”);
else如果(sel==0)
printf(“Brak komunikatow\n”);
否则{
对于(i=0;i0)
书写(1,&buf,rozmiar);
}//检查是否存在并写入标准输出
}//结束选择
对于(i=0,j=0;i
返回值上限

   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.

您需要保存并使用“代码>阅读<代码>的结果计数。您应该考虑使用<代码>投票/代码>而不是<代码>选择 <代码> VAL+ 1 是错误的,您需要MAX FD加上一个。请扔掉<代码>选择()/<代码>,使用<代码> PLEVER()
相反。它同样可移植、更高效、更易于使用。值得注意的是,您不必在每次调用之前重新生成感兴趣的fd列表。您也不必传入MaxFD。好的,但如何使用它?您可以展示一些代码示例吗?我可能应该使用sel并删除fd。。。
 if(sel ==-1)
                perror("Blad funkcji select");