Академический Документы
Профессиональный Документы
Культура Документы
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:
4/19/12
Semaphores
55
4/19/12
Semaphores
66
4/19/12
Semaphores
77
4/19/12
Semaphores
88
Ownership Recursive access Task deletion safety Protocols for avoiding problems inherent to mutual exclusion
4/19/12
Semaphores
99
Owner is the first task acquiring it Only owner can release it (contrast with binary semaphores)
4/19/12
Semaphores
1010
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
Task Deletion Safety (optional): using task deletion locks to ensure that the owner task cannot be deleted
4/19/12
Semaphores
1212
4/19/12
Semaphores
1313
4/19/12
Semaphores
1414
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
4/19/12
Semaphores
1919
3. Typical Use
v
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 :
4/19/12
Semaphores
2222
3. Typical Use
v
4/19/12
Semaphores
2323
3. Typical Use
v
tAccessTask () { : Acquire binary semaphore token Read or write to shared resource Release binary semaphore token : }
4/19/12
Semaphores
2424
3. Typical Use
v
4/19/12
Semaphores
2525
3. Typical Use
v
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
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
4/19/12
Semaphores
2828
3. Typical Use
v
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