Inter- Communication and Synchronization of Processes, Threads and Tasks:

Lesson-9: P and V SEMAPHORES

Chapter-9 L9: "Embedded Systems - Architecture, Programming and Design", 2015 Raj Kamal, Publs.: McGraw-Hill Education 1 1. P and V SEMAPHORES

Chapter-9 L9: "Embedded Systems - Architecture, Programming and Design", 2015 Raj Kamal, Publs.: McGraw-Hill Education 2 P and V semaphores

• An efficient synchronisation mechanism • POSIX 1003.1.b, an IEEE standard. • POSIX─ for portable OS interfaces in . • P and V semaphore ─ represents an integer in place of binary or unsigned integers

Chapter-9 L9: "Embedded Systems - Architecture, Programming and Design", 2015 Raj Kamal, Publs.: McGraw-Hill Education 3 P and V semaphore Variables

• The semaphore, apart from initialization, is accessed only through two standard atomic operations─ P and V • P (for wait operation)─ derived from a Dutch word ‘Proberen’, which means 'to test'. • V (for passing operation)─ derived from the word 'Verhogen' which means 'to increment'.

Chapter-9 L9: "Embedded Systems - Architecture, Programming and Design", 2015 Raj Kamal, Publs.: McGraw-Hill Education 4 P and V Functions for Semaphore

 P semaphore function signals that the requires a resource and if not available waits for it.

 V semaphore function signals which the task passes to the OS that the resource is now free for the other users.

Chapter-9 L9: "Embedded Systems - Architecture, Programming and Design", 2015 Raj Kamal, Publs.: McGraw-Hill Education 5 P Function─ P (&Sem1) 1. /* Decrease the semaphore variable*/ sem_1 = sem_1  1; 2. /* If sem_1 is less than 0, send a message to OS by calling a function waitCallToOS. Control of the process transfers to OS, because less than 0 means that some other process has already executed P function on sem_1. Whenever there is return for the OS, it will be to step 1. */ if (sem_1 < 0){waitCallToOS (sem_1);}

Chapter-9 L9: "Embedded Systems - Architecture, Programming and Design", 2015 Raj Kamal, Publs.: McGraw-Hill Education 6 V Function─ V (&Sem2) 3. /* Increase the semaphore variable*/ sem_2 = sem_2 + 1; 4. /* If sem_2 is less or equal to 0, send a message to OS by calling a function signalCallToOS. Control of the process transfers to OS, because < or = 0 means that some other process is already executed V function on sem_2. Whenever there is return for the OS, it will be to step 3. */ if (sem_2 < = 0){signalCallToOS (sem_2);}

Chapter-9 L9: "Embedded Systems - Architecture, Programming and Design", 2015 Raj Kamal, Publs.: McGraw-Hill Education 7 2. P and V SEMAPHORE FUNCTIONS WITH SIGNALING OR NOTIFICATION PROPERTY

Chapter-9 L9: "Embedded Systems - Architecture, Programming and Design", 2015 Raj Kamal, Publs.: McGraw-Hill Education 8 P and V SEMAPHORE FUNCTIONS WITH SIGNAL OR NOTIFICATION PROPERTY Process 1 (Task 1): while (true) { /* Codes */ . V (&sem_s); /* Continue Process 1 if sem_s is not equal to 0 or not less than 0. It means that no process is executing at present. */ . }; Chapter-9 L9: "Embedded Systems - Architecture, Programming and Design", 2015 Raj Kamal, Publs.: McGraw-Hill Education 9 P and V SEMAPHORE FUNCTIONS WITH SIGNAL OR NOTIFICATION PROPERTY Process 2 (Task 2): while (true) { /* Codes */ . P (&sem_s); /* The following codes will execute only when sem_s is not less than 0. */ . };

Chapter-9 L9: "Embedded Systems - Architecture, Programming and Design", 2015 Raj Kamal, Publs.: McGraw-Hill Education 10 3. P and V SEMAPHORE FUNCTIONS WITH MUTEX PROPERTY

Chapter-9 L9: "Embedded Systems - Architecture, Programming and Design", 2015 Raj Kamal, Publs.: McGraw-Hill Education 11 Use of P and V semaphores at the tasks I and J and actions by scheduler

Chapter-9 L9: "Embedded Systems - Architecture, Programming and Design", 2015 Raj Kamal, Publs.: McGraw-Hill Education 12 Program counter assignments (context Switch) to the process or function when using P and V semaphores

Chapter-9 L9: "Embedded Systems - Architecture, Programming and Design", 2015 Raj Kamal, Publs.: McGraw-Hill Education 13 P and V Semaphore functions with mutex property ─ Wait for starting Process 1 (Task 1): while (true) { /* Codes before a critical region*/ . /* Enter Process 1 Critical region codes*/ P (&sem_m); /* The following codes will execute only when sem_m is not less than 0. */ .

Chapter-9 L9: "Embedded Systems - Architecture, Programming and Design", 2015 Raj Kamal, Publs.: McGraw-Hill Education 14 P and V Semaphore functions with mutex property ─ running end exiting Critical Section Process 1 (Task 1): . /* Exit Process 1 critical region codes */ V (&sem_m); /* Continue Process 1 if sem_m is not equal to 0 or not less than 0. It means that no process is executing at present. */ . };

Chapter-9 L9: "Embedded Systems - Architecture, Programming and Design", 2015 Raj Kamal, Publs.: McGraw-Hill Education 15 P and V Semaphore functions with mutex property ─ Wait for starting other Critical Section Process 2 (Task 2): while (true) { /* Codes before a critical region*/ . /* Enter Process 2 Critical region codes*/ P (&sem_m); /* The following codes will execute only when sem_m is not less than 0. */ . Chapter-9 L9: "Embedded Systems - Architecture, Programming and Design", 2015 Raj Kamal, Publs.: McGraw-Hill Education 16 P and V Semaphore functions with mutex property ─ running end exiting the Critical Section Process 2 (Task 2): . /* Exit Process 2 critical region codes */ V (&sem_m); /* Continue Process 2 if sem_m is not equal to 0 or not less than 0. It means that no process is executing at present. */ . };

Chapter-9 L9: "Embedded Systems - Architecture, Programming and Design", 2015 Raj Kamal, Publs.: McGraw-Hill Education 17 4. P and V SEMAPHORE FUNCTIONS WITH COUNTING SEMAPHORE PROPERTY

Chapter-9 L9: "Embedded Systems - Architecture, Programming and Design", 2015 Raj Kamal, Publs.: McGraw-Hill Education 18 P and V Semaphore functions with Count property ─ producer Wait for empty place if = 0 Process c (Task c): while (true) {/* sem_c1 represent number of empty places*/ /* Codes before a producer region*/ . ./* Exit Process 3 region codes */ P (&sem_c1); /* Continue Process c if sem_c1 is not equal to 0 or not less than 0. */ . Chapter-9 L9: "Embedded Systems - Architecture, Programming and Design", 2015 Raj Kamal, Publs.: McGraw-Hill Education 19 5. P and V SEMAPHORE FUNCTIONS FOR PRODUCER-CONSUMER PROBLEM (BOUNDED BUFFER PROBLEM)

Chapter-9 L9: "Embedded Systems - Architecture, Programming and Design", 2015 Raj Kamal, Publs.: McGraw-Hill Education 20 Producer-Consumer and Bounded Buffer Problems Consider three examples:

 (i) a task transmits to an I/O for filling the available places at the stream;

 (ii) a process 'writes' an I/O stream to a printer-buffer; and

 (iii) a task of producing chocolates is being performed.

Chapter-9 L9: "Embedded Systems - Architecture, Programming and Design", 2015 Raj Kamal, Publs.: McGraw-Hill Education 21 Producer-Consumer and Bounded Buffer Problems

 Example (i) another task reads the I/O- stream bytes from the filled places and creates empty places.  Example (ii), from the print buffer an I/O stream prints after a buffer-read and after printing, more empty places are created.  Example (iii), a consumer is consuming the chocolates produced and more empty places (to stock the produced chocolates) are created.

Chapter-9 L9: "Embedded Systems - Architecture, Programming and Design", 2015 Raj Kamal, Publs.: McGraw-Hill Education 22 Bounded Buffer Problem

 A task blockage operational problem─ commonly called Bounded Buffer Problem.

 Example (i)─ A task cannot transmit to the I/O stream if there are no empty places at the stream.

Chapter-9 L9: "Embedded Systems - Architecture, Programming and Design", 2015 Raj Kamal, Publs.: McGraw-Hill Education 23 Bounded Buffer Problem

 Example (ii)─ The task cannot write from the memory to print buffer if there are no empty places at the print-buffer.

Chapter-9 L9: "Embedded Systems - Architecture, Programming and Design", 2015 Raj Kamal, Publs.: McGraw-Hill Education 24 Producer-Consumer Problem

 Example (iii)─ The producer cannot produce chocolates if there are no empty places at the consumer end.

Chapter-9 L9: "Embedded Systems - Architecture, Programming and Design", 2015 Raj Kamal, Publs.: McGraw-Hill Education 25 P and V Semaphore functions with producer- consumer problem solution Process 3 (Task 3): while (true) { /* Codes before a producer region. sem_c2 number of empty places created by process 4 */ . /* Enter Process 3 Producing region codes*/ P (&sem_c2); /* The following codes will execute only when sem_c2 is not less than 0. */ Chapter-9 L9: "Embedded Systems - Architecture, Programming and Design", .2015 Raj Kamal, Publs.: McGraw-Hill Education 26 P and V Semaphore functions with producer- consumer problem solution Process 3 (Task 3): . /* Exit Process 3 region codes */ V (&sem_c1); /* Continue Process 3 if sem_c1 is not equal to 0 or not less than 0. */ . };

Chapter-9 L9: "Embedded Systems - Architecture, Programming and Design", 2015 Raj Kamal, Publs.: McGraw-Hill Education 27 P and V Semaphore functions with producer- consumer problem solution Process 4 (Task 4): while (true) {/* Codes before a producer region. sem_c1 number of filled places created by process 3 */ . /* Enter Process 4 Consuming region codes*/ P (&sem_c1); /* The following codes will execute only when sem_c1 is not less than 0. */ . Chapter-9 L9: "Embedded Systems - Architecture, Programming and Design", 2015 Raj Kamal, Publs.: McGraw-Hill Education 28 P and V Semaphore functions with producer- consumer problem solution Process 4 (Task 4): . /* Exit Process 4 region codes */ V (&sem_c2); /* Continue Process 4 if sem_m is not equal to 0 or not less than 0. It means that no process is executing at present. */ . };

Chapter-9 L9: "Embedded Systems - Architecture, Programming and Design", 2015 Raj Kamal, Publs.: McGraw-Hill Education 29 6. function

Chapter-9 L9: "Embedded Systems - Architecture, Programming and Design", 2015 Raj Kamal, Publs.: McGraw-Hill Education 30 Lock ( )

 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 ( )

Chapter-9 L9: "Embedded Systems - Architecture, Programming and Design", 2015 Raj Kamal, Publs.: McGraw-Hill Education 31 Lock ( )

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

 Difference between use of Lock and mutex is that when mutex is used then OS permits other tasks to run except the needing same mutex and the lock is used to prevent scheduling of another task till unlock executes at locking task.

Chapter-9 L9: "Embedded Systems - Architecture, Programming and Design", 2015 Raj Kamal, Publs.: McGraw-Hill Education 32 Disadvantage • 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.

Chapter-9 L9: "Embedded Systems - Architecture, Programming and Design", 2015 Raj Kamal, Publs.: McGraw-Hill Education 33 Spinning lock ( ) • 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 spinning lock is a powerful tool in the situation described above.

Chapter-9 L9: "Embedded Systems - Architecture, Programming and Design", 2015 Raj Kamal, Publs.: McGraw-Hill Education 34 Spinlock ( )… • 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.

Chapter-9 L9: "Embedded Systems - Architecture, Programming and Design", 2015 Raj Kamal, Publs.: McGraw-Hill Education 35 Spinlock ( )… • A spin lock does not let a running task be blocked instantly • First successively tries decreasing the trial periods before finally blocking a task

Chapter-9 L9: "Embedded Systems - Architecture, Programming and Design", 2015 Raj Kamal, Publs.: McGraw-Hill Education 36 Summary

Chapter-9 L9: "Embedded Systems - Architecture, Programming and Design", 2015 Raj Kamal, Publs.: McGraw-Hill Education 37 We learnt • P and V semaphore functions are POSIX 100.3b IEEE accepted standard for the semaphore IPCs. • These can be used as event signaling, as mutex, as counting semaphore and as semaphores for the bounded buffer problem solution (producer–consumer problem)

Chapter-9 L9: "Embedded Systems - Architecture, Programming and Design", 2015 Raj Kamal, Publs.: McGraw-Hill Education 38 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 all the other processes.

Chapter-9 L9: "Embedded Systems - Architecture, Programming and Design", 2015 Raj Kamal, Publs.: McGraw-Hill Education 39 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.

Chapter-9 L9: "Embedded Systems - Architecture, Programming and Design", 2015 Raj Kamal, Publs.: McGraw-Hill Education 40 End of Lesson 9 of Chapter 9

Chapter-9 L9: "Embedded Systems - Architecture, Programming and Design", 2015 Raj Kamal, Publs.: McGraw-Hill Education 41