1. 替換進程鏡像
#include <unistd.h>
extern char **environ;
int execl(const char *path, const char*arg, ..., (char*)0);
int execlp(const char *file, const char*arg, ..., (char*)0);
int execle(const char *path, const char*arg, ..., char * const envp[]);
int execv(const char *path, char *constargv[]);
int execvp(const char *file, char *constargv[]);
int execve(const char *file, char *const argv[], char *const envp[]);
1) execl、execlp、execle參數可變,以null 指標結束;execv、execvp第二個參數為一字串數組。
2) 函數會把argv參數傳遞給main函數。
3) 以p結尾的函數會搜尋PATH環境變數來尋找新程式,不存在則使用絕對路徑來傳遞給函數。
4) exec函數不會返回調用它的函數。
2. 複製進程對象
#include <unistd.h>
#include<sys/types.h>
pid_t fork(void);
新進程與原進程一模一樣,執行代碼完全一樣,只是有自己的資料空間、環境以及檔案描述符。
傳回值:
-1:建立失敗;
0:當前進程為子進程;
非零:當前進程為父進程。
3. 進程等待
#include<sys/types.h>
#include<sys/wait.h>
pid_t wait(int*stat_loc);
pid_t waitpid(pid_t pid, int* stat_loc, int options);
int waitid(idtype_tidtype, id_t id, siginfo_t *infop, int options);
暫停父進程知道子進程結束為止。
stat_loc: