Linux system函式詳解

2021-09-07 07:32:05 字數 884 閱讀 5443

system

功能:system()函式呼叫

"/bin/sh -c command

"執行特定的命令,阻塞當前程序直到command命令執行完畢

原型

int system(const

char *command);

返回值:

如果無法啟動shell執行命令,system將返回127;出現不能執行system呼叫的其他錯誤時返回-1

。如果systenm能夠順利執行,返回那個命令的退出碼

system函式執行時,內部會呼叫fork,execve,waitpid等函式。

#include #include 

#include

#include

#include

#include

#include

//自己實現system函式

int my_system(const

char *command)

pid_t pid = 0

;

int status=0

; pid =fork();

if (pid < 0

)

if(pid==0

)

else

if(pid>0

)

}return

status;

}int main(int arg, char *args)

; read(stdin_fileno,buf,

sizeof

(buf));

my_system(buf);

return0;

}

linux system函式使用詳解

相關函式 fork,execv e,waitpid,popen 表頭檔案 i ncludelib.h 定義函式 int system con st char string 函式說明 system 會調 用fork 產生子程序,由子程序來呼叫 bin sh c string來執行參 數string字串...

linux system函式使用詳解

相關函式 fork,execve,waitpid,popen 表頭檔案 include 定義函式 int system const char string 函式說明 system 會呼叫fork 產生子程序,由子程序來呼叫 bin sh c string來執行引數string字串所代表的命令,此命令...

linux system系統呼叫

為了簡化執行命令的複雜程度,linux系統提供system系統呼叫,原理是通過fork的方式產生乙個子程序,在這個子程序中執行系統呼叫過程中引數裡面設定的command。includeint system const char command 利用fork建立子程序,然後用execl來執行 bin ...