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系統程式設計 第二版
150
|
第五章
取得行程識別碼以及父行程識別碼
getpid()
系統呼叫用於傳回進行調用行程(invoking process)的行程識別碼(process
ID):
#include <sys/types.h>
#include <unistd.h>
pid_t getpid (void);
getppid()
系統呼叫用於傳回進行調用行程之父行程的行程識別碼:
#include <sys/types.h>
#include <unistd.h>
pid_t getppid (void);
這兩個函式都不會傳回錯誤訊息。因此,很容易使用:
printf ("My pid=%jd\n", (intmax_t) getpid ());
printf ("Parent's pid=%jd\n", (intmax_t) getppid ());
此處,我們把傳回值轉型成
intmax_t
,這是 C/C++ 語言的一個型別,保證能夠在系統
上儲存任何帶正負號的整數值。換句話說,它可以等於或大於所有其他帶正負號的整數
型別。再加上一個
printf()
修飾符(
%j
),此做法讓我們得以安全地印出一個型別定義
所表示的整數。在
intmax_t
出現之前,沒有具移植的方法可用於完成此事(如果你的
系統不支援
intmax_t
,你可以把
pid_t
轉型為
int
,這是大多數 Unix 系統均支援的資料
型別)。
運行一個新行程
Unix 中,「將一個程式映像(program image)載入至記憶體並加以執行」的步驟與「建
立一個新行程」的步驟是分開的。有一個系統呼叫 ...
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