Lab6.1 - Pipes, Message queues, Signals

Alessio Carpegna and Stefano Di Carlo

Exercise 1 - Message Queue

Write a modified Linux shell based on a client/server architecture. The new shell must have the following characteristics:

  • The client receives commands on the command line.
  • Every command is sent to the server and executed in the background.
  • The shell terminates when the user enters the exit command.

Try to implement the client/server communication using Message Queues.

Exercise 2 - Pipes

Write a multi-process program that evaluates the following math series:

\sum_{i=1}^{n} \frac{e^i}{i}

The main process receives the N value as input (set max value for N to 10) and performs the final sum. The i-th child evaluates its internal term and sends the result to the parent through a pipe. Please note that each internal term generates a floating value.

Hint: Please notice that the read function is, by default, a blocking function: if there is not enough data to be read, the reading process is moved to the waiting state. In this configuration, there is no 0-byte read return to check: a 0 / less than 0 value means an error during the read operation!

Exercise 3 - FIFO

Write a client/server application where the client shares a sequence of strings with the server using a FIFO.

The server scans the content of every string and, as soon as the string is equal to ”house” or ”casa”, it prints the message ”house detected”.

The client and server must terminate if the string contains ”exit”.

Hint: Remember that the FIFO can be defined and shared among processes using the same FIFO name. Thus, you do not need to fork the process, but you must write two different programs: the server and the client. Run the server before the client.

Exercise 4 - Signals

Write a program that creates ten child processes.

The parent process must:

  • Print the list of active PIDs and prompt the user to choose a process to end.
  • Ask the selected project to terminate through the SIGQUIT signal.
  • Iterate until there are active processes

Each child process must:

  • Manifest itself every X random seconds (between 1 and 10) by printing some messages, including its ID.
  • Terminate when receiving the SIGQUIT signal

Lab6.2 - Pipes, Message queues, Signals

Exercise 5 - Signals

Write a C program that creates NUM_CHILDREN child processes using the fork() system call. NUM_CHILDREN is a constant defined in the program.

Each child process must register a SIGUSR1 signal handler.

The parent process performs an infinite loop. At every iteration, it sends a SIGUSR1 signal to a randomly selected active child process.

The child processes perform a busy wait without doing anything. When a child process receives a SIGUSR1 signal, it prints its PID and a message indicating that the signal has been received and then terminates its execution.

The parent process terminates when all children terminate.

Exercise 6 - Signals

Write a C program that handles two types of signals: SIGINT (generated by pressing Ctrl+C) and SIGUSR1 (a user-defined signal generated using the kill -SIGUSR1 <PID> command from the shell).

The program should not perform busy waiting (you can use the pause system call).

The program implements an infinite loop and should count how many times each signal is received. The count should be printed each time a signal is handled.

How can you terminate the program since Ctrl+C is not working?

Exercise 7 - Pipe

Write a C program that creates a child process.

The child enters a loop where it asks for a command from the stdin and sends it to the parent using a pipe.

The parent executes the command using the system() system call.

Both the parent and child terminate if the command is exit.

Exercise 8 - Pipe

Write a simple C program composed of two processes, the child and parent processes:

The parent reads a 4 x 4 matrix of integers from stdin using scanf. It then prints the even numbers on the screen and sends the odd numbers to the child using a pipe.

The child prints the received numbers.