File: wait_test.cpp

package info (click to toggle)
fakeroot-ng 0.12-3
  • links: PTS
  • area: main
  • in suites: lenny
  • size: 1,828 kB
  • ctags: 552
  • sloc: sh: 9,156; cpp: 3,186; ansic: 1,295; makefile: 132
file content (40 lines) | stat: -rw-r--r-- 847 bytes parent folder | download
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
#include <sys/types.h>
#include <sys/wait.h>
#include <unistd.h>
#include <stdio.h>
#include <stdlib.h>

int main( int argc, char *argv[] )
{
    pid_t child=fork();

    if( child==0 ) {
        // We are the child
        int exitcode=getpid()%253;
        printf("Child %d exit with code 0x%x\n", getpid(), exitcode );

        exit(exitcode);
    } else if( child==-1 ) {
        perror("Failed to fork");
    } else {
        int status;

        pid_t process=wait(&status);

        if( process<0 ) {
            perror("wait failed");
        } else {
            printf("PID %d returned 0x%x\n", process, status);
        }

        process=wait(&status);

        if( process<0 ) {
            perror("Second wait failed");
        } else {
            printf("PID %d returned 0x%x\n", process, status);
        }
    }

    return 0;
}