0% found this document useful (0 votes)
164 views3 pages

Lab08 - Synchronization Readers Writer Problem

This document discusses using semaphores for synchronization in a lab on implementing the readers-writers problem. It explains what semaphores are in POSIX, how to initialize, wait on, increment, get the value of, and destroy semaphores. It provides examples of using semaphores to synchronize threads printing in alphabetical order and incrementing/decrementing a global variable.

Uploaded by

Wasie Urrahman
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
164 views3 pages

Lab08 - Synchronization Readers Writer Problem

This document discusses using semaphores for synchronization in a lab on implementing the readers-writers problem. It explains what semaphores are in POSIX, how to initialize, wait on, increment, get the value of, and destroy semaphores. It provides examples of using semaphores to synchronize threads printing in alphabetical order and incrementing/decrementing a global variable.

Uploaded by

Wasie Urrahman
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 3

Lab 10: Semaphores (Implementation of Reader’s Writer’s Problem)

CS 2203: Operating Systems Spring 2017


Center for Advance Studies in Engineering (CASE) Computer Science Program
Lab 10: Semaphores
Lab Instructors: Waqas Ahmed, Kainat Iqbal

Objectives

• Learn to understand semaphores for synchronization.

Credits: Operating System concepts by silbershatz 9th edition.


http://man7.org/linux/man-pages/man3/sem_open.3.html

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;

To initialize a semaphore, use sem_init:


int sem_init(sem_t *sem, int pshared, unsigned int value);

 sem points to a semaphore object to initialize


 pshared is a flag indicating whether or not the semaphore should be shared
with fork()ed processes. LinuxThreads does not currently support shared
semaphores
 value is an initial value to set the semaphore to

Example of use:
sem_init(&sem_name, 0, 10);

To wait on a semaphore, use sem_wait:


int sem_wait(sem_t *sem);
Example of use:
sem_wait(&sem_name);
 If the value of the semaphore is negative, the calling process blocks; one of
the blocked processes wakes up when another process calls sem_post.

To increment the value of a semaphore, use sem_post:


int sem_post(sem_t *sem);
Example of use:
sem_post(&sem_name);

 It increments the value of the semaphore and wakes up a blocked process


waiting on the semaphore, if any.

To find out the value of a semaphore, use


int sem_getvalue(sem_t *sem, int *valp);

 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);

To destroy a semaphore, use


int sem_destroy(sem_t *sem);

 destroys the semaphore; no threads should be waiting on the semaphore if its


destruction is to succeed.

Example of use:
sem_destroy(&sem_name);

Using semaphores - a short example

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

Void * thread1(void * arg) Void * thread2(void * arg)


{ {
Printf(“thread1: A”); Printf(“thread1: B”);
Printf(“thread1: C”); Printf(“thread1: D”);
Printf(“thread1: E”); Printf(“thread1: F”);
} }

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.

Q 3. Write a muti-threaded program to implement readers and writers problem.

You might also like