gpt4 book ai didi

c - Linux 中的 Select 循环——如何让它变得更好?

转载 作者:太空宇宙 更新时间:2023-11-04 12:59:17 25 4
gpt4 key购买 nike

我选择了 N 个管道,我的问题是:捕获后,一个"pipe - 选择不起作用。可能我没有删除 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;
}

最佳答案

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");

关于c - Linux 中的 Select 循环——如何让它变得更好?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/34695650/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com