Sample exam tasks

TCP server and client with signal handling (medium difficulty)

Produce 2 programs, client and server. They will be passing data over TCP socket. They will be running on the same system. The client will accept command line argument saying signal number and optionally a port and connects to the server (use localhost implicitly). It will send its PID and signal number.

Server will send said signal to the PID, write a message to stdout. The client has to handle the signal, write a message to stdout and exits. Server is single threader, after reading the data and sending singal it will enter sleep again via accept().

Variant: use UDP

Thread pool (easy)

Main thread will create number of threads as specified on command line. Each thread simulates some work via sleep() for random time, 1 to 10 seconds. After that it signals to the main thread it is about to exit. Main thread will immediately create new thread.

The goal is to keep around the same number of threads. Each thread has its own number that will be written to stdout after the thread starts running and before its exit.

Use mutexes for synchronization and condition variables for signalling.

Shell command simulation (medium difficulty)

Copy /etc/services to current working directory using open, read, write, close. Then simulate command cat services | grep tcp | wc -l.

This has to be done so that each of the 3 commands is done using its own exec from its own process. Connect the processes with pipes and wait in the main process for the other processes to finish. Main process does not use exec and therefore it is necessary to call fork 3 times. Command wc does not have to pass the data to the main process, it is sufficient to write them out to the terminal.

inetd (difficult)

Write simple inetd server for TCP services. Configuration is stored in text file that looks like this:


Server listens on all ports. Uses select or poll. New connections leads to new process, its file descriptors 0, 1 and 2 are redirected to the new process and calls exec() for given program. These processes are then running independently.

For simplicity you can assume that the programs do not have other parameters. After each accept deal with zombies, if any.

The processing of the configuration file has to be done using open, read, close.

To check the functionality use Netcat. In case of /bin/cat it is sufficient to connect to given port and send some characters - they should be echoed back unchanged.

$ nc localhost 2222

Last change: