Posts

Showing posts from August, 2015

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();     data = data % 13;     NODE *ptr = (NODE *)malloc(s