Lec-8.ppt - Process Synchronization Producer while(true produce an item in next produced while(counter = BUFFER_SIZE do nothing buffer[in =

Lec-8.ppt - Process Synchronization Producer while(true...

This preview shows page 1 - 9 out of 9 pages.

Process Synchronization
2Producer while (true) {/* produce an item in next produced */ while (counter == BUFFER_SIZE) ; /* do nothing */ buffer[in] = next_produced; in = (in + 1) % BUFFER_SIZE; counter++; }
3Consumerwhile (true) {while (counter == 0) ; /* do nothing */ next_consumed = buffer[out]; out = (out + 1) % BUFFER_SIZE; counter--; /* consume the item in next consumed */ }
4Race Conditioncounter++ could be implemented asregister1 = counterregister1 = register1 + 1counter = register1counter--could be implemented asregister2 = counterregister2 = register2 - 1counter = register2Consider this execution interleaving with “count = 5” initially:S0: producer execute register1 = counter {register1 = 5}S1: producer execute register1 = register1 + 1 {register1 = 6} S2: consumer execute register2 = counter {register2 = 5} S3: consumer execute register2 = register2 – 1 {register2 = 4} S4: producer execute counter = register1 {counter = 6 } S5: consumer execute counter = register2 {counter = 4}
5Critical SectionGeneral structure of process Pi
6Critical-Section Handling in OS Two approaches depending on if kernel is preemptive or non- preemptive Preemptive– allows preemption of process when running in kernel modeNon-preemptive – runs until exits kernel mode, blocks, or voluntarily yields CPUEssentially free of race conditions in kernel mode
7Peterson’s SolutionTwo processes solutionAssume that the loadand storemachine-language instructions are atomicThe two processes share two variables:int turn; Boolean flag[2]The variable turnindicates whose turn it is to enter the critical sectionThe flagarray is used to indicate if a process is ready to enter the critical section. flag[i] = trueimplies that process Piis ready!
8Algorithm for Process Pido{ flag[i] = true; turn = j; while (flag[j] && turn == j); critical section flag[i] = false; remainder section } while (true);
9Petri net diagram for mutual exclusionturn

  • Left Quote Icon

    Student Picture

  • Left Quote Icon

    Student Picture

  • Left Quote Icon

    Student Picture