免费注册 查看新帖 |

Chinaunix

  平台 论坛 博客 文库
最近访问板块 发新帖
楼主: lululau
打印 上一主题 下一主题

还是管道和子shell的问题,越来越糊涂了 [复制链接]

论坛徽章:
0
11 [报告]
发表于 2009-06-13 18:10 |只看该作者
额。。。我知道怎么错的了
因为我根本就没搞明白什么是subshell
A subshell does not create a new instance of the shell, just a new environment.
我把subshell等同于一个新的shell进程实例了。。。
谢谢DX们~

论坛徽章:
5
2015年辞旧岁徽章
日期:2015-03-03 16:54:152015年迎新春徽章
日期:2015-03-04 09:50:282015年亚洲杯之朝鲜
日期:2015-03-13 22:47:33IT运维版块每日发帖之星
日期:2016-01-09 06:20:00IT运维版块每周发帖之星
日期:2016-03-07 16:27:44
12 [报告]
发表于 2009-06-13 23:40 |只看该作者

论坛徽章:
5
2015年辞旧岁徽章
日期:2015-03-03 16:54:152015年迎新春徽章
日期:2015-03-04 09:50:282015年亚洲杯之朝鲜
日期:2015-03-13 22:47:33IT运维版块每日发帖之星
日期:2016-01-09 06:20:00IT运维版块每周发帖之星
日期:2016-03-07 16:27:44
13 [报告]
发表于 2009-06-14 00:16 |只看该作者
cmda|cmdb的两个命令确实是分别在不同的subshell中执行。
但PID变了并且PPID为当前shell的PID, 这应该还不足以说明就是subshell中执行的。
比如
$ cmda;cmdb
也满足这个条件。

[ 本帖最后由 blackold 于 2009-6-14 00:17 编辑 ]

论坛徽章:
23
15-16赛季CBA联赛之吉林
日期:2017-12-21 16:39:27白羊座
日期:2014-10-27 11:14:37申猴
日期:2014-10-23 08:36:23金牛座
日期:2014-09-30 08:26:49午马
日期:2014-09-29 09:40:16射手座
日期:2014-11-25 08:56:112015年辞旧岁徽章
日期:2015-03-03 16:54:152015年迎新春徽章
日期:2015-03-04 09:49:0315-16赛季CBA联赛之山东
日期:2017-12-21 16:39:1915-16赛季CBA联赛之广东
日期:2016-01-19 13:33:372015亚冠之山东鲁能
日期:2015-10-13 09:39:062015亚冠之西悉尼流浪者
日期:2015-09-21 08:27:57
14 [报告]
发表于 2009-06-14 09:40 |只看该作者

回复 #13 blackold 的帖子

受教

论坛徽章:
0
15 [报告]
发表于 2009-06-14 10:05 |只看该作者
原帖由 blackold 于 2009-6-14 00:16 发表
cmda|cmdb的两个命令确实是分别在不同的subshell中执行。
但PID变了并且PPID为当前shell的PID, 这应该还不足以说明就是subshell中执行的。
比如
$ cmda;cmdb
也满足这个条件。

和我那天问的问题差不多,
PID和PPID是不是不能证明subshell的?

论坛徽章:
5
2015年辞旧岁徽章
日期:2015-03-03 16:54:152015年迎新春徽章
日期:2015-03-04 09:50:282015年亚洲杯之朝鲜
日期:2015-03-13 22:47:33IT运维版块每日发帖之星
日期:2016-01-09 06:20:00IT运维版块每周发帖之星
日期:2016-03-07 16:27:44
16 [报告]
发表于 2009-06-14 10:18 |只看该作者

回复 #14 ly5066113 的帖子

Tim,折杀我了。

我觉得要先弄清楚sub shell的定义。

查了些资料,发现subshell的定义有些混乱。
bashMan:

Each command in a pipeline is executed as a separate process (i.e., in a subshell).  

When a simple command other than a builtin or shell function is to be executed, it is invoked in a
separate execution environment that consists of the following. Unless otherwise noted, the values are
inherited from the shell.


这个separate execution是subshell吗?

A. 在当前shell执行外部命令,如shell> date, 是fork+exec, 算不是subshell?
B. ()是fork一个child shell,该child再fork+exec来执行命令。这个subshell和A中的"subshell"显然是不同的。

UNIX: The Textbook, by Syed Mansoor Sarvar, Robert Koretsky and Syed Aqeel Sarvar中提到:
A child shell is also called subshell

问题是fork+exec是fork一个child shell,然后在该child shell中exec.
而执行脚本(shell>scriptname)时,是fort一个child shell A,该child shell A再fork一个child shell B, 在B中再exec.

那么child shell是指哪种情况?



UNIX at Fermilab中的Important UNIX Concepts:

When you give the shell a command associated with a compiled executable or shell script, the shell
creates, or forks, a new process called a subshell.

外部命令也在subshell中执行。

To execute most built-in commands, the shell forks a subshell which executes the command directly (no
exec system call). For the built-in commands cd, set, alias and source, the current shell executes the
command; no subshell is forked.


shell> built-inCommands这样执行时,大部分内部命令也是在subshell中执行。
可见,UNIX at Fermilab认为fork 一个child shell就是subshell, 不管是fork-fork+exec, 还是 fork+exec。

[ 本帖最后由 blackold 于 2009-6-14 10:27 编辑 ]

论坛徽章:
5
2015年辞旧岁徽章
日期:2015-03-03 16:54:152015年迎新春徽章
日期:2015-03-04 09:50:282015年亚洲杯之朝鲜
日期:2015-03-13 22:47:33IT运维版块每日发帖之星
日期:2016-01-09 06:20:00IT运维版块每周发帖之星
日期:2016-03-07 16:27:44
17 [报告]
发表于 2009-06-14 11:30 |只看该作者

回复 #15 haimming 的帖子

要看如何理解什么是subshell。

论坛徽章:
0
18 [报告]
发表于 2009-06-14 11:34 |只看该作者

回复 #17 blackold 的帖子

我刚才又去翻了下ABS,定义与我的理解不一样
A subshell is a separate instance of the command processor -- the shell that gives you the prompt at the
console or in an xterm window. Just as your commands are interpreted at the command line prompt, similarly
does a script batch-process a list of commands. Each shell script running is, in effect, a subprocess (child
process) of the parent shell.

论坛徽章:
5
2015年辞旧岁徽章
日期:2015-03-03 16:54:152015年迎新春徽章
日期:2015-03-04 09:50:282015年亚洲杯之朝鲜
日期:2015-03-13 22:47:33IT运维版块每日发帖之星
日期:2016-01-09 06:20:00IT运维版块每周发帖之星
日期:2016-03-07 16:27:44
19 [报告]
发表于 2009-06-14 11:53 |只看该作者

回复 #18 haimming 的帖子

嗯,严格的subshell应该是指这种“定义”吧。

你理解的subshell是怎么样的?说来听听,让我学习学习。

论坛徽章:
0
20 [报告]
发表于 2009-06-14 11:59 |只看该作者
我觉得把subshell理解成一个新的独立的环境表就可以解决我的疑问了
而产生subshell的过程中,根本就没有fork过
您需要登录后才可以回帖 登录 | 注册

本版积分规则 发表回复

  

北京盛拓优讯信息技术有限公司. 版权所有 京ICP备16024965号-6 北京市公安局海淀分局网监中心备案编号:11010802020122 niuxiaotong@pcpop.com 17352615567
未成年举报专区
中国互联网协会会员  联系我们:huangweiwei@itpub.net
感谢所有关心和支持过ChinaUnix的朋友们 转载本站内容请注明原作者名及出处

清除 Cookies - ChinaUnix - Archiver - WAP - TOP