multithreading - Are Semaphore P and V operations atomic? -


are p() , v() operations can performed on semaphore guarantee atomic? can semaphore prevent 2 processes getting p()?

suppose have binary semaphore, s, has value 1, , 2 processes simultaneously attempt execute p on s. 1 of these operations able complete before next v operation on s; other process attempting perform p operation suspended.

taken university notes:

we can think if p , v controlling access resource:

when process wants use resource, performs p operation: if succeeds, decrements amount of resource available , process continues; if resource in use, process has wait.

when process finished resource, performs v operation: if there processes waiting on resource, 1 of these woken up;
if there no waiting processes, semaphore incremented indicating there more of resource free. note definition of v doesn’t specify process woken if more 1 process has been suspended on same semaphore.

semaphores can solve both mutual exclusion , condition synchronization problems. answer both questions is: yes.


Comments

Popular posts from this blog

linux - Mailx and Gmail nss config dir -

c# - Is it possible to remove an existing registration from Autofac container builder? -

php - Mysql PK and FK char(36) vs int(10) -