Posts

Showing posts from June, 2015

Synchronization primitives: Mutex and Semaphore (Explanation for Semaphore) - Article 5

Hi Folks, In the previous article, we saw what is Mutex or Mutual Exclusion. If critical section code is thought of as a single resource, only one thread will be allowed to use that resource. In this article, we shall see what is a semaphore and the solutions to few synchronization problems using the semaphore. A semaphore is very simple and easy to understand but it can solve complex synchronization problems. It is just a integer variable that can be initialized to any integer value (Usually non-negative: >= 0). After that, it is shared by the threads (Or processes). After initialization, only two operations can be performed on the semaphore: Decrement and Increment. The pseudo code of usage is as below: Semaphore MySem; MySem = 3; // Share MySem between n threads say 10 threads Thread 1: // Some code here MySem.Increment(); // MySem++ // Some more code here MySem.Decrement(); // MySem-- Thread 2: // Some code here MySem.Decrement(); // MySem -- // Some

Synchronization primitives: Mutex and Semaphore - Article 4

In this article, lets deep dive and understand the synchronization primitives - Mutex and Semaphore. Mutex or Mutual Exclusion is the basic form of synchronization primitive. Its a locking mechanism and ensures that only one thread or process can enter the critical section. After finishing the work (Like updating global variable or a data structure), the thread unlocks the mutex. NOTE: Mutex is used to synchronize threads. However, latest addition to POSIX standard says that the mutex can be used to synchronize processes if it's shared between them. A real world example follows: In olden days when there were no mobile phones, love birds (I mean lovers) used the STD phone booth extensively. What would they do? enter the phone booth, lock it and dial their partners home phone and start off on a long call - Outside, you could see a long line of people cursing the guy inside and waiting to get into the booth. i.e., one and only thread or process (Person) needs to enter critica

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 be

Synchronization: Discussion about Mutex and Semaphore - Article 2

Let's do a double this week. I had mentioned about synchronization in computers but if I had discussed in previous article, it would be very big, hence I have split the previous article to discuss examples of synchronization in computers separately. In the previous article, I discussed about synchronization giving real world examples here: Synchronization article - 1 Let's see what synchronization is all about in the computers: The processor executes instructions serially one by one if it's a uniprocessor system. But synchronization involves things executing in parallel. Let's dig deep. 1. In uniprocessor systems, the OS takes care of scheduling different processes (Not covered in detail here) and makes multi tasking and multi processing possible. - The parallelism is pseudo and virtual to the humans because humans can't see and perceive the speed of processor's execution. - Even though instructions are executed serially, it appears to the users that e

Synchronization: Discussion about Mutex and Semaphore - Article 1

This is the first article in the series about Mutex and Semaphore. In my blog, I shall follow below sequence: 1. First, I will try to explain the concept. 2. Next, we shall see what problems the concept may solve or what problems may occur if the concept is not applied. 3. Finally, we shall see how the concept will solve the problem. In this article, I will try to cover what Synchronization in Computers is all about and different problems that can occur if Synchronization is not used. First we will see the real world examples and see what Synchronization is all about. Dictionary meaning is as below: 1. To cause to occur or operate with exact coincidence in time or rate. 2. Synchronization is the coordination of events to operate a system in unison. Let's try to understand in depth. Consider below examples: 1. Imagine soldiers doing march past in a parade. - They operate at the same time and everybody march in order. - They appear synchronized. How do they do it? - Every

A brief introduction to this blog

Hi everyone reading this blog, Before starting to publish articles, I would like to give brief introduction about "Why this blog?". When I explored Internet for knowledge about C++, OO Design, Design patterns, OS, Threads, Synchronization, etc, I found good articles, but I felt things were scattered and confusing at times. Example 1: Mutex Vs. Semaphore - Some claim that Binary semaphore is same as Mutex and some argue that they are not same. Example 2: Design patterns - Even with simple examples in many articles, difficult to grasp and think where to apply when a problem is given. With this, I got an idea as to why not me start a blog and stitch all the information from various books, articles and Internet sites together and create a blog to give in depth information about below topics and not restricted to: 1. C++ 2. STL 3. Design patterns 4. UML 5. OO Design 6. OS Concepts 7. Linux 8. Synchronization With the above introduction, I would like to start off with