如何从C程序检查分叉的进程是否仍在运行 [英] How to check if a forked process is still running from the c program

查看:47
本文介绍了如何从C程序检查分叉的进程是否仍在运行的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个分叉的过程.现在,从我的C代码(在Linux上运行)中,我必须定期检查此进程是否仍在运行或终止.我不想使用诸如 wait() waitpid()之类的阻塞调用.需要(最好)一个非阻塞系统调用,它只会检查这个 pid 是否仍在运行并返回子进程的状态.

I have the pid of a forked process. Now, from my c code (running on Linux), I have to check periodically whether this process is still running or terminated. I do not want to use blocking calls like wait() or waitpid(). Need (preferably) a non-blocking system call which would just check whether this pid is still running and return the status of the child.

什么是最好和最简单的方法?

What is best and easiest way to do it?

推荐答案

waitpid()函数可以将选项值 WNOHANG 设为不阻止.一如既往,请参阅手册页.

The waitpid() function can take the option value WNOHANG to not block. See the manual page, as always.

也就是说,我不确定系统是否会确保不回收pid,这将使它处于竞争状态.

That said, I'm not sure that pids are guaranteed to not be recycled by the system, which would open this up to race conditions.

这篇关于如何从C程序检查分叉的进程是否仍在运行的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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