使用的waitpid()示例? [英] Example of waitpid() in use?

查看:150
本文介绍了使用的waitpid()示例?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我知道waitpid()用于等待进程完成,但是如何准确地使用它呢?

I know that waitpid() is used to wait for a process to finish, but how would one use it exactly?

我想做的是,创建两个孩子,等待第一个孩子完成,然后杀死第二个孩子,然后退出.

Here what I want to do is, create two children and wait for the first child to finish, then kill the second child before exiting.

//Create two children
pid_t child1;
pid_t child2;
child1 = fork();

//wait for child1 to finish, then kill child2
waitpid() ... child1 {
kill(child2) }

推荐答案

waitpid()的语法:

pid_t waitpid(pid_t pid, int *status, int options);

pid的值可以是:

  • < -1 :等待任何进程组ID等于pid的绝对值的子进程.
  • -1 :等待任何子进程.
  • 0 :等待任何进程组ID与调用进程的ID相等的子进程.
  • > 0 :等待进程ID等于pid值的子级.
  • < -1: Wait for any child process whose process group ID is equal to the absolute value of pid.
  • -1: Wait for any child process.
  • 0: Wait for any child process whose process group ID is equal to that of the calling process.
  • > 0: Wait for the child whose process ID is equal to the value of pid.

options的值是以下常量的零个或多个的或:

The value of options is an OR of zero or more of the following constants:

  • WNOHANG:如果没有孩子退出,则立即返回.
  • WUNTRACED:如果孩子已经停止,还返回.即使未指定此选项,也会提供已停止的跟踪子项的状态.
  • WCONTINUED:如果通过交付SIGCONT恢复了被阻止的孩子,也将返回.
  • WNOHANG: Return immediately if no child has exited.
  • WUNTRACED: Also return if a child has stopped. Status for traced children which have stopped is provided even if this option is not specified.
  • WCONTINUED: Also return if a stopped child has been resumed by delivery of SIGCONT.

要获取更多帮助,请使用man waitpid.

For more help, use man waitpid.

这篇关于使用的waitpid()示例?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆