Posts

Showing posts from July, 2015

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 ptr; } void print_node(NODE *ptr) {     printf("|%d|",ptr->data);     if(ptr->link != NULL) {  

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(readers == 1)         roomEmpty.wait(); // First reader locks and bars wri

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