This material is part of the course Practical Introduction to Parallel Programming and meant for educational purposes only.
Task
Implement a program in which two threads pass data through a FIFO (First In, First Out) buffer.
That is, one thread - the producer - reads characters from the standard input and puts those characters in the FIFO buffer.
The other thread - the consumer - reads characters from the FIFO buffer and writes them to the standard output.
The producer and the consumer threads should end when they encounter the end of the input stream.
Hints
The main thread initialises the FIFO buffer and spawns both the producer and the consumer thread. It then waits until those threads have finished executing.
Use the function getc
to read the next character from the standard input, and use the function putc
to write a character to the standard output. INGInious is configured to read the standard input from the file input.txt
(i.e. cat input.txt | your_code > output.txt
).
Define a struct fifo_t
that represents the FIFO buffer. It must contain a mutex, a condition variable and space for storing at least one character. It must also contain one or two integer variables indicating whether the buffer is full or empty.
To simplify your code, you can use a buffer of one character.
If you want to make your code more elegant you can define the functions fifo_init
and fifo_destroy
that initialise and destroy a buffer, respectively.
The main thread should create an instance of the FIFO buffer and pass its address to both child threads.