For faster navigation, this Iframe is preloading the Wikiwand page for 子进程.

子进程

计算机领域中,子进程為由另外一個进程(对应称之为父进程)所创建的进程。子进程继承了父进程的大部分属性,例如文件描述符

产生

[编辑]

Unix中,子进程通常为系统调用fork的产物。在此情况下,子进程一开始就是父进程的副本,而在这之后,根据具体需要,子进程可以借助exec英语Exec (operating system)调用来链式加载英语Chain loading另一程序。

与父进程的关系

[编辑]

一个进程可能下属多个子进程,但最多只能有1个父进程,而若某一进程没有父进程,则可知该进程很可能由内核直接生成。在Unix类Unix系统中,进程ID为1的进程(即init进程)是在系统引导阶段由内核直接创建的,且不会在系统运行过程中终止执行(可参见Linux启动流程);而对于其他无父进程的进程,则可能是为在用户空间完成各种后台任务而执行的。

当某一子进程结束、中断或恢复执行时,内核会发送SIGCHLD信号予其父进程。在默认情况下,父进程会以SIG_IGN函数忽略之[1]

“孤儿进程”与“僵尸进程”

[编辑]

在对应的父进程结束执行后,进程就会变成孤儿进程,但之后会立即由init进程“收养”为其子进程。

某一子进程终止执行后,若其父进程未提前调用wait,则内核会持续保留子进程的退出状态等信息,以使父进程可以wait获取之[2] 。而因为在这种情况下,子进程虽已终止,但仍在消耗系统资源,所以其亦称僵尸进程wait常于SIGCHLD信号的处理函数中调用。

解决与预防

[编辑]

POSIX.1-2001标准规定中,父进程可将SIGCHLD的处理函数设为SIG_IGN(亦为默认设定),或为SIGCHLD设定SA_NOCLDWAIT标记,以使内核可以自动回收已终止的子进程的资源。自Linux 2.6与FreeBSD 5.0起,两种内核皆支持了这两种方式[3]。但是,在忽略SIGCHLD信号的问题上,由于System VBSD由来已久的差异,若要回收派生出的子进程的资源,调用wait仍是最便捷的方式[2][4]

参见

[编辑]

参考资料

[编辑]
  1. ^ signal: overview of signals – 参考,单一UNIX®规范第7期,由國際開放標準組織发布
  2. ^ 2.0 2.1 wait: wait for process to change state – 参考,单一UNIX®规范第7期,由國際開放標準組織发布
  3. ^ http://fuse4bsd.creo.hu/localcgi/maSA_NOCLDWAITSA_NOCLDWAITn-cgi.cgi?signal+3[永久失效連結]
  4. ^ sigaction: examine and change a signal action – 参考,单一UNIX®规范第7期,由國際開放標準組織发布
  5. ^ pstree: print process trees – 参考,单一UNIX®规范第7期,由國際開放標準組織发布

本條目部分或全部内容出自以GFDL授權發佈的《自由線上電腦詞典》(FOLDOC)。

{{bottomLinkPreText}} {{bottomLinkText}}
子进程
Listen to this article

This browser is not supported by Wikiwand :(
Wikiwand requires a browser with modern capabilities in order to provide you with the best reading experience.
Please download and use one of the following browsers:

This article was just edited, click to reload
This article has been deleted on Wikipedia (Why?)

Back to homepage

Please click Add in the dialog above
Please click Allow in the top-left corner,
then click Install Now in the dialog
Please click Open in the download dialog,
then click Install
Please click the "Downloads" icon in the Safari toolbar, open the first download in the list,
then click Install
{{::$root.activation.text}}

Install Wikiwand

Install on Chrome Install on Firefox
Don't forget to rate us

Tell your friends about Wikiwand!

Gmail Facebook Twitter Link

Enjoying Wikiwand?

Tell your friends and spread the love:
Share on Gmail Share on Facebook Share on Twitter Share on Buffer

Our magic isn't perfect

You can help our automatic cover photo selection by reporting an unsuitable photo.

This photo is visually disturbing This photo is not a good choice

Thank you for helping!


Your input will affect cover photo selection, along with input from other users.

X

Get ready for Wikiwand 2.0 🎉! the new version arrives on September 1st! Don't want to wait?