Synchronization problems in computers - Article 3
In this article, we will discuss a few synchronization problems in computers. In the next article,
I will introduce Mutex and Semaphore and explain how they can be helpful in solving
synchronization problems.
1. Dining philosophers problem
This is a classical synchronization problem formulated by Edsger Dijkstra.
Five silent philosophers sit at a round table with bowls of spaghetti. Forks are
placed between each pair of adjacent philosophers. (An alternative problem formulation uses rice and chopsticks instead of spaghetti and forks).
Each philosopher must alternately think and eat. However, a philosopher can only eat
spaghetti when he has both left and right forks. Each fork can be held by only one philosopher
and so a philosopher can use the fork only if it is not being used by another philosopher.
After he finishes eating, he needs to put down both forks so they become available to others.
A philosopher can take the fork on his right or the one on his left as they become available,
but cannot start eating before getting both of them.
Eating is not limited by the remaining amounts of spaghetti or stomach space; an infinite supply is assumed.
The problem is how to design a discipline of behavior (a concurrent algorithm) such that each philosopher will not starve; i.e., can forever continue to alternate between eating and thinking, assuming that any philosopher cannot know when others may want to eat or think.
Think about how to simulate and solve this in Computers.
2. A stack problem
There is a global stack of say 10 integers shared by 2 threads. Elements 1 to 10 (1,2,3,4,5,6,7,8,9 and 10) are pushed on to the stack.
Now, the problem is to synchronize the 2 threads in such a way that thread 1 pops 10, thread 2 pops 9, so on and so forth. Basically, we need to alternate between threads exactly once.
3. Rendezvous problem
In this problem, two threads need to proceed after uniting at a rendezvous.
Suppose T1 code is as below:
Statement T1.A
Statement T1.B
T2 code is as below:
Statement T2.A
Statement T2.B
Now, we need to ensure that T1 and T2 have both executed their first statements (i.e., T1.A and T2.A). Only then they can proceed.
4. Producer consumer problem
This is a general problem in computers where there are producers and consumers. Examples: In event driven UI systems, events like mouse click, object selection, window events will be produced by users and they will be handled by handlers.
Imagine network packets coming. Network packets are produced and they are consumed by upper layeres.
5. Readers writers problem
This is another classical problem. In this problem, say one thread is writing and modifying a data structure or a database and n threads are reading.
We have to ensure that when writer is writing, no other threads can enter. But, when reader is reading, other threads can also read. But, writer cannot enter. Basically, one class of threads mutually exclude other class of threads.
I will introduce Mutex and Semaphore and explain how they can be helpful in solving
synchronization problems.
1. Dining philosophers problem
This is a classical synchronization problem formulated by Edsger Dijkstra.
Five silent philosophers sit at a round table with bowls of spaghetti. Forks are
placed between each pair of adjacent philosophers. (An alternative problem formulation uses rice and chopsticks instead of spaghetti and forks).
Each philosopher must alternately think and eat. However, a philosopher can only eat
spaghetti when he has both left and right forks. Each fork can be held by only one philosopher
and so a philosopher can use the fork only if it is not being used by another philosopher.
After he finishes eating, he needs to put down both forks so they become available to others.
A philosopher can take the fork on his right or the one on his left as they become available,
but cannot start eating before getting both of them.
Eating is not limited by the remaining amounts of spaghetti or stomach space; an infinite supply is assumed.
The problem is how to design a discipline of behavior (a concurrent algorithm) such that each philosopher will not starve; i.e., can forever continue to alternate between eating and thinking, assuming that any philosopher cannot know when others may want to eat or think.
Think about how to simulate and solve this in Computers.
2. A stack problem
There is a global stack of say 10 integers shared by 2 threads. Elements 1 to 10 (1,2,3,4,5,6,7,8,9 and 10) are pushed on to the stack.
Now, the problem is to synchronize the 2 threads in such a way that thread 1 pops 10, thread 2 pops 9, so on and so forth. Basically, we need to alternate between threads exactly once.
3. Rendezvous problem
In this problem, two threads need to proceed after uniting at a rendezvous.
Suppose T1 code is as below:
Statement T1.A
Statement T1.B
T2 code is as below:
Statement T2.A
Statement T2.B
Now, we need to ensure that T1 and T2 have both executed their first statements (i.e., T1.A and T2.A). Only then they can proceed.
4. Producer consumer problem
This is a general problem in computers where there are producers and consumers. Examples: In event driven UI systems, events like mouse click, object selection, window events will be produced by users and they will be handled by handlers.
Imagine network packets coming. Network packets are produced and they are consumed by upper layeres.
5. Readers writers problem
This is another classical problem. In this problem, say one thread is writing and modifying a data structure or a database and n threads are reading.
We have to ensure that when writer is writing, no other threads can enter. But, when reader is reading, other threads can also read. But, writer cannot enter. Basically, one class of threads mutually exclude other class of threads.
Comments
Post a Comment