You are on page 1of 33

Semaphores

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

1.1. Binary Semaphores


Binary semaphores are treated as
global resources

3/31/16

Semaphores

1.2. Counting Semaphores


global resources
bounded count vs unbounded count

3/31/16

Semaphores

1.3. Mutual Exclusion Semaphores


A mutual exclusion (mutex)
semaphore is a special binary
semaphore

3/31/16

Semaphores

1.3. Mutual Exclusion Semaphores


Ownership
Recursive access
Task deletion safety
Protocols for avoiding problems
inherent to mutual exclusion

3/31/16

Semaphores

1.3. Mutual Exclusion Semaphores


Owner is the first task acquiring it
Only owner can release it (contrast
with binary semaphores)

3/31/16

Semaphores

10

1.3. Mutual Exclusion Semaphores


Recursive locking (optional): owner
can acquire multiple times in the
locked state
Avoid causing the deadlock in
nested attempts to acquire the
resource

3/31/16

Semaphores

11

1.3. Mutual Exclusion Semaphores


Task Deletion Safety (optional):
using task deletion locks to
ensure that the owner task
cannot be deleted

3/31/16

Semaphores

12

1.3. Mutual Exclusion Semaphores


Priority Inversion Avoidance:

3/31/16

Semaphores

13

1.3. Mutual Exclusion Semaphores


Priority Inversion Avoidance:

3/31/16

Semaphores

14

1.3. Mutual Exclusion Semaphores


Priority Inversion Avoidance
Common protocols:
priority inheritance
ceiling priority

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

The semaphores functions:


ID semid = tk_cre_sem ( T_CSEM *pk_csem );

ER ercd = tk_del_sem ( ID semid ) ;

ER ercd = tk_sig_sem ( ID semid, INT cnt ) ;

ER ercd = tk_wai_sem ( ID semid, INT cnt,


TMO tmout ) ;
ER ercd = tk_ref_sem ( ID semid, T_RSEM
*pk_rsem ) ;

4/29/11

Semaphores

30

4. Implementation in T-Kernel
The mutex functions:
ID mtxid = tk_cre_mtx ( T_CMTX *pk_cmtx ) ;

ER ercd = tk_del_mtx ( ID mtxid ) ;


ER ercd = tk_loc_mtx ( ID mtxid, TMO tmout ) ;
ER ercd = tk_unl_mtx ( ID mtxid ) ;
ER ercd = tk_ref_mtx ( ID mtxid, T_RMTX
*pk_rmtx ) ;

4/29/11

Semaphores

31

4. Reference

T-kernel Specification T-engine Forum


Real-Time Concepts for Embedded Systems Qing Li with Caroline Yao

4/29/11

Semaphores

32

Thank You !

3/31/16

Semaphores

33

You might also like