用fork函數創建子進程后,子進程往往要調用一種exec函數以執行另一個程序,該子進程被新的程序替換,改變地址空間,進程映像和一些屬性,但是pid號不變。 execve(): #include int execve(const char *filename, char *const argv[], char *const envp[]); 參數含義: filename:路徑名,表示載入進程空間的新程序路徑。 argv[]:命令行參數,argv[0]為命令名。 envp[]:新程序的環境變量。 返回值:成功時不會返回,使用時不用檢查返回值,可通過errno檢查。 以下函數都是根據execve實現: int execl(const char *path, const char arg, …/ (char *) NULL */); int execlp(const char *file, const char arg, …/ (char *) NULL */); int execle(const char *path, const char arg, …/, (char *) NULL, char * const envp[] */); int execv(const char *path, char *const argv[]); int execvp(const char *file, char *const argv[]); int execvpe(const char *file, char *const argv[],char *const envp[]); 實驗代碼: 創建子進程,子進程使用execl調用ls查看當前目錄下的文件。 exec.c: 使用execl調用shell命令查看文件信息。 ![]() ![]() 執行結果: ![]() Linux系統基于這些函數實現了“popen”,“system”等函數,這些函數能夠直接調用shell等函數。 ![]() |