semaphore - Synchronization (Operating Systems) -


this problem faced in operating system's exam. not figure out right answer it. can help.given code synchronization many threads trying access global counter g using lock-

if(lock==1) wait(); //sleep thread until other thread wakes thread else lock=1; //enter in protected area                  //access global counter g// lock=0; //wake other thread waiting lock released 

what problem in above synchronization? choose of options given below

  1. the synchronization fine , run correctly.
  2. will run on uni-processor systems not on multiprocessor systems.
  3. will not run on system
  4. can’t say. need more data

the answer 3. code fails both @ safety , liveness long threads can preempted. safety, consider following interleaving of operations 2 threads t1 , t2:

  • t1 checks lock, skips else statement
  • os preempts t1 , schedules t2
  • t2 checks lock, skips else statement

and have 2 threads in critical section. why need sort of atomic test-and-set operation, or ability disable preemption, properly.

for liveness, consider following interleaving of operations 2 threads t1 , t2:

  • t1 checks lock, skips else statement
  • t1 sets lock 1
  • os preempts t1 , schedules t2
  • t2 checks lock, finds 1
  • os preemtps t2 , schedules t1
  • t1 sets lock 0
  • t1 finds no thread waiting , nothing else
  • os schedules t2 again
  • t2 starts waiting...

and t2 (potentially) waiting forever. solution synchronization primitive keep track of wake-ups (e.g., semaphore) or require testing condition , waiting done atomically (e.g., mutexes , condition variables).


Comments