In this post, let’s set up a scenario using two FIFOs and a client-server model to demonstrate a deadlock situation.
The server waits for the client to send a message on one 𝑭𝑰𝑭𝑶 (𝒍𝒆𝒕’𝒔 𝒄𝒂𝒍𝒍 𝒊𝒕 𝒇𝒊𝒇𝒐𝟏). At the same time, the client waits for a message from the server on another 𝑭𝑰𝑭𝑶 (𝒇𝒊𝒇𝒐𝟐). This creates a situation where both are waiting for each other to send a message, thus causing a deadlock.
The server expects the client to write to 𝒇𝒊𝒇𝒐𝟏and then 𝒇𝒊𝒇𝒐𝟐. The client, however, writes to 𝒇𝒊𝒇𝒐𝟐 first and then tries to write to 𝒇𝒊𝒇𝒐𝟏. Both processes end up waiting for each other to read from the FIFOs they wrote to, resulting in a deadlock.
What to expect when you run it?
1. When you run the 𝒔𝒆𝒓𝒗𝒆𝒓.𝒄 program, it will first try to read from 𝒇𝒊𝒇𝒐𝟏. Since nothing has been written to 𝒇𝒊𝒇𝒐𝟏 yet, it will block and wait for some data to be written.

2. When you run the 𝒄𝒍𝒊𝒆𝒏𝒕.𝒄 program, it will immediately try to write to 𝒇𝒊𝒇𝒐𝟐. However, the server is not reading from 𝒇𝒊𝒇𝒐𝟐 yet; it’s still waiting on 𝒇𝒊𝒇𝒐𝟏. So, the client will block at this point too.

3. As a result, both the server and the client will be stuck (deadlocked). (Picture 3) Neither will proceed, and you won’t see any output from either program.

So, how to we know if we are getting what we were expecting?
We can do this by echo “Test message” > 𝒇𝒊𝒇𝒐𝟏 in another terminal.
(Picture 4)

What do you expect after the above?
1. The server successfully read from 𝒇𝒊𝒇𝒐𝟏 and printed the message “Text message”. This means something (or someone) wrote to 𝒇𝒊𝒇𝒐𝟏 before or while the server was waiting to read from it. It could have been an external input, like using the echo command previously seen, or perhaps from a previous run of the client.
2. After reading from 𝒇𝒊𝒇𝒐𝟏, the server moved on to read from 𝒇𝒊𝒇𝒐𝟐.
3. When you run the client, it successfully wrote the message “Hello from client on fifo2!” to 𝒇𝒊𝒇𝒐𝟐, which the server then read and printed.
4. Both programs completed their main execution flow after this, so they both exited. (Picture 4)
There was no deadlock in this scenario because the condition for the deadlock (server waiting on 𝒇𝒊𝒇𝒐𝟏 while the client writes to 𝒇𝒊𝒇𝒐𝟐 first) wasn’t met. Instead, the server was able to read from 𝒇𝒊𝒇𝒐𝟏 and then move on to read from 𝒇𝒊𝒇𝒐𝟐.
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
An Article by: Yashwanth Naidu Tikkisetty
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
