Homework Assignment 1 (Solution)
Question 1 (Processes, 8%)
a.) What is the purpose of the WNOHANG option of the waitpid (man 2 waitpid)
system call?
The calling parent process will return immediately, i.e., no blocking if
no child has exited.
b.) Explain what happens if a parent process does not 'wait' for
the termination of a child process?
The child process is a zombie process; it will continue to hold resources.
Question 2 (Threads, 12%)
a.) Explain the difference between a thread and a process. Name at least two
advantages and one disadvantage of threads.
See lecture slides regarding differences. Advantages: 'cheaper' context
switches, ease of inter-thread communication, etc. Disadvantage: need for
synchronization.
b.) Name four different ways to
stop a thread.
Return from thread routine, parent process exits,
pthread_cancel, pthread_exit, ...
c.) Explain why pthread_cond_signal and pthread_cond_wait have to be
called while the calling process holds the mutex?
To avoid race conditions. Claiming and freeing of the mutex are part of
the calls and happen atomically. If this was not the case, a signal sent
could get lost because the process the signal is meant for may not have
called pthread_cond_wait yet.
Question 3 (Signals, 20%)
a.) What are the differences between POSIX signals and real-time signals?
The two important differences I was looking for: real-time signals are queued and can carry small amount of data.
b.) Write a program that explores what happens if a signal arrives (same one or
different one) while
your program is inside a signal handler. Provide the signal handler code
used in your program and summarize your results on a Linux computer (mention
the kernel version used).
You should find something like: first signal gets caught, all others are lost, etc.
Question 4 (Linux, 10%)
a.) Find the include file for sockets on a Linux system (hint: look at
/usr/include/sys/socket.h and possibly at the include files used by this file)
and search for a data structure containing the allowed socket types. List them
and explain each of them with one sentence.
Look at 'enum __socket_type' in /usr/include/bits/socket.h.
b.) Find out how many system calls are supported on your Linux system, mention the first one and the last one on the list, the number used for the getpid() system call, and the kernel version of your Linux
system (Hint: search for the include file containing this information
in the /usr/include directory and
its subdirectories).
Look at /usr/include/asm/unistd.h, the number of system calls is around
270, the first being 'exit', the last one being 'tgkill', and getpid having
number 20 (but the file content can vary depending on Linux version/distribution).
Question 5 (Performance, 25%)
Write a small program that measures the performance of the getpid() (man 2 getpid) system call and the fork (man 2 fork) system call.
Use gettimeofday (man 2 gettimeofday) to measure the
system call execution time. Provide the source code (no error handling required) and measure the performance ten times for each of the two system calls
and provide the timing results and compute an average for each system call.
You should notice that the getpid call is very 'cheap', it is one of the simplest system calls,
while the fork system call is rather expensive in comparison. You may also have noticed that the first measurement may be signicantly larger than the other ones (after the first measurement code/data can be found in cache, making these calls faster...).
Question 6 (Programming, 25% + 5%)
Write a program (no error handling required) that takes a file name
as an argument, opens the file, reads it, and closes the file. The file
should contain a string with the name of another application (e.g., 'ls' or
'ps' or any of your own applications) and the program forks a new
process that executes the application named in the file. Optional:
For 5% extra points
pass at least one parameter to the application (i.e., the file contains
a string such as 'ps -ax' or 'ls -lt').
Simply use fork followed by execve, etc., or you could also use 'system'.