Вы находитесь на странице: 1из 33

Semaphores

Prepared Click to edit by: Lm Ngc n Tng Trng Tuyn

Master subtitle style

4/19/12 4/19/12

Semaphores Semaphores

11

Contents
1. 2. 3. 4.
Definition Operators Typical Use Implementation in T-kernel

4/19/12

Semaphores

22

1. Definition
v

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

4/19/12

Semaphores

33

1. Definition

4/19/12

Semaphores

44

1. Definition
v

3 groups:

Binary Semaphores Counting Semaphores Mutual Exclusion Semaphores

4/19/12

Semaphores

55

1.1. Binary Semaphores


v

Binary semaphores are treated as global resources

4/19/12

Semaphores

66

1.2. Counting Semaphores


v v

global resources bounded count vs unbounded count

4/19/12

Semaphores

77

1.3. Mutual Exclusion Semaphores


v

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

4/19/12

Semaphores

88

1.3. Mutual Exclusion Semaphores


v v v v

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

4/19/12

Semaphores

99

1.3. Mutual Exclusion Semaphores


v v

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

4/19/12

Semaphores

1010

1.3. Mutual Exclusion Semaphores


v

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

4/19/12

Semaphores

1111

1.3. Mutual Exclusion Semaphores


v

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

4/19/12

Semaphores

1212

1.3. Mutual Exclusion Semaphores


v

Priority Inversion Avoidance:

4/19/12

Semaphores

1313

1.3. Mutual Exclusion Semaphores


v

Priority Inversion Avoidance:

4/19/12

Semaphores

1414

1.3. Mutual Exclusion Semaphores


v

Priority Inversion Avoidance

Common protocols:
priority inheritance ceiling priority

4/19/12

Semaphores

1515

2. Operator
v v v

creating and deleting semaphores acquiring and releasing semaphores clearing a semaphores task-waiting list getting semaphore information.

4/19/12

Semaphores

1616

3. Typical Use
v

Wait-and-Signal Synchronization

4/19/12

Semaphores

1717

3. Typical Use
v

Wait-and-Signal Synchronization

tWaitTask ( ) { : Acquire binary semaphore token : } tSignalTask ( ) { : Release binary semaphore token : }

4/19/12

Semaphores

1818

3. Typical Use
v

Multiple-Task Wait-and-Signal Synchronization

4/19/12

Semaphores

1919

3. Typical Use
v

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 : }
4/19/12 Semaphores 2020

3. Typical Use
v

Credit-Tracking Synchronization

4/19/12

Semaphores

2121

3. Typical Use
v

Credit-Tracking Synchronization
tWaitTask () { : Acquire counting semaphore token :

} tSignalTask () { : Release counting semaphore token : }

4/19/12

Semaphores

2222

3. Typical Use
v

Single Shared-Resource-Access Synchronization

4/19/12

Semaphores

2323

3. Typical Use
v

Single Shared-Resource-Access Synchronization

tAccessTask () { : Acquire binary semaphore token Read or write to shared resource Release binary semaphore token : }

4/19/12

Semaphores

2424

3. Typical Use
v

Multiple Shared-Resource-Access Synchronization

4/19/12

Semaphores

2525

3. Typical Use
v

Multiple Shared-Resource-Access Synchronization

tAccessTask () { : Acquire a counting semaphore token Read or Write to shared resource Release a counting semaphore token : }

4/19/12

Semaphores

2626

3. Typical Use
v

Multiple Shared-Resource-Access 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 : }

4/19/12

Semaphores

2727

3. Typical Use
v

Recursive Shared-ResourceAccess Synchronization

4/19/12

Semaphores

2828

3. Typical Use
v

Recursive Shared-ResourceAccess Synchronization


Routine B () { : Acquire mutex Access shared resource Release mutex : }

4/19/12

tAccessTask () { Acquire mutex Access shared resource Call Routine A Release mutex } Routine A () { Acquire mutex Access shared resource Call Routine B Release mutex }

Semaphores

2929

4. Implementation in T-Kernel
v

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 4/19/12

Semaphores Semaphores

30

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

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 4/19/12

Semaphores Semaphores

31

4. Reference
v v

T-kernel Specification T-engine Forum Real-Time Concepts for Embedded Systems Qing Li with Caroline Yao

4/29/11 4/19/12

Semaphores Semaphores

32

Thank You !
Click to edit Master subtitle style

4/19/12 4/19/12

Semaphores Semaphores

3333

Вам также может понравиться