0% found this document useful (0 votes)
31 views

7 Synchronization

The document discusses synchronization in operating systems. It introduces the concepts of critical sections, race conditions, and mutual exclusion. It describes different approaches to achieve mutual exclusion such as busy waiting using flags, Peterson's algorithm, hardware interrupts, and Test-and-Set instructions. Finally, it discusses sleep/wakeup approaches using semaphores and solving synchronization problems like the producer-consumer problem and dining philosophers problem using semaphores.
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
31 views

7 Synchronization

The document discusses synchronization in operating systems. It introduces the concepts of critical sections, race conditions, and mutual exclusion. It describes different approaches to achieve mutual exclusion such as busy waiting using flags, Peterson's algorithm, hardware interrupts, and Test-and-Set instructions. Finally, it discusses sleep/wakeup approaches using semaphores and solving synchronization problems like the producer-consumer problem and dining philosophers problem using semaphores.
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 24

Synchronization

CS 333: Introduction to Operating Systems


FIT
HCMUS

HCMUS CS333: Operating Systems


Synchronization

Problem
Threads must share data
Data consistency must be maintained
Example
Suppose my wife wants to withdraw $5 from our account and I want to
deposit $10
What should the balance be after the two transactions have been
completed?
What might happen instead if the two transactions were executed
concurrently?

HCMUS 2 CS333: Operating Systems


Race Condition

………
if (balance – withdraw >= 0)
balance = balance - withdraw;
else
cout << “not enough money in your account”;
……….
Note that y = y + 1 might be compiled to something like
loadi 2000, R1
load R1, R2
add R2, 1, R2
store R1, R2

HCMUS 3 CS333: Operating Systems


Terminologies

Critical section: a section of code which reads or writes shared


data
Race condition: potential for interleaved execution of a critical
section by multiple threads
Results are non-deterministic
Mutual exclusion: synchronization mechanism to avoid race
conditions by ensuring exclusive execution of critical sections
Deadlock: permanent blocking of threads
Starvation: execution but no progress

HCMUS 4 CS333: Operating Systems


Requirements for ME

1. No assumptions on hardware: speed, # of processors


2. Mutual exclusion is maintained – that is, only one thread at a
time can be executing inside a CS
3. Execution of CS takes a finite time
4. A thread/process not in CS cannot prevent other
threads/processes to enter the CS
5. Entering CS cannot de delayed indefinitely: no deadlock or
starvation

HCMUS 5 CS333: Operating Systems


Approaches

• BUSY WAITING
–Software
•Flag
•Alternative
•Peterson

–Hardware
•Disable interrupt
•TSL (Test-and-Set)

• SLEEP and WAKEUP


–Semaphores
–Monitors

HCMUS 6 CS333: Operating Systems


Busy waiting: Flag

while (TRUE) {
while (lock == 1); // wait
lock = 1;
critical-section ();
lock = 0;
noncritical-section ();
}

HCMUS 7 CS333: Operating Systems


Busy waiting: alternative

while (TRUE) { while (TRUE) {


while (turn != 0); // wait while (turn != 1); // wait
critical-section (); critical-section ();
turn = 1; turn = 0;
noncritical-section (); noncritical-section ();
} }
(a) Thread A (b) Thread B

HCMUS 8 CS333: Operating Systems


Busy waiting: Peterson

int turn;
int interested[2]; /* initial FALSE*/

void enter_region(int process) {//process 0 or 1


int other; /* other process
other = 1 − process; /* other process*/
interested[process] = TRUE; // want to enter critical section
turn = other;
while (turn == other && interested[other] == TRUE) ;
}

void leave_region (int process) {


interested[process] = FALSE;
}
HCMUS 9 CS333: Operating Systems
Busy waiting: hardware: disable interrupts

• When a thread enters CS then it will disable all interrupts


• → does it feasible !?

HCMUS 10 CS333: Operating Systems


Busy waiting: hardware :TSL (Test-and-Set)

enter_region:
TSL RX, LOCK | copy lock into RX and assign lock = 1
CMP RX, #0 | compare with 0
JNE enter_region | jump if not 0
RET | enter CS

leave_region:
MOVE LOCK, #0 | lock = 0
RET | return

HCMUS 11 CS333: Operating Systems


SLEEP and WAKEUP

• Peterson and TSL are both correct, however threads occupy


CPU even there is no progress
• → sleep and wakeup

HCMUS 12 CS333: Operating Systems


Main ideas

while (TRUE) {
if (busy){
blocked = blocked + 1;
sleep();
}
else busy = 1;
critical-section ();
busy = 0;
if(blocked){
wakeup(process);
blocked = blocked - 1;
}
noncritical-section ();
}
HCMUS 13 CS333: Operating Systems
Semaphore

▪ Dijkstra proposed in 1965


▪ semaphore is an object with two methods: down() and up()
public class BankAccount
{
Semaphore mutex=1
int balance = 0;
public void withdrawal(int amount)
... {
down(mutex);
public void deposit(int amount)
balance = balance - amount;
{
up(mutex);
down(mutex);
}
balance = balance + amount;
}
up(mutex);
}

HCMUS 14 CS333: Operating Systems


Bài toán Producer-Consumer

HCMUS 15 CS333: Operating Systems


#define N 100 /* buffer size*/
semaphore mutex = 1; /* */
semaphore empty = N;
semaphore full = 0;

void Producer(void) {
int item;
while (TRUE) {
item = produce_item();
down(&empty);
down(&mutex);
insert_item(item);
up(&mutex);
up(&full);
}
}
HCMUS 16 CS333: Operating Systems
void Consumer(void) {
int item;
while (TRUE) {
down(&full);
down(&mutex);
item a= remove_item();
up(&mutex);
up(&empty);
consume_item(item);
}
}

HCMUS 17 CS333: Operating Systems


The Dining-Philosophers Problem

Let’s solve the following problem together:


Consider 5 philosophers who spend their lives thinking and eating. The
philosophers share a common circular table. Each philosopher has a bowl
of rice (that magically replenishes itself). There are 5 chopsticks, each
between a pair of philosophers. Occasionally, a philosopher gets hungry.
He/she needs to get both the right and left chopsticks before he can eat.
He eats for a while until he’s full, at which point he puts the chopsticks
back down on the table and resumes thinking.
How can we help the philosophers to synchronize their use of the
chopsticks?

HCMUS 18 CS333: Operating Systems


Philosophy dinner

HCMUS 19 CS333: Operating Systems


Solution 1

void philosopher(int i) // ID
{
while (TRUE) {
think( ); //
take_fork(i); // left fork
take_fork((i+1) % N); // right fork
eat(); //
put_fork(i); //
put_fork((i+1) % N);
}
}
→ If each philosopher keeps one fork

HCMUS 20 CS333: Operating Systems


Solution 1*

void philosopher(int i) {
while (TRUE) {
think( );
down(mutex);
take_fork(i);
take_fork((i+1) % N);
eat();
put_fork(i);
put_fork((i+1) % N);
up(mutex);
}
}
→ Only one philosopher can eat at a moment

HCMUS 21 CS333: Operating Systems


Giải pháp 2

void philosopher (int i) {


while (TRUE) {
think();
take_forks(i);
eat();
put_forks(i);
}
}

HCMUS 22 CS333: Operating Systems


Solution 2 (cont’)

void take_forks(int i) void put_forks(i)


{ {
down(&mutex); down(&mutex);
state[i] = HUNGRY; state[i] = THINKING;
test(i); // try to get 2 forks test(LEFT); //
up(&mutex); test(RIGHT); //
down(&s[i]); up(&mutex); //
} }

HCMUS 23 CS333: Operating Systems


Solution 2 (cont’)

void test(i) {
if (state[i] == HUNGRY &&
state[LEFT] != EATING &&
state[RIGHT] != EATING) {
state[i] = EATING;
up(&s[i]);
}
}

HCMUS 24 CS333: Operating Systems

You might also like