Professional Documents
Culture Documents
Prepared by:
Lm Ngc n
Tng Trng Tuyn
3/31/16
Semaphores
Contents
3/31/16
1.
Definition
2.
Operators
3.
Typical Use
4.
Implementation in T-kernel
Semaphores
1. Definition
A semaphore (sometimes called
a semaphore token) is a kernel
object that one or more threads
of execution can acquire or
release for the purposes of
synchronization or mutual
exclusion
3/31/16
Semaphores
1. Definition
3/31/16
Semaphores
1. Definition
3 groups:
Binary Semaphores
Counting Semaphores
Mutual Exclusion Semaphores
3/31/16
Semaphores
3/31/16
Semaphores
3/31/16
Semaphores
3/31/16
Semaphores
3/31/16
Semaphores
3/31/16
Semaphores
10
3/31/16
Semaphores
11
3/31/16
Semaphores
12
3/31/16
Semaphores
13
3/31/16
Semaphores
14
3/31/16
Semaphores
15
2. Operator
creating and deleting semaphores
acquiring and releasing
semaphores
clearing a semaphores taskwaiting list
getting semaphore information.
3/31/16
Semaphores
16
3. Typical Use
Wait-and-Signal Synchronization
3/31/16
Semaphores
17
3. Typical Use
Wait-and-Signal Synchronization
tWaitTask ( ) {
:
Acquire binary semaphore token
:
}
tSignalTask ( ) {
:
Release binary semaphore token
:
}
3/31/16
Semaphores
18
3. Typical Use
Multiple-Task Wait-and-Signal
Synchronization
3/31/16
Semaphores
19
3. Typical Use
Multiple-Task Wait-and-Signal
Synchronization
tWaitTask () {
:
Do some processing specific to task Acquire binary semaphore
token
:
}
tSignalTask () {
:
Do some processing Flush binary semaphore's task-waiting list
:
}
3/31/16
Semaphores
20
3. Typical Use
Credit-Tracking Synchronization
3/31/16
Semaphores
21
3. Typical Use
Credit-Tracking Synchronization
tWaitTask () {
:
Acquire counting semaphore token
:
}
tSignalTask () {
:
Release counting semaphore token
:
}
3/31/16
Semaphores
22
3. Typical Use
Single Shared-Resource-Access
Synchronization
3/31/16
Semaphores
23
3. Typical Use
Single Shared-Resource-Access
Synchronization
tAccessTask () {
:
Acquire binary semaphore token Read or write to shared
resource Release binary semaphore token
:
}
3/31/16
Semaphores
24
3. Typical Use
Multiple Shared-ResourceAccess Synchronization
3/31/16
Semaphores
25
3. Typical Use
Multiple Shared-ResourceAccess Synchronization
tAccessTask () {
:
Acquire a counting semaphore token
Read or Write to shared resource
Release a counting semaphore token
:
}
3/31/16
Semaphores
26
3. Typical Use
Multiple Shared-ResourceAccess Synchronization
tAccessTask () {
:
Acquire first mutex in non-blocking way
If not successful then acquire 2nd mutex in a blocking way
Read or Write to shared resource
Release the acquired mutex
:
}
3/31/16
Semaphores
27
3. Typical Use
Recursive Shared-ResourceAccess Synchronization
3/31/16
Semaphores
28
3. Typical Use
Recursive Shared-ResourceAccess Synchronization
tAccessTask () {
Acquire mutex
Access shared resource
Call Routine A
Release mutex
}
Routine A () {
Acquire mutex
Access shared resource
Call Routine B
Release mutex
}
3/31/16
Semaphores
Routine B () {
:
Acquire mutex
Access shared resource
Release mutex
:
}
29
4. Implementation in T-Kernel
4/29/11
Semaphores
30
4. Implementation in T-Kernel
The mutex functions:
ID mtxid = tk_cre_mtx ( T_CMTX *pk_cmtx ) ;
4/29/11
Semaphores
31
4. Reference
4/29/11
Semaphores
32
Thank You !
3/31/16
Semaphores
33