fork(), exec(), wait() ostep/code/cpu-api p1.c - after fork parent or child could be scheduled next: ---- % ./p1 hello world (pid:21918) hello, I am parent of 21919 (pid:21918) hello, I am child (pid:21919) % ./p1 hello world (pid:21920) hello, I am child (pid:21921) hello, I am parent of 21921 (pid:21920) % - perror() if fork() fails p2.c - parent waits for child to go first ---- % ./p2 hello world (pid:21937) hello, I am child (pid:21938) hello, I am parent of 21938 (wc:21938) (pid:21937) % ./p2 hello world (pid:21939) hello, I am child (pid:21940) hello, I am parent of 21940 (wc:21940) (pid:21939) % check: man 2 wait and/or man 3 wait pid_t wait(int *wstatus); wait returns pid of child -> try checking the child exit status % ./p2 hello world (pid:22202) hello, I am child (pid:22203) hello, I am parent of 22203 (wc:22203) (pid:22202) s = 0x1200 WIFEXITED = 1 WEXITSTATUS = 18 % - perror() if fork() fails p3.c - child exec's wc ---- % ./p3 hello world (pid:22206) hello, I am child (pid:22207) 32 123 966 p3.c hello, I am parent of 22207 (wc:22207) (pid:22206) % - perror() if fork() fails - strdup not needed - perror() and exit() after execvp(), if it returns it failed p4.c - child redirects stdout to file p4.output then runs wc ---- % ./p4 % cat p4.output 34 114 884 p4.c % - perror() if fork() fails - strdup not needed - perror() and exit() after execvp(), if it returns it failed