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. 使用nmon监控Linxu服务器性能

    nmon是IBM开发的Linux软件工具.能够监控多项Linux服务,最常见的如:CPU使用率.内存使用率.硬盘读写.网卡流量等. 并能设置参数,将记录的数据保存到文件,利用Excel做统计分析. 安 ...

  2. 缺省源和 Vim 配置

    缺省源 #include <bits/stdc++.h> #define x first #define y second #define pb push_back #define mp ...

  3. 【CF891C】Envy(最小生成树)

    [CF891C]Envy(最小生成树) 题面 Codeforces 洛谷 题解 考虑\(MST\)的构建过程,对于所有权值相同的边一起考虑. 显然最终他们连出来的结果是固定的. 把连边改为把联通块联通 ...

  4. Mysql连接报错:1130 - Host ‘118.111.111.111’ is not allowed to connect to this MariaDB server

    这个问题是因为在数据库服务器中的mysql数据库中的user的表中没有权限(也可以说没有用户),下面将记录我遇到问题的过程及解决的方法. 在搭建完LNMP环境后用Navicate连接出错 遇到这个问题 ...

  5. js 获取 url 参数

    /** * 根据页面地址获取所有参数对象 * @return Object{} 返回所有参数 * ------------------------------ * 根据页面地址获取指定参数对象 * @ ...

  6. 【洛谷P3275】糖果

    题目大意:维护 M 个差分约束关系,问是否可以满足所有约束,如果满足输出一组解.\(N<=1e5\) 题解:差分约束模型可以通过构建一张有向图来求解.是否满足所有约束可以利用 spfa 进行判断 ...

  7. 2019阿里校招测评题,光明小学完全图最短路径问题(python实现)

    题目:光明小学的小朋友们要举行一年一度的接力跑大赛了,但是小朋友们却遇到了一个难题:设计接力跑大赛的线路,你能帮助他们完成这项工作么?光明小学可以抽象成一张有N个节点的图,每两点间都有一条道路相连.光 ...

  8. 洛谷 P2622 关灯问题II(状压DP入门题)

    传送门 https://www.cnblogs.com/violet-acmer/p/9852294.html 题解: 相关变量解释: int n,m; ];//a[i][j] : 第i个开关对第j个 ...

  9. (贪心部分背包问题)Saving HDU HDU2111

    Saving HDU Time Limit: 3000/1000 MS (Java/Others)    Memory Limit: 32768/32768 K (Java/Others)Total ...

  10. 关键字(3):order by/group by/having/where/sum/count(*)...查询结果筛选关键字

    ORDER BY <属性表> 只要在WHERE子句的选择条件后面加上如下子句:ORDER BY <属性表> 就可以实现输出的排序,默认的顺序为升序(ASC).可以在属性的后面加 ...