4-Linux 进程组的精确理解和 Daemon进程

20 篇文章 0 订阅

shell 进程: http://c.biancheng.net/view/739.html

为了验证 if you log into a machine, your shell starts a session from: https://blog.csdn.net/cpxsxn/article/details/107371177

root@robert-Ubuntu:/tmp# ps -xj
 PPID   PID  PGID   SID TTY      TPGID STAT   UID   TIME COMMAND
	0     1     1     1 ?           -1 Ss       0   0:02 /sbin/init splash
	0     2     0     0 ?           -1 S        0   0:00 [kthreadd]
	.......
	1   869   869   869 ?           -1 Ss       0   0:00 /usr/sbin/sshd -D
	1   922   922   922 ?           -1 Ss       0   0:00 nginx: master process /usr/sbin/nginx -g daemon 
  688   943   943   688 ?           -1 S        0   0:00 /sbin/dhclient -d -q -sf /usr/lib/NetworkManager
	1   949   949   949 ?           -1 Ssl      0   0:00 /usr/sbin/lightdm
  688   954   954   688 ?           -1 S        0   0:00 /sbin/dhclient -d -q -sf /usr/lib/NetworkManager
  688   956   956   688 ?           -1 S        0   0:00 /sbin/dhclient -d -q -sf /usr/lib/NetworkManager
	1   987   985   985 ?           -1 Sl       0   0:03 /usr/sbin/VBoxService --pidfile /var/run/vboxadd
  949  1003  1003  1003 tty7      1003 Ssl+     0   0:00 /usr/lib/xorg/Xorg -core :0 -seat seat0 -auth /v
	1  1447  1447  1447 tty1      1447 Ss+      0   0:00 /sbin/agetty --noclear tty1 linux
  949  1456   949   949 ?           -1 Sl       0   0:00 lightdm --session-child 16 19
  949  1508   949   949 ?           -1 S        0   0:00 lightdm --session-child 12 19
	1  1577  1577  1577 ?           -1 Ssl      0   0:00 /usr/lib/upower/upowerd
	2  1884     0     0 ?           -1 I        0   0:00 [kworker/u2:2]
  869  1993  1993  1993 ?           -1 Ss       0   0:00 sshd: root@pts/8,pts/10
	1  1995  1995  1995 ?           -1 Ss       0   0:00 /lib/systemd/systemd --user
 1995  2006  1995  1995 ?           -1 S        0   0:00 (sd-pam)
 .......
 1993  2068  2068  2068 pts/8     2356 Ss       0   0:00 -bash
 1993  2225  2225  2225 pts/10    2357 Ss       0   0:00 -bash
	2  2298     0     0 ?           -1 I        0   0:00 [kworker/0:2]
	2  2317     0     0 ?           -1 I        0   0:00 [kworker/u2:0]
	2  2328     0     0 ?           -1 I        0   0:00 [kworker/0:0]
	2  2352     0     0 ?           -1 I        0   0:00 [kworker/0:1]
 2068  2356  2356  2068 pts/8     2356 S+       0   0:00 python executor.py    executor.py 主体就是 sleep(10) ,前后各一句打印  
 2225  2357  2357  2225 pts/10    2357 R+       0   0:00 ps -xj
root@robert-Ubuntu:/tmp# 



#!/usr/bin/env python
# -*- coding: utf-8 -*-
import os
import time
import signal
def main():
	log.info("In the main")
	time.sleep(5)

为了验证:父子进程是一个进程组,但是子进程不是进程组组长

root@robert-Ubuntu:/tmp# ps -xj
 PPID   PID  PGID   SID TTY      TPGID STAT   UID   TIME COMMAND
	0     1     1     1 ?           -1 Ss       0   0:02 /sbin/init splash
	0     2     0     0 ?           -1 S        0   0:00 [kthreadd]
	.....
	1   869   869   869 ?           -1 Ss       0   0:00 /usr/sbin/sshd -D
	1   922   922   922 ?           -1 Ss       0   0:00 nginx: master process /usr/sbin/nginx -g daemon 
  688   943   943   688 ?           -1 S        0   0:00 /sbin/dhclient -d -q -sf /usr/lib/NetworkManager
	1   949   949   949 ?           -1 Ssl      0   0:00 /usr/sbin/lightdm
  688   954   954   688 ?           -1 S        0   0:00 /sbin/dhclient -d -q -sf /usr/lib/NetworkManager
  688   956   956   688 ?           -1 S        0   0:00 /sbin/dhclient -d -q -sf /usr/lib/NetworkManager
	1   987   985   985 ?           -1 Sl       0   0:02 /usr/sbin/VBoxService --pidfile /var/run/vboxadd
  949  1003  1003  1003 tty7      1003 Ssl+     0   0:00 /usr/lib/xorg/Xorg -core :0 -seat seat0 -auth /v
	1  1447  1447  1447 tty1      1447 Ss+      0   0:00 /sbin/agetty --noclear tty1 linux
  949  1456   949   949 ?           -1 Sl       0   0:00 lightdm --session-child 16 19
  949  1508   949   949 ?           -1 S        0   0:00 lightdm --session-child 12 19
	1  1577  1577  1577 ?           -1 Ssl      0   0:00 /usr/lib/upower/upowerd
	2  1884     0     0 ?           -1 I        0   0:00 [kworker/u2:2]
  869  1993  1993  1993 ?           -1 Ss       0   0:00 sshd: root@pts/8,pts/10
	1  1995  1995  1995 ?           -1 Ss       0   0:00 /lib/systemd/systemd --user
 1995  2006  1995  1995 ?           -1 S        0   0:00 (sd-pam)
	.......
 1993  2068  2068  2068 pts/8     2339 Ss       0   0:00 -bash
 1993  2225  2225  2225 pts/10    2341 Ss       0   0:00 -bash
	2  2298     0     0 ?           -1 I        0   0:00 [kworker/0:2]
	2  2317     0     0 ?           -1 I        0   0:00 [kworker/u2:0]
	2  2328     0     0 ?           -1 I        0   0:00 [kworker/0:0]
 2068  2339  2339  2068 pts/8     2339 S+       0   0:00 python executor.py
 2339  2340  2339  2068 pts/8     2339 S+       0   0:00 python executor.py   子进程不是进程组长
 2225  2341  2341  2225 pts/10    2341 R+       0   0:00 ps -xj
root@robert-Ubuntu:/tmp# 


#!/usr/bin/env python
# -*- coding: utf-8 -*-
import os
import time
import signal

def main():
	log.info("In the main")
	pid = os.fork()
	if pid == 0:
		log.info("The first sub process: pid = {0}".format(os.getpid()))
		time.sleep(3)
	else:
		log.info("The parent process: pid = {0}".format(os.getpid()))
		time.sleep(3)
		exit(0)

为了验证 os.setsid(); executor.py 主体就是一个真正的 daemon 进程

root@robert-Ubuntu:/tmp# ps -xj
 PPID   PID  PGID   SID TTY      TPGID STAT   UID   TIME COMMAND
	0     1     1     1 ?           -1 Ss       0   0:02 /sbin/init splash
	0     2     0     0 ?           -1 S        0   0:00 [kthreadd]
	.......
	1   869   869   869 ?           -1 Ss       0   0:00 /usr/sbin/sshd -D
	1   922   922   922 ?           -1 Ss       0   0:00 nginx: master process /usr/sbin/nginx -g daemon 
  688   943   943   688 ?           -1 S        0   0:00 /sbin/dhclient -d -q -sf /usr/lib/NetworkManager
	1   949   949   949 ?           -1 Ssl      0   0:00 /usr/sbin/lightdm
  688   954   954   688 ?           -1 S        0   0:00 /sbin/dhclient -d -q -sf /usr/lib/NetworkManager
  688   956   956   688 ?           -1 S        0   0:00 /sbin/dhclient -d -q -sf /usr/lib/NetworkManager
	1   987   985   985 ?           -1 Sl       0   0:02 /usr/sbin/VBoxService --pidfile /var/run/vboxadd
  949  1003  1003  1003 tty7      1003 Ssl+     0   0:00 /usr/lib/xorg/Xorg -core :0 -seat seat0 -auth /v
	1  1447  1447  1447 tty1      1447 Ss+      0   0:00 /sbin/agetty --noclear tty1 linux
  949  1456   949   949 ?           -1 Sl       0   0:00 lightdm --session-child 16 19
  949  1508   949   949 ?           -1 S        0   0:00 lightdm --session-child 12 19
	1  1577  1577  1577 ?           -1 Ssl      0   0:00 /usr/lib/upower/upowerd
	2  1884     0     0 ?           -1 I        0   0:00 [kworker/u2:2]
  869  1993  1993  1993 ?           -1 Ss       0   0:00 sshd: root@pts/8,pts/10
	1  1995  1995  1995 ?           -1 Ss       0   0:00 /lib/systemd/systemd --user
 1995  2006  1995  1995 ?           -1 S        0   0:00 (sd-pam)
	.......
 1993  2068  2068  2068 pts/8     2312 Ss       0   0:00 -bash
	2  2210     0     0 ?           -1 I        0   0:00 [kworker/0:0]
 1993  2225  2225  2225 pts/10    2315 Ss       0   0:00 -bash
	2  2252     0     0 ?           -1 I        0   0:00 [kworker/0:1]
	2  2277     0     0 ?           -1 I        0   0:00 [kworker/u2:1]
	2  2298     0     0 ?           -1 I        0   0:00 [kworker/0:2]
	1  2307  2307  2307 ?           -1 Ssl      0   0:00 /usr/lib/NetworkManager/nm-dispatcher
 2068  2312  2312  2068 pts/8     2312 S+       0   0:00 python executor.py
 2312  2313  2313  2313 ?           -1 Ss       0   0:00 python executor.py  (第一个子进程创建了一个新 session,然后确实变成了进程组长和 session leader)
 2313  2314  2313  2313 ?           -1 S        0   0:00 python executor.py
 2225  2315  2315  2225 pts/10    2315 R+       0   0:00 ps -xj
root@robert-Ubuntu:/tmp# 





#!/usr/bin/env python
# -*- coding: utf-8 -*-
import os
import time
import signal

def main():
	log.info("In the main")
	pid = os.fork()
	if pid == 0:
		log.info("The first sub process: pid = {0}".format(os.getpid()))
		os.setsid()
		pid = os.fork()
		if pid == 0:
			log.info("The second sub process: pid = {0}".format(os.getpid()))
			'''
			1. Change the work dir of the second subprocess
			2. Change the umask of the second subprocess
			3. Add the signal handler for SIGCHLD
			4. Close the opened files
			'''
			os.chdir('/')
			os.umask(0)
			signal.signal(signal.SIGCHLD, signal.SIG_IGN)
			open_file_list = [0, 1, 2]
			for file in open_file_list:
			    os.close(file)
			poll_db_task()
		else:
			log.info("The parent of second sub process: pid = {0}".format(os.getpid()))
			time.sleep(3)
			exit(0)
	else:
		log.info("The parent process: pid = {0}".format(os.getpid()))
		time.sleep(3)
		exit(0)
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值