在C ++中检查子进程的状态 [英] Checking the status of a child process in C++

查看:144
本文介绍了在C ++中检查子进程的状态的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个程序使用 fork()创建一个子进程。我看到了使用 wait()等待子进程在结束之前结束的各种示例,但我想知道我可以做什么只是检查文件过程是否仍然在运行。

I have a program that uses fork() to create a child process. I have seen various examples that use wait() to wait for the child process to end before closing, but I am wondering what I can do to simply check if the file process is still running.

我基本上有一个无限循环,我想做一些事情:

I basically have an infinite loop and I want to do something like:

if(子进程已结束)break;

我该如何做?

推荐答案

选项。

int status;
pid_t result = waitpid(ChildPID, &status, WNOHANG);
if (result == 0) {
  // Child still alive
} else if (result == -1) {
  // Error 
} else {
  // Child exited
}

这篇关于在C ++中检查子进程的状态的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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