Lab08 - Synchronization Readers Writer Problem
Lab08 - Synchronization Readers Writer Problem
Objectives
Posix semaphores
All POSIX semaphore functions and types are prototyped or defined in semaphore.h.
To define a semaphore object, use
sem_t sem_name;
Example of use:
sem_init(&sem_name, 0, 10);
gets the current value of sem and places it in the location pointed to by valp
Example of use:
int value;
sem_getvalue(&sem_name, &value);
printf("The value of the semaphors is %d\n", value);
Example of use:
sem_destroy(&sem_name);
Consider the problem we had before and now let us use semaphores:
Declare the semaphore global (outside of any funcion):
sem_t mutex;
Initialize the semaphore in the main function:
sem_init(&mutex, 0, 1);
Problems:
Q 1. Write a program having two threads, thread1 and thread2 contain following
printf statements. Use one or more semaphore between printf statements to
synchronize the sequence in Alphabetical order (considering only last capital
letter).
Q 2 write a program having two threads, thread1 should increment global variable
200000 times and thread2 decrement same global variable by same number of
time. Produce and observe output of global variable in main thread with and
without semaphore.