select <0时调用exit(0),为什么程序没有退出,而是继续执行select了?

qsxiaoyao 2008-12-20 04:54:09
公司产品上有段以前的代码,在select <0时调用exit(0),为什么程序没有退出,而是继续执行select了?
...全文
153 10 打赏 收藏 转发到动态 举报
写回复
用AI写文章
10 条回复
切换为时间正序
请发表友善的回复…
发表回复
qsxiaoyao 2008-12-22
  • 打赏
  • 举报
回复
还有就是不管用exit(0)还是continue,进程都能正常工作,板子工作也正常,通过webserver访问也能看到视频听到音频
qsxiaoyao 2008-12-22
  • 打赏
  • 举报
回复
[Quote=引用 8 楼 guosha 的回复:]
没看明白啥意思,有没有定义CONFIG_WATCHDOG?

另外并不是select返回小于0就肯定是出错了,select被信号中断时也是会返回小于0的
[/Quote]

CONFIG_WATCHDOG没有定义,是我想在这个进程中起用看门狗加的,这时候发现那个exit(0)会引起看门狗不喂狗而板子重启,所以改成了continue;但这时select<0的那个printf就会一直打印出来。
我也觉得是信号量或中断引起的<0,但就是不明白为什么开始用exit(0)后就不会打印出selece<0的信息
快乐田伯光 2008-12-22
  • 打赏
  • 举报
回复
没看明白啥意思,有没有定义CONFIG_WATCHDOG?

另外并不是select返回小于0就肯定是出错了,select被信号中断时也是会返回小于0的
qsxiaoyao 2008-12-22
  • 打赏
  • 举报
回复
代码如上,在select<0的那个printf每次会打印出来,如果用exit(0),以后就不会再进到这来,但程序没有退出,一直在执行。
如果用continue,则经常会进入到<0这来。
qsxiaoyao 2008-12-22
  • 打赏
  • 举报
回复
int main(int argc, char **argv)
{
fd_set rfds, wfds;
struct timeval tv;
int max_sock, retval;

#ifdef CONFIG_WATCHDOG
/*******************watchdog init*********************/
if ((fd = open("/dev/wdt", O_RDWR)) < 0) {
printf("Open /dev/wdt error!\n");
return -1;
}

signal(SIGUSR1, disable_wdt);

ioctl(fd, WDT_SET_TIMEOUT, 353200);
ioctl(fd, WDT_SET_MODE, 1);
ioctl(fd, WDT_ENABLE, 1);

init_sigaction();
init_time();
/*******************watchdog init********************/
#endif

init();

signal(SIGTERM, term_handler);

if (open_server_sock() < 0) {
printf("Open socket error!\n");
return -1;
}

while(1) {
FD_ZERO(&rfds);
FD_ZERO(&wfds);
max_sock = 0;

/*read a frame for sending. 30 frames per second*/
av_read_frame();

select_server_sock(&max_sock, &rfds, &wfds, NULL);
select_parse_conn(&max_sock, &rfds, &wfds, NULL);
select_redir_conn(&max_sock, &rfds, &wfds, NULL);
select_av_conn(&max_sock, &rfds, &wfds, NULL);
select_audio_conn(&max_sock, &rfds, &wfds, NULL);

tv.tv_sec = 0;
tv.tv_usec = 33333;

retval = select(max_sock + 1, &rfds, &wfds, NULL, &tv);

if (retval == 0) {
// timeout process
//puts("main() : select = 0");
continue;
}

if (retval < 0) {
printf("ipcamd: error on select: %s\n", strerror(errno));
#ifdef CONFIG_WATCHDOG
continue;
#else
exit(0);
#endif
}
process_server_sock(&rfds, &wfds, NULL);
process_parse_conn(&rfds, &wfds, NULL);
process_redir_conn(&rfds, &wfds, NULL);
process_av_conn(&rfds, &wfds, NULL);
process_audio_conn(&rfds, &wfds, NULL);
}

return 0;
}
cceczjxy 2008-12-22
  • 打赏
  • 举报
回复
那换_exit()看看
hngsc_0 2008-12-20
  • 打赏
  • 举报
回复
select 一般不会出错,查看下你的代码,估计不是小于0了,很有可能是超时了
正常返回的是数据准备好的文件描述符或者超时返回
outblue 2008-12-20
  • 打赏
  • 举报
回复
select成功返回正数,超时返回0,出错返回负数。你怎么知道select出错了呢?
threeleafzerg007 2008-12-20
  • 打赏
  • 举报
回复
完整代码 基本如果你gdb单步调到exit(0);程序一定会推出个 所以给我们看完整代码。。。
qsxiaoyao 2008-12-20
  • 打赏
  • 举报
回复
在exit(0);下面再加一个exit(0);时,程序就会退出,这是为什么了?

23,128

社区成员

发帖
与我相关
我的任务
社区描述
Linux/Unix社区 应用程序开发区
社区管理员
  • 应用程序开发区社区
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告
暂无公告

试试用AI创作助手写篇文章吧