Download presentation
Presentation is loading. Please wait.
Published byΕυδοξία Δαμασκηνός Modified over 6 years ago
1
The Critical-Section Problem (Two-Process Solution)
Algorithm 1 What is the problem here? //turn is global and initialized to 0 repeat while turn<>i do no-op; //enters critical section turn := j; //remainder section until false; 11/18/2018
2
The Critical-Section Problem (Two-Process Solution)
Algorithm 2 What is the problem here? var flag: array [0..1] of boolean; repeat flag[i] = true; while flag[j] do no-op; //enters critical section flag[i] = false; //remainder section until false; 11/18/2018
3
The Critical-Section Problem (Two-Process Solution)
Algorithm 3 Why does this solution work? Does it satisfy all three properties? var flag: arry [0..1] of boolean; turn: 0..1; repeat flag[i] = true; turn = j; while (flag[j] and turn = j) do no-op; //enters critical section flag[i] = false; //remainder section until false; 11/18/2018
4
The Critical-Section Problem (using Test-and-Set)
What does this hardware primitive do? (how is it defined?) How do we use it to solve the critical section problem? 11/18/2018
5
The Critical-Section Problem (using Test-and-Set)
Solution (p.165 in the book) Does it satisfy all three properties? //lock is global and initialized to false repeat while Test-and-Set(lock) do no-op; //enters critical section lock = false; //remainder section until false; 11/18/2018
6
The Critical-Section Problem (using Semaphores)
Semaphore definition Usage for solving critical section problem What about changing semaphore definition to: Does it satisfy all three properties? wait(S): while s<=0 do no-op; S := S-1; signal(S): S := S+1; //mutex is a semaphore and is initialized to 1 repeat wait(mutex); //enters critical section signal(mutex); //remainder section until false; wait(S): S := S-1; signal(S): S := S+1; 11/18/2018
7
The Bounded-Buffer Problem (using Semaphores)
Algorithm //mutex, empty, full are all semaphores //initially, mutex=1; empty=n; full=0; //producer repeat //produce r P(empty); P(mutex); add r to buffer; V(mutex); V(full); until false; //consumer P(full); remove r from buffer; V(empty); process r; 11/18/2018
8
The Bounded-Buffer Problem (using Semaphores)
Question: Is this solution efficient? (Imagine a buffer with n slots) How can it be changed to improve its efficiency? 11/18/2018
9
More on Semaphores... Historical background
What is the problem with semaphores? First introduced by Dijkstra when describing the THE system (Edsger W. Dijkstra. The Structure of the "THE" Multiprogramming System. Communications of the ACM 11(5), May 1968.) Semaphores only work when all processes are well-behaved! 11/18/2018
10
More on Semaphores... How to overcome the problem with “untrustworthy” semaphores? Critical regions Monitors Using message buffering and message queues for each process (Per Brinch Hansen. The Nucleus of a Multiprogramming System. Communications of the ACM 13(4), April ) send message (receiver, message, buffer); wait message (sender, message, buffer); send answer (result, answer, buffer); wait answer (result, answer, buffer); What are the potential complications here? 11/18/2018
Similar presentations
© 2025 SlidePlayer.com. Inc.
All rights reserved.