Download presentation
Presentation is loading. Please wait.
Published byStefan Bachelder Modified over 10 years ago
1
Previously… Processes –Process States –Context Switching –Process Queues Threads –Thread Mappings Scheduling –FCFS –SJF –Priority scheduling –Round Robin –Multi Level Queues –Real Systems
2
CMT603 Lecture 5 Process Interaction-1
3
Contents Definition Process interaction Problems associated with Process interaction Software solutions –Criteria for the solutions Hardware solutions –Locks
4
Process interaction Processes can share data –Shared memory space Processes aggree to remove the OS protection on the shared memory space –Array calculation –Consumer / Producer processes –Message passing Messages passed between concurently executing processes –Distributed itterative processes that transferre boundary conditions
5
Introduction Shared memory QUESTION –What are the issues with two processes accessing the same memory? OS Process 1 Process 1&2 Process 2 Process 3
6
A Simple Example Shared variable A = 5 Process 1 A++ What is the value of A after both processes have run? Process 2 A--
7
What is a critical section of code? open(file1); while (more lines){ read(fileLine) count++; process (file line) } while (!finished){ } print(count) open(file2); while (more lines){ read(fileLine) count++; process (file line) } finished = true; Process P1
8
Solutions to the critical section problem. Criteria for the solutions –Mutual exclusion If one process is executing the critical section all other processes must be excluded. –Progression If no processes are executing the critical section and a process wishes to –Bounded wait There is a bound on the number of times that a process is allowed to enter its critical section, after another process has made a request to enter its critical section, before that request is granted
9
Using Non-Atomic operations Think of some solutions
10
Using Non-Atomic operations Locks –Break a lock into its low level code…… –We have another critical section problem. Take turns –Dekker and Peterson proposed some algorithms Problem first proposed 1962, final algorithm 1981
11
Algorithm 1 Process 1 while(turn!=1) {wait;} critical section; turn = 2; remainder section; Process 2 while(turn!=2) {wait;} critical section; turn = 1; remainder section;
12
Analysis of Algorithm 1 Does this algorithm satisfy the 3 criteria mentioned. –Mutual Exclusion –Progress –Bounded wait
13
Analysis of Algorithm 1 Other drawbacks: –Busy wait – waste CPU utilization –What happens if one process needs to enter the critical section more frequently than the other? –lockstep synchronization problem If one process needs to enter its critical section more frequently than the other, the faster process will be constrained to operate at the speed of the slower process
14
Algorithm 2 Replace the variable turn with the following two variables: –boolean p1WantToEnter = false; –boolean p2WantToEnter = false; p1WantToEnter = true indicates that process P1 desires to enter the critical section. p2WantToEnter = true indicates that process P2 desires to enter the critical section.
15
Algorithm 2 Process 1 p1WantToEnter=true; while(p2WantToEnter) {wait;} critical section; p1WantToEnter=false; non-critical section; Process 2 p2WantToEnter=true; while(p1WantToEnter) {wait;} critical section; p2WantToEnter=false; non-critical section;
16
Analysis of Algorithm 2 Deadlock can occur Busy wait – waste CPU utilization
17
Analysis of Algorithm 2 Does this algorithm satisfy the 3 criteria mentioned. –Mutual Exclusion –Progress –Bounded wait
18
Algorithm 3 a Combined Version of Algorithm 1 and 2 –Set a Boolean variable for each process to indicate its desire to enter the critical section: boolean p1WantToEnter = false; boolean p2WantToEnter = false; –Set a shared variable turn (initialized to 1). If turn=i then process Pi is allowed to enter the critical section int turn = 1;
19
Algorithm 3 Process 1 p1WantToEnter=true; turn=2; while(p2WantToEnter && turn==2 ) {wait;} critical section; p1WantToEnter=false; non-critical section; Process 2 p2WantToEnter=true; turn=1; while(p1WantToEnter && turn==1 ) {wait;} critical section; p2WantToEnter=false; non-critical section;
20
Analysis of Algorithm 3 Does this algorithm satisfy the 3 criteria mentioned. –Mutual Exclusion –Progress –Bounded wait Busy wait – waste CPU utilization Peterson, G. L. Myths about the mutual exclusion problem. Information Processing Letters 12, 3 (13 June 1981), 115--116. >2 processes -- Operating Systems Review Jan 1990, pp. 18-22
21
Hardware solutions Disable Interrupts –The reason mutual exclusion is needed is largely because pre-emption allows multiple threads to work on shared data asynchronously. –Threads are typically pre-empted by interrupts. –Not practical. (what if a thread enters an indefinite loop in its critical section?)
22
Atomic Locks Atomic –The operating system will not pre-empt the procedure ISA – a CISC command Would a straight forward lock variable work?
23
Locks Atomic operations on locks –TestAndSet() Eg. In the motorola 68000 (See.PDF) TestAndSet() Boolean TestAndSet(boolean target){ boolean returnValue = target; target = true; return returnValue; }
24
Critical section solution using TestAndSet() Process n While ( TestAndSet(lock) ) {wait} // critical section; lock = false;
25
Locks Does this satisfy the critical section criteria? Mutual Exclusion Progress Bounded wait
26
A Better Solution Using –Boolean waiting[n] –Boolean lock All set to false –Problems Complicated Unfair order –Hardware solution to OS problem Know that it exists and be able to understand it. Process i waiting [i] = true; key = true; //---------------------------------------------------------------------------------------------- while ( waiting[i] && key){ key = TestAndSet(lock); } waiting [i] = false; //--------------------------------------------------------------------------------------------- // critical section; //--------------------------------------------------------------------------------------------- j = (i+1) %n; while ( (j!=i) && !waiting[j]) j = (j+1) %n; //---------------------------------------------------------------------- if (j ==i) lock = false; else waiting[j] = false;
27
Summary Definition Process interaction Problems associated with Process interaction Software solutions –Criteria for the solutions Hardware solutions –Locks
Similar presentations
© 2025 SlidePlayer.com. Inc.
All rights reserved.