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


int main(int argc, char **argv) {
    printf("before fork my pid %d\n", getpid());

    pid_t childpid = fork();
    if (childpid == -1)
        perror("fork"), exit(0);

    if (childpid == 0) {
        printf("child: my pid %d, my parent's pid is %d\n", getpid(), getppid());
        char *argv_for_ls[] = { "1", "2", "4", NULL };
        int ret = execvp("./printmyargs", argv_for_ls);
        printf("exec returned %d\n", ret);
            
    } else {
        int status;
        printf("parent: my pid %d, my child's pid is %d\n", getpid(), childpid);
        pid_t childexited = wait(&status);
        if (childexited == -1) {
            perror("wait");
            return 0;
        }
                
        if (WIFEXITED(status)) {
            printf("child exited pid %d exit status %d\n", childexited, WEXITSTATUS(status));
        } else if (WIFSIGNALED(status)) {
            printf("child crashed/killed pid %d, signal %d\n", childexited, WTERMSIG(status));
        } else {
            printf("something else\n");
        }
    }
}