如何让windows的子进程,在父进程退出子进程的时候也退出

windows 下使用 taskkill 杀死子进程, 如何找到 pid
00:34:08 +08:00 · 3838 次点击
用node-webkit写的应用,通过chlid_process .spawn执行gulp, gulp 在本地建了文件服务器和livereload服务器,如果只是kill掉这个child_process.spawn 不能终止服务器的进程,查了一下说windows下可以用系统命令 taskkill, 但是如何找到运行服务器的那个进程的pid ?
12 回复 &| &直到
22:05:21 +08:00
& & 01:44:03 +08:00 via Android
要是进程名确定的话可以直接taskkill /f /im example.exe
& & 02:26:25 +08:00
tasklist | findstr /i process_name
手上没有windows机器... 你先测试下先吧
& & 02:35:07 +08:00
@ 进程名是 node.exe, 有好几个呢,不能误杀。。。
& & 02:37:04 +08:00
tasklist可以看pid
另如果是老版本windows可以用ntsd -c q -p pid来关进程
& & 09:09:32 +08:00
任务管理器就能看了
& & 09:41:18 +08:00
好像歪楼了,我想问在node程序里怎么杀死子进程。。
& & 09:56:06 +08:00
& & 12:09:09 +08:00
其实 Windows 下没有办法直接查到一个进程的子进程有哪些,因为 Windows 不注重进程间的父子关系。。。一个 workaround 是遍历所有进程,看每一个进程的父进程是否为指定进程,就能得知指定进程的子进程有哪些。
另外,如果你是想杀掉自己的程序生成的所有子进程,可以把生成的第一个子进程放入 Job 对象里,然后杀掉的话结束那个 Job 就好了。
但是系统命令应该都做不到吧。。。
& & 15:00:50 +08:00
@ 对进程这些不太了解啊,只是看了下node文档,我在博客里详细描述了问题,求助啦
& & 15:01:54 +08:00
@ 忘贴地址了。。
& & 22:02:49 +08:00
找到一个module可以搞定这个
不过还是不明白什么原理。。
& & 22:05:21 +08:00
看看源码,欠缺底层知识。。
& · & 924 人在线 & 最高记录 3541 & · &
创意工作者们的社区
World is powered by solitude
VERSION: 3.9.8.0 · 63ms · UTC 23:40 · PVG 07:40 · LAX 15:40 · JFK 18:40? Do have faith in what you're doing.新手园地& & & 硬件问题Linux系统管理Linux网络问题Linux环境编程Linux桌面系统国产LinuxBSD& & & BSD文档中心AIX& & & 新手入门& & & AIX文档中心& & & 资源下载& & & Power高级应用& & & IBM存储AS400Solaris& & & Solaris文档中心HP-UX& & & HP文档中心SCO UNIX& & & SCO文档中心互操作专区IRIXTru64 UNIXMac OS X门户网站运维集群和高可用服务器应用监控和防护虚拟化技术架构设计行业应用和管理服务器及硬件技术& & & 服务器资源下载云计算& & & 云计算文档中心& & & 云计算业界& & & 云计算资源下载存储备份& & & 存储文档中心& & & 存储业界& & & 存储资源下载& & & Symantec技术交流区安全技术网络技术& & & 网络技术文档中心C/C++& & & GUI编程& & & Functional编程内核源码& & & 内核问题移动开发& & & 移动开发技术资料ShellPerlJava& & & Java文档中心PHP& & & php文档中心Python& & & Python文档中心RubyCPU与编译器嵌入式开发驱动开发Web开发VoIP开发技术MySQL& & & MySQL文档中心SybaseOraclePostgreSQLDB2Informix数据仓库与数据挖掘NoSQL技术IT业界新闻与评论IT职业生涯& & & 猎头招聘IT图书与评论& & & CU技术图书大系& & & Linux书友会二手交易下载共享Linux文档专区IT培训与认证& & & 培训交流& & & 认证培训清茶斋投资理财运动地带快乐数码摄影& & & 摄影器材& & & 摄影比赛专区IT爱车族旅游天下站务交流版主会议室博客SNS站务交流区CU活动专区& & & Power活动专区& & & 拍卖交流区频道交流区
富足长乐, 积分 6090, 距离下一级还需 1910 积分
论坛徽章:51
下面是一段示意代码 ttt.py:#!/usr/bin/python
# -*- coding: utf-8 -*-
import multiprocessing
import time
import os
class test(multiprocessing.Process):
& && &&&def run(self):
& && && && && & print 'sub process [ppid]: %d,[pid]: %d -- sleep 5 --' %(os.getppid(),os.getpid())
& && && && && & time.sleep(5)
& && && && && & print 'sub process [ppid]: %d,[pid]: %d -- done --' % (os.getppid(),os.getpid())
while True:
& && &&&print '-- [pid]: %d main process starting --' %os.getpid()
& && &&&while b & 10 :
& && && && && & p = test()
& && && && && & p.start()
& && && && && & b +=1
& && &&&print '-- [pid]: %d main process sleep 10 --' %os.getpid()
& && &&&time.sleep(100)复制代码运行后:[root@localhost pytest]# python ttt.py
-- [pid]: 23695 main process starting --
sub process [ppid]: 23695,[pid]: 23696 -- sleep 5 --
sub process [ppid]: 23695,[pid]: 23697 -- sleep 5 --
sub process [ppid]: 23695,[pid]: 23698 -- sleep 5 --
sub process [ppid]: 23695,[pid]: 23699 -- sleep 5 --
sub process [ppid]: 23695,[pid]: 23700 -- sleep 5 --
sub process [ppid]: 23695,[pid]: 23702 -- sleep 5 --
sub process [ppid]: 23695,[pid]: 23701 -- sleep 5 --
sub process [ppid]: 23695,[pid]: 23703 -- sleep 5 --
sub process [ppid]: 23695,[pid]: 23704 -- sleep 5 --
-- [pid]: 23695 main process sleep 10 --
sub process [ppid]: 23695,[pid]: 23705 -- sleep 5 --
sub process [ppid]: 23695,[pid]: 23697 -- done --
sub process [ppid]: 23695,[pid]: 23696 -- done --
sub process [ppid]: 23695,[pid]: 23698 -- done --
sub process [ppid]: 23695,[pid]: 23699 -- done --
sub process [ppid]: 23695,[pid]: 23700 -- done --
sub process [ppid]: 23695,[pid]: 23701 -- done --
sub process [ppid]: 23695,[pid]: 23704 -- done --
sub process [ppid]: 23695,[pid]: 23703 -- done --
sub process [ppid]: 23695,[pid]: 23702 -- done --
sub process [ppid]: 23695,[pid]: 23705 -- done --复制代码产生了10个子进程,完成之后 ,我 ps -ef | grep python, 得到:[root@localhost ~]# ps -ef | grep [p]ython
root& &&&2&&0 00:19 pts/4& & 00:00:00 python ttt.py
root& &&&&&0 00:19 pts/4& & 00:00:00 [python] &defunct&
root& &&&&&0 00:19 pts/4& & 00:00:00 [python] &defunct&
root& &&&&&0 00:19 pts/4& & 00:00:00 [python] &defunct&
root& &&&&&0 00:19 pts/4& & 00:00:00 [python] &defunct&
root& &&&&&0 00:19 pts/4& & 00:00:00 [python] &defunct&
root& &&&&&0 00:19 pts/4& & 00:00:00 [python] &defunct&
root& &&&&&0 00:19 pts/4& & 00:00:00 [python] &defunct&
root& &&&&&0 00:19 pts/4& & 00:00:00 [python] &defunct&
root& &&&&&0 00:19 pts/4& & 00:00:00 [python] &defunct&
root& &&&&&0 00:19 pts/4& & 00:00:00 [python] &defunct&
[root@localhost ~]#
复制代码子进程都是僵尸进程,怎么会这样呢? 如何正确退出子进程? python 新手,求解答!
小富即安, 积分 3309, 距离下一级还需 1691 积分
论坛徽章:13
#!/usr/bin/python
# -*- coding: utf-8 -*-
import multiprocessing
import time
class test(multiprocessing.Process):
& && &&&def run(self):
& && && && && & print 'sub process [ppid]: %d,[pid]: %d -- sleep 5 --' %(os.getppid(),os.getpid())
& && && && && & time.sleep(5)
& && && && && & print 'sub process [ppid]: %d,[pid]: %d -- done --' % (os.getppid(),os.getpid())
while True:
& && &&&print '-- [pid]: %d main process starting --' %os.getpid()
& && &&&p={}
& && &&&for i in range(10) :
& && && && &p[i] = test()
& && && && &p[i].start()
& && && && &p[i].join()
& && &&&print '-- [pid]: %d main process sleep 10 --' %os.getpid()
& && &&&time.sleep(100)
大富大贵, 积分 11216, 距离下一级还需 8784 积分
论坛徽章:11
Start the process’s activity.
This must be called at most once per process object. It arranges for the object’s run() method to be invoked in a separate process.
join([timeout])
If the optional argument timeout is None (the default), the method blocks until the process whose join() method is called terminates. If timeout is a positive number, it blocks at most timeout seconds.
A process can be joined many times.
A process cannot join itself because this would cause a deadlock. It is an error to attempt to join a process before it has been started.
看文档,start下面就是join,回收子进程,和C系统调用wait/waitpid功能类似。
富足长乐, 积分 6090, 距离下一级还需 1910 积分
论坛徽章:51
bikong0411
& & 这样的话是执行完一个,退出一个。 有没有办法,同时运行 10 个子进程呢 ?
富足长乐, 积分 6090, 距离下一级还需 1910 积分
论坛徽章:51
& & 我的意图是:
while Ture:
& &data = &从数据库里获取数据&
& &for i in data:
& && &&&#然后
& && &&&p = class(i)
& && &&&p.start()
& && &&&p.join() 如果这里使用了这个,就停在这里了,得for循环里的子进程全部完成后while循环才能继续走。 我是希望产生子进程后,子进程在运行,但又不影响while循环继续。
但是这里不使用join。 子进程结束后就变成僵尸进程了,如我主题所述。
不知道咋搞了,能指点一二么?
富足长乐, 积分 6090, 距离下一级还需 1910 积分
论坛徽章:51
其实,我的需求是:
1.&&不断从数据库获取数据,一次性可能是获取5条数据
2. 每条数据相当于一个任务,一个任务产生一个子进程去处理它,获取的任务立即执行,如立即执行5个任务
3. 主程序不等待任务(子进程)执行完,继续从数据库中获取数据(任务),如果获取到了,又立刻执行,如此反复
PS: 无需担心有多少条数据(任务)在运行!
---------------
但是,之前主程序衍生出的子进程,我现在不知道如何正确退出,不正确退出,就会变成僵尸进程了。
比如,以下的一个例子:#!/usr/bin/python
# -*- coding: utf-8 -*-
import multiprocessing
import time
import os
class test(multiprocessing.Process):
& && &&&def run(self):
& && && && && & print 'sub process [ppid]: %d,[pid]: %d -- sleep 5 --' %(os.getppid(),os.getpid())
& && && && && & time.sleep(5)
& && && && && & print 'sub process [ppid]: %d,[pid]: %d -- done --' % (os.getppid(),os.getpid())
while True:
& && &&&i = 0
& && &&&p = {}
& && &&&print '-- [pid]: %d main process starting --' %os.getpid()
& && &&&while i & 10 :
& && && && && & p[i] = test()
& && && && && & p[i].start()
& && && && && & i+=1
& && &&&print '-- [pid]: %d main process sleep 100 --' %os.getpid()
& && &&&#sleep 100
& && &&&time.sleep(100)复制代码我执行后,是可以立即执行10个子进程的,而主进程会等待 100 秒 ,5秒钟后子进程都执行完了 ,主进程还在 sleep .
此时,我去 ps -ef 查询的时候,这些子进程就变成了僵尸进程拉。因为我没有正确退出子进程。[root@localhost ~]# ps -ef | grep [p]ython
root& && &&&0 20:24 pts/1& & 00:00:00 python ttt.py
root& && &&&0 20:24 pts/1& & 00:00:00 [python] &defunct&
root& && &&&0 20:24 pts/1& & 00:00:00 [python] &defunct&
root& && &&&0 20:24 pts/1& & 00:00:00 [python] &defunct&
root& && &&&0 20:24 pts/1& & 00:00:00 [python] &defunct&
root& && &&&0 20:24 pts/1& & 00:00:00 [python] &defunct&
root& && &&&0 20:24 pts/1& & 00:00:00 [python] &defunct&
root& && &&&0 20:24 pts/1& & 00:00:00 [python] &defunct&
root& && &&&0 20:24 pts/1& & 00:00:00 [python] &defunct&
root& && &&&0 20:24 pts/1& & 00:00:00 [python] &defunct&
root& && &&&0 20:24 pts/1& & 00:00:00 [python] &defunct&
[root@localhost ~]# 复制代码两位大神, 我这个需求如何去做呢? 求指点!  
大富大贵, 积分 11216, 距离下一级还需 8784 积分
论坛徽章:11
这种需求用进程池或线程池更简单,无需关心进程管理的问题。
富足长乐, 积分 6090, 距离下一级还需 1910 积分
论坛徽章:51
主程序不等待任务(子进程)执行完,继续从数据库中获取数据(任务)!& & 能否帮我写个简单的示例?
大富大贵, 积分 11216, 距离下一级还需 8784 积分
论坛徽章:11
如果你很确定,每条数据新建一个进程没有问题,比如进程数限制或服务器负载,那么也有一种办法。
基本思路:
1. 主进程管理子进程的创建和销毁
2. proxy进程负责数据IO操作
3. worker进程处理具体数据
下面是一个可正常运行的框架:# -*- coding: utf-8 -*-
import multiprocessing as mp
import os
import random
from signal import signal, SIGINT, SIG_IGN, siginterrupt
import time
def data_source():
& & &&&数据源。
& & 随机选择一个浮点数,作为worker进程的sleep时间,
& & 具体实践时可以将这部分实现改为读取数据库。
& & &&&
& & dataset = [0.1, 0.2, 0.3, 0.4, 0.5]
& & while True:
& && &&&time.sleep(0.2)
& && &&&yield random.choice(dataset)
def proc_proxy(cntl_q, data_q, exit_flag):
& & &&&从数据源读取数据。
& & 先通过cntl_q通知主进程,
& & 再将数据通过data_q发给worker。
& & &&&
& & for item in data_source():
& && &&&cntl_q.put({'event': 'data'})
& && &&&data_q.put(item)
& && &&&if exit_flag.is_set():
& && && && &cntl_q.put({'event': 'exit', 'pid': os.getpid()})
& && && && &break
def proc_worker(cntl_q, data_q):
& & &&&处理数据。
& & 从data_q获取数据,处理完毕后通过cntl_q通知主进程,
& & 然后退出。
& & &&&
& & item = data_q.get()
& & time.sleep(item)
& & cntl_q.put({'event': 'exit', 'pid': os.getpid()})
def main():
& & proc_pool = {} # 记录创建的所有子进程
& & cntl_q = mp.Queue() # 控制信息传递队列
& & data_q = mp.Queue() # 具体数据传递队列
& & exit_flag = mp.Event() # 退出标记,初始值为False
& & # 收到SIGINT,通知proxy停止读取数据
& & signal(SIGINT, lambda x, y: exit_flag.set())
& & siginterrupt(SIGINT, False)
& & # 启动proxy进程,后续按需启动woker进程
& & print 'main {} started'.format(os.getpid())
& & proc = mp.Process(target=proc_proxy, args=(cntl_q, data_q, exit_flag))
& & proc.start()
& & proc_pool[proc.pid] = proc
& & print 'proxy {} started'.format(proc.pid)
& & while True:
& && &&&item = cntl_q.get()
& && &&&if item['event'] == 'data':
& && && && &proc = mp.Process(target=proc_worker, args=(cntl_q, data_q))
& && && && &proc.start()
& && && && &proc_pool[proc.pid] = proc
& && && && &print 'worker {} started'.format(proc.pid)
& && &&&elif item['event'] == 'exit':
& && && && &proc = proc_pool.pop(item['pid'])
& && && && &proc.join()
& && && && &print 'child {} stopped'.format(item['pid'])
& && &&&else:
& && && && &print 'It\'s impossible !'
& && &&&if not proc_pool: # 所有子进程均已退出
& && && && &break
& & print 'main {} stopped'.format(os.getpid())
if __name__ == '__main__':
& & main()复制代码
富足长乐, 积分 6090, 距离下一级还需 1910 积分
论坛徽章:51
& & 太感谢啦,经过实际测试,完美符合我的需求,而且加入了信号控制,很完善!! &&灰常感谢!
& & 关于你说的进程控制,我是在数据库层面,查询任务的时候做好了控制的,最多不会超过查询的任务数,所以不用担心。帐号:密码:下次自动登录{url:/nForum/slist.json?uid=guest&root=list-section}{url:/nForum/nlist.json?uid=guest&root=list-section}
贴数:11&分页:上帝之选发信人: godsman (上帝之选), 信区: Programming
标&&题: 菜鸟弱问:父进程结束,子进程是不是也会跟着挂掉?
发信站: 水木社区 (Thu Jan 24 17:19:18 2013), 站内 && rt。谢谢!
-- && 我的博客 && ※ 来源:·水木社区 ·[FROM: 115.156.216.*]
factoryman发信人: makejiang (M++(v)), 信区: Programming
标&&题: Re: 菜鸟弱问:父进程结束,子进程是不是也会跟着挂掉?
发信站: 水木社区 (Thu Jan 24 17:24:23 2013), 站内 && 一切皆有可能。
-- && ※ 来源:·水木社区 ·[FROM: 122.224.134.*]
蓝色幽灵发信人: Tux (蓝色幽灵), 信区: Programming
标&&题: Re: 菜鸟弱问:父进程结束,子进程是不是也会跟着挂掉?
发信站: 水木社区 (Thu Jan 24 17:33:25 2013), 站内 && 可能挂掉, 也可能不挂掉, 取决于你的操作系统怎么设计 &&&& 【 在 godsman (上帝之选) 的大作中提到: 】
: rt。谢谢!
You know nothing, 囧 Snow! &&&& ※ 来源:·水木社区 newsmth.net·[FROM: 124.90.143.*]
WS “净坛使者”发信人: libgcc (传说中的动态库), 信区: Programming
标&&题: Re: 菜鸟弱问:父进程结束,子进程是不是也会跟着挂掉?
发信站: 水木社区 (Thu Jan 24 23:27:58 2013), 站内 && linux下被init托管 &&&& 【 在 godsman (上帝之选) 的大作中提到: 】
: rt。谢谢!
keyboard not found. Press F1 to continue... &&&& ※ 来源:·水木社区 newsmth.net·[FROM: 114.66.4.*]
苦逼100年啊100年发信人: redbird314 (苦逼100年啊100年), 信区: Programming
标&&题: Re: 菜鸟弱问:父进程结束,子进程是不是也会跟着挂掉?
发信站: 水木社区 (Fri Jan 25 07:16:48 2013), 站内 && 变丧尸了 && 【 在 godsman 的大作中提到: 】
: rt。谢谢!
-- && ※ 来源:·水木社区 ·[FROM: 58.67.157.*]
沉迷于TC中发信人: happyTC (沉迷于TC中), 信区: Programming
标&&题: Re: 菜鸟弱问:父进程结束,子进程是不是也会跟着挂掉?
发信站: 水木社区 (Fri Jan 25 17:05:38 2013), 站内 && 在Win下,给它重新找个干爸就不会挂掉
【 在 godsman (上帝之选) 的大作中提到: 】
: 标&&题: 菜鸟弱问:父进程结束,子进程是不是也会跟着挂掉?
: 发信站: 水木社区 (Thu Jan 24 17:19:18 2013), 站内
: rt。谢谢!
: 我的博客
: ※ 来源:·水木社区 ·[FROM: 115.156.216.*]
※ 来源:·水木社区 newsmth.net·[FROM: 222.29.19.*] &&&& ※ 来源:·水木社区 newsmth.net·[FROM: 112.112.90.*]
Alomost Mad发信人: SmileFat (Alomost Mad), 信区: Programming
标&&题: Re: 菜鸟弱问:父进程结束,子进程是不是也会跟着挂掉?
发信站: 水木社区 (Sun Jan 27 09:20:03 2013), 站内 && 都什么什么回答呀。 && 父进程挂掉根子进程有个鸟关系啊。 && 子进程这个时候如果正常运行的话,当然正常运行了。
只不过父子进程关系在操作系统内部维护的时候更新一下而已。
在Linux下面会给其找一个新的父进程init
在Windows下面本来就没有父子关系,都是独立的,就更加没有关系了。 &&&& 当然,你在console中按了一个ctrl+c,那是会给当前所有的会话中的进程发信号。
所有人都相应这个信号,跟父子进程也没有毛关系。 && 【 在 godsman (上帝之选) 的大作中提到: 】
: rt。谢谢!
&&&& -- && ※ 修改:·SmileFat 于 Jan 27 09:21:44 2013 修改本文·[FROM: 123.108.223.*]
※ 来源:·水木社区 newsmth.net·[FROM: 123.108.223.*]
蓝色幽灵发信人: Tux (蓝色幽灵), 信区: Programming
标&&题: Re: 菜鸟弱问:父进程结束,子进程是不是也会跟着挂掉?
发信站: 水木社区 (Sun Jan 27 10:38:25 2013), 站内 && 搜一下cascading termination && 【 在 SmileFat (Alomost Mad) 的大作中提到: 】
: 都什么什么回答呀。
: 父进程挂掉根子进程有个鸟关系啊。
: 子进程这个时候如果正常运行的话,当然正常运行了。
: ...................
You know nothing, 囧 Snow! &&&& ※ 来源:·水木社区 newsmth.net·[FROM: 124.90.143.*]
NULL发信人: heracules (我愿拱手河山讨你欢), 信区: Programming
标&&题: Re: 菜鸟弱问:父进程结束,子进程是不是也会跟着挂掉?
发信站: 水木社区 (Sun Jan 27 15:06:02 2013), 站内 && 变僵尸是父进程没有wait又没有exit
【 在 redbird314 (苦逼100年啊100年) 的大作中提到: 】
: 变丧尸了
把我最后的爱留给你 &&&& ※ 来源:·水木社区 newsmth.net·[FROM: 159.226.169.*]
Alomost Mad发信人: SmileFat (Alomost Mad), 信区: Programming
标&&题: Re: 菜鸟弱问:父进程结束,子进程是不是也会跟着挂掉?
发信站: 水木社区 (Sun Jan 27 23:11:47 2013), 站内 && 受教了,还真没见过这个概念。 && linux 不是这样的操作系统吧,否则daemon怎么做出来,console的session倒是有这个
意思,daemon可以脱离这个session。操作系统在内部数据结构中可以做到这一点,
提供接口构成一个进程组可以完成这种功能。
所以说父子进程关系提供的功能还是比较完善的,让应用可以自己组织。 && Windows操作系统也应该不是,都没有父子关系。 &&&& 【 在 Tux (蓝色幽灵) 的大作中提到: 】
: 搜一下cascading termination
&&&& -- && ※ 来源:·水木社区 newsmth.net·[FROM: 123.108.223.*]
文章数:11&分页:如下面的程序,对系统的system函数进行了改写。
#!/bin/bash
declare -i i=1
until ((30&i))
bakRun.c,编译:gcc -g bakRun.c -o bakRun
#include &stdio.h&
#include&stdlib.h&
#include&string.h&
#include&pthread.h&
#include&unistd.h&
#include&signal.h&
#include &sys/prctl.h&
#include&sys/wait.h&
int mysystem(const char *cmd)
if((pid=fork()) & 0)
printf("fork error\n");
else if(pid &0)
waitpid(pid,&status,0);
printf("parent\n");
printf("receive msg\n");
prctl(PR_SET_PDEATHSIG,SIGHUP);
execl("/bin/sh","sh","-c","./bakRun.sh",(char*)0);
printf("exec cmd\n");
return -1;
int main(int argc ,char* argv[])
mysystem("./backRun.sh");
上面的程序中,需要设置选项
prctl(PR_SET_PDEATHSIG,SIGHUP);
设置该选项,在父进程死后,子进程会收到SIGHUP信号,子进程因此也会退出,解决了,父进程异常退出,而子进程来不及回收资源的问题。
如果去掉了上面的设置,在杀死bakRun之后,会发现sh脚本还在运行。
原文链接:
本文已收录于以下专栏:
他的最新文章
他的热门文章
您举报文章:
举报原因:
原文地址:
原因补充:
(最多只允许输入30个字)win32父进程退出后,子进程的退出问题0收藏分享举报{&debug&:false,&apiRoot&:&&,&paySDK&:&https:\u002F\u002Fpay.zhihu.com\u002Fapi\u002Fjs&,&wechatConfigAPI&:&\u002Fapi\u002Fwechat\u002Fjssdkconfig&,&name&:&production&,&instance&:&column&,&tokens&:{&X-XSRF-TOKEN&:null,&X-UDID&:null,&Authorization&:&oauth c3cef7c66aa9e6a1e3160e20&}}{&database&:{&Post&:{&&:{&isPending&:false,&contributes&:[],&title&:&win32父进程退出后,子进程的退出问题&,&author&:&zhang-da-liang-17&,&content&:&背景: windows下,父进程退出后,子进程无法自动退出;父子进程之间通过进程间通信。父进程是服务端,子进程是客户端;\u003Cp\u003E需求:如何让子进程不残留。\u003C\u002Fp\u003E\u003Cp\u003E方案1:网络心跳包。但是进程卡顿的时候,往往会导致心跳包模块判断失误。\u003C\u002Fp\u003E\u003Cp\u003E方案2:父进程启动子进程以后,把自己的进程id告诉子进程,子进程每隔4s去判断父进程是否存在,不存在,则退出自己。\u003C\u002Fp\u003E\u003Cp\u003E选择了方案2,最近碰到了一个问题,父进程已经退出,任务管理器中也已经看不到父进程了,子进程判断父进程是否存在时,却一直返回父进程句柄存在,导致子进程未退出。\u003C\u002Fp\u003E\u003Cp\u003E问题查看:\u003C\u002Fp\u003E\u003Cp\u003E用procexp.exe打开父进程的堆栈信息以后,退出父进程,发现上述问题重现。 猜测应该有其他第三方进程打开了父进程,引用计数加1,导致子进程那边判断父进程退出失败导致。\u003C\u002Fp\u003E\u003Cbr\u003E\u003Cp\u003E问题解决:\u003C\u002Fp\u003E\u003Cp\u003Eprocexp.exe查看父进程的Pid,发现有道进程有使用。。。关闭有道进程后,子进程优雅退出。\u003Cbr\u003E\u003C\u002Fp\u003E\u003Cp\u003E自我总结:\u003C\u002Fp\u003E\u003Cp\u003E无法保证其他第三方进程打开父进程的进程句柄,影响引用计数;所以用进程id来判断父进程退出的方式不优雅。退而求其次,选择方案1,超时时间适当放长。\u003C\u002Fp\u003E\u003Cp\u003E有道进程为何要打开进程呢?\u003C\u002Fp\u003E&,&updated&:new Date(&T08:02:32.000Z&),&canComment&:false,&commentPermission&:&anyone&,&commentCount&:1,&collapsedCount&:0,&likeCount&:0,&state&:&published&,&isLiked&:false,&slug&:&&,&isTitleImageFullScreen&:false,&rating&:&none&,&titleImage&:&&,&links&:{&comments&:&\u002Fapi\u002Fposts\u002F2Fcomments&},&reviewers&:[],&topics&:[{&url&:&https:\u002F\u002Fwww.zhihu.com\u002Ftopic\u002F&,&id&:&&,&name&:&C++&}],&adminClosedComment&:false,&titleImageSize&:{&width&:0,&height&:0},&href&:&\u002Fapi\u002Fposts\u002F&,&excerptTitle&:&&,&tipjarState&:&closed&,&annotationAction&:[],&sourceUrl&:&&,&pageCommentsCount&:1,&hasPublishingDraft&:false,&snapshotUrl&:&&,&publishedTime&:&T16:02:32+08:00&,&url&:&\u002Fp\u002F&,&lastestLikers&:[],&summary&:&背景: windows下,父进程退出后,子进程无法自动退出;父子进程之间通过进程间通信。父进程是服务端,子进程是客户端;需求:如何让子进程不残留。方案1:网络心跳包。但是进程卡顿的时候,往往会导致心跳包模块判断失误。方案2:父进程启动子进程以后,把…&,&reviewingCommentsCount&:0,&meta&:{&previous&:null,&next&:null},&annotationDetail&:null,&commentsCount&:1,&likesCount&:0,&FULLINFO&:true}},&User&:{&zhang-da-liang-17&:{&isFollowed&:false,&name&:&张大亮&,&headline&:&&,&avatarUrl&:&https:\u002F\u002Fpic4.zhimg.com\u002Fda8e974dc_s.jpg&,&isFollowing&:false,&type&:&people&,&slug&:&zhang-da-liang-17&,&bio&:&程序员&,&hash&:&033aaf8db64abbb994e80&,&uid&:032400,&isOrg&:false,&description&:&&,&badge&:{&identity&:null,&bestAnswerer&:null},&profileUrl&:&https:\u002F\u002Fwww.zhihu.com\u002Fpeople\u002Fzhang-da-liang-17&,&avatar&:{&id&:&da8e974dc&,&template&:&https:\u002F\u002Fpic4.zhimg.com\u002F{id}_{size}.jpg&},&isOrgWhiteList&:false,&isBanned&:false}},&Comment&:{},&favlists&:{}},&me&:{},&global&:{&experimentFeatures&:{&ge3&:&ge3_9&,&ge2&:&ge2_1&,&growthSearch&:&s2&,&nwebQAGrowth&:&experiment&,&qawebRelatedReadingsContentControl&:&close&,&liveStore&:&ls_a2_b2_c1_f2&,&qawebThumbnailAbtest&:&new&,&nwebSearch&:&nweb_search_heifetz&,&rt&:&y&,&showVideoUploadAttention&:&true&,&isOffice&:&false&,&enableTtsPlay&:&post&,&editorVideomakerEntrance&:&e&,&enableVoteDownReasonMenu&:&enable&,&newLiveFeedMediacard&:&new&,&newMobileAppHeader&:&true&,&androidPassThroughPush&:&all&,&hybridZhmoreVideo&:&no&,&nwebGrowthPeople&:&default&,&nwebSearchSuggest&:&default&,&qrcodeLogin&:&qrcode&,&seE&:&0&,&androidDbFollowRecommendHide&:&open&,&isShowUnicomFreeEntry&:&unicom_free_entry_off&,&newMobileColumnAppheader&:&new_header&,&feedHybridTopicRecomButtonIcon&:&yes&,&androidDbRecommendAction&:&open&,&zcmLighting&:&zcm&,&androidDbFeedHashTagStyle&:&button&,&appStoreRateDialog&:&close&,&default&:&None&,&isNewNotiPanel&:&no&,&wechatShareModal&:&wechat_share_modal_show&,&growthBanner&:&default&,&androidProfilePanel&:&panel_b&}},&columns&:{&next&:{}},&columnPosts&:{},&columnSettings&:{&colomnAuthor&:[],&uploadAvatarDetails&:&&,&contributeRequests&:[],&contributeRequestsTotalCount&:0,&inviteAuthor&:&&},&postComments&:{},&postReviewComments&:{&comments&:[],&newComments&:[],&hasMore&:true},&favlistsByUser&:{},&favlistRelations&:{},&promotions&:{},&switches&:{&couldSetPoster&:false},&draft&:{&titleImage&:&&,&titleImageSize&:{},&isTitleImageFullScreen&:false,&canTitleImageFullScreen&:false,&title&:&&,&titleImageUploading&:false,&error&:&&,&content&:&&,&draftLoading&:false,&globalLoading&:false,&pendingVideo&:{&resource&:null,&error&:null}},&drafts&:{&draftsList&:[],&next&:{}},&config&:{&userNotBindPhoneTipString&:{}},&recommendPosts&:{&articleRecommendations&:[],&columnRecommendations&:[]},&env&:{&edition&:{&baidu&:false,&yidianzixun&:false,&qqnews&:false},&isAppView&:false,&appViewConfig&:{&content_padding_top&:128,&content_padding_bottom&:56,&content_padding_left&:16,&content_padding_right&:16,&title_font_size&:22,&body_font_size&:16,&is_dark_theme&:false,&can_auto_load_image&:true,&app_info&:&OS=iOS&},&isApp&:false,&userAgent&:{&ua&:&Mozilla\u002F5.0 (compatible, MSIE 11, Windows NT 6.3; Trident\u002F7.0; rv:11.0) like Gecko&,&browser&:{&name&:&IE&,&version&:&11&,&major&:&11&},&engine&:{&version&:&7.0&,&name&:&Trident&},&os&:{&name&:&Windows&,&version&:&8.1&},&device&:{},&cpu&:{}}},&message&:{&newCount&:0},&pushNotification&:{&newCount&:0}}

我要回帖

更多关于 fork 父进程退出 的文章

 

随机推荐