# waitpid

`waitpid()` is an advanced version of `wait()` system call. You can specify more in the parameters.

```c
pid_t waitpid(pid_t pid, int *status, int options);
```

Take a note of the following code to see how it works:

<pre class="language-c"><code class="lang-c">/* Wait/waitpid.c */
<strong>#include &#x3C;stdio.h>
</strong>#include &#x3C;unistd.h>
#include &#x3C;sys/types.h>
#include &#x3C;sys/wait.h>
#include &#x3C;stdlib.h>

int main(int argc,char *argv[])
{
    int pid;
    int status;
    if(!(pid = fork()))
    {
        printf("My PID: %d\n",getpid()); 
        exit(0); 
    }
    waitpid(pid,&#x26;status,WUNTRACED);
    if(WIFEXITED(status)) 
    { 
        printf("Exit Normally\n");
        printf("Exit status: %d\n",WEXITSTATUS(status)); 
    } 
    else 
    { 
        printf("Exit NOT Normal\n"); 
    } 
    return 0;
}
</code></pre>

`waitpid()` takes 3 parameters. The _first_ one is child pid, therefore this is more specific than the previous `wait()`.

The second one stores the status of the child. Whenever the child terminates/suspends, it will store the status code in the middle parameter. Note that it accepts a **pointer** only.

The third is the options for `waitpid()`. You can change various behavior, for example, returns immediately if no child has exited: `WNOHANG`; also returns if the child is stopped (e.g.: by Ctrl-Z): `WUNTRACED`.

There are macros to further tell you more information about the child. You can use `WIFEXITED(status)` to check if the child is returned normally (i.e. by `return` or `exit()`). In addition, you even can know the exit code (Only least significant 8 bits of status code are available to parents) by using `WEXITSTATUS(status)`.
