Unit 2: Concurrent Processes




Concurrent Processes

Process Concept

A process is a program in execution.

Simple explanation

When a program is running, it becomes a process.

Components of a Process

  • Program code
  • Data
  • Stack
  • Program Counter (PC)
  • Registers

Process States

StateDescription
NewProcess is being created
ReadyWaiting for CPU
RunningExecuting on CPU
WaitingWaiting for I/O
TerminatedFinished execution

Principle of Concurrency

Concurrency means multiple processes execute at the same time.

Simple explanation: Many programs share the CPU by taking turns very fast.

Why Concurrency is Needed

  • Better CPU utilization
  • Faster response
  • Resource sharing

Problem with Concurrency

  • Data inconsistency
  • Race conditions
  • Deadlocks

Producer–Consumer Problem

A classic synchronization problem involving two processes.

Simple explanation

  • Producer produces data
  • Consumer consumes data
  • Both share a buffer

Issues

  • Producer should not produce if buffer is full
  • Consumer should not consume if buffer is empty

Need

Proper synchronization is required to avoid data errors.

Mutual Exclusion

Mutual exclusion ensures only one process accesses shared resource at a time.

Simple explanation: If one process is using shared data, others must wait.

Example: Only one person can use a bathroom at a time.

Critical Section Problem

The critical section is the part of code that accesses shared resources.

Requirements of Critical Section Problem

ConditionDescription
Mutual ExclusionOnly one process allowed
ProgressDecision cannot be delayed
Bounded WaitingLimited waiting time

Dekker’s Solution

One of the first software solutions for mutual exclusion.

Characteristics

  • Works for two processes

Uses two shared variables:

  • flag[]
  • turn

Simple explanation: Processes take turns entering critical section.

Limitations

  • Complex logic
  • Works only for two processes

Peterson’s Solution

An improved software solution for mutual exclusion.

Characteristics

  • Works for two processes

Uses:

  • flag[2]
  • turn

Simple explanation: Process declares interest and gives priority to the other.

Advantages

  • Simple and correct
  • Satisfies all critical section requirements

Limitations

  • Works only for two processes
  • Busy waiting

Semaphores

A semaphore is a synchronization tool using an integer variable.

Types of Semaphores

TypeDescription
Binary Semaphore0 or 1 (Mutex)
Counting SemaphoreAny integer value

Semaphore Operations

  • wait() / P() → Decrease value
  • signal() / V() → Increase value

Simple explanation

Controls access to shared resources using signals.

Semaphore Example (Producer–Consumer)

  • empty – counts empty slots
  • full – counts filled slots
  • mutex – ensures mutual exclusion

Test-and-Set (Test)

A hardware-based solution for mutual exclusion.

Simple explanation

A special machine instruction that:

  • Tests a memory value
  • Sets it atomically (in one step)

Characteristics

  • Prevents race conditions
  • Uses busy waiting

Advantages

  • Simple
  • Fast

Disadvantages

  • Wastes CPU time
  • Starvation possible

Summary Table

TopicKey Point
ProcessProgram in execution
ConcurrencyMultiple processes at same time
Producer–ConsumerShared buffer problem
Mutual ExclusionOne process at a time
Critical SectionCode accessing shared data
DekkerSoftware solution (2 processes)
PetersonImproved software solution
SemaphoreSynchronization tool
Test-and-SetHardware solution

Set Operation (in Operating Systems)

In Operating Systems, set operations are mainly used in resource allocation, scheduling, and deadlock handling.

Common Set Operations

OperationMeaning
Union (A ∪ B)Combine two sets
Intersection (A ∩ B)Common elements
Difference (A − B)Elements in A not in B
SubsetOne set contained in another

Simple Explanation

  • Processes and resources are often represented as sets
  • OS applies set operations to analyze resource usage

Example

  • Set of running processes
  • Set of available resources

Classical Problems in Concurrency

Classical problems explain synchronization and resource-sharing issues.

Dining Philosopher Problem

A classic problem that demonstrates deadlock and starvation.

Problem Description

  • Five philosophers sit around a circular table
  • Each philosopher needs two chopsticks to eat
  • One chopstick between each philosopher

Rules

  • Philosopher can think or eat
  • Must pick up both chopsticks to eat

Problem

  • If all philosophers pick up one chopstick, deadlock occurs

Issues Highlighted

  • Deadlock
  • Resource starvation

Solutions

  • Limit number of philosophers
  • Use semaphores
  • Pick up both chopsticks at once

Sleeping Barber Problem

Demonstrates process synchronization and waiting problems.

Problem Description

  • One barber
  • One barber chair
  • Limited waiting chairs

Rules

  • If no customers → barber sleep

If customer arrives:

  • If barber sleeping → wake up
  • If chairs full → customer leaves

Issues Highlighted

  • Synchronization
  • Deadlock avoidance
  • Queue management

Solutions

  • Semaphores
  • Mutex locks

Inter-Process Communication (IPC)

IPC allows processes to exchange data and coordinate actions.

Simple Explanation

Processes cannot directly share memory. IPC provides safe communication methods.

IPC Models

(A) Shared Memory Model

FeatureDescription
MemoryShared between processes
SpeedFast
ControlProgrammer manages synchronization

Example

  • Producer–Consumer problem

(B) Message Passing Model

FeatureDescription
CommunicationSend & receive messages
MemoryNo shared memory
SpeedSlower than shared memory

Example

  • Client–Server systems

IPC Schemes

Common IPC Schemes

SchemeDescription
PipesOne-way communication
Named Pipes (FIFO)Two-way communication
Message QueuesMessages stored in queue
Shared MemoryShared address space
SemaphoresSynchronization
SocketsNetwork communication

Process Generation

Process generation refers to creation of new processes.

Simple Explanation: A running process can create another process.

Parent and Child Process

  • Parent process → creates child
  • Child process → executes independently

Example

  • UNIX fork() system call

Methods of Process Generation

MethodDescription
fork()Creates a copy of process
exec()Replaces process code
wait()Parent waits for child

Reasons for Process Generation

  • Multitasking
  • Parallel execution
  • Better performance

Summary Table

TopicKey Point
Set OperationUsed for resource analysis
Dining PhilosopherDeadlock problem
Sleeping BarberSynchronization problem
IPC ModelsShared memory & message passing
IPC SchemesPipes, queues, semaphores
Process GenerationCreating child processes