16
2008 Chapter-7 L14: "Embedded Systems - Architecture, Programming and Design" , Raj Kamal, Publs.: McGraw-Hill, Inc. 1 INTER-PROCESS COMMUNICATION AND SYNCHRONISATION: Lesson-14: Mutex, Lock and Spin Lock functions

Chap 7Lesson14NewMutexandSpinLock

Embed Size (px)

Citation preview

Page 1: Chap 7Lesson14NewMutexandSpinLock

2008 Chapter-7 L14: "Embedded Systems - Architecture, Programming and Design" , Raj Kamal, Publs.: McGraw-Hill, Inc. 1

INTER-PROCESS COMMUNICATION AND SYNCHRONISATION:

Lesson-14: Mutex, Lock and Spin Lock functions

Page 2: Chap 7Lesson14NewMutexandSpinLock

2008 Chapter-7 L14: "Embedded Systems - Architecture, Programming and Design" , Raj Kamal, Publs.: McGraw-Hill, Inc. 2

1. Mutex Semaphore1. Mutex Semaphore

Page 3: Chap 7Lesson14NewMutexandSpinLock

2008 Chapter-7 L14: "Embedded Systems - Architecture, Programming and Design" , Raj Kamal, Publs.: McGraw-Hill, Inc. 3

� Process using a Mutex blocks a critical section in a task for taking the mutex and unblocks on releasing the mutex.

� The mutex wait for lock can be specified a timeout .

Mutex SemaphoreMutex Semaphore

Page 4: Chap 7Lesson14NewMutexandSpinLock

2008 Chapter-7 L14: "Embedded Systems - Architecture, Programming and Design" , Raj Kamal, Publs.: McGraw-Hill, Inc. 4

Task A taking semaphore at T2, releasing at T3, B then runs Task A taking semaphore at T2, releasing at T3, B then runs at T4at T4

Page 5: Chap 7Lesson14NewMutexandSpinLock

2008 Chapter-7 L14: "Embedded Systems - Architecture, Programming and Design" , Raj Kamal, Publs.: McGraw-Hill, Inc. 5

2. Lock function2. Lock function

Page 6: Chap 7Lesson14NewMutexandSpinLock

2008 Chapter-7 L14: "Embedded Systems - Architecture, Programming and Design" , Raj Kamal, Publs.: McGraw-Hill, Inc. 6

� Process using lock ( ) enters a critical section locks the resources to a critical section till at the end of the section unlock ( ) is used.

� A wait loop creates on executing lock ( ) and wait is over when other critical section executes unlock ( )

� lock ( ) and unlock ( ) involves little overhead (number f operations) than the OSSemPend ( ) and OSSemPost ( )

Lock ( )Lock ( )

Page 7: Chap 7Lesson14NewMutexandSpinLock

2008 Chapter-7 L14: "Embedded Systems - Architecture, Programming and Design" , Raj Kamal, Publs.: McGraw-Hill, Inc. 7

3. Lock function disadvantages3. Lock function disadvantages

Page 8: Chap 7Lesson14NewMutexandSpinLock

2008 Chapter-7 L14: "Embedded Systems - Architecture, Programming and Design" , Raj Kamal, Publs.: McGraw-Hill, Inc. 8

• A resource of high priority should not lock the other processes by blocking an already running task in the following situation. Suppose a task is running and a little time is left for its completion.

DisadvantageDisadvantage

Page 9: Chap 7Lesson14NewMutexandSpinLock

2008 Chapter-7 L14: "Embedded Systems - Architecture, Programming and Design" , Raj Kamal, Publs.: McGraw-Hill, Inc. 9

4. Spin Lock function4. Spin Lock function

Page 10: Chap 7Lesson14NewMutexandSpinLock

2008 Chapter-7 L14: "Embedded Systems - Architecture, Programming and Design" , Raj Kamal, Publs.: McGraw-Hill, Inc. 10

• Suppose a task is running and a little time is left for its completion.

• The running time left for it is less compared to the time that would be taken in blocking it and context switching.

• There is an innovative concept of spin locking in certain schedulers.

• A spin lock is a powerful tool in the situation described above.

SpinlockSpinlock ( ) ( )

Page 11: Chap 7Lesson14NewMutexandSpinLock

2008 Chapter-7 L14: "Embedded Systems - Architecture, Programming and Design" , Raj Kamal, Publs.: McGraw-Hill, Inc. 11

• The scheduler locking processor for a task waits to cause the blocking of the running task first for a time-interval t, then for (t -δt), then (t - 2δt) and so on.

• When this time interval spin downs to 0, the task that requested the lock of the processor now unlocks the running task and blocks it from further running. The request is now granted.

SpinlockSpinlock ( )… ( )…

Page 12: Chap 7Lesson14NewMutexandSpinLock

2008 Chapter-7 L14: "Embedded Systems - Architecture, Programming and Design" , Raj Kamal, Publs.: McGraw-Hill, Inc. 12

• A spin lock does not let a running task be blocked instantly

• First successively tries decreasing the trial periods before finally blocking a task

SpinlockSpinlock ( )…( )…

Page 13: Chap 7Lesson14NewMutexandSpinLock

2008 Chapter-7 L14: "Embedded Systems - Architecture, Programming and Design" , Raj Kamal, Publs.: McGraw-Hill, Inc. 13

SummarySummary

Page 14: Chap 7Lesson14NewMutexandSpinLock

2008 Chapter-7 L14: "Embedded Systems - Architecture, Programming and Design" , Raj Kamal, Publs.: McGraw-Hill, Inc. 14

We learnt� OS provides the IPC functions Create, Post,

Pend, Accept and Query for using mutex semaphores. The time out and error handling function can be provided with Pend function as arguments.

� OS provides the IPC functions for creating and using lock ( ) and unlock ( ) for the resource for a process and to lock and unlock the resources for the other processes.

Page 15: Chap 7Lesson14NewMutexandSpinLock

2008 Chapter-7 L14: "Embedded Systems - Architecture, Programming and Design" , Raj Kamal, Publs.: McGraw-Hill, Inc. 15

We learnt� OS provides the IPC functions for creating

and using spinlock ( ) and unlock ( ) for the resource for a process and to lock and unlock the resources for the other processes for successively decreasing trial periods for before doing unlocking.

Page 16: Chap 7Lesson14NewMutexandSpinLock

2008 Chapter-7 L14: "Embedded Systems - Architecture, Programming and Design" , Raj Kamal, Publs.: McGraw-Hill, Inc. 16

End of Lesson-14 on Mutex, Lock and Spin Lock functions