在 exec 中分叉和 运行 dhclient(带有参数 -nw -w)创建一个已失效的进程和一个守护进程
Forking and running dhclient (with params -nw -w) in exec creates a defunct process and a daemon process
当我从父进程派生然后使用其参数执行 dhclient 进程时,我看到了一个僵尸进程和 dhclient 进程
这是输出
root 31298 0.0 0.0 0 0 pts/2 Z 09:22 0:00 [dhclientForData] <defunct>
root 31410 0.0 0.0 24260 9108 ? Ss 09:23 0:00 -nw -w -sf etc/dhclient-script -pf /var/run/dhclient-vEth0.pid -lf /var/lib/dhcp/dhclient-vEth0.leases -cf /etc/sysconfig/dhclient.conf vEth0
我已经在父进程中将 linux 系统 dhclient 重命名为 dhclientForData。请注意上面输出中的第二行我没有看到 dhclient 进程名称只是 argurmnts 我不确定为什么,任何人?
我的代码
func_parent()
{
pid_t child = 0;
child = fork();
if (child < 0)
{
return 0;
}
if (child == 0)
{
wanIntfDhclientHandlerProcessChildAdd ();
}
shmWlanTunMonitrConfig->childProcs[index].procId = child;
sleep(1);
}
uint8_t wanIntfDhclientHandlerProcessChildAdd ()
{
pid_t mypid = getpid();
uint8_t idx = gindex;
//for (; idx < MAX_WAN_PORTS; idx++)
{
if (!shmWlanTunMonitrConfig->childProcs[idx].isRunning && shmWlanTunMonitrConfig->childProcs[idx].cmdIdx != 0xFF)
{
shmWlanTunMonitrConfig->childProcs[idx].isRunning = 1;
char *const argV[] = {commands[idx].cmd[0],
commands[idx].cmd[1],
commands[idx].cmd[2],
commands[idx].cmd[3],
commands[idx].cmd[4],
commands[idx].cmd[5],
commands[idx].cmd[6],
commands[idx].cmd[7],
commands[idx].cmd[8],
commands[idx].cmd[9],
commands[idx].cmd[10],
NULL};
int ret = execvp(WAN_EXECUTABLE, argV);
}
}
}
我已经注册到 sigchld 并将在该进程上等待 pid。我有多个进程 运行
pid_t pid = waitpid(shmWlanTunMonitrConfig->childProcs[index].procId, &status, WNOHANG);
如果不再需要该作业,我只是从我的父进程中删除该进程。
char cmd[512] = {0};
sprintf(cmd, "for KILLPID in `ps ax | grep \'/var/run/dhclient\' | grep \'%s\' | awk \' { print ;}\'`; do kill -9 $KILLPID; done", portIntfAr[index].intfName);
system (cmd);
谁能指出我做错了什么。
赛义德
僵尸进程 31298
只要父进程还没有等待它就存在,所以您的进程 table 或您的 waitpid
调用可能有问题。
我无法检查,因为您的代码不完整。
除了等待特定的 PID,您还可以等待任何子进程或进程组,并从结果 siginfo_t
结构中找出哪个进程刚刚报告了它的状态变化。
我不知道你分配的是哪个字符串
char *const argV[] = {commands[idx].cmd[0],
/* ... */
argv[0]
应该是程序的文件名,即与 WAN_EXECUTABLE
相同
当我从父进程派生然后使用其参数执行 dhclient 进程时,我看到了一个僵尸进程和 dhclient 进程
这是输出
root 31298 0.0 0.0 0 0 pts/2 Z 09:22 0:00 [dhclientForData] <defunct>
root 31410 0.0 0.0 24260 9108 ? Ss 09:23 0:00 -nw -w -sf etc/dhclient-script -pf /var/run/dhclient-vEth0.pid -lf /var/lib/dhcp/dhclient-vEth0.leases -cf /etc/sysconfig/dhclient.conf vEth0
我已经在父进程中将 linux 系统 dhclient 重命名为 dhclientForData。请注意上面输出中的第二行我没有看到 dhclient 进程名称只是 argurmnts 我不确定为什么,任何人?
我的代码
func_parent()
{
pid_t child = 0;
child = fork();
if (child < 0)
{
return 0;
}
if (child == 0)
{
wanIntfDhclientHandlerProcessChildAdd ();
}
shmWlanTunMonitrConfig->childProcs[index].procId = child;
sleep(1);
}
uint8_t wanIntfDhclientHandlerProcessChildAdd ()
{
pid_t mypid = getpid();
uint8_t idx = gindex;
//for (; idx < MAX_WAN_PORTS; idx++)
{
if (!shmWlanTunMonitrConfig->childProcs[idx].isRunning && shmWlanTunMonitrConfig->childProcs[idx].cmdIdx != 0xFF)
{
shmWlanTunMonitrConfig->childProcs[idx].isRunning = 1;
char *const argV[] = {commands[idx].cmd[0],
commands[idx].cmd[1],
commands[idx].cmd[2],
commands[idx].cmd[3],
commands[idx].cmd[4],
commands[idx].cmd[5],
commands[idx].cmd[6],
commands[idx].cmd[7],
commands[idx].cmd[8],
commands[idx].cmd[9],
commands[idx].cmd[10],
NULL};
int ret = execvp(WAN_EXECUTABLE, argV);
}
}
}
我已经注册到 sigchld 并将在该进程上等待 pid。我有多个进程 运行
pid_t pid = waitpid(shmWlanTunMonitrConfig->childProcs[index].procId, &status, WNOHANG);
如果不再需要该作业,我只是从我的父进程中删除该进程。
char cmd[512] = {0};
sprintf(cmd, "for KILLPID in `ps ax | grep \'/var/run/dhclient\' | grep \'%s\' | awk \' { print ;}\'`; do kill -9 $KILLPID; done", portIntfAr[index].intfName);
system (cmd);
谁能指出我做错了什么。
赛义德
僵尸进程 31298
只要父进程还没有等待它就存在,所以您的进程 table 或您的 waitpid
调用可能有问题。
我无法检查,因为您的代码不完整。
除了等待特定的 PID,您还可以等待任何子进程或进程组,并从结果 siginfo_t
结构中找出哪个进程刚刚报告了它的状态变化。
我不知道你分配的是哪个字符串
char *const argV[] = {commands[idx].cmd[0],
/* ... */
argv[0]
应该是程序的文件名,即与 WAN_EXECUTABLE