Presentation is loading. Please wait.

Presentation is loading. Please wait.

Chapter 8: Mutual Exclusion Semaphores

Similar presentations


Presentation on theme: "Chapter 8: Mutual Exclusion Semaphores"— Presentation transcript:

1 Chapter 8: Mutual Exclusion Semaphores

2 Priority ceiling protocol
PCP is used in scheduling to avoid deadlock due to priority inversion. In PCP, each resource is assigned a “priority ceiling”, which is a priority equal to the highest priority of any task which may lock the resource. A process will not get scheduled if any resource it may lock actually has been locked by another process.

3 Priority ceiling protocol
When a task locks the resource, its priority is temporarily raised to the “priority ceiling”, thus no task that may lock the resource is able to get scheduled. this allows a low priority task to defer execution of a higher-priority task by doing this it prevents deadlock

4 Priority ceiling protocol
Task 1 Task 2 Task 3 Task 4

5 Mutual Exclusion Semaphores
A mutex is used by your application code to reduce the priority inversion problem. The kernel can raise the priority of the lower priority task to shorten the blocking time suffered by the higher priority task. In order to implement mutex, a kernel needs to provide the ability to support multiple tasks at the same priority.

6 Example1 OSMutexCreate(VH, &err); void taskPrioM { while(1) {
void taskPrioH { whle(1) { /*…*/ OSMutexPend(Mutex, 0, &err); OSMutexPost(Mutex); } void taskPrioM { while(1) { /*…*/ } void taskPrioL { whle(1) { /*…*/ OSMutexPend(Mutex, 0, &err); OSMutexPost(Mutex); }

7 Example - without PIP H ready Lock(sem) continue M ready L ready
Unlock(sem)

8 Example - with PIP H ready Lock(sem) continue M ready L ready
Unlock(sem)

9 Example - μC/OS-II Sem = min(H, L) - 1 H ready Lock(sem) continue M
Unlock(sem)

10 Functions OSMutexCreate() OSMutexDel() OSMutexPend() OSMutexAccept()
OSMutexPost() OSMutexQuery()

11 OSMutexCreate() The major difference between OSMutexCreate() and OSSemCreate() OSMutexCreate() would reserve a priority slot for priority inheritance protocol OS_EVENT_TYPE_SEM OS_EVENT_TYPE_SEM 0x00 ??? PIP prio 0xFF PIP prio Task priority (owner) NULL tcb_ptr 0x00 … 0x00 ??? … ??? available Owned by a task

12 OSEventWaitListInit;
OSMutexCreate() input:  prio output:     OSEvent*  Err TskTbl[prio] != 1? Reserve the priority; get a ECB; ECB.ower = AVALIABLE; ECB.prio = prio; OSEventWaitListInit; retrun;

13 Reserve a priority by placing a non-null value
osmutexCreate()

14 OSMutexDel() opt =? DEL_ALWAYS DEL_NO_PEND EventGrp = 0 EventGrp = 0
EventTaskRdy TskTbl[prio] =0 Free(Sem) N EventGrp = 0 reterun err 14 Y

15 OSMutexDel() Notice that to setup the field to UNUSED can prevent user from miss using the kernel object

16 OSMutexDel()

17 OSMutexPend() <<input>> OSEvent* timeout err
the resource is free Y Acquire the resource OSEvent->owner= cur return; N pri[cur] > pri[owner] N Rdy Q => Waiting Q Call scheduler Y return; <<priority inheritance>> //pri[owner] = pri[resource] Update the TCB of the task “owner” if (“owner” in the ready Q) update the RdyTbl & RdyGrp

18 OSMutexPend()

19

20 OSMutexPost() <<input>> OSEvent* PIP == cur.Priority? N
Make the HPT in waiting Q ready to run Call scheduler Y <<priority inheritance>> Return to the original priority

21 OSMutexPost()

22 OSMutexPost()

23 OSMutexAccept()


Download ppt "Chapter 8: Mutual Exclusion Semaphores"

Similar presentations


Ads by Google