os7 - Modified from Lecture Slides for Operating System...

Info iconThis preview shows pages 1–5. Sign up to view the full content.

View Full Document Right Arrow Icon
Operating Systems : Process Synchronization (Chapter 7) Sung-Yong Park, Ph.D. Distributed Computing & Communication Laboratory Department of Computer Science Sogang University Modified from Lecture Slides for Operating System Concepts (6 th Edition) by Silberschatz et. al. Sogang University Distributed Computing & Communication Lab. Page 2 Need for Process Synchronization ± Concurrent access to shared data may result in data inconsistency. ± Maintaining data consistency requires mechanisms to ensure the orderly execution of cooperating processes. ± Example: Shared-memory solution to bounded-buffer problem producer() { ---------- write(src,buff[i]); } consumer() { read(src,buff[i]); } buff[0] buff[1] shared buffer
Background image of page 1

Info iconThis preview has intentionally blurred sections. Sign up to view the full version.

View Full DocumentRight Arrow Icon
Sogang University Distributed Computing & Communication Lab. Page 3 Producer and Consumer Code ± Shared buffer #define BUFFER_SIZE 10 typedef struct { . . . } item; item buffer[BUFFER_SIZE]; int in = 0; int out = 0; int counter = 0 ; ± Producer process item nextProduced; while ( 1 ) { while ( counter == BUFFER_SIZE ) ; buffer[in] = nextProduced; in = (in + 1) % BUFFER_SIZE; counter++; } ± Consumer process item nextConsumed; while ( 1 ) { while ( counter == 0 ) ; nextConsumed = buffer[out]; out = (out + 1) % BUFFER_SIZE; counter--; } Sogang University Distributed Computing & Communication Lab. Page 4 Possibility of Interleaving ± The statement “count++” may be implemented in machine language as: register1 = counter register1 = register1 + 1 counter = register1 ± The statement “count—” may be implemented as: register2 = counter register2 = register2 – 1 counter = register2 ± If both the producer and consumer attempt to update the buffer concurrently, the assembly statements may get interleaved . ± Interleaving depends upon how the producer and consumer processes are scheduled.
Background image of page 2
Sogang University Distributed Computing & Communication Lab. Page 5 Example: Incorrect Interleaving ± Assume counter is initially 5. One interleaving of statements is: producer: register1 = counter ( register1 = 5 ) producer: register1 = register1 + 1 ( register1 = 6 ) consumer: register2 = counter ( register2 = 5 ) consumer: register2 = register2 – 1 ( register2 = 4 ) producer: counter = register1 ( counter = 6 ) consumer: counter = register2 ( counter = 4 ) ± The value of count may be either 4 or 6, where the correct result should be 5. Sogang University Distributed Computing & Communication Lab. Page 6 Race Condition ± Race condition : The situation where several processes access and manipulate shared data concurrently. The final value of the shared data depends upon which process finishes last. ± To prevent race conditions, concurrent processes must be synchronized . ± The statements counter++; counter--; must be performed atomically . ± Atomic operation means an operation that completes in its entirety without interruption.
Background image of page 3

Info iconThis preview has intentionally blurred sections. Sign up to view the full version.

View Full DocumentRight Arrow Icon
Sogang University Distributed Computing & Communication Lab. Page 7 Critical Section (CS) ± n processes all competing to use some shared data.
Background image of page 4
Image of page 5
This is the end of the preview. Sign up to access the rest of the document.

Page1 / 20

os7 - Modified from Lecture Slides for Operating System...

This preview shows document pages 1 - 5. Sign up to view the full document.

View Full Document Right Arrow Icon
Ask a homework question - tutors are online