Foreword
The following exercises needs for you to access a Unix system. A Linux
operating system would be better (because I didn't try these exercises
on Solaris) but probably not mandatory.
1. Timeout
Using the alarms (
SIGALRM
) make a program that wait 10
seconds for a keyboard event, exit if none is given or wait for
another 10 seconds if one key is pressed. Use the
signal()
function and then the
sigaction()
function.
2. Critical Code
Make a program the block all signals during the execution of the
following piece of code:
int computation() {
int i;
float array[100];
for (i=0; i < 100; i++)
array[i] = 1000*sin(cos((float) i) + tan((float (100-i))));
exit(0);
}
Use the
signal()
function and then the
sigaction()
function (don't forget to restore the
pending signals once the computation is finished).
3. Signal Pong
Make a program that fork and have the following behaviour:
- Once forked, the parent send a signal
SIGUSR1
to its
child and enter an infinite loop.
- The child wait in an infinite loop.
- Both (parent and child) when receiving a
SIGUSR1
signal reply by the same (parent to child and child to parent).
What happen if you replace the infinites loops by
pause()
. Use the
signal()
function and then
the
sigaction()
function.
4. Pipe Pong
Make a program that fork and have the following behaviour:
- Once forked, the parent send a message "
pong
" to its
child and enter an infinite loop.
- The child wait in an infinite loop.
- Both (parent and child) when receiving a
pong
message reply by the same (parent to child and child to parent).
Use an unnamed pipe and then a named pipe.
5. Pipe Race
Try to create 1 master process and 10 racing processes where the
master process is feeding a pipe with "Win", and then broadcast the
signal
SIGUSR1
to its group of process. When a racing
process get the signal
SIGUSR1
, it tries to read the
pipe, the one getting the "Win" message is out for the rest of the
game. At the end of the game, each process has a number which is the
round number in which it caught the message "Win".