加入收藏 | 设为首页 | 会员中心 | 我要投稿 李大同 (https://www.lidatong.com.cn/)- 科技、建站、经验、云计算、5G、大数据,站长网!
当前位置: 首页 > 综合聚焦 > 服务器 > Linux > 正文

linux – 当“bash -c”会导致子shell创建?

发布时间:2020-12-14 00:54:30 所属栏目:Linux 来源:网络整理
导读:在我的centos7.4服务器上,如果运行 bash -c "python -m SimpleHTTPServer"pstree -sp 10784 (PID of python server) 将会呈现 systemd(1)───sshd(922)───sshd(11595)───sshd(11597)───bash(11598)───python(11617) 与 bash -c "python -V pyt
在我的centos7.4服务器上,如果运行

bash -c "python -m SimpleHTTPServer"

pstree -sp 10784 (PID of python server)

将会呈现

systemd(1)───sshd(922)───sshd(11595)───sshd(11597)───bash(11598)───python(11617)

bash -c "python -V && python -m SimpleHTTPServer"

我有

systemd(1)───sshd(922)───sshd(11595)───sshd(11597)───bash(11598)───bash(11638)───python(11640)

为什么在第二个命令中创建子shell?

解决方法

第二个命令使用子shell来处理&&运算符 – 它需要等待SIGCHLD第一个命令终止,然后决定是否需要运行第二个命令.

在第一种情况下 – 这似乎发生:
Bash启动并发现它需要运行一个简单的字符串.作为优化,然后execs命令首先没有分叉,子shell被python命令替换.

对于第二种情况,此优化不起作用,并且bash forks运行python -V命令并等待结果.收到退出该命令的SIGCHLD后,它将运行第二个命令(并且不应用优化以防止额外的fork)

见1370 here号线

(编辑:李大同)

【声明】本站内容均来自网络,其相关言论仅代表作者个人观点,不代表本站立场。若无意侵犯到您的权利,请及时与联系站长删除相关内容!

    推荐文章
      热点阅读