linux kill 进程失效,在Linux上对进程执行kill -9怎么可能没有效果?

I lately get trapped into a pitfall of Double Fork and had landed to this page before finally finding my answer. The symptoms are identical even if the problem is not the same:

WYKINWYT :What You Kill Is Not What You Thought

The minimal test code is shown below based on an example for an SNMP Daemon

#include

#include

#include

#include

int main(int argc, char* argv[])

{

//We omit the -f option (do not Fork) to reproduce the problem

char * options[]={"/usr/local/sbin/snmpd",/*"-f","*/-d","--master=agentx", "-Dagentx","--agentXSocket=tcp:localhost:1706", "udp:10161", (char*) NULL};

pid_t pid = fork();

if ( 0 > pid ) return -1;

switch(pid)

{

case 0:

{ //Child launches SNMP daemon

execv(options[0],options);

exit(-2);

break;

}

default:

{

sleep(10); //Simulate "long" activity

kill(pid,SIGTERM);//kill what should be child,

//i.e the SNMP daemon I assume

printf("Signal sent to %d\n",pid);

sleep(10); //Simulate "long" operation before closing

waitpid(pid);

printf("SNMP should be now down\n");

getchar();//Blocking (for observation only)

break;

}

}

printf("Bye!\n");

}

During the first phase the main process (7699) launches the SNMP daemon (7700) but we can see that this one is now Defunct/Zombie. Beside we can see another process (7702) with the options we specified

[nils@localhost ~]$ ps -ef | tail

root 7439 2 0 23:00 ? 00:00:00 [kworker/1:0]

root 7494 2 0 23:03 ? 00:00:00 [kworker/0:1]

root 7544 2 0 23:08 ? 00:00:00 [kworker/0:2]

root 7605 2 0 23:10 ? 00:00:00 [kworker/1:2]

root 7698 729 0 23:11 ? 00:00:00 sleep 60

nils 7699 2832 0 23:11 pts/0 00:00:00 ./main

nils 7700 7699 0 23:11 pts/0 00:00:00 [snmpd]

nils 7702 1 0 23:11 ? 00:00:00 /usr/local/sbin/snmpd -Lo -d --master=agentx -Dagentx --agentXSocket=tcp:localhost:1706 udp:10161

nils 7727 3706 0 23:11 pts/1 00:00:00 ps -ef

nils 7728 3706 0 23:11 pts/1 00:00:00 tail

After the 10 sec simulated we will try to kill the only process we know (7700). What we succeed at last with waitpid(). But Process 7702 is still here

[nils@localhost ~]$ ps -ef | tail

root 7431 2 0 23:00 ? 00:00:00 [kworker/u256:1]

root 7439 2 0 23:00 ? 00:00:00 [kworker/1:0]

root 7494 2 0 23:03 ? 00:00:00 [kworker/0:1]

root 7544 2 0 23:08 ? 00:00:00 [kworker/0:2]

root 7605 2 0 23:10 ? 00:00:00 [kworker/1:2]

root 7698 729 0 23:11 ? 00:00:00 sleep 60

nils 7699 2832 0 23:11 pts/0 00:00:00 ./main

nils 7702 1 0 23:11 ? 00:00:00 /usr/local/sbin/snmpd -Lo -d --master=agentx -Dagentx --agentXSocket=tcp:localhost:1706 udp:10161

nils 7751 3706 0 23:12 pts/1 00:00:00 ps -ef

nils 7752 3706 0 23:12 pts/1 00:00:00 tail

After giving a character to the getchar() function our main process terminates but the SNMP daemon with the pid 7002 is still here

[nils@localhost ~]$ ps -ef | tail

postfix 7399 1511 0 22:58 ? 00:00:00 pickup -l -t unix -u

root 7431 2 0 23:00 ? 00:00:00 [kworker/u256:1]

root 7439 2 0 23:00 ? 00:00:00 [kworker/1:0]

root 7494 2 0 23:03 ? 00:00:00 [kworker/0:1]

root 7544 2 0 23:08 ? 00:00:00 [kworker/0:2]

root 7605 2 0 23:10 ? 00:00:00 [kworker/1:2]

root 7698 729 0 23:11 ? 00:00:00 sleep 60

nils 7702 1 0 23:11 ? 00:00:00 /usr/local/sbin/snmpd -Lo -d --master=agentx -Dagentx --agentXSocket=tcp:localhost:1706 udp:10161

nils 7765 3706 0 23:12 pts/1 00:00:00 ps -ef

nils 7766 3706 0 23:12 pts/1 00:00:00 tail

Conclusion

The fact that we ignored the double fork mechanism made us think that the kill action did not succeed. But in fact we simply killed the wrong process !!

By adding the -f option ( Do Not (Double) Fork ) all go as expected

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值