#include /* exec, getcwd */ #include /* printf */ #include /* need for wait */ #include /* wait() */ /* Exec example code */ /* This program forks and the child process exec's execs "/bin/ls" The parent waits for the child process to die, then prints out a message */ void child(void) { int pid = getpid(); printf("Child process PID is %d\n",pid); printf("Child now ready to exec ls\n"); execl("/bin/ls","ls",NULL); } void parent(void) { int pid = getpid(); int stat; printf("Parent process PID is %d\n",pid); printf("Parent process is waiting for child\n"); wait(&stat); printf("Child is done. Parent now transporting to the surface\n"); } void main(void) { printf("In main - starting things with a fork()\n"); if (fork()) { /* fork() returned a non-zero so this is the parent */ parent(); } else { /* fork() returns a zero so this is the child */ child(); } printf("Done in main()\n"); }