Script started on Wed Dec 3 18:23:32 2025 macbook:unix_ch13> gcc fork_and_exec_v2.c macbook:unix_ch13> cat fork_and_exec_v2.c // See the // Unix book by Glass and Ables, page 478 // -MCW #include #include // exit() #include // fork() #include // for fork, sleep #include // for wait int main () { int pid, status; pid = fork (); /* Duplicate */ printf ("I'm process %d \n", getpid ()); int childPid = 0; /* Branch based on return value from fork () */ if (pid != 0) { /* Wait for a child to terminate. */ printf("As parent, I will wait for the child\n"); childPid = wait (&status); } else { printf("As child, I will call echo\n"); // NULL indicates last command. There could be more than 1 execl ("/bin/echo", "echo", "hello", "there", NULL); /* Execute ls */ } printf("The parent is ending\n"); return 0; } macbook:unix_ch13> ./a.out I'm process 14408 As parent, I will wait for the child I'm process 14409 As child, I will call echo hello there The parent is ending macbook:unix_ch13> cp fork_and_exec_v2.c fork_and_exec_v3.c macbook:unix_ch13> vi fork_and_exec_v3.c macbook:unix_ch13> cat process2_v2.c // This function is from Glass and Ables' Unix book // See monitor.c (starting on p 453) #include /* For printf, fprintf */ #include // exit() #include /* For strcmp */ #include /* For isdigit */ #include /* For O_RDONLY */ #include /* For getdents */ #include /* For IS macros */ #include /* For modet */ #include /* For localtime, asctime */ void processDirectory (char* dirName) { /* Process all files in the named directory */ DIR *dp; struct dirent *dep; //char fileName [MAX_FILENAME]; dp = opendir (dirName); /* Open for reading */ if (dp == NULL) { perror("monitor: "); exit(1); } while ((dep = readdir(dp))) /* Read all dir entries */ { if (strcmp (dep->d_name, ".") != 0&& strcmp (dep->d_name, "..") != 0) /* Skip . .. */ { printf ("%s/%s \n", dirName, dep->d_name); // was sprintf //monitorFile (fileName); /* Call recursively */ } } closedir (dp); /* Close directory */ } int main() { processDirectory("."); return 0; } macbook:unix_ch13> gcc process2_v2.c -o process2_v2 macbook:unix_ch13> vi fork_and_exec_v3.c macbook:unix_ch13> gcc fork_and_exec_v3.c -o fork_and_exec_v3 macbook:unix_ch13> ./fork_and_exec_v3 I'm process 14513 As parent, I will wait for the child I'm process 14514 As child, I will call process2_v2 ./myfork.c ./monitor_original.c ./pgrp2.c ./myexec.c ./myfork2.c ./monitor.c ./pipe_fork_signal_wait.c ./Dec3_2025.log ./fork_and_exec_v2.c ./unnamed_pipes_figure.txt ./process2.c ./processDirectory.c ./exec_man_page.html ./nonexist.txt ./seek_example.c ./mychdir.c ./sparse.c ./pgrp1.c ./sparse.txt ./fork_and_exec.c ./myexit.c ./lstat_example.c ./mydup.c ./fork_example2.c ./zombie.c ./talk.c ./myfork4.c ./reverse_ch13_KandR.c ./a.out ./process2_v2.c ./reverse_unix_ch13.c ./pipe_example.c ./mydup_w_comments.c ./pipe_example_3.c ./test.txt ./pipe_example_v2.c ./fork_example.c ./orphan.c ./processDirectory_v2.c ./process2_v2 ./myfork3.c ./wait.c ./reverse_unix_ch13_original.c ./fork_and_exec_v3.c ./mywait.c ./fileIOerror.c ./fork_and_exec_v3 ./talk ./normal.txt The parent is ending macbook:unix_ch13> cat fork_and_exec_v3.c // See the // Unix book by Glass and Ables, page 478 // -MCW #include #include // exit() #include // fork() #include // for fork, sleep #include // for wait int main () { int pid, status; pid = fork (); /* Duplicate */ printf ("I'm process %d \n", getpid ()); int childPid = 0; /* Branch based on return value from fork () */ if (pid != 0) { /* Wait for a child to terminate. */ printf("As parent, I will wait for the child\n"); childPid = wait (&status); } else { printf("As child, I will call process2_v2 \n"); // NULL indicates last command. There could be more than 1 execl ("./process2_v2", "process2_v2", NULL); /* Execute ls */ } printf("The parent is ending\n"); return 0; } macbook:unix_ch13> exit Saving session... ...copying shared history... ...saving history...truncating history files... ...completed. Deleting expired sessions... 9 completed. Script done on Wed Dec 3 18:50:33 2025