SlideShare una empresa de Scribd logo
1 de 44
Copyright ©: University of Illinois CS 241 Staff 1
Synchronization and
Semaphores
Discussion
 In uni-processors
 Concurrent processes cannot be overlapped, only interleaved
 A process runs until it invokes a system call, or is interrupted
 To guarantee mutual exclusion, hardware support could help
by allowing the disabling of interrupts
While(true) {
/* disable interrupts */
/* critical section */
/* enable interrupts */
/* remainder */
}
 What’s the problem with this solution?
2
Copyright ©: University of Illinois CS 241 Staff
Discussion
 In multi-processors
 Several processors share memory
 Processors behave independently in a peer relationship
 Interrupt disabling will not work
 We need hardware support where access to a memory
location excludes any other access to that same location
 The hardware support is based on execution of multiple
instructions atomically (test and set)
3
Copyright ©: University of Illinois CS 241 Staff
Test and Set Instruction
boolean Test_And_Set(boolean* lock)
atomic {
boolean initial;
initial = *lock;
*lock = true;
return initial;
}
Note: this is more accurate
than the textbook version
atomic = executed in a single shot
without any interruption
4
Copyright ©: University of Illinois CS 241 Staff
Using Test_And_Set for
Mutual Exclusion
Pi {
while(1) {
while(Test_And_Set(lock)) {
/* spin */
}
/* Critical Section */
lock =0;
/* remainder */
}
}
void main () {
lock = 0;
parbegin(P1,…,Pn);
}
5
What's the problem?
Copyright ©: University of Illinois CS 241 Staff
Using Test_And_Set for
Mutual Exclusion
Pi {
while(1) {
while(Test_And_Set(lock)) {
/* spin */
}
/* Critical Section */
lock =0;
/* remainder */
}
}
void main () {
lock = 0;
parbegin(P1,…,Pn);
}
Works, but has performance loss because of busy waiting.
6
Copyright ©: University of Illinois CS 241 Staff
Semaphores
 Fundamental Principle:
 Two or more processes want to
cooperate by means of simple signals
 Special Variable: semaphore s
 A special kind of “int” variable
 Can’t just modify or set or increment or
decrement it
7
Copyright ©: University of Illinois CS 241 Staff
Semaphores
 Before entering critical section
 semWait(s)
 Receive signal via semaphore s
 “down” on the semaphore
 Also: P – proberen
 After finishing critical section
 semSignal(s)
 Transmit signal via semaphore s
 “up” on the semaphore
 Also: V – verhogen
 Implementation requirements
 semSignal and semWait must be atomic
8
Copyright ©: University of Illinois CS 241 Staff
Semaphores vs. Test_and_Set
Semaphore
semaphore s = 1;
Pi {
while(1) {
semWait(s);
/* Critical Section */
semSignal(s);
/* remainder */
}
}
Test_and_Set
lock = 0;
Pi {
while(1) {
while(Test_And_Set(lock));
/* Critical Section */
lock =0;
/* remainder */
}
}
9
Copyright ©: University of Illinois CS 241 Staff
 Avoid busy waiting by suspending
 Block if s == False
 Wakeup on signal (s = True)
Inside a Semaphore
 Requirement
 No two processes can execute wait() and signal() on
the same semaphore at the same time!
 Critical section
 wait() and signal() code
 Now have busy waiting in critical section implementation
 Implementation code is short
 Little busy waiting if critical section rarely occupied
 Bad for applications may spend lots of time in critical sections
Copyright ©: University of Illinois CS 241 Staff 10
Inside a Semaphore
 Add a waiting queue
 Multiple process
waiting on s
 Wakeup one of the
blocked processes
upon getting a signal
 Semaphore data structure
typedef struct {
int count;
queueType queue;
/* queue for procs.
waiting on s */
} SEMAPHORE;
11
Copyright ©: University of Illinois CS 241 Staff
Binary Semaphores
typedef struct bsemaphore {
enum {0,1} value;
queueType queue;
} BSEMAPHORE;
void semSignalB (bsemaphore s)
{
if (s.queue is empty())
s.value = 1;
else {
remove P from s.queue;
place P on ready list;
}
}
void semWaitB(bsemaphore s) {
if (s.value == 1)
s.value = 0;
else {
place P in s.queue;
block P;
}
}
12
Copyright ©: University of Illinois CS 241 Staff
General Semaphore
typedef struct {
int count;
queueType queue;
} SEMAPHORE;
void semSignal(semaphore s) {
s.count++;
if (s.count ≤ 0) {
remove P from s.queue;
place P on ready list;
}
}
void semWait(semaphore s) {
s.count--;
if (s.count < 0) {
place P in s.queue;
block P;
}
}
13
Copyright ©: University of Illinois CS 241 Staff
void semWait(semaphore s) {
s.count--;
if (s.count < 0) {
place P in s.queue;
block P;
}
}
Making the operations atomic
 Isn’t this exactly what semaphores were trying to
solve? Are we stuck?!
 Solution: resort to test-and-set
14
typedef struct {
boolean lock;
int count;
queueType queue;
} SEMAPHORE;
void semWait(semaphore s) {
while (test_and_set(lock)) { }
s.count--;
if (s.count < 0) {
place P in s.queue;
block P;
}
lock = 0;
}
Copyright ©: University of Illinois CS 241 Staff
Making the operations atomic
 Busy-waiting again!
 Then how are
semaphores better
than just using
test_and_set?
15
void semWait(semaphore s) {
while (test_and_set(lock)) { }
s.count--;
if (s.count < 0) {
place P in s.queue;
block P;
}
lock = 0;
}
 T&S: busy-wait during critical section
 Sem.: busy-wait just during semWait, semSignal:
very short operations!
Copyright ©: University of Illinois CS 241 Staff
Mutual Exclusion Using
Semaphores
semaphore s = 1;
Pi {
while(1) {
semWait(s);
/* Critical Section */
semSignal(s);
/* remainder */
}
}
16
Copyright ©: University of Illinois CS 241 Staff
Value of
Semaphore
lock
Queue A
semWait(lock)
0
1
semWait(lock)
B
-1
semSignal(lock)
0
semSignal(lock)
1
Process Process Critical Region
Normal Execution
Blocked on
semaphore
lock
B
17
Copyright ©: University of Illinois CS 241 Staff
Semaphore Example 1
semaphore s = 2;
Pi {
while(1) {
semWait(s);
/* CS */
semSignal(s);
/* remainder */
}
}
 What happens?
 When might this be
desirable?
18
Copyright ©: University of Illinois CS 241 Staff
Semaphore Example 1
semaphore s = 2;
Pi {
while(1) {
semWait(s);
/* CS */
semSignal(s);
/* remainder */
}
}
 What happens?
 When might this be
desirable?
19
Copyright ©: University of Illinois CS 241 Staff
s = 2 1
× 0
× -1
×
Semaphore Example 1
semaphore s = 2;
Pi {
while(1) {
semWait(s);
/* CS */
semSignal(s);
/* remainder */
}
}
 What happens?
 Allows up to 2
processes to enter CS
 When might this be
desirable?
 Need up to 2 processes
inside CS
 e.g., limit number of
processes reading a var
 Be careful not to violate
mutual exclusion
inside CS!
20
Copyright ©: University of Illinois CS 241 Staff
Semaphore Example 2
semaphore s = 0;
Pi {
while(1) {
semWait(s);
/* CS */
semSignal(s);
/* remainder */
}
}
 What happens?
 When might this be
desirable?
21
Copyright ©: University of Illinois CS 241 Staff
Semaphore Example 2
semaphore s = 0;
Pi {
while(1) {
semWait(s);
/* CS */
semSignal(s);
/* remainder */
}
}
 What happens?
 When might this be
desirable?
22
Copyright ©: University of Illinois CS 241 Staff
s = 0 -1
× -2
× -3
×
Semaphore Example 2
semaphore s = 0;
Pi {
while(1) {
semWait(s);
/* CS */
semSignal(s);
/* remainder */
}
}
 What happens?
 No one can enter CS!
Ever!
 When might this be
desirable?
 Never!
23
Copyright ©: University of Illinois CS 241 Staff
Semaphore Example 3
semaphore s = 0;
P1 {
/* do some stuff */
semWait(s);
/* do some more stuff */
}
semaphore s; /* shared */
P2 {
/* do some stuff */
semSignal(s);
/* do some more stuff */
}
 What happens?
 When might this be desirable?
24
Copyright ©: University of Illinois CS 241 Staff
Semaphore Example 3
semaphore s = 0;
P1 {
/* do some stuff */
semWait(s);
/* do some more stuff */
}
semaphore s; /* shared */
P2 {
/* do some stuff */
semSignal(s);
/* do some more stuff */
}
 What happens?
 When might this be desirable?
25
Copyright ©: University of Illinois CS 241 Staff
s = 1 0
× 1
×
Semaphore Example 3
semaphore s = 0;
P1 {
/* do some stuff */
semWait(s);
/* do some more stuff */
}
semaphore s; /* shared */
P2 {
/* do some stuff */
semSignal(s);
/* do some more stuff */
}
 What happens?
 P1 waits until P2 signals
 if P2 signals first, P1 does not wait
 When might this be desirable?
 Having a process/thread wait for another process/thread
26
Copyright ©: University of Illinois CS 241 Staff
Semaphore Example 4
Process 1 executes:
while(1) {
semWait(S);
a;
semSignal(Q);
}
Process 2 executes:
while(1) {
semWait(Q);
b;
semSignal(S);
}
 Two processes
 Two semaphores: S and Q
 Protect two critical variables ‘a’ and ‘b’.
 What happens in the pseudocode if Semaphores S and
Q are initialized to 1 (or 0)?
27
Copyright ©: University of Illinois CS 241 Staff
Semaphore Example 4
Process 1 executes:
while(1) {
semWait(S);
a;
semSignal(Q);
}
Process 2 executes:
while(1) {
semWait(Q);
b;
semSignal(S);
}
28
Copyright ©: University of Illinois CS 241 Staff
S = 0
Q = 0
-1
×
-1
×
Semaphore Example 4
Process 1 executes:
while(1) {
semWait(S);
a;
semSignal(Q);
}
Process 2 executes:
while(1) {
semWait(Q);
b;
semSignal(S);
}
29
Copyright ©: University of Illinois CS 241 Staff
S = 1
Q = 1
0
×
0
×
1
×
1
×
0
×
0
×
Semaphore Example 4
Process 1 executes:
while(1) {
semWait(S);
a;
semSignal(Q);
}
Process 2 executes:
while(1) {
semWait(Q);
b;
semSignal(S);
}
30
Copyright ©: University of Illinois CS 241 Staff
S = 1
Q = 1
0
×
0
×
-1
×
1
×
0
×
0
×
1
×
Be careful!
Copyright ©: University of Illinois CS 241 Staff 31
semSignal(s);
critical_section();
semWait(s);
critical_section();
semSignal(s);
semWait(s);
critical_section();
semWait(s);
critical_section();
semWait(s);
semWait(s);
semWait(s);
critical_section();
semSignal(s);
semSignal(s);
1
2
3
4
5
Deadlock or Violation of Mutual Exclusion?
Be careful!
Copyright ©: University of Illinois CS 241 Staff 32
semSignal(s);
critical_section();
semWait(s);
critical_section();
semSignal(s);
semWait(s);
critical_section();
semWait(s);
critical_section();
semWait(s);
Mutual exclusion violation
Mutual exclusion violation
Possible deadlock
Certain deadlock!
semWait(s);
semWait(s);
critical_section();
semSignal(s);
semSignal(s);
Deadlock again!
1
2
3
4
5
POSIX Semaphores
 Named Semaphores
 Provides synchronization between unrelated process and
related process as well as between threads
 Kernel persistence
 System-wide and limited in number
 Uses sem_open
 Unnamed Semaphores
 Provides synchronization between threads and between
related processes
 Thread-shared or process-shared
 Uses sem_init
33
Copyright ©: University of Illinois CS 241 Staff
POSIX Semaphores
 Data type
 Semaphore is a variable of type sem_t
 Include <semaphore.h>
 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);
34
Copyright ©: University of Illinois CS 241 Staff
Unnamed Semaphores
#include <semaphore.h>
int sem_init(sem_t *sem, int pshared, unsigned
value);
 Initialize an unnamed semaphore
 Returns
 0 on success
 -1 on failure, sets errno
 Parameters
 sem:
 Target semaphore
 pshared:
 0: only threads of the creating process can use the semaphore
 Non-0: other processes can use the semaphore
 value:
 Initial value of the semaphore
You cannot make a copy of a
semaphore variable!!!
35
Copyright ©: University of Illinois CS 241 Staff
Sharing Semaphores
 Sharing semaphores between threads
within a process is easy, use pshared==0
 Forking a process creates copies of any
semaphore it has… sem_t semaphores are not
shared across processes
 A non-zero pshared allows any process
that can access the semaphore to use it
 Places the semaphore in the global (OS)
environment
36
Copyright ©: University of Illinois CS 241 Staff
sem_init can fail
 On failure
 sem_init returns -1 and sets errno
sem_t semA;
if (sem_init(&semA, 0, 1) == -1)
perror(“Failed to initialize semaphore semA”);
Value > sem_value_max
Resources exhausted
Insufficient privileges
EINVAL
ENOSPC
EPERM
cause
errno
37
Copyright ©: University of Illinois CS 241 Staff
Semaphore Operations
#include <semaphore.h>
int sem_destroy(sem_t *sem);
 Destroy an semaphore
 Returns
 0 on success
 -1 on failure, sets errno
 Parameters
 sem:
 Target semaphore
 Notes
 Can destroy a sem_t only once
 Destroying a destroyed semaphore gives undefined results
 Destroying a semaphore on which a thread is blocked gives undefined
results
38
Copyright ©: University of Illinois CS 241 Staff
Semaphore Operations
#include <semaphore.h>
int sem_post(sem_t *sem);
 Unlock a semaphore
 Returns
 0 on success
 -1 on failure, sets errno (== EINVAL if semaphore doesn’t exist)
 Parameters
 sem:
 Target semaphore
 sem > 0: no threads were blocked on this semaphore, the semaphore
value is incremented
 sem == 0: one blocked thread will be allowed to run
 Notes
 sem_post() is reentrant with respect to signals and may be invoked from
a signal-catching function
39
Copyright ©: University of Illinois CS 241 Staff
Semaphore Operations
#include <semaphore.h>
int sem_wait(sem_t *sem);
 Lock a semaphore
 Blocks if semaphore value is zero
 Returns
 0 on success
 -1 on failure, sets errno (== EINTR if interrupted by a signal)
 Parameters
 sem:
 Target semaphore
 sem > 0: thread acquires lock
 sem == 0: thread blocks
40
Copyright ©: University of Illinois CS 241 Staff
Semaphore Operations
#include <semaphore.h>
int sem_trywait(sem_t *sem);
 Test a semaphore’s current condition
 Does not block
 Returns
 0 on success
 -1 on failure, sets errno (== AGAIN if semaphore already locked)
 Parameters
 sem:
 Target semaphore
 sem > 0: thread acquires lock
 sem == 0: thread returns
41
Copyright ©: University of Illinois CS 241 Staff
Example: bank balance
 Want shared variable
balance to be protected
by semaphore when used
in:
 decshared – a function
that decrements the current
value of balance
 incshared – a function
that increments the
balance variable.
Copyright ©: University of Illinois CS 241 Staff 42
Example: bank balance
int decshared() {
while (sem_wait(&balance_sem) == -1)
if (errno != EINTR)
return -1;
balance--;
return sem_post(&balance_sem);
}
int incshared() {
while (sem_wait(&balance_sem) == -1)
if (errno != EINTR)
return -1;
balance++;
return sem_post(&balance_sem);
}
Copyright ©: University of Illinois CS 241 Staff 43
Summary
 Semaphores
 Semaphore implementation
 POSIX Semaphore
 Programming with semaphores
 Next time: solving real problems with
semaphores & more
44
Copyright ©: University of Illinois CS 241 Staff

Más contenido relacionado

Similar a 14-Semaphores.ppt

Mca ii os u-2 process management & communication
Mca  ii  os u-2 process management & communicationMca  ii  os u-2 process management & communication
Mca ii os u-2 process management & communicationRai University
 
Operating system 24 mutex locks and semaphores
Operating system 24 mutex locks and semaphoresOperating system 24 mutex locks and semaphores
Operating system 24 mutex locks and semaphoresVaibhav Khanna
 
Lecture18-19 (1).ppt
Lecture18-19 (1).pptLecture18-19 (1).ppt
Lecture18-19 (1).pptssuserf67e3a
 
Network security mannual (2)
Network security mannual (2)Network security mannual (2)
Network security mannual (2)Vivek Kumar Sinha
 
Advanced patterns in asynchronous programming
Advanced patterns in asynchronous programmingAdvanced patterns in asynchronous programming
Advanced patterns in asynchronous programmingMichael Arenzon
 
Gabriele Petronella - FP for front-end development: should you care? - Codemo...
Gabriele Petronella - FP for front-end development: should you care? - Codemo...Gabriele Petronella - FP for front-end development: should you care? - Codemo...
Gabriele Petronella - FP for front-end development: should you care? - Codemo...Codemotion
 
forwarder.java.txt java forwarder class waits for an in.docx
forwarder.java.txt java forwarder class waits for an in.docxforwarder.java.txt java forwarder class waits for an in.docx
forwarder.java.txt java forwarder class waits for an in.docxbudbarber38650
 
Locks? We Don't Need No Stinkin' Locks - Michael Barker
Locks? We Don't Need No Stinkin' Locks - Michael BarkerLocks? We Don't Need No Stinkin' Locks - Michael Barker
Locks? We Don't Need No Stinkin' Locks - Michael BarkerJAX London
 
Lock? We don't need no stinkin' locks!
Lock? We don't need no stinkin' locks!Lock? We don't need no stinkin' locks!
Lock? We don't need no stinkin' locks!Michael Barker
 
Verilog_Cheat_sheet_1672542963.pdf
Verilog_Cheat_sheet_1672542963.pdfVerilog_Cheat_sheet_1672542963.pdf
Verilog_Cheat_sheet_1672542963.pdfsagar414433
 
Verilog_Cheat_sheet_1672542963.pdf
Verilog_Cheat_sheet_1672542963.pdfVerilog_Cheat_sheet_1672542963.pdf
Verilog_Cheat_sheet_1672542963.pdfsagar414433
 
Lab Assignment 4 CSE330 Spring 2014 Skeleton Code for ex.docx
 Lab Assignment 4 CSE330 Spring 2014  Skeleton Code for ex.docx Lab Assignment 4 CSE330 Spring 2014  Skeleton Code for ex.docx
Lab Assignment 4 CSE330 Spring 2014 Skeleton Code for ex.docxMARRY7
 
dokumen.tips_verilog-basic-ppt.pdf
dokumen.tips_verilog-basic-ppt.pdfdokumen.tips_verilog-basic-ppt.pdf
dokumen.tips_verilog-basic-ppt.pdfVelmathi Saravanan
 
Computer notes data structures - 9
Computer notes   data structures - 9Computer notes   data structures - 9
Computer notes data structures - 9ecomputernotes
 
A tale of two RTC fuzzing approaches
A tale of two RTC fuzzing approachesA tale of two RTC fuzzing approaches
A tale of two RTC fuzzing approachesSandro Gauci
 
The Need for Async @ ScalaWorld
The Need for Async @ ScalaWorldThe Need for Async @ ScalaWorld
The Need for Async @ ScalaWorldKonrad Malawski
 

Similar a 14-Semaphores.ppt (20)

Lab
LabLab
Lab
 
CH05.pdf
CH05.pdfCH05.pdf
CH05.pdf
 
Mca ii os u-2 process management & communication
Mca  ii  os u-2 process management & communicationMca  ii  os u-2 process management & communication
Mca ii os u-2 process management & communication
 
Operating system 24 mutex locks and semaphores
Operating system 24 mutex locks and semaphoresOperating system 24 mutex locks and semaphores
Operating system 24 mutex locks and semaphores
 
Lecture18-19 (1).ppt
Lecture18-19 (1).pptLecture18-19 (1).ppt
Lecture18-19 (1).ppt
 
Network security mannual (2)
Network security mannual (2)Network security mannual (2)
Network security mannual (2)
 
Advanced patterns in asynchronous programming
Advanced patterns in asynchronous programmingAdvanced patterns in asynchronous programming
Advanced patterns in asynchronous programming
 
Gabriele Petronella - FP for front-end development: should you care? - Codemo...
Gabriele Petronella - FP for front-end development: should you care? - Codemo...Gabriele Petronella - FP for front-end development: should you care? - Codemo...
Gabriele Petronella - FP for front-end development: should you care? - Codemo...
 
forwarder.java.txt java forwarder class waits for an in.docx
forwarder.java.txt java forwarder class waits for an in.docxforwarder.java.txt java forwarder class waits for an in.docx
forwarder.java.txt java forwarder class waits for an in.docx
 
Locks? We Don't Need No Stinkin' Locks - Michael Barker
Locks? We Don't Need No Stinkin' Locks - Michael BarkerLocks? We Don't Need No Stinkin' Locks - Michael Barker
Locks? We Don't Need No Stinkin' Locks - Michael Barker
 
Lock? We don't need no stinkin' locks!
Lock? We don't need no stinkin' locks!Lock? We don't need no stinkin' locks!
Lock? We don't need no stinkin' locks!
 
Os3
Os3Os3
Os3
 
Verilog Cheat sheet-2 (1).pdf
Verilog Cheat sheet-2 (1).pdfVerilog Cheat sheet-2 (1).pdf
Verilog Cheat sheet-2 (1).pdf
 
Verilog_Cheat_sheet_1672542963.pdf
Verilog_Cheat_sheet_1672542963.pdfVerilog_Cheat_sheet_1672542963.pdf
Verilog_Cheat_sheet_1672542963.pdf
 
Verilog_Cheat_sheet_1672542963.pdf
Verilog_Cheat_sheet_1672542963.pdfVerilog_Cheat_sheet_1672542963.pdf
Verilog_Cheat_sheet_1672542963.pdf
 
Lab Assignment 4 CSE330 Spring 2014 Skeleton Code for ex.docx
 Lab Assignment 4 CSE330 Spring 2014  Skeleton Code for ex.docx Lab Assignment 4 CSE330 Spring 2014  Skeleton Code for ex.docx
Lab Assignment 4 CSE330 Spring 2014 Skeleton Code for ex.docx
 
dokumen.tips_verilog-basic-ppt.pdf
dokumen.tips_verilog-basic-ppt.pdfdokumen.tips_verilog-basic-ppt.pdf
dokumen.tips_verilog-basic-ppt.pdf
 
Computer notes data structures - 9
Computer notes   data structures - 9Computer notes   data structures - 9
Computer notes data structures - 9
 
A tale of two RTC fuzzing approaches
A tale of two RTC fuzzing approachesA tale of two RTC fuzzing approaches
A tale of two RTC fuzzing approaches
 
The Need for Async @ ScalaWorld
The Need for Async @ ScalaWorldThe Need for Async @ ScalaWorld
The Need for Async @ ScalaWorld
 

Más de KamranAli649587

UNIT-2-liang-barsky-clipping-algorithm-KM.pdf
UNIT-2-liang-barsky-clipping-algorithm-KM.pdfUNIT-2-liang-barsky-clipping-algorithm-KM.pdf
UNIT-2-liang-barsky-clipping-algorithm-KM.pdfKamranAli649587
 
Data design and analysis of computing tools
Data design and analysis of computing toolsData design and analysis of computing tools
Data design and analysis of computing toolsKamranAli649587
 
graphs data structure and algorithm link list
graphs data structure and algorithm link listgraphs data structure and algorithm link list
graphs data structure and algorithm link listKamranAli649587
 
lecture10 date structure types of graph and terminology
lecture10 date structure types of graph and terminologylecture10 date structure types of graph and terminology
lecture10 date structure types of graph and terminologyKamranAli649587
 
Encoder-and-decoder.pptx
Encoder-and-decoder.pptxEncoder-and-decoder.pptx
Encoder-and-decoder.pptxKamranAli649587
 
Radio propagation model...pptx
Radio propagation model...pptxRadio propagation model...pptx
Radio propagation model...pptxKamranAli649587
 
Loops_and_FunctionsWeek4_0.ppt
Loops_and_FunctionsWeek4_0.pptLoops_and_FunctionsWeek4_0.ppt
Loops_and_FunctionsWeek4_0.pptKamranAli649587
 
Lecture+06-TypesVars.ppt
Lecture+06-TypesVars.pptLecture+06-TypesVars.ppt
Lecture+06-TypesVars.pptKamranAli649587
 
radiopropagation-140328202308-phpapp01.pdf
radiopropagation-140328202308-phpapp01.pdfradiopropagation-140328202308-phpapp01.pdf
radiopropagation-140328202308-phpapp01.pdfKamranAli649587
 
Week11-EvaluationMethods.ppt
Week11-EvaluationMethods.pptWeek11-EvaluationMethods.ppt
Week11-EvaluationMethods.pptKamranAli649587
 
Week6-Sectionsofapaper.ppt
Week6-Sectionsofapaper.pptWeek6-Sectionsofapaper.ppt
Week6-Sectionsofapaper.pptKamranAli649587
 

Más de KamranAli649587 (20)

UNIT-2-liang-barsky-clipping-algorithm-KM.pdf
UNIT-2-liang-barsky-clipping-algorithm-KM.pdfUNIT-2-liang-barsky-clipping-algorithm-KM.pdf
UNIT-2-liang-barsky-clipping-algorithm-KM.pdf
 
Data design and analysis of computing tools
Data design and analysis of computing toolsData design and analysis of computing tools
Data design and analysis of computing tools
 
graphs data structure and algorithm link list
graphs data structure and algorithm link listgraphs data structure and algorithm link list
graphs data structure and algorithm link list
 
lecture10 date structure types of graph and terminology
lecture10 date structure types of graph and terminologylecture10 date structure types of graph and terminology
lecture10 date structure types of graph and terminology
 
Encoder-and-decoder.pptx
Encoder-and-decoder.pptxEncoder-and-decoder.pptx
Encoder-and-decoder.pptx
 
Radio propagation model...pptx
Radio propagation model...pptxRadio propagation model...pptx
Radio propagation model...pptx
 
Loops_and_FunctionsWeek4_0.ppt
Loops_and_FunctionsWeek4_0.pptLoops_and_FunctionsWeek4_0.ppt
Loops_and_FunctionsWeek4_0.ppt
 
Lecture+06-TypesVars.ppt
Lecture+06-TypesVars.pptLecture+06-TypesVars.ppt
Lecture+06-TypesVars.ppt
 
C++InputOutput.PPT
C++InputOutput.PPTC++InputOutput.PPT
C++InputOutput.PPT
 
radiopropagation-140328202308-phpapp01.pdf
radiopropagation-140328202308-phpapp01.pdfradiopropagation-140328202308-phpapp01.pdf
radiopropagation-140328202308-phpapp01.pdf
 
cluster.pptx
cluster.pptxcluster.pptx
cluster.pptx
 
Week11-EvaluationMethods.ppt
Week11-EvaluationMethods.pptWeek11-EvaluationMethods.ppt
Week11-EvaluationMethods.ppt
 
Week6-Sectionsofapaper.ppt
Week6-Sectionsofapaper.pptWeek6-Sectionsofapaper.ppt
Week6-Sectionsofapaper.ppt
 
null-13.pdf
null-13.pdfnull-13.pdf
null-13.pdf
 
Reaches
ReachesReaches
Reaches
 
null-6.pdf
null-6.pdfnull-6.pdf
null-6.pdf
 
null-1.pptx
null-1.pptxnull-1.pptx
null-1.pptx
 
Lect-01.ppt
Lect-01.pptLect-01.ppt
Lect-01.ppt
 
Db_05.ppt
Db_05.pptDb_05.ppt
Db_05.ppt
 
ch7.ppt
ch7.pptch7.ppt
ch7.ppt
 

Último

Top 5 Best Data Analytics Courses In Queens
Top 5 Best Data Analytics Courses In QueensTop 5 Best Data Analytics Courses In Queens
Top 5 Best Data Analytics Courses In Queensdataanalyticsqueen03
 
NLP Data Science Project Presentation:Predicting Heart Disease with NLP Data ...
NLP Data Science Project Presentation:Predicting Heart Disease with NLP Data ...NLP Data Science Project Presentation:Predicting Heart Disease with NLP Data ...
NLP Data Science Project Presentation:Predicting Heart Disease with NLP Data ...Boston Institute of Analytics
 
Predicting Salary Using Data Science: A Comprehensive Analysis.pdf
Predicting Salary Using Data Science: A Comprehensive Analysis.pdfPredicting Salary Using Data Science: A Comprehensive Analysis.pdf
Predicting Salary Using Data Science: A Comprehensive Analysis.pdfBoston Institute of Analytics
 
PKS-TGC-1084-630 - Stage 1 Proposal.pptx
PKS-TGC-1084-630 - Stage 1 Proposal.pptxPKS-TGC-1084-630 - Stage 1 Proposal.pptx
PKS-TGC-1084-630 - Stage 1 Proposal.pptxPramod Kumar Srivastava
 
RS 9000 Call In girls Dwarka Mor (DELHI)⇛9711147426🔝Delhi
RS 9000 Call In girls Dwarka Mor (DELHI)⇛9711147426🔝DelhiRS 9000 Call In girls Dwarka Mor (DELHI)⇛9711147426🔝Delhi
RS 9000 Call In girls Dwarka Mor (DELHI)⇛9711147426🔝Delhijennyeacort
 
Identifying Appropriate Test Statistics Involving Population Mean
Identifying Appropriate Test Statistics Involving Population MeanIdentifying Appropriate Test Statistics Involving Population Mean
Identifying Appropriate Test Statistics Involving Population MeanMYRABACSAFRA2
 
Multiple time frame trading analysis -brianshannon.pdf
Multiple time frame trading analysis -brianshannon.pdfMultiple time frame trading analysis -brianshannon.pdf
Multiple time frame trading analysis -brianshannon.pdfchwongval
 
Heart Disease Classification Report: A Data Analysis Project
Heart Disease Classification Report: A Data Analysis ProjectHeart Disease Classification Report: A Data Analysis Project
Heart Disease Classification Report: A Data Analysis ProjectBoston Institute of Analytics
 
20240419 - Measurecamp Amsterdam - SAM.pdf
20240419 - Measurecamp Amsterdam - SAM.pdf20240419 - Measurecamp Amsterdam - SAM.pdf
20240419 - Measurecamp Amsterdam - SAM.pdfHuman37
 
毕业文凭制作#回国入职#diploma#degree澳洲中央昆士兰大学毕业证成绩单pdf电子版制作修改#毕业文凭制作#回国入职#diploma#degree
毕业文凭制作#回国入职#diploma#degree澳洲中央昆士兰大学毕业证成绩单pdf电子版制作修改#毕业文凭制作#回国入职#diploma#degree毕业文凭制作#回国入职#diploma#degree澳洲中央昆士兰大学毕业证成绩单pdf电子版制作修改#毕业文凭制作#回国入职#diploma#degree
毕业文凭制作#回国入职#diploma#degree澳洲中央昆士兰大学毕业证成绩单pdf电子版制作修改#毕业文凭制作#回国入职#diploma#degreeyuu sss
 
Predictive Analysis for Loan Default Presentation : Data Analysis Project PPT
Predictive Analysis for Loan Default  Presentation : Data Analysis Project PPTPredictive Analysis for Loan Default  Presentation : Data Analysis Project PPT
Predictive Analysis for Loan Default Presentation : Data Analysis Project PPTBoston Institute of Analytics
 
2006_GasProcessing_HB (1).pdf HYDROCARBON PROCESSING
2006_GasProcessing_HB (1).pdf HYDROCARBON PROCESSING2006_GasProcessing_HB (1).pdf HYDROCARBON PROCESSING
2006_GasProcessing_HB (1).pdf HYDROCARBON PROCESSINGmarianagonzalez07
 
Consent & Privacy Signals on Google *Pixels* - MeasureCamp Amsterdam 2024
Consent & Privacy Signals on Google *Pixels* - MeasureCamp Amsterdam 2024Consent & Privacy Signals on Google *Pixels* - MeasureCamp Amsterdam 2024
Consent & Privacy Signals on Google *Pixels* - MeasureCamp Amsterdam 2024thyngster
 
ASML's Taxonomy Adventure by Daniel Canter
ASML's Taxonomy Adventure by Daniel CanterASML's Taxonomy Adventure by Daniel Canter
ASML's Taxonomy Adventure by Daniel Cantervoginip
 
Saket, (-DELHI )+91-9654467111-(=)CHEAP Call Girls in Escorts Service Saket C...
Saket, (-DELHI )+91-9654467111-(=)CHEAP Call Girls in Escorts Service Saket C...Saket, (-DELHI )+91-9654467111-(=)CHEAP Call Girls in Escorts Service Saket C...
Saket, (-DELHI )+91-9654467111-(=)CHEAP Call Girls in Escorts Service Saket C...Sapana Sha
 
Easter Eggs From Star Wars and in cars 1 and 2
Easter Eggs From Star Wars and in cars 1 and 2Easter Eggs From Star Wars and in cars 1 and 2
Easter Eggs From Star Wars and in cars 1 and 217djon017
 
办理学位证纽约大学毕业证(NYU毕业证书)原版一比一
办理学位证纽约大学毕业证(NYU毕业证书)原版一比一办理学位证纽约大学毕业证(NYU毕业证书)原版一比一
办理学位证纽约大学毕业证(NYU毕业证书)原版一比一fhwihughh
 
Generative AI for Social Good at Open Data Science East 2024
Generative AI for Social Good at Open Data Science East 2024Generative AI for Social Good at Open Data Science East 2024
Generative AI for Social Good at Open Data Science East 2024Colleen Farrelly
 
Indian Call Girls in Abu Dhabi O5286O24O8 Call Girls in Abu Dhabi By Independ...
Indian Call Girls in Abu Dhabi O5286O24O8 Call Girls in Abu Dhabi By Independ...Indian Call Girls in Abu Dhabi O5286O24O8 Call Girls in Abu Dhabi By Independ...
Indian Call Girls in Abu Dhabi O5286O24O8 Call Girls in Abu Dhabi By Independ...dajasot375
 

Último (20)

Top 5 Best Data Analytics Courses In Queens
Top 5 Best Data Analytics Courses In QueensTop 5 Best Data Analytics Courses In Queens
Top 5 Best Data Analytics Courses In Queens
 
NLP Data Science Project Presentation:Predicting Heart Disease with NLP Data ...
NLP Data Science Project Presentation:Predicting Heart Disease with NLP Data ...NLP Data Science Project Presentation:Predicting Heart Disease with NLP Data ...
NLP Data Science Project Presentation:Predicting Heart Disease with NLP Data ...
 
Predicting Salary Using Data Science: A Comprehensive Analysis.pdf
Predicting Salary Using Data Science: A Comprehensive Analysis.pdfPredicting Salary Using Data Science: A Comprehensive Analysis.pdf
Predicting Salary Using Data Science: A Comprehensive Analysis.pdf
 
PKS-TGC-1084-630 - Stage 1 Proposal.pptx
PKS-TGC-1084-630 - Stage 1 Proposal.pptxPKS-TGC-1084-630 - Stage 1 Proposal.pptx
PKS-TGC-1084-630 - Stage 1 Proposal.pptx
 
RS 9000 Call In girls Dwarka Mor (DELHI)⇛9711147426🔝Delhi
RS 9000 Call In girls Dwarka Mor (DELHI)⇛9711147426🔝DelhiRS 9000 Call In girls Dwarka Mor (DELHI)⇛9711147426🔝Delhi
RS 9000 Call In girls Dwarka Mor (DELHI)⇛9711147426🔝Delhi
 
Identifying Appropriate Test Statistics Involving Population Mean
Identifying Appropriate Test Statistics Involving Population MeanIdentifying Appropriate Test Statistics Involving Population Mean
Identifying Appropriate Test Statistics Involving Population Mean
 
Multiple time frame trading analysis -brianshannon.pdf
Multiple time frame trading analysis -brianshannon.pdfMultiple time frame trading analysis -brianshannon.pdf
Multiple time frame trading analysis -brianshannon.pdf
 
Heart Disease Classification Report: A Data Analysis Project
Heart Disease Classification Report: A Data Analysis ProjectHeart Disease Classification Report: A Data Analysis Project
Heart Disease Classification Report: A Data Analysis Project
 
20240419 - Measurecamp Amsterdam - SAM.pdf
20240419 - Measurecamp Amsterdam - SAM.pdf20240419 - Measurecamp Amsterdam - SAM.pdf
20240419 - Measurecamp Amsterdam - SAM.pdf
 
毕业文凭制作#回国入职#diploma#degree澳洲中央昆士兰大学毕业证成绩单pdf电子版制作修改#毕业文凭制作#回国入职#diploma#degree
毕业文凭制作#回国入职#diploma#degree澳洲中央昆士兰大学毕业证成绩单pdf电子版制作修改#毕业文凭制作#回国入职#diploma#degree毕业文凭制作#回国入职#diploma#degree澳洲中央昆士兰大学毕业证成绩单pdf电子版制作修改#毕业文凭制作#回国入职#diploma#degree
毕业文凭制作#回国入职#diploma#degree澳洲中央昆士兰大学毕业证成绩单pdf电子版制作修改#毕业文凭制作#回国入职#diploma#degree
 
Predictive Analysis for Loan Default Presentation : Data Analysis Project PPT
Predictive Analysis for Loan Default  Presentation : Data Analysis Project PPTPredictive Analysis for Loan Default  Presentation : Data Analysis Project PPT
Predictive Analysis for Loan Default Presentation : Data Analysis Project PPT
 
2006_GasProcessing_HB (1).pdf HYDROCARBON PROCESSING
2006_GasProcessing_HB (1).pdf HYDROCARBON PROCESSING2006_GasProcessing_HB (1).pdf HYDROCARBON PROCESSING
2006_GasProcessing_HB (1).pdf HYDROCARBON PROCESSING
 
Consent & Privacy Signals on Google *Pixels* - MeasureCamp Amsterdam 2024
Consent & Privacy Signals on Google *Pixels* - MeasureCamp Amsterdam 2024Consent & Privacy Signals on Google *Pixels* - MeasureCamp Amsterdam 2024
Consent & Privacy Signals on Google *Pixels* - MeasureCamp Amsterdam 2024
 
ASML's Taxonomy Adventure by Daniel Canter
ASML's Taxonomy Adventure by Daniel CanterASML's Taxonomy Adventure by Daniel Canter
ASML's Taxonomy Adventure by Daniel Canter
 
Saket, (-DELHI )+91-9654467111-(=)CHEAP Call Girls in Escorts Service Saket C...
Saket, (-DELHI )+91-9654467111-(=)CHEAP Call Girls in Escorts Service Saket C...Saket, (-DELHI )+91-9654467111-(=)CHEAP Call Girls in Escorts Service Saket C...
Saket, (-DELHI )+91-9654467111-(=)CHEAP Call Girls in Escorts Service Saket C...
 
E-Commerce Order PredictionShraddha Kamble.pptx
E-Commerce Order PredictionShraddha Kamble.pptxE-Commerce Order PredictionShraddha Kamble.pptx
E-Commerce Order PredictionShraddha Kamble.pptx
 
Easter Eggs From Star Wars and in cars 1 and 2
Easter Eggs From Star Wars and in cars 1 and 2Easter Eggs From Star Wars and in cars 1 and 2
Easter Eggs From Star Wars and in cars 1 and 2
 
办理学位证纽约大学毕业证(NYU毕业证书)原版一比一
办理学位证纽约大学毕业证(NYU毕业证书)原版一比一办理学位证纽约大学毕业证(NYU毕业证书)原版一比一
办理学位证纽约大学毕业证(NYU毕业证书)原版一比一
 
Generative AI for Social Good at Open Data Science East 2024
Generative AI for Social Good at Open Data Science East 2024Generative AI for Social Good at Open Data Science East 2024
Generative AI for Social Good at Open Data Science East 2024
 
Indian Call Girls in Abu Dhabi O5286O24O8 Call Girls in Abu Dhabi By Independ...
Indian Call Girls in Abu Dhabi O5286O24O8 Call Girls in Abu Dhabi By Independ...Indian Call Girls in Abu Dhabi O5286O24O8 Call Girls in Abu Dhabi By Independ...
Indian Call Girls in Abu Dhabi O5286O24O8 Call Girls in Abu Dhabi By Independ...
 

14-Semaphores.ppt

  • 1. Copyright ©: University of Illinois CS 241 Staff 1 Synchronization and Semaphores
  • 2. Discussion  In uni-processors  Concurrent processes cannot be overlapped, only interleaved  A process runs until it invokes a system call, or is interrupted  To guarantee mutual exclusion, hardware support could help by allowing the disabling of interrupts While(true) { /* disable interrupts */ /* critical section */ /* enable interrupts */ /* remainder */ }  What’s the problem with this solution? 2 Copyright ©: University of Illinois CS 241 Staff
  • 3. Discussion  In multi-processors  Several processors share memory  Processors behave independently in a peer relationship  Interrupt disabling will not work  We need hardware support where access to a memory location excludes any other access to that same location  The hardware support is based on execution of multiple instructions atomically (test and set) 3 Copyright ©: University of Illinois CS 241 Staff
  • 4. Test and Set Instruction boolean Test_And_Set(boolean* lock) atomic { boolean initial; initial = *lock; *lock = true; return initial; } Note: this is more accurate than the textbook version atomic = executed in a single shot without any interruption 4 Copyright ©: University of Illinois CS 241 Staff
  • 5. Using Test_And_Set for Mutual Exclusion Pi { while(1) { while(Test_And_Set(lock)) { /* spin */ } /* Critical Section */ lock =0; /* remainder */ } } void main () { lock = 0; parbegin(P1,…,Pn); } 5 What's the problem? Copyright ©: University of Illinois CS 241 Staff
  • 6. Using Test_And_Set for Mutual Exclusion Pi { while(1) { while(Test_And_Set(lock)) { /* spin */ } /* Critical Section */ lock =0; /* remainder */ } } void main () { lock = 0; parbegin(P1,…,Pn); } Works, but has performance loss because of busy waiting. 6 Copyright ©: University of Illinois CS 241 Staff
  • 7. Semaphores  Fundamental Principle:  Two or more processes want to cooperate by means of simple signals  Special Variable: semaphore s  A special kind of “int” variable  Can’t just modify or set or increment or decrement it 7 Copyright ©: University of Illinois CS 241 Staff
  • 8. Semaphores  Before entering critical section  semWait(s)  Receive signal via semaphore s  “down” on the semaphore  Also: P – proberen  After finishing critical section  semSignal(s)  Transmit signal via semaphore s  “up” on the semaphore  Also: V – verhogen  Implementation requirements  semSignal and semWait must be atomic 8 Copyright ©: University of Illinois CS 241 Staff
  • 9. Semaphores vs. Test_and_Set Semaphore semaphore s = 1; Pi { while(1) { semWait(s); /* Critical Section */ semSignal(s); /* remainder */ } } Test_and_Set lock = 0; Pi { while(1) { while(Test_And_Set(lock)); /* Critical Section */ lock =0; /* remainder */ } } 9 Copyright ©: University of Illinois CS 241 Staff  Avoid busy waiting by suspending  Block if s == False  Wakeup on signal (s = True)
  • 10. Inside a Semaphore  Requirement  No two processes can execute wait() and signal() on the same semaphore at the same time!  Critical section  wait() and signal() code  Now have busy waiting in critical section implementation  Implementation code is short  Little busy waiting if critical section rarely occupied  Bad for applications may spend lots of time in critical sections Copyright ©: University of Illinois CS 241 Staff 10
  • 11. Inside a Semaphore  Add a waiting queue  Multiple process waiting on s  Wakeup one of the blocked processes upon getting a signal  Semaphore data structure typedef struct { int count; queueType queue; /* queue for procs. waiting on s */ } SEMAPHORE; 11 Copyright ©: University of Illinois CS 241 Staff
  • 12. Binary Semaphores typedef struct bsemaphore { enum {0,1} value; queueType queue; } BSEMAPHORE; void semSignalB (bsemaphore s) { if (s.queue is empty()) s.value = 1; else { remove P from s.queue; place P on ready list; } } void semWaitB(bsemaphore s) { if (s.value == 1) s.value = 0; else { place P in s.queue; block P; } } 12 Copyright ©: University of Illinois CS 241 Staff
  • 13. General Semaphore typedef struct { int count; queueType queue; } SEMAPHORE; void semSignal(semaphore s) { s.count++; if (s.count ≤ 0) { remove P from s.queue; place P on ready list; } } void semWait(semaphore s) { s.count--; if (s.count < 0) { place P in s.queue; block P; } } 13 Copyright ©: University of Illinois CS 241 Staff
  • 14. void semWait(semaphore s) { s.count--; if (s.count < 0) { place P in s.queue; block P; } } Making the operations atomic  Isn’t this exactly what semaphores were trying to solve? Are we stuck?!  Solution: resort to test-and-set 14 typedef struct { boolean lock; int count; queueType queue; } SEMAPHORE; void semWait(semaphore s) { while (test_and_set(lock)) { } s.count--; if (s.count < 0) { place P in s.queue; block P; } lock = 0; } Copyright ©: University of Illinois CS 241 Staff
  • 15. Making the operations atomic  Busy-waiting again!  Then how are semaphores better than just using test_and_set? 15 void semWait(semaphore s) { while (test_and_set(lock)) { } s.count--; if (s.count < 0) { place P in s.queue; block P; } lock = 0; }  T&S: busy-wait during critical section  Sem.: busy-wait just during semWait, semSignal: very short operations! Copyright ©: University of Illinois CS 241 Staff
  • 16. Mutual Exclusion Using Semaphores semaphore s = 1; Pi { while(1) { semWait(s); /* Critical Section */ semSignal(s); /* remainder */ } } 16 Copyright ©: University of Illinois CS 241 Staff
  • 17. Value of Semaphore lock Queue A semWait(lock) 0 1 semWait(lock) B -1 semSignal(lock) 0 semSignal(lock) 1 Process Process Critical Region Normal Execution Blocked on semaphore lock B 17 Copyright ©: University of Illinois CS 241 Staff
  • 18. Semaphore Example 1 semaphore s = 2; Pi { while(1) { semWait(s); /* CS */ semSignal(s); /* remainder */ } }  What happens?  When might this be desirable? 18 Copyright ©: University of Illinois CS 241 Staff
  • 19. Semaphore Example 1 semaphore s = 2; Pi { while(1) { semWait(s); /* CS */ semSignal(s); /* remainder */ } }  What happens?  When might this be desirable? 19 Copyright ©: University of Illinois CS 241 Staff s = 2 1 × 0 × -1 ×
  • 20. Semaphore Example 1 semaphore s = 2; Pi { while(1) { semWait(s); /* CS */ semSignal(s); /* remainder */ } }  What happens?  Allows up to 2 processes to enter CS  When might this be desirable?  Need up to 2 processes inside CS  e.g., limit number of processes reading a var  Be careful not to violate mutual exclusion inside CS! 20 Copyright ©: University of Illinois CS 241 Staff
  • 21. Semaphore Example 2 semaphore s = 0; Pi { while(1) { semWait(s); /* CS */ semSignal(s); /* remainder */ } }  What happens?  When might this be desirable? 21 Copyright ©: University of Illinois CS 241 Staff
  • 22. Semaphore Example 2 semaphore s = 0; Pi { while(1) { semWait(s); /* CS */ semSignal(s); /* remainder */ } }  What happens?  When might this be desirable? 22 Copyright ©: University of Illinois CS 241 Staff s = 0 -1 × -2 × -3 ×
  • 23. Semaphore Example 2 semaphore s = 0; Pi { while(1) { semWait(s); /* CS */ semSignal(s); /* remainder */ } }  What happens?  No one can enter CS! Ever!  When might this be desirable?  Never! 23 Copyright ©: University of Illinois CS 241 Staff
  • 24. Semaphore Example 3 semaphore s = 0; P1 { /* do some stuff */ semWait(s); /* do some more stuff */ } semaphore s; /* shared */ P2 { /* do some stuff */ semSignal(s); /* do some more stuff */ }  What happens?  When might this be desirable? 24 Copyright ©: University of Illinois CS 241 Staff
  • 25. Semaphore Example 3 semaphore s = 0; P1 { /* do some stuff */ semWait(s); /* do some more stuff */ } semaphore s; /* shared */ P2 { /* do some stuff */ semSignal(s); /* do some more stuff */ }  What happens?  When might this be desirable? 25 Copyright ©: University of Illinois CS 241 Staff s = 1 0 × 1 ×
  • 26. Semaphore Example 3 semaphore s = 0; P1 { /* do some stuff */ semWait(s); /* do some more stuff */ } semaphore s; /* shared */ P2 { /* do some stuff */ semSignal(s); /* do some more stuff */ }  What happens?  P1 waits until P2 signals  if P2 signals first, P1 does not wait  When might this be desirable?  Having a process/thread wait for another process/thread 26 Copyright ©: University of Illinois CS 241 Staff
  • 27. Semaphore Example 4 Process 1 executes: while(1) { semWait(S); a; semSignal(Q); } Process 2 executes: while(1) { semWait(Q); b; semSignal(S); }  Two processes  Two semaphores: S and Q  Protect two critical variables ‘a’ and ‘b’.  What happens in the pseudocode if Semaphores S and Q are initialized to 1 (or 0)? 27 Copyright ©: University of Illinois CS 241 Staff
  • 28. Semaphore Example 4 Process 1 executes: while(1) { semWait(S); a; semSignal(Q); } Process 2 executes: while(1) { semWait(Q); b; semSignal(S); } 28 Copyright ©: University of Illinois CS 241 Staff S = 0 Q = 0 -1 × -1 ×
  • 29. Semaphore Example 4 Process 1 executes: while(1) { semWait(S); a; semSignal(Q); } Process 2 executes: while(1) { semWait(Q); b; semSignal(S); } 29 Copyright ©: University of Illinois CS 241 Staff S = 1 Q = 1 0 × 0 × 1 × 1 × 0 × 0 ×
  • 30. Semaphore Example 4 Process 1 executes: while(1) { semWait(S); a; semSignal(Q); } Process 2 executes: while(1) { semWait(Q); b; semSignal(S); } 30 Copyright ©: University of Illinois CS 241 Staff S = 1 Q = 1 0 × 0 × -1 × 1 × 0 × 0 × 1 ×
  • 31. Be careful! Copyright ©: University of Illinois CS 241 Staff 31 semSignal(s); critical_section(); semWait(s); critical_section(); semSignal(s); semWait(s); critical_section(); semWait(s); critical_section(); semWait(s); semWait(s); semWait(s); critical_section(); semSignal(s); semSignal(s); 1 2 3 4 5 Deadlock or Violation of Mutual Exclusion?
  • 32. Be careful! Copyright ©: University of Illinois CS 241 Staff 32 semSignal(s); critical_section(); semWait(s); critical_section(); semSignal(s); semWait(s); critical_section(); semWait(s); critical_section(); semWait(s); Mutual exclusion violation Mutual exclusion violation Possible deadlock Certain deadlock! semWait(s); semWait(s); critical_section(); semSignal(s); semSignal(s); Deadlock again! 1 2 3 4 5
  • 33. POSIX Semaphores  Named Semaphores  Provides synchronization between unrelated process and related process as well as between threads  Kernel persistence  System-wide and limited in number  Uses sem_open  Unnamed Semaphores  Provides synchronization between threads and between related processes  Thread-shared or process-shared  Uses sem_init 33 Copyright ©: University of Illinois CS 241 Staff
  • 34. POSIX Semaphores  Data type  Semaphore is a variable of type sem_t  Include <semaphore.h>  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); 34 Copyright ©: University of Illinois CS 241 Staff
  • 35. Unnamed Semaphores #include <semaphore.h> int sem_init(sem_t *sem, int pshared, unsigned value);  Initialize an unnamed semaphore  Returns  0 on success  -1 on failure, sets errno  Parameters  sem:  Target semaphore  pshared:  0: only threads of the creating process can use the semaphore  Non-0: other processes can use the semaphore  value:  Initial value of the semaphore You cannot make a copy of a semaphore variable!!! 35 Copyright ©: University of Illinois CS 241 Staff
  • 36. Sharing Semaphores  Sharing semaphores between threads within a process is easy, use pshared==0  Forking a process creates copies of any semaphore it has… sem_t semaphores are not shared across processes  A non-zero pshared allows any process that can access the semaphore to use it  Places the semaphore in the global (OS) environment 36 Copyright ©: University of Illinois CS 241 Staff
  • 37. sem_init can fail  On failure  sem_init returns -1 and sets errno sem_t semA; if (sem_init(&semA, 0, 1) == -1) perror(“Failed to initialize semaphore semA”); Value > sem_value_max Resources exhausted Insufficient privileges EINVAL ENOSPC EPERM cause errno 37 Copyright ©: University of Illinois CS 241 Staff
  • 38. Semaphore Operations #include <semaphore.h> int sem_destroy(sem_t *sem);  Destroy an semaphore  Returns  0 on success  -1 on failure, sets errno  Parameters  sem:  Target semaphore  Notes  Can destroy a sem_t only once  Destroying a destroyed semaphore gives undefined results  Destroying a semaphore on which a thread is blocked gives undefined results 38 Copyright ©: University of Illinois CS 241 Staff
  • 39. Semaphore Operations #include <semaphore.h> int sem_post(sem_t *sem);  Unlock a semaphore  Returns  0 on success  -1 on failure, sets errno (== EINVAL if semaphore doesn’t exist)  Parameters  sem:  Target semaphore  sem > 0: no threads were blocked on this semaphore, the semaphore value is incremented  sem == 0: one blocked thread will be allowed to run  Notes  sem_post() is reentrant with respect to signals and may be invoked from a signal-catching function 39 Copyright ©: University of Illinois CS 241 Staff
  • 40. Semaphore Operations #include <semaphore.h> int sem_wait(sem_t *sem);  Lock a semaphore  Blocks if semaphore value is zero  Returns  0 on success  -1 on failure, sets errno (== EINTR if interrupted by a signal)  Parameters  sem:  Target semaphore  sem > 0: thread acquires lock  sem == 0: thread blocks 40 Copyright ©: University of Illinois CS 241 Staff
  • 41. Semaphore Operations #include <semaphore.h> int sem_trywait(sem_t *sem);  Test a semaphore’s current condition  Does not block  Returns  0 on success  -1 on failure, sets errno (== AGAIN if semaphore already locked)  Parameters  sem:  Target semaphore  sem > 0: thread acquires lock  sem == 0: thread returns 41 Copyright ©: University of Illinois CS 241 Staff
  • 42. Example: bank balance  Want shared variable balance to be protected by semaphore when used in:  decshared – a function that decrements the current value of balance  incshared – a function that increments the balance variable. Copyright ©: University of Illinois CS 241 Staff 42
  • 43. Example: bank balance int decshared() { while (sem_wait(&balance_sem) == -1) if (errno != EINTR) return -1; balance--; return sem_post(&balance_sem); } int incshared() { while (sem_wait(&balance_sem) == -1) if (errno != EINTR) return -1; balance++; return sem_post(&balance_sem); } Copyright ©: University of Illinois CS 241 Staff 43
  • 44. Summary  Semaphores  Semaphore implementation  POSIX Semaphore  Programming with semaphores  Next time: solving real problems with semaphores & more 44 Copyright ©: University of Illinois CS 241 Staff