9久久伊人精品综合,亚洲一区精品视频在线,成 人免费va视频,国产一区二区三区黄网,99国产精品永久免费视频,亚洲毛片多多影院,精品久久久无码人妻中文字幕,无码国产欧美一区二区三区不卡
學習啦>學習電腦>操作系統>Linux教程>

linux system函數

時間: 佳洲1085 分享

  system()函數是Linux下的一個函數,那么它具體作用是干什么呢?下面由學習啦小編為大家整理了linux system()函數的相關知識,希望對大家有幫助!

  一,linux system()函數理解

  功能:system()函數調用“/bin/sh -c command”執行特定的命令,阻塞當前進程直到command命令執行完畢

  原型:

  int system(const char *command);

  返回值:

  如果無法啟動shell運行命令,system將返回127;出現不能執行system調用的其他錯誤時返回-1。如果system能夠順利執行,返回那個命令的退出碼。

  說明:

  man幫助:

  #include

  int system(const char *command);

  DESCRIPTION

  system() executes a command specified in command by calling /bin/sh -c

  command, and returns after the command has been completed. During exe-

  cution of the command, SIGCHLD will be blocked, and SIGINT and SIGQUIT

  will be ignored.

  RETURN VALUE

  The value returned is -1 on error (e.g. fork(2) failed), and the

  return status of the command otherwise. This latter return status is

  in the format specified in wait(2). Thus, the exit code of the command

  will be WEXITSTATUS(status). In case /bin/sh could not be executed,

  the exit status will be that of a command that does exit(127).

  If the value of command is NULL, system() returns non-zero if the shell

  is available, and zero if not.

  system() does not affect the wait status of any other children.

  二,linux system()函數原理

  system函數執行時,會調用fork、execve、waitpid等函數。

  linux版system函數的源碼:

  int system(const char * cmdstring)

  {

  pid_t pid;

  int status;

  if(cmdstring == NULL){

  return (1);

  }

  if((pid = fork())<0){

  status = -1;

  }

  else if(pid == 0){

  execl("/bin/sh", "sh", "-c", cmdstring, (char *)0);

  _exit(127); //子進程正常執行則不會執行此語句

  }

  else{

  while(waitpid(pid, &status, 0) < 0){

  if(errno != EINTER){

  status = -1;

  break;

  }

  }

  }

  return status;

  }

  三,linux system()函數說明

  system()會調用fork()產生子進程,由子進程來調用/bin/sh-c string來執行參數string字符串所代表的命令,此命>令執行完后隨即返回原調用的進程。

  在調用system()期間SIGCHLD 信號會被暫時擱置,SIGINT和SIGQUIT 信號則會被忽略。

  返回值

  =-1:出現錯誤

  =0:調用成功但是沒有出現子進程

  >0:成功退出的子進程的id

  如果system()在調用/bin/sh時失敗則返回127,其他失敗原因返回-1。若參數string為空指針(NULL),則返回非零值>。如果system()調用成功則最后會返回

  執行shell命令后的返回值,但是此返回值也有可能為 system()調用/bin/sh失敗所返回的127,因此最好能再檢查errno 來確認執行成功。

  附加說明

  在編寫具有SUID/SGID權限的程序時請勿使用system(),system()會繼承環境變量,通過環境變量可能會造成系統安全的問題。

  system函數對返回值的處理,涉及3個階段:

  階段1:創建子進程等準備工作。如果失敗,返回-1。

  階段2:調用/bin/sh拉起shell腳本,如果拉起失敗或者shell未正常執行結束(參見備注1),原因值被寫入到status的低8~15比特位中。system的man中只說明了會寫了127這個值,但實測發現還會寫126等值。

  階段3:如果shell腳本正常執行結束,將shell返回值填到status的低8~15比特位中。

3582371 主站蜘蛛池模板: 人妻影音先锋啪啪AV资源| 成人自拍小视频免费观看| 亚洲男人天堂2021| 一区一区三区产品乱码| 精品国产精品中文字幕| 国产视频深夜在线观看| 国内精品久久黄色三级乱| 真实国产老熟女无套中出| 免费av网站| 欲乱人妻少妇邻居毛片| 国产成人一区二区三区免费| 国产成人一区二区不卡| 亚洲天堂男人的天堂在线| 日韩av不卡一区二区在线| 国产精品自拍自在线播放| 久久精品国产免费观看频道| 亚洲成a人无码av波多野| 亚洲精品成人A在线观看| 国产精品鲁鲁鲁| 久久精品免视看国产成人| 熟妇人妻无码中文字幕老熟妇| 亚洲国产精品综合色在线| 亚洲熟女国产熟女二区三区| 亚洲sm另类一区二区三区| 国产欧美日韩精品第二区| 国产欧美另类久久久精品丝瓜| 日本一区二区三区四区黄色| 9191国语精品高清在线| 男人添女人下部高潮视频| 玩弄漂亮少妇高潮白浆| 国产精品白嫩极品在线看| 国产成人精品亚洲日本片| 日本狂喷奶水在线播放212| 国内精品久久人妻无码妲| 韩国午夜理伦三级| 国产偷国产偷亚洲高清午夜| 久久精品日日躁夜夜躁 | 国产视频一区二区三区四区视频| 久久不卡精品| 欧美寡妇xxxx黑人猛交| 毛片免费观看天天干天天爽 |