Skip to Content
LINUX系統程式設計 第二版
book

LINUX系統程式設計 第二版

by Robert Love
December 2013
Intermediate to advanced
496 pages
8h 57m
Chinese
GoTop Information, Inc.
Content preview from LINUX系統程式設計 第二版
行程管理
|
165
return 0;
}
此程式會派生一個子行程,而該子行程會立即結束。然後父行程會執行
wait()
系統呼
叫,以判斷其子行程的狀態。此行程會印出子行程的 pid 以及它是如何死亡的。因為此
例中,子行程的終止係肇因於從
main()
返回,所以我們知道我們將看到如下的輸出:
$ ./wait
pid=8529
Normal termination with exit status=1
如果,不考慮讓子行程返回,而是讓它叫用
abort()
3
,以便送出
SIGABRT
信號給自己,
我們將看到如下的輸出:
$ ./wait
pid=8678
Killed by signal=6
等待特定的行程
子行程行為的觀察很重要。然而,一個行程往往具有多個子行程,而且不會想要等待所
有子行程,但會希望等待特定的子行程。解決方案之一就是對
wait()
進行多次調用,並
觀察每次調用的傳回值。但是,這相當麻煩—如果你後來想要檢查另一個已終止行程?
父行程必須儲存
wait()
的所有輸出,以備之後所需。
如果你知道所要等待之行程的 pid,你可以使用
waitpid()
系統呼叫:
#include <sys/types.h>
#include <sys/wait.h>
pid_t waitpid (pid_t pid, int *status, int options);
相較於
wait()
waitpid()
系統呼叫是一個功能更強大的版本。
waitpid()
所提供的額外
資訊,讓你得以進行微調(fine-tuning)。
pid
參數用於指定所要等待的是哪個行程或哪些行程。它的值可分成四大部分: ...
Become an O’Reilly member and get unlimited access to this title plus top books and audiobooks from O’Reilly and nearly 200 top publishers, thousands of courses curated by job role, 150+ live events each month,
and much more.
Start your free trial

You might also like

優雅的SciPy|Python科學研究的美學

優雅的SciPy|Python科學研究的美學

Juan Nunez-Iglesias, Stéfan van der Walt, Harriet Dashnow
C++语言导学(原书第2版)

C++语言导学(原书第2版)

本贾尼 斯特劳斯特鲁普

Publisher Resources

ISBN: 9789862769812