posix_spawn 创建僵尸进程并 returns 成功
posix_spawn creates a zombie process and returns success
我正在尝试使用 posix_spawn()
生成一个子进程。我给出了可执行文件的名称(存在),但 posix_spawn()
创建了一个僵尸进程(我在 ps
中搜索该进程,它显示为 <defunct>
)。即使我指定了一个不存在的可执行文件名,也会创建僵尸进程。
我的问题是我需要知道进程是否成功生成,但是由于 posix_spawn
returns 0(成功)和子进程的 ID 有效,我没有办法收到发生错误的通知。
这是我的代码(P.S。可执行文件 "dummy" 不存在):
#include <iostream>
#include <spawn.h>
extern char **environ;
int main()
{
const char *args[] = { "dummy", nullptr };
pid_t pid = 0;
posix_spawn(&pid, "dummy", nullptr, nullptr, const_cast<char **>(args), environ);
if (pid == 0)
{
// doesn't get here
}
else
// this gets executed instead, pid has some value
std::cout << pid << std::endl;
}
获得状态:
#include <iostream>
#include <spawn.h>
extern char **environ;
int main()
{
const char *args[] = { "dummy", nullptr };
int status = posix_spawn(nullptr, "dummy", nullptr, nullptr, const_cast<char **>(args), environ);
if (status != 0)
{
// doesn't get here
}
else
// this gets executed, status is 0
std::cout << status << std::endl;
}
子进程是僵尸意味着它 finished/died 并且您需要使用 wait/waitpid
获得它的退出状态。
#include <sys/wait.h>
//...
int wstatus;
waitpid(pid,&wstatus,0);
我正在尝试使用 posix_spawn()
生成一个子进程。我给出了可执行文件的名称(存在),但 posix_spawn()
创建了一个僵尸进程(我在 ps
中搜索该进程,它显示为 <defunct>
)。即使我指定了一个不存在的可执行文件名,也会创建僵尸进程。
我的问题是我需要知道进程是否成功生成,但是由于 posix_spawn
returns 0(成功)和子进程的 ID 有效,我没有办法收到发生错误的通知。
这是我的代码(P.S。可执行文件 "dummy" 不存在):
#include <iostream>
#include <spawn.h>
extern char **environ;
int main()
{
const char *args[] = { "dummy", nullptr };
pid_t pid = 0;
posix_spawn(&pid, "dummy", nullptr, nullptr, const_cast<char **>(args), environ);
if (pid == 0)
{
// doesn't get here
}
else
// this gets executed instead, pid has some value
std::cout << pid << std::endl;
}
获得状态:
#include <iostream>
#include <spawn.h>
extern char **environ;
int main()
{
const char *args[] = { "dummy", nullptr };
int status = posix_spawn(nullptr, "dummy", nullptr, nullptr, const_cast<char **>(args), environ);
if (status != 0)
{
// doesn't get here
}
else
// this gets executed, status is 0
std::cout << status << std::endl;
}
子进程是僵尸意味着它 finished/died 并且您需要使用 wait/waitpid
获得它的退出状态。
#include <sys/wait.h>
//...
int wstatus;
waitpid(pid,&wstatus,0);