People’s Education Society
P.E.S. COLLEGE OF ENGINEERING
NAGSENVANA, AURANGABAD
DEPARTMENT: Department of Data Science and Engineering
CLASS: SY DS LABORATORY: Lab 3
SUBJECT: Operating System (OS) Lab
Semester-IV Experiment No. 7
Aim : Implementation of Synchronization Primitives: Semaphore, Locks, and Conditional Va
riables.
Theory:
Semaphore Implementation
Objective: To implement process synchronization using semaphores.
Code:
#include <stdio.h>
#include <pthread.h>
#include <semaphore.h>
#include <unistd.h>
sem_t semaphore;
void* threadFunction(void* arg) {
sem_wait(&semaphore);
printf("Thread %d: Entered critical section\n", *(int*)arg);
sleep(1);
printf("Thread %d: Exiting critical section\n", *(int*)arg);
sem_post(&semaphore);
return NULL;
}
int main() {
pthread_t threads[3];
int thread_ids[3] = {1, 2, 3};
sem_init(&semaphore, 0, 1);
for (int i = 0; i < 3; i++) {
pthread_create(&threads[i], NULL, threadFunction, &thread_ids[i]);
}
for (int i = 0; i < 3; i++) {
pthread_join(threads[i], NULL);
}
sem_destroy(&semaphore);
return 0;
}
Output:
Thread 1: Entered critical section
Thread 1: Exiting critical section
Thread 2: Entered critical section
Thread 2: Exiting critical section
Thread 3: Entered critical section
Thread 3: Exiting critical section
Explanation:
● A semaphore is initialized with a value of 1, allowing only one thread to enter the critical secti
on at a time.
● Each thread waits (sem_wait) before entering and signals (sem_post) upon exiting the critical
section.
● The output demonstrates serialized access to the critical section.
Mutex Lock Implementation
Objective: To implement mutual exclusion using pthread mutex locks.
Code:
#include <stdio.h>
#include <pthread.h>
#include <unistd.h>
pthread_mutex_t lock;
void* threadFunction(void* arg) {
pthread_mutex_lock(&lock);
printf("Thread %d: Entered critical section\n", *(int*)arg);
sleep(1);
printf("Thread %d: Exiting critical section\n", *(int*)arg);
pthread_mutex_unlock(&lock);
return NULL;
}
int main() {
pthread_t threads[3];
int thread_ids[3] = {1, 2, 3};
pthread_mutex_init(&lock, NULL);
for (int i = 0; i < 3; i++) {
pthread_create(&threads[i], NULL, threadFunction, &thread_ids[i]);
}
for (int i = 0; i < 3; i++) {
pthread_join(threads[i], NULL);
}
pthread_mutex_destroy(&lock);
return 0;
}
Output:
Thread 1: Entered critical section
Thread 1: Exiting critical section
Thread 2: Entered critical section
Thread 2: Exiting critical section
Thread 3: Entered critical section
Thread 3: Exiting critical section
Explanation:
● A mutex lock ensures that only one thread can execute the critical section at a time.
● The lock is acquired using pthread_mutex_lock and released using pthread_mutex_unlock.
● The serialized execution demonstrates mutual exclusion.
Conditional Variable Implementation
Objective: To synchronize threads using conditional variables.
Code:
#include <stdio.h>
#include <pthread.h>
#include <unistd.h>
pthread_mutex_t mutex;
pthread_cond_t cond;
int shared_data = 0;
void* producer(void* arg) {
pthread_mutex_lock(&mutex);
shared_data = 1;
printf("Producer: Data produced\n");
pthread_cond_signal(&cond);
pthread_mutex_unlock(&mutex);
return NULL;
}
void* consumer(void* arg) {
pthread_mutex_lock(&mutex);
while (shared_data == 0) {
pthread_cond_wait(&cond, &mutex);
}
printf("Consumer: Data consumed\n");
pthread_mutex_unlock(&mutex);
return NULL;
}
int main() {
pthread_t prod, cons;
pthread_mutex_init(&mutex, NULL);
pthread_cond_init(&cond, NULL);
pthread_create(&cons, NULL, consumer, NULL);
sleep(1);
pthread_create(&prod, NULL, producer, NULL);
pthread_join(prod, NULL);
pthread_join(cons, NULL);
pthread_mutex_destroy(&mutex);
pthread_cond_destroy(&cond);
return 0;
}
Output:
Producer: Data produced
Consumer: Data consumed
Explanation:
● The producer thread sets shared_data and signals the condition variable.
● The consumer waits using pthread_cond_wait until it is signaled by the producer.
● This ensures synchronization between producer and consumer threads.
Conclusion:
Semaphores, mutex locks, and condition variables provide different ways to handle synchronization. S
emaphores allow controlled access to resources, mutex locks ensure mutual exclusion, and condition v
ariables facilitate inter-thread communication.
Prepared By H.O.D.
Prof. [Link] Dr. A. A. Tupe