Linux環境編程–waitpid與fork與execlp

來源:互聯網
上載者:User
 waitpid  waitpid(等待子進程中斷或結束)
  表標頭檔
  #include<sys/types.h>
  #include<sys/wait.h>
  定義函數 pid_t waitpid(pid_t pid,int * status,int options);
  函數說明
  waitpid()會暫時停止目前進程的執行,直到有訊號來到或子進程
  結束。如果在調用 wait()時子進程已經結束,則 wait()會立即
  返回子進程結束狀態值。 子進程的結束狀態值會由參數 status 返回,
  而子進程的進程識別碼也會一起返回。如果不在意結束狀態值,則
  參數 status 可以設成 NULL。參數 pid 為欲等待的子進程識別碼,
  其他數值意義如下:
  pid<-1 等待進程組識別碼為 pid 絕對值的任何子進程。
  pid=-1 等待任何子進程,相當於 wait()。
  pid=0 等待進程組識別碼與目前進程相同的任何子進程。
  pid>0 等待任何子進程識別碼為 pid 的子進程。
  參數 option 可以為 0 或下面的 OR 組合:
  WNOHANG 如果沒有任何已經結束的子進程則馬上返回, 不予以等待。
  WUNTRACED 如果子進程進入暫停執行情況則馬上返回,但結束狀態不予以理會。
  子進程的結束狀態返回後存於 status,底下有幾個宏可判別結束情況:
  WIFEXITED(status)如果子進程正常結束則為非 0 值。
  WEXITSTATUS(status)取得子進程 exit()返回的結束代碼,一般會先用 WIFEXITED 來判斷是否正常結束才能使用此宏。
  WIFSIGNALED(status)如果子進程是因為訊號而結束則此宏值為真
  WTERMSIG(status) 取得子進程因訊號而中止的訊號代碼,一般會先用 WIFSIGNALED 來判斷後才使用此宏。
  WIFSTOPPED(status) 如果子進程處於暫停執行情況則此宏值為真。一般只有使用 WUNTRACED 時才會有此情況。
  WSTOPSIG(status) 取得引發子進程暫停訊號代碼,一般會先用 WIFSTOPPED 來判斷後才使用此宏。
  如果執行成功則返回子進程識別碼(PID) ,如果有錯誤發生則返回
  傳回值-1。失敗原因存於 errno 中。
  /******
  * waitpid.c - Simple wait usage
  *********/
  #include <unistd.h>
  #include <sys/types.h>
  #include <sys/wait.h>
  #include <stdio.h>
  #include <stdlib.h>
  int main( void )
  {
  pid_t childpid;
  int status;
  childpid = fork();
  if ( -1 == childpid )
  {
  perror( "fork()" );
  exit( EXIT_FAILURE );
  }
  else if ( 0 == childpid )
  {
  puts( "In child process" );
  sleep( 3 );//讓子進程睡眠3秒,看看父進程的行為
  printf("\tchild pid = %d\n", getpid());
  printf("\tchild ppid = %d\n", getppid());
  exit(EXIT_SUCCESS);
  }
  else
  {
  waitpid( childpid, &status, 0 );
  puts( "in parent" );
  printf( "\tparent pid = %d\n", getpid() );
  printf( "\tparent ppid = %d\n", getppid() );
  printf( "\tchild process exited with status %d \n", status );
  }
  exit(EXIT_SUCCESS);
  }
  [root@localhost src]# gcc waitpid.c
  [root@localhost src]# ./a.out
  In child process
  child pid = 4469
  child ppid = 4468
  in parent
  parent pid = 4468
  parent ppid = 4379
  child process exited with status 0
  [root@localhost src]#
  如果將上面“waitpid( childpid, &status, 0 );”行注釋掉,程式執行效果如下:
  [root@localhost src]# ./a.out
  In child process
  in parent
  parent pid = 4481
  parent ppid = 4379
  child process exited with status 1331234400
  [root@localhost src]# child pid = 4482
  child ppid = 1
  子進程還沒有退出,父進程已經退出了。fork  fork()函數,Linux系統調用
  標頭檔:
  #include <unistd.h>
  函數定義:
  int fork( void );
  傳回值:
  子進程中返回0,父進程中返回子進程ID,出錯返回-1
  函數說明:
  一個現有進程可以調用fork函數建立一個新進程。由fork建立的新進程被稱為子進程(child process)。fork函數被調用一次但返回兩次。兩次返回的唯一區別是子進程中返回0值而父進程中返回子進程ID。
  子進程是父進程的副本,它將獲得父進程資料空間、堆、棧等資源的副本。注意,子進程持有的是上述儲存空間的“副本”,這意味著父子進程間不共用這些儲存空間,它們之間共用的儲存空間只有程式碼片段。
  範例程式碼:
  #include <unistd.h>
  #include <stdio.h>
  int main(int argc, void ** argv )
  {
  int pid = fork();
  if(pid < 0 ) {
  // print("error!");
  } else if( pid == 0 ) {
  // print("This is the child process!");
  } else {
  // print("This is the parent process! child process id = %d", pid);
  }
  return 0;
  }execlp  execlp(從PATH 環境變數中尋找檔案並執行)
  相關函數:
  fork,execl,execle,execv,execve,execvp
  表標頭檔:
  #include<unistd.h>
  定義函數:
  int execlp(const char * file,const char * arg,……);
  函數說明:
  execlp()會從PATH 環境變數所指的目錄中尋找符合參數file的檔案名稱,找到後便執行該檔案,然後將第二個以後的參數當做該檔案的argv[0]、argv[1]……,最後一個參數必須用null 指標(NULL)作結束。
  傳回值:
  如果執行成功則函數不會返回,執行失敗則直接返回-1,失敗原因存於errno 中。
  錯誤碼 參考execve()。
  範例:
  /* 執行ls -al /etc/passwd execlp()會依PATH 變數中的/bin找到/bin/ls */
  #include<unistd.h>
  main()
  {
  execlp(“ls”,”ls”,”-al”,”/etc/passwd”,(char *)0);
  }
  執行:
  -rw-r--r-- 1 root root 705 Sep 3 13 :52 /etc/passwd
  ————————————————————————————————add by love_aiqiu
  NAME
  execl, execlp, execle, execv, execvp - execute a file
  SYNOPSIS
  #include <unistd.h>
  extern char **environ;
  int execl(const char *path, const char *arg, ...);
  int execlp(const char *file, const char *arg, ...);
  int execle(const char *path, const char *arg , ..., char * const envp[]);
  int execv(const char *path, char *const argv[]);
  int execvp(const char *file, char *const argv[]);
相關文章

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.