本章包含內容有:
?
1 進程標識符(Process Identifiers)每個進程都有一個唯一的標識符,進程ID(process ID)。
進程的ID是可重用的,如果一個進程被終止,那么它的進程ID會被系統回收,但是會延遲使用,防止該進程ID標識的新進程被誤認為是以前的進程。
三個特殊ID的進程:
獲取進程各種ID的相關函數:
函數聲明:
#include <unistd.h>
pid_t getpid(void); ? ? // Returns: process ID of calling process
pid_t getppid(void); ? ? ? ?// Returns: parent process ID of calling process
uid_t getuid(void); ? ? ? ?// Returns: real user ID of calling process
uid_t geteuid(void); ? ? ? // Returns: effective user ID of calling process
gid_t getgid(void); ? ? ? ?// Returns: real group ID of calling process
gid_t getegid(void); ? ? ? ?// Returns: effective group ID of calling process
這里的各種ID在前面第三篇中有說明,http://www.CUOXin.com/suzhou/p/4295535.html
?
2 fork函數fork函數用于一個已存在的進程創建一個新的進程。
函數聲明:
#include <unistd.h>
pid_t fork(void);
函數細節:
Example:
#include "apue.h"
?
int ? ? globvar = 6;? ? ? ? /* external variable in initialized data */
char? ? buf[] = "a write to stdout/n";
?
int
main(void)
{
? ? int ? ? var;? ? ? ? /* automatic variable on the stack */
? ? pid_t ? pid;
?
? ? var = 88;
? ? if (write(STDOUT_FILENO, buf, sizeof(buf)-1) != sizeof(buf)-1)
? ? ? ? err_sys("write error");
? ? printf("before fork/n");? ? /* we don't flush stdout */
?
? ? if ((pid = fork()) < 0) {
? ? ? ? err_sys("fork error");
? ? } else if (pid == 0) {? ? ? /* child */
? ? ? ? globvar++;? ? ? ? ? ? ? /* modify variables */
? ? ? ? var++;
? ? } else {
? ? ? ? sleep(2); ? ? ? ? ? ? ? /* parent */
? ? }
?
? ? printf("pid = %ld, glob = %d, var = %d/n", (long)getpid(), globvar,
? ? ? var);
? ? exit(0);
}
執行結果:
pid為12291的進程為子進程,對變量glob和var進行了加1。
當把輸出重定向到一個文件時,我們發現結果和直接輸出到終端中不太一樣:
原因:?
?
文件共享(File Sharing)當調用fork函數時,父進程的所有打開的文件描述符都會復制一份到子進程中,包括文件偏移量(file offset)。
所以當父子進程同時寫文件時,他們的操作都會更新同一個文件偏移量(file offset),加入子進程向文件中寫入了一部分數據,同時更新了file offset,那么父進程進行寫入操作時,會使用跟新以后的offset,從而避免了覆蓋了子進程寫入的數據。
父子進程共享文件如下圖所示:
我們可以發現,父子進程擁有相同的文件描述符,又沒有其他的同步方式,所以他們的輸出可能會混起來(intermixed)。
fork之后,常見的處理父子進程擁有的文件描述符有兩種方式:
除了打開的文件描述,其他的子進程會繼承自父進程的內容包括:
父子進程不同的地方包括:
?
3 vforkvfork和fork有相同的返回值。
vfork和fork的不同點:
?Example:
#include "apue.h"
?
int ? ? globvar = 6;? ? ? ? /* external variable in initialized data */
?
int
main(void)
{
? ? int ? ? var;? ? ? ? /* automatic variable on the stack */
? ? pid_t ? pid;
?
? ? var = 88;
? ? printf("before vfork/n"); ? /* we don't flush stdio */
? ? if ((pid = vfork()) < 0) {
? ? ? ? err_sys("vfork error");
? ? } else if (pid == 0) {? ? ? /* child */
? ? ? ? globvar++;? ? ? ? ? ? ? /* modify parent's variables */
? ? ? ? var++;
? ? ? ? _exit(0); ? ? ? ? ? ? ? /* child terminates */
? ? }
?
? ? /* parent continues here */
? ? printf("pid = %ld, glob = %d, var = %d/n", (long)getpid(), globvar,
? ? ? var);
?
? ? exit(0);
}
運行結果:
?
4 進程退出和僵尸進程正常退出:三個函數exit,?
如果子進程不正常退出,則內核保證記錄該進程的異常退出狀態,該進程的父進程可以通過調用wait或者waitpid函數獲取該子進程的異常退出狀態。
如果父進程在子進程之前終止,則init進程成為該子進程的父進程。從而保證每個進程都有父進程。
如果子進程先終止(異常終止或者正常退出),內核會保存該子進程的部分信息,包括進程pid,進程終止時的狀態和該進程占用的CPU時間,同時內核會清除該進程占用的內存,關閉所有已經打開的文件描述符。父進程可以通過檢查該信息獲取子進程的終止情況。
如果子進程先終止,而沒有父進程調用waitpid獲取該子進程的信息,那么這種進程被成為僵尸進程。使用ps命令可以看到僵尸進程的相關信息。
如果父進程為init進程,那么子進程異常終止并不會成為僵尸進程,因為init進程會對它的所有子進程調用wait函數獲取子進程的終止狀態。
?
5 wait和waitpid函數子進程終止,內核會向父進程發送SIGCHLD信號。父進程默認的行為是忽略該信號,父進程也可以設置一個信號處理函數,當捕捉到該信號時,調用該處理函數,在后面的相關章節會介紹信號相關的概念。
本節介紹的wait和waitpid函數的作用是:
需要注意的一點是,如果我們在接收到SIGCHLD信號后,調用wait函數,則該函數會立刻返回。在其他情況下調用wait函數,則會阻塞。
函數聲明:
#include <sys/wait.h>
pid_t wait(int *statloc);
pid_t waitpid(pid_t pid, int *statloc, int options);?
// Both return: process ID if OK, 0,or -1 on error
兩個函數之間的區別:
函數細節:
返回值檢查:
使用四個宏來檢查wait和waitpid函數來獲取子進程的終止狀態(terminated status),如退出狀態,信號值等信息。
四個宏的具體說明見下表所示:
pid的取值對waitpid函數行為的影響:
參數option的取值:
waitpid函數提供了三個wait沒有的特性:
?
Example:#include "apue.h"
#include <sys/wait.h>
?
int
main(void)
{
? ? pid_t ? pid;
?
? ? if ((pid = fork()) < 0) {
? ? ? ? err_sys("fork error");
? ? } else if (pid == 0) {? ? ? /* first child */
? ? ? ? if ((pid = fork()) < 0)
? ? ? ? ? ? err_sys("fork error");
? ? ? ? else if (pid > 0)
? ? ? ? {
? ? ? ? ? ? exit(0);? ? /* parent from second fork == first child */
? ? ? ? }
?
? ? ? ? /*
?? ? ? ? * We're the second child; our parent becomes init as soon
?? ? ? ? * as our real parent calls exit() in the statement above.
?? ? ? ? * Here's where we'd continue executing, knowing that when
?? ? ? ? * we're done, init will reap our status.
? ? ? ? ?*/
? ? ? ? sleep(2);
? ? ? ? printf("second child, parent pid = %ld/n", (long)getppid());
? ? ? ? exit(0);
? ? }
?
? ? if (waitpid(pid, NULL, 0) != pid) ? /* wait for first child */
? ? ? ? err_sys("waitpid error");
?
? ? /*
?? ? * We're the parent (the original process); we continue executing,
?? ? * knowing that we're not the parent of the second child.
?? ? */
? ? exit(0);
}
執行結果:
結果分析:
在這里我們fork了兩次,原因是,當我們想fork一個子進程出來,而我們不希望父進程阻塞在wait函數,并且不希望由于父進程沒有調用wait函數先退出導致子進程成為僵尸進程,那么fork兩次,并且退出第一個子進程,可以使得父進程及時退出,并且第二個子進程的父進程變成init進程。
?
小結本篇主要介紹了fork、vfork、僵尸進程、wait和waitpid函數,這些在unix環境中都是很重要的概念和函數,并且在面試中也經常問到。
下一篇的內容包括:
?
參考資料:
《Advanced Programming in the UNIX Envinronment 3rd》
?
新聞熱點
疑難解答