Synchronization Primitive - Semaphores in System Programming, Study notes of Computer Science

Lecture notes for cs241 spring 2007, system programming course, discussing the concept of semaphores as a synchronization primitive, their implementation, and usage for mutual exclusion. It also covers the test and set instruction and the busy waiting issue.

Typology: Study notes

Pre 2010

Uploaded on 03/16/2009

koofers-user-6a1-1
koofers-user-6a1-1 🇺🇸

5

(1)

10 documents

1 / 21

Toggle sidebar

This page cannot be seen from the preview

Don't miss anything!

bg1
CS 241 Spring 2007
System Programming
1
Synchronization Primitive - Semaphores
Lecture 11
Klara Nahrstedt
pf3
pf4
pf5
pf8
pf9
pfa
pfd
pfe
pff
pf12
pf13
pf14
pf15

Partial preview of the text

Download Synchronization Primitive - Semaphores in System Programming and more Study notes Computer Science in PDF only on Docsity!

CS 241 Spring 2007System Programming

Synchronization Primitive - Semaphores^ Lecture 11^ Klara Nahrstedt

CS241 Administrative †^ SMP2 Quiz on Monday, 2/12/07 †^ Read Chapter 5.3 in Stallings Book andChapter 14 in R&R

Discussion^ †^ In multi-processors^ „^ Several processors share memory^ „^ Processors behave independently in a peerrelationship^ „^ Interrupt disabling will not work^ „^ We need hardware support^ „^ The hardware support is based on execution ofmultiple instructions atomically^ „^ Atomic execution of a set of instructions means thatthese instructions are treated as a single step thatcannot be interrupted

Test and Set Instruction boolean Test_And_Set(boolean* lock)

atomic^ {

boolean initial;initial = lock;lock = true;return initial;} pseudo-code!

Semaphore Concept Fundamental Principle:

two or more processes want to cooperate by means of simple signals For signaling introduce „^ Special Variable :

Semaphore^ s „^ Primitives:^ †^ semSignal(s)

  • transmit signal via semaphore s † semWait(s) – receive signal via semaphore s Primitives semSignal and semWait must be ATOMIC!!! Note: Different notation is used for semSignal and semWait (P for semWait, V for semSignal, ‘wait’ forsemWait, ‘signal’ for semSignal)

Definition of Semaphore Primitives(Counting Semaphore) struct^ semaphore{ int^ count; queueType^ queue; };void^ semWait(semaphore s) { s.count--; if^ (s.count < 0){ place this process in s.queue;block this process;} }

void^ semSignal(semaphore s) { s.count++;^ if^ (s.count^ ≤

0) { remove a process P froms.queue;place process P on ready list;}

Mutual Exclusion Using Semaphores semaphore s = 1;Pi { while(1)^ {^ semWait(s);^ /* Critical Section /^ semSignal(s);^ / remainder */} }

lock = 0;Pi { while(1) {^ while(Test_And_Set(lock)) { };^ /* Critical Section /^ lock =0;^ / remainder */} }

Value of Queue Semaphore lock

A semWait(lock) (^10)

B semWait(lock) -1 B

Process^ semSignal(lock) 0 semSignal(lock) 1

Process^ Critical Region^ Normal Execution^ Blocked onsemaphorelock

Implementation of Semaphores in POSIX^ POSIX:SEM semaphore is^ variable of type sem_t^ Atomic Operations:^ int sem_init(sem_t *sem, int pshared, unsigned value);^ int sem_destroy(sem_t *sem);^ int sem_post(sem_t *sem);^ Int sem_trywait(sem_t *sem);^ Int sem_wait(sem_t *sem);^ Use <semaphore.h>

Unnamed Semaphores Ch 14 pp 491-501^ #include<semaphore.h>^ Sem_t sem;^ You cannot make a copy of a semaphore variable!!!^ #include <semaphore.h>^ int sem_init(sem_t *sem, int pshared,unsigned value);^ pshared == 0 only threads of process creating semaphore canuse semaphore.

sem_init can fail!!! In unsuccessful, sem_initreturns -1 and setserrno.

Value > sem_value_maxResources exhaustedInsufficient privileges EINVALENOSPCEPERM

cause errno

Initialization Example sem_t semA; if (sem_init(&semA, 0, 1) == -1)^ perror(“Failed to initialize semaphore semA”);

Semaphore Operations #include <semaphore.h> int sem_post(sem_t *sem); signal safe can be used in signal handlers if unsuccessful, returns -1 and sets errno errno == EINVAL if semaphore doesnt exist

Semaphore Operations int sem_trywait(sem_t *sem); doesn’t block returns -1 and errno==EAGAIN if semaphore zero can be interrupted by signal – errno == EINTR int sem_wait(sem_t *sem); blocks if semaphore zero can be interrupted by signal – errno == EINTR