Introduction
Announcements

Schedule
Labs
Assignments
TA office hours

Tests, exam

Topic videos
Some course notes
Extra problems
Lecture recordings

Discussion board

Grades so far

CSC 209 extra problems regarding processes


1. Write a program which creates five child processes, numbered 0 through 4. Each child process computes the sum of the integers 5i through 5i+4, where i is its index number. It then returns this value as the process exit status. The parent waits for them all and outputs the total of the exit statuses, which will be the sum of the integers from 0 to 24 inclusive.

[solution]


2. There is a network server program in /usr/local/sbin/myserver. It is buggy and frequently crashes. Fortunately, when it gets into trouble it tends to give a segmentation exception and terminate, so it is easy to tell when it has crashed.

Write a complete C program which invokes /usr/local/sbin/myserver, and restarts it when it exits. After /usr/local/sbin/myserver exits, you should sleep for three seconds and then execute it again, and loop.

[solution]


3. Write a very trivial shell: print a prompt; call fgets() to read a line and remove any terminating newline; execute that whole line as a program; and loop around until eof. The input line must be a complete path name for the program you're trying to run (e.g. /bin/cat rather than cat) and it must be only the program name (e.g. if you type "/bin/cat file" it will try to run a program named "cat file" in the /bin directory).


4. Someone has removed the 'ps' command for alleged security reasons. Write a program to list all of the processes you own, assuming that the current process's pid is larger than all previously-existing pids. The algorithm is to loop from 1 to getpid(), doing a kill with "signal" 0 on each process id number. This just checks for your ability to signal the process without actually doing it, thus checking existence and ownership.

[solution]


5. Write an "autologout" program which kills its parent after one hour of tty inactivity (to be invoked by the user as "autologout&", and then the user goes and does other commands... or does not do other commands, and gets kicked off). Determine inactivity by calling ttyname(0) to get the file name for the terminal which is on stdin (e.g. "/dev/ttyp4"), and then stat it inbetween sleeps. The idle time is the current time minus the tty's mtime. "getppid()" tells you your parent's process id number, which you'll assume is the user's login shell. Kill it with signal 1 (SIGHUP).

(For easier testing, use an idle time of something like ten seconds, and start a new shell to run it in.)

(Be clever in the value you pass to sleep(). Considerable cleverness is possible here, rather than waking up just to do a time calculation and then sleeping again. And you don't have to risk sleeping too long and giving them more than an hour, either. [solution re sleep amount algorithm] )


6. Write a program which runs an arbitrary program with a time limit of 10 seconds. The program to run is specified on the command-line; pass argv+1 to execve or similar. (Its first element will be a path name; you do not need to use a search path.) You will fork, but the parent will not do a wait(), but rather, a sleep and then possibly a kill(pid, 9). If you do terminate the program in this way, print a suitable message to stderr.

[solution]


7. Write a program which runs an arbitrary program with an output limit (rather than the time limit in question #5). Use a pipe and a fork to read the stdout and stderr from the subprocess. (It's a better exercise if you refrain from using popen(), although in real life we would prefer to use popen() if it did what we needed.)

[popen-using solution]


8. Write a program something like xargs.


9. Write the system() library function, except don't worry about signals.


10. Write the popen() and pclose() library functions.


11. Add a built-in "cd" command to assignment three's "mysh". This has to be a builtin so as to change the current working directory of mysh itself, rather than a subprocess. Somewhere before the fork(), if argv[0] is "cd", handle it specially.


12. Implement some portion of the dining philosophers problem in C. Try different locking methods. In addition or instead, implement it in Java using Java threads.