Assignment 1, due Jan 31
Part of
the homework for 22C:169, Spring 2006
|
Always, on every assignment, please write your name legibly as it appears on your University ID and on the class list! All assignments will be due at the start of class on the day indicated, and unless there is what insurance companies call "an act of God" - something outside your control; the only exceptions to this rule will be by advance arrangement.
/* fork demo */ #include <stdio.h> #include <stdlib.h> #include <sys/types.h> #include <unistd.h> main() { pid_t pid; fputs( "parent, pre-fork\n", stderr ); if (pid = fork()) { /* parent, pid identifies child */ int status; fputs( "parent running\n", stderr ); if (pid == -1) { fputs( "cannot fork\n", stderr ); exit( EXIT_FAILURE ); } while (wait( &status ) != pid) { fputs( "unexpected wake-up\n", stderr ); } fputs( "child terminated\n", stderr ); } else { /* child, pid is zero */ fputs( "child running\n", stderr ); exit( EXIT_SUCCESS ); } }
a) It works. Try it on a Linux system. Explain what conditions could cause the cannot fork message.
b) Explain what could cause the unexpected wake-up message.
c) Make the child launch the application known as /bin/ls.
d) Fix it so that the parent passes its own environment to /bin/ls.
For parts c and d, you can turn in a single listing of the program incorporating both changes. For parts a and b, careful reading of the Unix programmer's reference manual plus this program should be helpful.