Docsity

Process Synchronization in Operating Systems: Concepts and Solutions, Lecture notes of Operating Systems

Toggle sidebar
bg1
Silberschatz, Galvin and Gagne ©2013
Operating System Concepts – 9th Edition
Chapter 5: Process
Synchronization
5.2 Silberschatz, Galvin and Gagne ©2013
Operating System Concepts – 9th Edition
Chapter 5: Process Synchronization
Background
The Critical-Section Problem
Peterson’s Solution
Synchronization Hardware
Mutex Locks
Semaphores
Classic Problems of Synchronization
Monitors
Synchronization Examples
Alternative Approaches
5.3 Silberschatz, Galvin and Gagne ©2013
Operating System Concepts – 9th Edition
Objectives
To present the concept of process synchronization.
To introduce the critical-section problem, whose solutions
can be used to ensure the consistency of shared data
To present both software and hardware solutions of the
critical-section problem
To examine several classical process-synchronization
problems
To explore several tools that are used to solve process
synchronization problems
5.4 Silberschatz, Galvin and Gagne ©2013
Operating System Concepts – 9th Edition
Process synchronization is required when multiple processes or threads share
resources (memory, variables, files, CPU, etc.) to prevent race conditions, data
inconsistency, and deadlocks.
Two types of Processes in Synchronizations
1. Independent Process
–Does not share data/resources with other processes.
–Runs without affecting or being affected by other processes.
–Example: A media player running in the background while you type in a text editor.
2. Cooperative Process (Dependent Process)
–Shares data/resources with other processes.
–Needs synchronization to ensure correct execution.
–Example: Multiple threads updating a shared bank account balance.
Process Synchronization
pf3
pf4
pf5
pf8
pf9

Partial preview of the text

Download Process Synchronization in Operating Systems: Concepts and Solutions and more Lecture notes Operating Systems in PDF only on Docsity!

Silberschatz, Galvin and Gagne © Operating System Concepts – 9

th Edition

Chapter 5: Process

Synchronization

Silberschatz, Galvin and Gagne © Operating System Concepts – 9

th Edition

Chapter 5: Process Synchronization

Background

The Critical-Section Problem

Peterson’s Solution

Synchronization Hardware

Mutex Locks

Semaphores

Classic Problems of Synchronization

Monitors

Synchronization Examples

Alternative Approaches

Silberschatz, Galvin and Gagne © th

Objectives

To present the concept of process synchronization.

To introduce the critical-section problem, whose solutions

can be used to ensure the consistency of shared data

To present both software and hardware solutions of the

critical-section problem

To examine several classical process-synchronization

problems

To explore several tools that are used to solve process

synchronization problems

Silberschatz, Galvin and Gagne © th

Process synchronization is required when multiple processes or threads share

resources (memory, variables, files, CPU, etc.) to prevent race conditions, data

inconsistency, and deadlocks.

Two types of Processes in Synchronizations

  1. Independent Process

    Does not share data/resources with other processes. 
    • Runs without affecting or being affected by other processes.
    • Example: A media player running in the background while you type in a text editor.
  1. Cooperative Process (Dependent Process)
    • Shares data/resources with other processes.
    • Needs synchronization to ensure correct execution.
    • Example: Multiple threads updating a shared bank account balance.

Process Synchronization

Silberschatz, Galvin and Gagne © Operating System Concepts – 9

th Edition

Shared Variables

  • If two processes modify the same variable simultaneously, incorrect results may occur.
  • Example: Two ATM withdrawals happening at the same time, causing incorrect balance

updates.

Shared Memory

  • If multiple processes read/write to shared memory, synchronization is needed to avoid

data corruption.

  • Example: A producer-consumer problem where one process produces data and another

consumes it.

Shared Code Execution

  • If multiple threads execute the same code section concurrently, race conditions can

occur.

  • Example: Two users trying to book the last available seat in an airline system.

Shared Resources (Files, Printers, etc.)

  • Prevent multiple processes from accessing resources incorrectly or causing deadlocks.
  • Example: Multiple users printing documents to the same printer queue.

Why Process Synchronization?

Silberschatz, Galvin and Gagne © Operating System Concepts – 9

th Edition

Common Synchronization Solutions

Mutex (Mutual Exclusion Object)

Allows only one process/thread to access a resource at

a time.

Semaphore

Manages multiple processes trying to access a limited

resource.

Monitor

  • High-level synchronization that wraps shared data with

controlled access.

Atomic Operations

Ensures operations on shared data happen indivisibly.

Silberschatz, Galvin and Gagne © Operating System Concepts – 9

th Edition

Producer

while (true) {

/* produce an item in next produced */

while (counter == BUFFER_SIZE) ;

/* do nothing */

buffer[in] = next_produced;

in = (in + 1) % BUFFER_SIZE;

counter++;

Silberschatz, Galvin and Gagne © Operating System Concepts – 9

th Edition

Critical Section Problem (Cont.)

The Critical Section is the part of a program where shared resources

are modified.

Example:

Bank Account Transaction: Two processes transferring money at

the same time can lead to incorrect balance calculations.

  • File Writing: If multiple processes write to the same file at once,

data corruption may happen.

Goal: Ensure that only one process is inside the critical section at a

time. Each process follows these steps:

  • Entry Section → Process requests permission to enter the critical section.
  • Critical Section → Process executes the critical operation (e.g., writing to a

shared file).

  • Exit Section → Process leaves the critical section, allowing others to enter.
  • Remainder Section → Any other non-critical work done by the process.

Silberschatz, Galvin and Gagne © th

Algorithm for Process P

i

do {

while (turn == j);

critical section

turn = j;

remainder section

} while (true);

Silberschatz, Galvin and Gagne © th

Solution to Critical-Section Problem

  1. Mutual Exclusion - If Process Pᵢ is inside its critical section,

no other process should be allowed in.

  • Imagine an ATM: If one user is withdrawing cash, another

user shouldn’t be able to access the same account

simultaneously.

  1. Progress - If no process is in the critical section, a waiting

process should be allowed to enter. The system must not

postpone a process forever if it wants to enter the critical

section.

  • If no car is on the bridge, waiting cars should be allowed to

cross without unnecessary delay.

  1. Bounded Waiting - A process waiting to enter the critical

section must eventually get its turn. There should be a limit on

how many times other processes can enter before a waiting

process gets a chance. Prevents starvation, where a process

keeps waiting indefinitely.

  • At a checkout line in a store, if new customers keep

skipping ahead, one customer might never get to pay.

Bounded waiting ensures fairness!

Silberschatz, Galvin and Gagne © Operating System Concepts – 9

th Edition

Critical-Section Handling in OS

Two approaches depending on if kernel is preemptive or non-

preemptive

Preemptive – allows preemption of process when running

in kernel mode

Non-preemptive – runs until exits kernel mode, blocks, or

voluntarily yields CPU

Essentially free of race conditions in kernel mode

Silberschatz, Galvin and Gagne © Operating System Concepts – 9

th Edition

Peterson’s Solution

Good algorithmic description of solving the problem

Two process solution

Assume that the load and store machine-language

instructions are atomic; that is, cannot be interrupted

The two processes share two variables:

 int turn;

 Boolean flag[2]

The variable turn indicates whose turn it is to enter the critical

section

The flag array is used to indicate if a process is ready to enter

the critical section. flag[i] = true implies that process P

i

is

ready!

Silberschatz, Galvin and Gagne © th

Algorithm for Process P

i

do {

flag[i] = true;

turn = j;

while (flag[j] && turn = = j);

critical section

flag[i] = false;

remainder section

} while (true);

Algorithm Steps:

Process Pi sets flag[i] = true (it wants to enter).

  • It sets turn = j (lets the other process go first if it also

wants to enter).

Wait until the other process is not in the critical section

(flag[j] == false).

  • Enter the critical section once safe.
  • Exit and set flag[i] = false (indicating it has left).

Silberschatz, Galvin and Gagne © th

Synchronization Hardware

Many systems provide hardware support for implementing the

critical section code.

All solutions below based on idea of locking

Protecting critical regions via locks

Uniprocessors – could disable interrupts

Currently running code would execute without preemption

Generally too inefficient on multiprocessor systems

Operating systems using this not broadly scalable

Modern machines provide special atomic hardware instructions

Atomic = non-interruptible

Either test memory word and set value

Or swap contents of two memory words

Silberschatz, Galvin and Gagne © Operating System Concepts – 9

th Edition

Bounded-Buffer Problem

n buffers, each can hold one item

Semaphore mutex initialized to the value 1

Semaphore full initialized to the value 0

Semaphore empty initialized to the value n

Silberschatz, Galvin and Gagne © Operating System Concepts – 9

th Edition

Readers-Writers Problem

A data set is shared among a number of concurrent processes

Readers – only read the data set; they do not perform any updates

Writers – can both read and write

Problem – allow multiple readers to read at the same time

Only one single writer can access the shared data at the same time

Several variations of how readers and writers are considered – all

involve some form of priorities

Silberschatz, Galvin and Gagne © th

Dining-Philosophers Problem

 Philosophers spend their lives alternating thinking and eating

 Don’t interact with their neighbors, occasionally try to pick up 2

chopsticks (one at a time) to eat from bowl

Need both to eat, then release both when done

In the case of 5 philosophers

Shared data

 Bowl of rice (data set)

 Semaphore chopstick [5] initialized to 1

Silberschatz, Galvin and Gagne © th

Dining-Philosophers Problem Algorithm (Cont.)

Deadlock handling

Allow at most 4 philosophers to be sitting

simultaneously at the table.

Allow a philosopher to pick up the chop sticks only if

both are available (picking must be done in a critical

section.

Use an asymmetric solution -- an odd-numbered

philosopher picks up first the left chopstick and then

the right chopstick. Even-numbered philosopher picks

up first the right chopstick and then the left chopstick.

Silberschatz, Galvin and Gagne © Operating System Concepts – 9

th Edition

Each philosopher i invokes the operations pickup() and

putdown() in the following sequence:

DiningPhilosophers.pickup(i);

EAT

DiningPhilosophers.putdown(i);

No deadlock, but starvation is possible

Solution to Dining Philosophers (Cont.)

Silberschatz, Galvin and Gagne © Operating System Concepts – 9

th Edition

Synchronization Examples

Solaris

Windows

Linux

Pthreads

Silberschatz, Galvin and Gagne © th

Solaris Synchronization

Implements a variety of locks to support multitasking, multithreading

(including real-time threads), and multiprocessing

Uses adaptive mutexes for efficiency when protecting data from short

code segments

 Starts as a standard semaphore spin-lock

 If lock held, and by a thread running on another CPU, spins

 If lock held by non-run-state thread, block and sleep waiting for signal of

lock being released

Uses readers-writers locks when longer sections of code need

access to data

Uses turnstiles to order the list of threads waiting to acquire either an

adaptive mutex or reader-writer lock

 Turnstiles are per-lock-holding-thread, not per-object

Priority-inheritance per-turnstile gives the running thread the highest of

the priorities of the threads in its turnstile

Silberschatz, Galvin and Gagne © th

Windows Synchronization

Uses interrupt masks to protect access to global resources on

uniprocessor systems

Uses spinlocks on multiprocessor systems

Spinlocking-thread will never be preempted

Also provides dispatcher objects user-land which may act

mutexes, semaphores, events, and timers

Events

An event acts much like a condition variable

Timers notify one or more thread when time expired

Dispatcher objects either signaled-state (object available)

or non-signaled state (thread will block)