Posts

Synchronization primitives: Mutex and Semaphore (Condition Variables) - Article 11

Hi Folks, In previous articles, we have understood the synchronization constructs Mutex and Semaphore. Semaphore is a superior construct compared to a Mutex. Mutex is just a lock where as Semaphore can be used to solve variety of problems. I have explained Mutex & Semaphore using pseudo code and in recent articles, I have provided real PThreads & Mutex code which can be executed & tested in Linux. Going forward, we shall explore Condition Variables & the differences between Mutex & Semaphore. We shall also write some real Semaphore code in Linux. We shall also solve few more synchronization problems & conclude Mutex & Semaphore. In this article, let's explore the concept of Condition Variable. -> Mutex is just a locking mechanism. -> Semaphore can be used as a lock or as a signalling mechanism. Now, how can we use Mutex to solve serialization problems? That's where Condition Variables come into the picture. When we use the combinati...

Synchronization primitives: Mutex and Semaphore (Sample Program) - Article 10

Hi Folks, I have run this program on Intel I5 processor running Ubuntu Linux. You can imagine the speed and this program is a very tiny one. But still the output is garbled. |3|The list is full Count: 3 ->|3|The list is full |1|The list is full ->|7||3||3|->->The list is full ->|1||2|The list is full ->->|3||1| ->The list is full The list is full The list is full |7|The list is full |7|The list is full The above is not expected output. You can see that list is not displayed properly. The synchronized version of program is shown below using Mutex variable by name pro(tection). #include<sys/types.h> #include<sys/ipc.h> #include<sys/sem.h> #include<pthread.h> #include<stdio.h> #include<stdlib.h> typedef struct node {     struct node *link;     int data; } NODE; static int count = 0; NODE *head = NULL; pthread_mutex_t pro; NODE *get_node(void) {     int data = rand();  ...

Synchronization primitives: Mutex and Semaphore (Sample Program) - Article 9

Hi Folks, In this article, I am going to present my version of producer consumer problem which we need to synchronize. The producer can produce a maximum of 5 elements and the consumer consumes 1 element at a time. All data is shared and hence, synchronization is needed. In this article, I am going to present the program. In the next article, we shall apply synchronization. #include<sys/types.h> #include<sys/ipc.h> #include<sys/sem.h> #include<pthread.h> #include<stdio.h> #include<stdlib.h> typedef struct node {     struct node *link;     int data; } NODE; static int count = 0; NODE *head = NULL; NODE *get_node(void) {     int data = rand();     data = data % 13;     NODE *ptr = (NODE *)malloc(sizeof(NODE));     if(ptr != NULL) {         ptr->data = data;         ptr->link = NULL;     }     return...

Synchronization primitives: Mutex and Semaphore (Readers & Writers problem) - Article 8

Hi Friends, In this article, we shall solve the Readers & Writers problem. Readers & Writers problem: There may be m writers & n readers related to a shared data structure or a record in the database (some shared data). Only one writer thread can be updating the data, but there can be many readers accessing the data. The constraints are: 1. Any number of readers can be accessing the data in critical section. 2. Only one writer thread should be allowed to update the data in critical section. Solution: int readers = 0; // Shared variable to keep track of number of readers,                  // yes this also needs to be protected Semaphore readersProtect = 1; Semaphore roomEmpty = 1; Writers thread code: roomEmpty.wait();     // Update shared data, critical section for writers roomEmpty.signal(); Readers thread code: readersProtect.wait();     readers = readers + 1;     if(r...

Synchronization primitives: Mutex and Semaphore (Rendezvous problem) - Article 7

In this article, we shall solve Rendezvous problem. Rendezvous problem: Imagine you and your friend plan to go to a trip and decide to meet in Railway station. None of you can proceed before the other has arrived. Once you have arrived, you inform (or signal) your friend via a message or a call then you wait for your friend. Your friend would also do the same thing if he came first. Similar real life solution can be applied to the threads Rendezvous problem. Thread 1: Event A1; Event B1; Thread 2: Event A2; Event B2; We have to ensure that T1 and T2 should proceed only after both A1 and A2 are complete. But, anybody can complete A1 or A2 first. Solution: Semaphore T1_Arrived = 0; Semaphore T2_Arrived = 0; Thread 1: Event A1; T1_Arrived.signal(); // Signal T1 has arrived T2_Arrived.wait(); // Wait for T2 Event B1; Thread 2: Event A2; T2_Arrived.signal(); // Signal T2 has arrived T1_Arrived.wait(); // Wait for T1 Event B2; Suppose T2 finishes A2, i...

Synchronization primitives: Mutex and Semaphore (Serialization & Mutex problems using Semaphore) - Article 6

Hi Friends, In the previous articles, we have gained knowledge about Mutex and Semaphore. In this article, we shall see how a Semaphore can be used to solve serialization problem and Mutual Exclusion (Mutex) problem. Serialization problem: Thread 1: Event A; // Could be any code Thread 2: Event B; // Could be any code The problem is, we have to ensure that Event B or a particular code or instructions should always be executed after Event A or some other particular code. Basically, we are serializing the instructions irrespective of how the threads are scheduled to run. Solution: There are no resources here, we need to just signal, initialize Semaphore to 0. Semaphore SignalThread2 = 0; Thread 1: Event A; SignalThread2.signal(); Thread 2: SignalThread2.wait(); Event B; The code for Thread 1 and Thread 2 is as above. Irrespective of which thread is scheduled, Event A always occurs before Event B. How? Let's say Thread 2 is scheduled first, it executes...

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...