linux select return 0

linux select return 0 

试图启动一个进程,第一次return 1, 后面都return 0 


          zoombie_pid = waitpid(-1, &child_status, WNOHANG);  

WNOHANG 若pid指定的子进程没有结束,则waitpid()函数返回0,不予以等待。若结束,则返回该子进程的ID。持续返回值为PID,说明这个子进程已经结束。

Jun 21 22:01:31 lc4 DENG: select_ret: 0x1, num_fds : 0xc,
Jun 21 22:01:31 lc4 DENG: go event handler
Jun 21 22:01:32 lc4 DENG: select_ret: 0x0, num_fds : 0xc,
Jun 21 22:01:33 lc4 DENG: select_ret: 0x0, num_fds : 0xc,




定义函数 pid_t waitpid(pid_t pid,int * status,int options);

函数说明

编辑
如果在调用 waitpid()时子进程已经结束,则 waitpid()会立即
返回子进程结束状态值。 子进程的结束状态值会由参数 status 返回,
而子进程的进程识别码也会一起返回。如果不在意结束状态值,则
参数 status 可以设成 NULL。参数 pid 为欲等待的子进程识别码,
其他数值意义如下:
pid<-1 等待 进程组识别码为 pid 绝对值的任何子进程。
pid=-1 等待任何子进程,相当于 wait()。
pid=0 等待进程组识别码与目前进程相同的任何子进程。
pid>0 等待任何子进程识别码为 pid 的子进程。
参数options提供了一些额外的选项来控制waitpid,参数 option 可以为 0 或可以用"|"运算符把它们连接起来使用,比如:
ret=waitpid(-1,NULL,WNOHANG | WUNTRACED);
如果我们不想使用它们,也可以把options设为0,如:
ret=waitpid(-1,NULL,0);
WNOHANG 若pid指定的子进程没有结束,则waitpid()函数返回0,不予以等待。若结束,则返回该子进程的ID。
WUNTRACED 若子进程进入暂停状态,则马上返回,但子进程的结束状态不予以理会。WIFSTOPPED(status)宏确定返回值是否对应与一个暂停子进程。
子进程的结束状态返回后存于 status,底下有几个宏可判别结束情况:
WIFEXITED(status)如果若为正常结束子进程返回的状态,则为真;对于这种情况可执行WEXITSTATUS(status),取子进程传给exit或_eixt的低8位。
WEXITSTATUS(status)取得子进程 exit()返回的结束代码,一般会先用 WIFEXITED 来判断是否正常结束才能使用此宏。
WIFSIGNALED(status)若为异常结束子进程返回的状态,则为真;对于这种情况可执行WTERMSIG(status),取使子进程结束的信号编号。
WTERMSIG(status) 取得子进程因信号而中止的信号代码,一般会先用 WIFSIGNALED 来判断后才使用此宏。
WIFSTOPPED(status) 若为当前暂停子进程返回的状态,则为真;对于这种情况可执行WSTOPSIG(status),取使子进程暂停的信号编号。
WSTOPSIG(status) 取得引发子进程暂停的信号代码,一般会先用 WIFSTOPPED 来判断后才使用此宏。
如果执行成功则返回子进程识别码(PID) ,如果有错误发生则返回
返回值-1。失败原因存于 errno 中。

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
/******
* waitpid.c - Simple wait usage
*********/
#include <unistd.h>
#include <sys/types.h>
#include <sys/wait.h>
#include <stdio.h>
#include <stdlib.h>
int  main(  void  )
{
     pid_t childpid;
     int  status;
     childpid = fork();
if  ( childpid < 0 )
{
     perror "fork()"  );
     exit ( EXIT_FAILURE );
}
else  if  ( childpid == 0 )
{
     puts "In child process"  );
     sleep( 3 ); //让子进程睡眠3秒,看看父进程的行为 <--等三秒。。。。
     printf ( "\tchild pid = %d\n" , getpid());
     printf ( "\tchild ppid = %d\n" , getppid());
     exit (EXIT_SUCCESS);
}
else
{
     waitpid( childpid, &status, 0 );
     puts "in parent"  );
     printf "\tparent pid = %d\n" , getpid() );
     printf "\tparent ppid = %d\n" , getppid() );
     printf "\tchild process exited with status %d \n" , status );
}
exit (EXIT_SUCCESS);
}
1
2
3
4
5
6
7
8
9
10
[root@localhost src] # gcc waitpid.c
[root@localhost src] # ./a.out
In child process
child pid = 4469
child ppid = 4468
in  parent
parent pid = 4468
parent ppid = 4379
child process exited with status 0
[root@localhost src] #
如果将上面“waitpid( childpid, &status, 0 );”行注释掉,程序执行效果如下:
1
2
3
4
5
6
7
8
[root@localhost src] # ./a.out
In child process
in  parent
parent pid = 4481
parent ppid = 4379
child process exited with status1
[root@localhost src] # child pid = 4482
child ppid = 1
子进程还没有退出, 父进程已经退出了。

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
`select()` 是一个在 Linux 系统中提供的函数,用于检查多个文件描述符(sockets、标准输入输出、文件等)是否处于可读、可写或者异常等状态。 函数原型如下: ```c #include <sys/select.h> int select(int nfds, fd_set *readfds, fd_set *writefds, fd_set *exceptfds, struct timeval *timeout); ``` 参数说明: - `nfds`:所有文件描述符集合中的最大描述符加 1。 - `readfds`:可读文件描述符的集合。 - `writefds`:可写文件描述符的集合。 - `exceptfds`:异常文件描述符的集合。 - `timeout`:超时时间,如果为 NULL,则表示一直等待。 函数返回值: - 大于 0:有文件描述符发生了变化,可以进行 I/O 操作。 - 等于 0:超时,没有文件描述符发生变化。 - 小于 0:发生错误。 在使用 `select()` 函数前,需要创建一个文件描述符集合,用 `FD_ZERO()` 函数将其全部清零,然后使用 `FD_SET()` 函数将需要检查的文件描述符添加到集合中。 示例代码: ```c #include <stdio.h> #include <stdlib.h> #include <sys/select.h> #include <unistd.h> int main() { int fd[2]; pipe(fd); // 创建管道 fd_set readfds; FD_ZERO(&readfds); // 初始化集合 FD_SET(fd[0], &readfds); // 将读端添加到集合中 struct timeval timeout; timeout.tv_sec = 5; // 设置超时时间为 5 秒 timeout.tv_usec = 0; int ret = select(fd[0] + 1, &readfds, NULL, NULL, &timeout); // 检查文件描述符是否可读 if (ret == -1) { perror("select error"); exit(EXIT_FAILURE); } else if (ret == 0) { printf("timeout\n"); } else { printf("fd[0] is ready for read\n"); } return 0; } ``` 上述代码中,使用了 `pipe()` 函数创建了一个管道,然后使用 `FD_ZERO()` 和 `FD_SET()` 函数初始化了文件描述符集合并将读端添加到集合中,最后使用 `select()` 函数检查文件描述符是否可读,如果超时则输出 "timeout",否则输出 "fd[0] is ready for read"。

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值