2018-01-03@望京

关于fork()函数,Unix/Linux提供的fork()系统调用,fork()一次返回两次,

操作系统自动把当前进程(称为父进程)复制了一份(称为子进程),然后,分别在父进程和子进程内返回;

子进程永远返回 0,而父进程返回子进程的ID。

父进程结束时,子进程并不会随父进程立刻结束;同样,父进程不会等待子进程执行完。

  - Python里fork()的使用

示例一

#!/usr/bin/python3

import os

print('Process (%s) start...' % os.getpid())
# Only works on Unix/Linux/Mac
pid = os.fork()
if pid == 0:
print('I am child process (%s) and my parent is %s.' % (os.getpid(), os.getppid()))
else:
print('I (%s) just created a child process (%s).' % (os.getpid(), pid)) ####### 结果 #########
Process (25912) start...
I (25912) just created a child process (25913).
I am child process (25913) and my parent is 25912.

示例二

#!/usr/bin/python3

import os
import time #创建子进程之前声明的变量
source = 10
try:
pid = os.fork()
if pid == 0: #子进程
print('I am child process (%s) and my parent is %s.' % (os.getpid(), os.getppid()))
#在子进程中source自减1
source -= 1
time.sleep(1)
else: #父进程
print('I (%s) just created a child process (%s).' % (os.getpid(), pid))
print(source)
time.sleep(2)
except OSError as e:
print("exception ",e) ####### 结果 ########
I (12807) just created a child process (12808).
10
I am child process (12808) and my parent is 12807.
9

  

  - 服务器里的僵尸进程是怎么产生的?

参考:https://www.cnblogs.com/yuxingfirst/p/3165407.html

参考:http://blog.csdn.net/qq_20218109/article/details/52078076

参考:http://blog.51cto.com/forlinux/1422438

子进程结束后,但是父进程还没有结束的时候,子进程是出于Zombie状态的,这个需要父进程去收集子进程的信息释放子进程。

如果父进程结束了子进程没有结束,那么子进程就会寄托给pid为1的进程来管理。

给进程设置僵尸状态的目的是维护子进程的信息,以便父进程在以后某个时间获取,

这些信息包括子进程的进程ID、终止状态以及资源利用信息(CPU时间,内存使用量等等);

僵尸进程的产生示例

子进程先于父进程退出,同时父进程又没有调用wait/waitpid,则该子进程将成为僵尸进程。

#!/usr/bin/python3

import os
import time try:
pid = os.fork()
if pid == 0: #子进程
print('I am child process (%s) and my parent is %s.' % (os.getpid(), os.getppid()))
else: #父进程
print('I (%s) just created a child process (%s).' % (os.getpid(), pid))
time.sleep(50)
print('%s end' % os.getpid())
except OSError as e:
print("exception ",e) ###### 结果 ######
I (19668) just created a child process (19669).
I am child process (19669) and my parent is 19668.
19669 end
19668 end # 这里sleep了50秒才打印

程序运行时,结合ps命令查看僵尸进程情况:

root@10.13.17.16[13:30:40]$ ps -A -ostat,ppid,pid,cmd | grep -e '^[Zz]'
Z+ 19668 19669 [forkkk.py] <defunct>
root@10.13.17.16[13:31:28]$
root@10.13.17.16[13:31:28]$ ps -A -ostat,ppid,pid,cmd | grep forkkk
S+ 10582 19668 /usr/bin/python3 ./forkkk.py
Z+ 19668 19669 [forkkk.py] <defunct> # 显然pid是19669,即子进程的id,子进程已经执行结束了,但是此时父进程19668还没有执行完。
S+ 17673 19678 grep forkkk
root@10.13.17.16[13:31:30]$

  

  - 如何避免出现僵尸进程呢/如何回收僵尸进程?

1. 信号方式

当子进程退出的时候,内核都会给父进程一个SIGCHLD信号,

所以我们可以建立一个捕获SIGCHLD信号的信号处理函数,在函数体中调用wait(或waitpid),就可以清理退出的子进程以达到防止僵尸进程的目的。

#!/usr/bin/python3

import os
import time
import signal def childhandler(*args, **kwargs):
while True:
try:
result = os.waitpid(-1, os.WNOHANG)
except:
break
else:
print("Reaped child process %(pid)d, status is %(status)s" % \
{'pid':result[0], 'status':result[1]}) if __name__ == '__main__':
signal.signal(signal.SIGCHLD, childhandler)
print("Before the fork, parent PID is %s" % os.getpid())
pid = os.fork()
if pid:
print("Hello from the parent, the child PID is %s" % pid)
print("Parent sleeping 10 seconds ...")
time.sleep(10)
print("Parent sleep done.")
else:
print("Child sleeping 3 seconds ...")
time.sleep(3)
print("Child sleep done.") ###### 结果 ######
Before the fork, parent PID is 18998
Hello from the parent, the child PID is 18999
Parent sleeping 10 seconds ...
Child sleeping 3 seconds ...
Child sleep done.
Reaped child process 18999, status is 0
Parent sleep done.
os.waitpid()

-1            表示等待主进程的所有子进程终止
os.WNOHANG 表示如果没有已经终止的子进程就立即返回

2. 父进程轮询检查子进程并回收

#!/usr/bin/python3

import os
import time def recovery():
while True:
try:
result = os.waitpid(-1, os.WNOHANG)
#result = os.wait()
except:
break
else:
return result if __name__ == '__main__':
print("Before the fork, parent PID is %s" % os.getpid())
pid = os.fork()
if pid:
print("Hello from the parent, the child PID is %s" % pid)
while True:
res = recovery()
time.sleep(0.5)
print('one loop...')
if res[0]:
print("Reaped child process %(pid)d, status is %(status)s" % \
{'pid':res[0], 'status':res[1]})
break
print("Parent run end...")
else:
print("Child sleeping 3 seconds ...")
time.sleep(3)
print("Child sleep done.") ###### 结果 ######
Before the fork, parent PID is 3391
Hello from the parent, the child PID is 3392
Child sleeping 3 seconds ...
one loop...
one loop...
one loop...
one loop...
one loop...
one loop...
Child sleep done.
one loop...
one loop...
Reaped child process 3392, status is 0
Parent run end...

轮询的方式没有办法很及时的处理僵尸进程(取决于轮询的时间间隔)。

注意:

- result = os.waitpid(-1, os.WNOHANG)  这种方式不会阻塞调用者

- result = os.wait()  这种会阻塞调用者,导致不会进行轮询。

3. fork()两次/Python实现守护进程

父进程一次fork()后产生一个子进程,随后父进程立即执行waitpid(子进程pid, NULL, 0)来等待子进程结束,然后子进程fork()后产生孙子进程随后立即exit(0)

这样子进程顺利终止(父进程仅仅给子进程收尸,并不需要子进程的返回值),然后父进程继续执行

这时的孙子进程由于失去了它的父进程(即是父进程的子进程),将被转交给Init进程托管

于是父进程与孙子进程无继承关系了,它们的父进程均为Init,Init进程在其孙子进程结束时会自动收尸,这样也就不会产生僵尸进程了。

详情见:http://blog.tangyingkang.com/post/2016/10/20/python-daemon/ *****

fork()两次不是必须的:https://segmentfault.com/a/1190000008556669 *****

import os
import sys
import atexit def daemonize(pid_file=None):
"""
创建守护进程
:param pid_file: 保存进程id的文件
:return:
"""
# 从父进程fork一个子进程出来
pid = os.fork()
# 子进程的pid一定为0,父进程大于0
if pid:
# 退出父进程,sys.exit()方法比os._exit()方法会多执行一些刷新缓冲工作
sys.exit(0) # 子进程默认继承父进程的工作目录,最好是变更到根目录,否则回影响文件系统的卸载
os.chdir('/')
# 子进程默认继承父进程的umask(文件权限掩码),重设为0(完全控制),以免影响程序读写文件
os.umask(0)
# 让子进程成为新的会话组长和进程组长
os.setsid() # 注意了,这里是第2次fork,也就是子进程的子进程,我们把它叫为孙子进程
_pid = os.fork()
if _pid:
# 退出子进程
sys.exit(0) # 此时,孙子进程已经是守护进程了,接下来重定向标准输入、输出、错误的描述符(是重定向而不是关闭, 这样可以避免程序在 print 的时候出错) # 刷新缓冲区先,小心使得万年船
sys.stdout.flush()
sys.stderr.flush() # dup2函数原子化地关闭和复制文件描述符,重定向到/dev/nul,即丢弃所有输入输出
with open('/dev/null') as read_null, open('/dev/null', 'w') as write_null:
os.dup2(read_null.fileno(), sys.stdin.fileno())
os.dup2(write_null.fileno(), sys.stdout.fileno())
os.dup2(write_null.fileno(), sys.stderr.fileno()) # 写入pid文件
if pid_file:
with open(pid_file, 'w+') as f:
f.write(str(os.getpid()))
# 注册退出函数,进程异常退出时移除pid文件
atexit.register(os.remove, pid_file)

  - 扩展:Python中fork的代价

在Unix系统上面启动守护进程

fork()和僵尸进程的更多相关文章

  1. fork和僵尸进程

    1. 关于fork fork()函数: 用于创建一个进程,所创建的进程复制父进程的代码段/数据段/BSS段/堆/栈等所有用户空间信息:在内核中操作系统重新为其申请了一个PCB,并使用父进程的PCB进行 ...

  2. 为什么fork()2次会避免产生僵尸进程

    什么是僵尸进程:用fork()创建子进程后,子进程已终止但父进程没有对它进行善后处理,那么子进程的进程描述符就一直保存在内存中,子进程就是僵尸进程. 怎么产生僵尸进程: 1.父进程没有SIGCHLD信 ...

  3. 2次使用fork避免产生僵尸进程和不去处理SIGCHLD信号

    1.如下代码所示 #include <unistd.h> #include <sys/types.h> #include <unistd.h> int main(i ...

  4. 【转】Linux杀死fork产生的子进程的僵尸进程defunct

    僵尸进程 就是 已经结束,但是还没有清理出去的.用kill -9 $PID 也无法杀死. 所以程序中应该避免出现僵尸进程. 用fork之后,父进程如果没有wait /waitpid 等待子进程的话,子 ...

  5. 为何要fork()两次来避免产生僵尸进程??

    最近安装书上说的,开始搞多进程了..看到了一个好帖子,学习学习 http://blog.sina.com.cn/s/blog_9f1496990100y420.html 首先我们要明白,为什么要避免僵 ...

  6. 为何要fork()两次来避免产生僵尸进程?

    为何要fork()两次来避免产生僵尸进程?   当我们只fork()一次后,存在父进程和子进程.这时有两种方法来避免产生僵尸进程: 父进程调用waitpid()等函数来接收子进程退出状态. 父进程先结 ...

  7. 1.1 Linux中的进程 --fork、孤儿进程、僵尸进程、文件共享分析

    操作系统经典的三态如下: 1.就绪态 2.等待(阻塞) 3.运行态 其转换状态如下图所示: 操作系统内核中会维护多个队列,将不同状态的进程加入到不同的队列中,其中撤销是进程运行结束后,由内核收回. 以 ...

  8. linux系统编程之进程(三):进程复制fork,孤儿进程,僵尸进程

    本节目标: 复制进程映像 fork系统调用 孤儿进程.僵尸进程 写时复制 一,进程复制(或产生)      使用fork函数得到的子进程从父进程的继承了整个进程的地址空间,包括:进程上下文.进程堆栈. ...

  9. UNIX高级环境编程(9)进程控制(Process Control)- fork,vfork,僵尸进程,wait和waitpid

    本章包含内容有: 创建新进程 程序执行(program execution) 进程终止(process termination) 进程的各种ID   1 进程标识符(Process Identifie ...

随机推荐

  1. Hdoj 1374.Knight Moves 题解

    Problem Description A friend of you is doing research on the Traveling Knight Problem (TKP) where yo ...

  2. 自学Linux命令行与Shell脚本之路

    自学Linux命令行与Shell脚本之路[第一回]:初识Linux   1.1 自学Linux Shell1.1-Linux初识 1.2 自学Linux Shell1.2-Linux目录结构 1.3  ...

  3. NOIP2018保卫王国

    题目大意:给一颗有点权的树,每次规定两个点选还是不选,求这棵树的最小权点覆盖. 题解 ZZ码农题. 要用动态dp做,这题就是板子,然鹅并不会,留坑代填. 因为没有修改,所以可以静态倍增. 我们先做一遍 ...

  4. DP题组

    按照顺序来. Median Sum 大意: 给你一个集合,求其所有非空子集的权值的中位数. 某集合的权值即为其元素之和. 1 <= n <= 2000 解: 集合配对,每个集合都配对它的补 ...

  5. 【SPOJ116】Intervals

    题目大意:有 N 个区间,在区间 [a, b] 中至少取任意互不相同的 c 个整数.求在满足 N 个区间约束的情况下,至少要取多少个正整数. 题解:差分约束系统模板题. 差分约束系统是对于 N 个变量 ...

  6. bzoj2553 禁忌

    题目链接 题意 给出一个\(n\)个字符串的字典.对于一个字符串,他的贡献是这个字符串中最多的在字典中出现的不重叠子串的数量. 然后问一个长度为\(len\)的,字符集为前\(alphabet\)个字 ...

  7. Adapter的getView

    http://blog.csdn.net/yelbosh/article/details/7831812 BaseAdapter就Android应用程序中经常用到的基础数据适配器,它的主要用途是将一组 ...

  8. (最小生成树 并查集)P1111 修复公路 洛谷

    题目背景 A地区在地震过后,连接所有村庄的公路都造成了损坏而无法通车.政府派人修复这些公路. 题目描述 给出A地区的村庄数N,和公路数M,公路是双向的.并告诉你每条公路的连着哪两个村庄,并告诉你什么时 ...

  9. 关于MyBase 7.0 破解的方法

    Mybase 是一个功能强劲且可随心所欲自定义格式及层次关系的通用资料管理软件, 可用于管理各种各样的信息,如一:各类文档.文件.资料.名片.事件.日记.项目.笔记.下载的精华.收集的各种资料等等,即 ...

  10. qml:: QVariant转为自定义类型

    QVariant可以实现C++与qml之间的自定义类型的传递: 以QObject类型为例: 1.  QObject转为QVariant QVariant var = QVariant::fromVal ...