Chapter 8 Mutual Exclusion Semaphores 1 Priority ceiling











![OSMutex. Create() input: prio output: OSEvent* Err Tsk. Tbl[pri o] != 1? Reserve OSMutex. Create() input: prio output: OSEvent* Err Tsk. Tbl[pri o] != 1? Reserve](https://slidetodoc.com/presentation_image_h/112952d0f9bd68a848bfcd2ef6bbc9f8/image-12.jpg)











- Slides: 23
Chapter 8: Mutual Exclusion Semaphores 1
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. 2
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 3
Priority ceiling protocol R 1 Task 1 R 2 Task 2 R 3 Task 4 4
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. … 5
Example 1 OSMutex. Create(VH, &err); void task. Prio. H { whle(1) { /*…*/ OSMutex. Pend(Mutex, 0, &err); /*…*/ OSMutex. Post(Mutex); void task. Prio. M { while(1) { /*…*/ } } void task. Prio. L { whle(1) { /*…*/ OSMutex. Pend(Mutex, 0, &err); /*…*/ OSMutex. Post(Mutex); } } 6
Example - without PIP H ready Lock(sem) continue M ready Lock(sem) Unlock(sem) 7
Example - with PIP H ready Lock(sem) continue M ready Lock(sem) Unlock(sem) 8
Example - μC/OS-II Sem = min(H, L) - 1 H ready Lock(sem) continue M ready Lock(sem) Unlock(sem) 9
Functions • • • OSMutex. Create() OSMutex. Del() OSMutex. Pend() OSMutex. Accept() OSMutex. Post() OSMutex. Query() 10
OSMutex. Create() • The major difference between OSMutex. Create() and OSSem. Create() – OSMutex. Create() would reserve a priority slot for priority inheritance protocol OS_EVENT_TYPE_SEM 0 x 00 ? ? ? PIP prio 0 x. FF PIP prio Task priority NULL tcb_ptr 0 x 00 … 0 x 00 ? ? ? … ? ? ? available Owned by a task (owner) 11
OSMutex. Create() input: prio output: OSEvent* Err Tsk. Tbl[pri o] != 1? Reserve the priority; get a ECB; ECB. ower = AVALIABLE; ECB. prio = prio; OSEvent. Wait. List. Init; retrun; 12
Reserve a priority by placing a non-null value 13
OSMutex. Del() opt =? DEL_ALWAYS DEL_NO_PEND Event. Grp = 0 N Event. Grp = 0 Y N Event. Task. Rdy Y Tsk. Tbl[prio] =0 Free(Sem) N Event. Grp = 0 reterun err 14 Y 14
OSMutex. Del() Notice that to setup the field to UNUSED can prevent user from miss using the kernel object 15
OSMutex. Del() 16
OSMutex. Pend() <<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 <<priority inheritance>> //pri[owner] = pri[resource] Update the TCB of the task “owner” if (“owner” in the ready Q) update the Rdy. Tbl & Rdy. Grp … return; 17
OSMutex. Pend() 18
19
OSMutex. Post() <<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 20
OSMutex. Post() 21
OSMutex. Post() 22
OSMutex. Accept() 23