{mutexGet(); // Other threads can no longer get the mutex.
// Make changes to the protected object.// ...
objectModify(); // Also gets/puts the mutex. Only allowed from this thread context.
// Make more changes to the protected object.// ...
mutexPut(); // Finally allows other threads to get the mutex.}
Count down from 5000:i. Execute the test-and-set instructionii. If the mutex is clear, we have acquired it in the previous instructionso we can exit the loopiii. When we get to zero, give up our time slice.
(somewhere in the program startup)Initialise the semaphore to its start-up value.
Acquiring a semaphorei. (synchronised) Attempt to decrement the semaphore valueii. If the value would be less than zero, put the task on the tail of the list of tasks waiting on the semaphore and give up the time slice.
Posting a semaphorei. (synchronised) Increment the semaphore valueii. If the value is greater or equal to the amount requested in the post at the front of the queue, take that task off the queue and make it runnable.iii. Repeat (ii) for all tasks until the posted value is exhausted or there are no more tasks waiting.
initial value of SemaVar=0
Producer Consumer--- SemaWait()->decrement SemaVarproduce data---SemaSignal SemaVar or SemaVar++ --->consumer unblocks as SemVar is 1 now.