6第六章程序建立以及程序間通訊

2023-01-14 09:57:03 字數 4071 閱讀 3658

6.1 程序介紹

6.1.1 程序定義

在乙個作業系統中,每個正在執行的工作都可以稱為程序(process)。它包括程序識別符號和相關的資料,相關資料又包含程序變數、外部變數以及程序堆疊等。當乙個程序啟動之後,系統會指定乙個唯一的數值來作為該程序的程序識別符號,即pid。

linux系統檢視程序可以使用ps命令。ps命令用來顯示系統中正在執行的程序及其狀態(如圖6-1所示)。如果想檢視所有程序及其它們的cpu、記憶體佔用率以及所屬使用者等資訊,可以用top命令(如圖6-2所示)。

圖6-1 執行ps命令顯示結果

圖6-2 執行top命令顯示的部分結果

6.1.2 程序的相關函式

● getpid():用來取得當前程序的程序識別符號。返回當前程序的pid。

/* get the process id of the calling process. */

extern __pid_t getpid (void) __throw;

● getppid():用來取得父程序的程序識別符號。返回當前程序的父程序的pid。

/* get the process id of the calling process's parent. */

extern __pid_t getppid (void) __throw;

● exec():建立新程序來取代舊程序,新程序的pid數值和原有舊程序的pid數值相同。

以上三個函式都在/usr/include/中有定義。其中exec()函式族執行成功不會返回,執行失敗則返回-1。該函式族包括如下函式:

extern int execv (__const char *__path, char *__const __ar**) __throw;

/* execute path with all arguments after path until a null pointer, and the argument after that for environment. */

extern int execle (__const char *__path, __const char *__arg, ...) __throw;

/* execute path with all arguments after path until a null pointer and environment from `environ'. */

extern int execl (__const char *__path, __const char *__arg, ...) __throw;

/* execute file, searching in the `path' environment variable if it contains no slashes, with arguments ar** and environment from `environ'. */

extern int execvp (__const char *__file, char *__const __ar**) __throw;

/* execute file, searching in the `path' environment variable if it contains no slashes, with all arguments after file until a null pointer and environment from `environ'. */

extern int execlp (__const char *__file, __const char *__arg, ...) __throw;

● system():該函式可以使用系統的函式庫賴建立新程序。使用格式如下:

int system(const char *string);

即可以將/bin/sh下的string命令列傳到execve函式中,之後執行該程式即相當於執行string字串所代表的命令。該函式呼叫/bin/sh時失敗則返回127;其他失敗則返回-1;如果自變數string為空指標則返回非零值。

● fork():某個父程序可以複製多個子程序的操作。

該函式用來產生新的子程序,而呼叫fork函式的程序則稱為父程序。子程序會複製父程序的資料,並且繼承父程序的各種引數,但是由於子程序和父程序並不使用相同的記憶體空間,所以並不同步。如果建立成功,則在父程序中會返回新建立的子程序**(pid),子程序中成功返回0,失敗則返回-1。

/* clone the calling process, creating an exact copy. return -1 for errors, 0 to the new process, and the process id of the new process to the old process. */

extern __pid_t fork (void) __throw;

● exit():用來終止程序。

extern void exit (int __status)

status為程序的退出狀態。如果為0,則表示程序成功退出;否則如果非0則表示出錯。

● wait():該函式能夠暫停父程序的執行,使其等待子程序。

當子程序執行完畢之後,等待中的父程序將會重新執行。不過如果有多個子程序在執行,則當第乙個子程序完成並返回,則父程序開始重新執行。

/* wait for a child to die. when one does, put its status in *stat_loc and return its process id. for errors, return (pid_t) -1.

*/

extern __pid_t wait (__wait_status __stat_loc) __throw;

wait()函式執行成功之後返回子程序pid,如果失敗則返回-1。

● waitpid():該函式能夠暫停當前程序的執行,使其等待子程序。

當子程序執行完畢之後,等待中的父程序將會重新執行。waitpid()函式執行成功之後返回子程序pid,如果失敗則返回-1。

/* wait for a child matching pid to die.

if pid is greater than 0, match any process whose process id is pid.

if pid is (pid_t) -1, match any process.

if pid is (pid_t) 0, match any process with the same process group as the current process.

if pid is less than -1, match any process whose process group is the absolute value of pid.

if the wnohang bit is set in options, and that child is not already dead, return (pid_t) 0.

if successful, return pid and store the dead child's status in stat_loc.

return (pid_t) -1 for errors. if the wuntraced bit is set in options, return status for stopped children; otherwise don't. */

extern __pid_t waitpid (__pid_t __pid, int *__stat_loc, int __options) __throw;

其中有關的幾個引數說明如表6-1所示:

表6-1 函式waitpid()引數說明

以上兩個函式wait()和waitpid()都是為了清除zombie程序。所謂的zombie程序,是指已經結束執行,卻還沒有被清除的程序。當父程序結束了之後,子程序有可能還沒有結束,這樣的話子程序就會作為zombie程序而沒有被清除。

因此在父程序中呼叫wait()或者waitpid()函式來在父程序結束之前自動清除子程序,保證zombie程序的清除。

6.1.3 實驗9 —— 編譯fork程式

建立乙個新目錄,並在其中編寫和makefile檔案。理解新程序的建立和消除zombie程序的過程。如表6-2所示。

表6-2 實驗9操作步驟

6第六章工作分析

第六章工作分析 答案解析 一 單項選擇題 1.是通過系統分析的方法來確定工作的職責,以及所需的知識和技能的過程。a.工作描述 b.工作規範 c.工作設計 d.工作分析 2.建立薪酬體系要以 為基礎。a.績效管理 b.工作評價 c.人力資源規劃 d.人力資源培訓與開發 3.下列不屬於工作分析在人力資源...

6第六章技術標準

6.1.1 決定道路等級的因素 1 專案路功能 寧丹公路位於南京市西南部區域,貫穿雨花台 江寧兩區,在2003年江蘇省省道網調整前是一條傳統的省際幹線公路。寧丹公路目前是雨花台區鐵心橋街道 江寧西南部橫溪 陶吳等城鎮與南京主城之間最為便捷的聯絡道路,承擔著沿線城鎮之間及各城鎮與南京主城之間的交通聯絡...

第六章建立圖符與例項

圖符 symbol 是指乙個可連續應用的影象 動畫或按鈕。將圖符應用到工作區就成為例項 instance 一 建立圖符 在flash中,為了簡化電影的製作,通常要將一些重複或迴圈進行的動作 如汽車在賓士時車輪的迴圈運動 製成圖符。建立新圖符是通過直接進入圖符編輯模式,建立和編輯圖符的內容。建立的圖符...