## waitpid ##
等待子進程中斷或結束
### 相關函數 ###
wait,fork
### 表頭文件 ###
```
#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中。
### 范例 ###
參考wait()。
- 字符測試篇
- isalnum
- isalpha
- isascii
- iscntrl
- isdigit
- isgraphis
- islower
- isprint
- isspace
- ispunct
- isupper
- isxdigit
- 字符串轉換篇
- atof
- atoi
- atol
- gcvt
- strtod
- strtol
- strtoul
- toascii
- tolower
- toupper
- 內存控制篇
- calloc
- free
- getpagesize
- malloc
- mmap
- munmap
- 日期時間篇
- asctime
- ctime
- gettimeofday
- gmtime
- localtime
- mktime
- settimeofday
- time
- 內存及字符串操作篇
- bcmp
- bcopy
- bzero
- index
- memccpy
- memchr
- memcmp
- memcpy
- memmove
- memset
- rindex
- strcasecmp
- strcat
- strchr
- strcmp
- strcoll
- strcpy
- strcspn
- strdup
- strlen
- strncasecmp
- strncat
- strncpy
- strpbrk
- strrchr
- strspn
- strstr
- strtok
- 常用數學函數篇
- abs
- acos
- asin
- atan
- atan2
- ceil
- cos
- cosh
- exp
- frexp
- ldexp
- log
- log10
- pow
- sin
- sinh
- sqrt
- tan
- tanh
- 用戶組篇
- endgrent
- endpwent
- endutent
- fgetgrent
- fgetpwent
- getegid
- geteuid
- getgid
- getgrent
- getgrgid
- getgrnam
- getgroups
- getpw
- getpwent
- getpwnam
- getpwuid
- getuid
- getutent
- getutid
- getutline
- initgroups
- pututline
- seteuid
- setfsgid
- setfsuid
- setgid
- setgrent
- setgroups
- setpwent
- setregid
- setreuid
- setuid
- setutent
- utmpname
- 數據結構及算法篇
- crypt
- bsearch
- lfind
- lsearch
- qsort
- rand
- srand
- 文件操作篇
- close
- creat
- dup
- dup2
- fcntl
- flock
- fsync
- lseek
- mkstemp
- open
- read
- sync
- write
- 文件內容操作篇
- clearerr
- fclose
- fdopen
- feof
- fflush
- fgetc
- fgets
- fileno
- fopen
- fputc
- fputs
- fread
- freopen
- fseek
- ftell
- fwrite
- getc
- getchar
- gets
- mktemp
- putc
- putchar
- rewind
- setbuf
- setbuffer
- setlinebuf
- setvbuf
- ungetc
- 進程操作篇
- atexit
- execl
- execlp
- execv
- execve
- execvp
- exit
- _exit
- vfork
- getpgid
- getpgrp
- getpid
- getppid
- getpriority
- nice
- on_exit
- setpgid
- setpgrp
- setpriority
- system
- wait
- waitpid
- 格式化輸入輸出篇
- fprintf
- fscanf
- printf
- scanf
- sprintf
- sscanf
- vfprintf
- vfscanf
- vprintf
- vscanf
- vsprintf
- vsscanf
- 文件權限控制篇
- access
- alphasort
- chdir
- chmod
- chown
- chroot
- closedir
- fchdir
- fchmod
- fchown
- fstat
- ftruncate
- getcwd
- link
- lstat
- opendir
- readdir
- readlink
- remove
- rename
- rewinddir
- seekdir
- stat
- symlink
- telldir
- truncate
- umask
- unlink
- utime
- utimes
- 信號處理篇
- alarm
- kill
- pause
- sigaction
- sigaddset
- sigdelset
- sigemptyset
- sigfillset
- sigismember
- signal
- sigpending
- sigprocmask
- sleep
- ferror
- perror
- strerror
- mkfifo
- pclose
- pipe
- popen
- 接口處理篇
- accept
- bind
- connect
- endprotoent
- endservent
- getsockopt
- htonl
- htons
- inet_addr
- inet_aton
- inet_ntoa
- listen
- ntohl
- ntohs
- recv
- recvfrom
- recvmsg
- send
- sendmsg
- sendto
- setprotoent
- setservent
- setsockopt
- shutdown
- socket
- 環境變量篇
- getenv
- putenv
- setenv
- 終端控制篇
- getopt
- isatty
- select
- ttyname