1. 程式人生 > >fork與vfork、wait與waitpidqubie

fork與vfork、wait與waitpidqubie

fork之後父程序子程序分離,各自有各自的記憶體空間,兩個程序在OS管理下執行,無法確定誰先執行

vfork後,父子程式共享記憶體空間,一般vfork搭配execve函式,建立子程序拉起別的程序(不用複製記憶體空間效率也更高)。子程序執行結束後執行父程序。

execve替換程式碼段、資料段、堆疊段、程序控制塊PCB。

pid_t wait(int *status);

The wait() system call suspends execution of the calling process until a child specified by pid argument has changed stse.

wait()系統呼叫暫停執行呼叫程序,直到pid引數指定的子程序發生更改為止。(父程序阻塞直到pid子程序結束)。

pid_t waitpid(pid_t pid, int *status, int options);

The waitpid() waits only for terminated children,but behavior is modifiable via the options argument.

waitpid()只等待已終止的子節點,但行為可以通過options引數修改.(waitpid等待任意子程式結束,通過options引數修改)。

<-1  meaning wait for any child process whose process group ID is equal to the absolute value of pid.

-1 meaning wit for any child process.

0 meaning wait for any child process whose process group ID is equal to that of the calling process.

>0 meaning wait for the child whose process ID is equal to the value of pid.