
#include <sys/types.h>
#include <sys/wait.h>
#include <stdio.h>
#include <unistd.h>
#include <stdlib.h>

int main ()
{
        pid_t pid, pid2;
        int status;

        printf ("pid %d ppid %d\n", getpid (), getppid ());

        pid = fork ();
        printf ("pid %d ppid %d fork %d (after)\n", getpid (), getppid (), pid);

        if (pid < 0)
        {
                fprintf (stderr, "fork didn't fork");
                exit (1);
        }

        // pid == 0 --> child
        if (pid == 0)
        {
                printf ("This is the child\n");
                exit (123);
        }
        else
        {
                // pid != 0 --> parent
                printf ("This is the parent\n");
                pid2 = wait (&status);
                printf ("parent: pid2 %d exit code %d\n", pid2, WEXITSTATUS(status));
        }
        exit (0);
}

