LinuxSir.cn,穿越时空的Linuxsir!

 找回密码
 注册
搜索
热搜: shell linux mysql
查看: 715|回复: 3

新人请教个关于父子进程关系的问题!

[复制链接]
发表于 2007-6-10 15:24:26 | 显示全部楼层 |阅读模式
看到一个英文教材上的一段话,原文如下:

The shell initiates the execution, submits arguments, and waits until they are done before displaying a new prompt.

This is what happens when executing a simple command like echo:

[devid@nittedal david]$ echo "hello"
hello

The shell(bash) reads the line echo "hello" and parses the echo command from the argument. Themn the following happens:

1.The process is divided into two exact copies.
2.The parent process waits for the child to terminate.
2.The child swaps the old program (bash for the new command(echo "hello")).
4.The child executes the program.
5.The child terminates.
6.The parent process goes on and you get your shell prompt back.

The process is divided into two identical copies. In order that a process copies itself, the fork function call must be started. After fork, you have two processes: one parent process and one child process. The difference between them is their PID number. The two processes share open files, and each process knows that there is a parent/child relationship to the other.

请问,在1里面,process是指bash 还是指?
请问,在后面process复制了自己,这里的process还是bash,如果这样的话,bash的效率不是太低了?
是不是process是指别的什么?
真的很困惑,请大家帮助,谢谢。
发表于 2007-6-10 16:42:18 | 显示全部楼层
process 就是你说的父进程。复制出来的叫做子进程。这里的复制只是内存页面描述表的复制, 真正的内存复制发生在 copy-on-write,  当然了复制过程是由系统自动进行的, 而不是bash.  至于效率低的说法 fork确实不如vfork 或者clone , 但是fork提供了隔离进程的手段, 使得子进程的崩溃不影响父进程。
回复 支持 反对

使用道具 举报

 楼主| 发表于 2007-6-10 17:23:05 | 显示全部楼层
谢谢楼上的大大,刚才我又跑到图书管翻了一些书,加上您说的,现在大概理解了。英文读物,理解起来,困难啊~~~

我只是大概在脑中具体化了一下,process可以理解为运行实体,BASH,或其它的command,program是不是都是通过在运行实体上的操作来达到运算或其它目的的呢?而上面的例子中,bash是父进程而已? 再假设一个复杂一点例子,是不是在bash中用例如 vi 的时候,process中的一个父进程是bash,vi是bash的子进程,vi是vi中的命令的父进程,vi的命令是子进程呢?而在process中呈现一个树状结构?产生同样数目的wait和execute?这时process这个实体也被分为 0.5 0.25 0.25 这样三个小实体(理论状态)?
回复 支持 反对

使用道具 举报

发表于 2007-6-12 05:50:17 | 显示全部楼层
Post by howard228

我只是大概在脑中具体化了一下,process可以理解为运行实体,BASH,或其它的command,program是不是都是通过在运行实体上的操作来达到运算或其它目的的呢?而上面的例子中,bash是父进程而已? 再假设一个复杂一点例子,是不是在bash中用例如 vi 的时候,process中的一个父进程是bash,vi是bash的子进程,vi是vi中的命令的父进程,vi的命令是子进程呢?而在process中呈现一个树状结构?产生同样数目的wait和execute?这时process这个实体也被分为 0.5 0.25 0.25 这样三个小实体(理论状态)?

差不多,但上述树状进程结构只对bash这种shell应用成立。可以用pstree看看系统的当前进程树,加深理解。vi是另一回事。你自己多编几个程序就比较懂了。
回复 支持 反对

使用道具 举报

您需要登录后才可以回帖 登录 | 注册

本版积分规则

快速回复 返回顶部 返回列表