[Linux學習筆記]進程概念及控制

來源:互聯網
上載者:User

內容:進程的標識、運行、終止、進程間競爭和進程操作

1.進程的運行和終止

每個進程都有唯一的ID,ID為0 的系統進程swapper稱作交換進程,是一個系統調度進程;ID為1的進程init是一個使用者進程,負責讀與系統有關的初始設定檔案;ID為2的進程是pagedaemon頁精靈進程,是系統進程。這些進程是特殊的進程,是不能被中止的。

關於進程的運行和終止的一些函數:fork、vfork、exec、exit。

fork函數是Linux系統建立新進程的惟一方法。fork函數可以建立除交換進程、init進程和頁精靈進程之外的使用者進程。

vfork函數用來建立一個新進程,進程建立以後馬上執行exec一執行一個新程式。

exec本身不能建立新進程,因為執行exec前後原父進程和子進程的ID並沒有改變。

exit函數調用由atexit註冊的清除的函數,並關閉所有的標準I/O流。進程的退出狀態很重要,一般以0值表示正常退出。

函數的原型為:

#include <sys/types.h>#include <unistd.h>pid_t fork(void);pid_t vfork(void);//#include <unistd.h>int execl(const char *pathname, const char arg0,...);int execv(const char *pathname, char *const argv[]);int execle(const char *pathname, const char *arg0,...);int execve(const char *pathname, char *const argv[], char *const envp[]);int excelp(const *filename, const char *arg0,...);int execvp(const char *filename, char *const argv[]);//#inlcude <stdlib.h>void exit(int status);#inlcude <unistd.h>void _exit(int status);

2.wait操作

當調用wait或者waitpid系統調用,進程可能出現3種狀態:阻塞(當其所有子進程還在運行)、子進程處於終止狀態(立即返回)和出錯立即返回(沒有子進程)。

wait函數的格式:

#include <sys/types.h>#include <sys/wait.h>pid_t wait(int *statloc);pid_t waitpid(pid_t pid, int statloc, int options);

//成功返回進程ID,失敗返回-1

相關文章

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.