Resource Concepts

Definition

Resources are any components (physical or logical) that processes/threads need to execute and share. Resource management is critical for system stability. Understanding resource concepts is prerequisite for understanding deadlock. Resources can be classified, allocated, and released in various ways.

Types of Resources

1. Physical (Hardware) Resources

Tangible hardware components:

CPU Time:

  • Shared by scheduler
  • Can’t hold and wait indefinitely

Memory (RAM):

  • Allocated to processes
  • May be paged to disk

I/O Devices:

  • Disk drives (read/write)
  • Printers (exclusive use)
  • Network adapters
  • Serial ports (exclusive)

Data/Files:

  • Stored on disk
  • Accessed by multiple processes

2. Logical (Software) Resources

Abstract software concepts:

Locks/Mutexes:

  • Semaphores
  • Condition variables
  • Monitors

Database Records:

  • Locked during transaction
  • Multiple readers, single writer

Message Queues:

  • Limited buffer space
  • Produced/consumed by processes

Licenses/Tokens:

  • License allows N concurrent users
  • Processes get token or wait

Resource Characteristics

1. Renewable (Non-Consumable)

Resource remains after use (same resource can be reused)

Examples:

  • CPU (process uses, another uses)
  • Printer (one job prints, next job can use)
  • Mutex (one thread releases, another acquires)

2. Non-Renewable (Consumable)

Resource consumed and gone (can’t be reused)

Examples:

  • Message packets (consumed when read)
  • Money (spent is gone)
  • Credit (once used, must be renewed)

3. Sharable vs Exclusive

Sharable (Concurrent Use):

  • Read-only files (many can read simultaneously)
  • CPU time (many threads share via scheduling)

Exclusive (One at a Time):

  • Printer (only one job can print)
  • Database table lock (only one writer)
  • Mutex (only one thread holds)

Resource Allocation

Preallocation

Process declared needed resources at start:

Process created with:
- Memory: 512MB
- Disk: 100MB
- Printer: 1 unit

All allocated at once. If not available, wait.

Advantages:

  • ✓ Predictable
  • ✓ Easy to handle (know total needs)

Disadvantages:

  • ❌ Wastes resources (allocated but unused)
  • ❌ Resource fragmentation
  • ❌ Can’t handle dynamic needs

Dynamic Allocation

Process requests resources as needed:

Process starts
→ Requests memory (allocated)
→ Requests file access (allocated)
→ Requests printer (allocated)
→ Finishes, returns all resources

Advantages:

  • ✓ Efficient resource usage
  • ✓ Handles dynamic needs
  • ✓ Better utilization

Disadvantages:

  • ❌ Can cause deadlock (hold-and-wait)
  • ❌ Harder to predict/guarantee

Resource Request and Release

Request Pattern

// Process requests resource
request_resource(resource_id, quantity);
// If available, allocated
// If unavailable, process blocked

Release Pattern

// Process uses resource
use_resource();

// Process releases resource
release_resource(resource_id, quantity);
// Resource returned to system
// Waiting processes notified

Hold-and-Wait Pattern

request_resource(A);    // Get resource A
request_resource(B);    // While holding A, request B
use_resources(A, B);
release_resource(A);
release_resource(B);

This pattern can cause deadlock!

Resource Allocation Graph

Visual representation of resource allocation:

Vertices:
- Process nodes (circles): P1, P2, P3
- Resource nodes (squares): R1, R2, R3

Edges:
- P→R (Request): Process waiting for resource
- R→P (Allocation): Resource held by process

Example:
    P1 ──→ R1 ──→ P2
    ↓             ↑
    R2 ←───────┘

    P3

Meaning:
- P1 holds R2, requests R1
- R1 held by P2
- P2 requests R2
- Cycle: P1→R1→P2→R2→P1 → DEADLOCK!

Cycle Detection

If cycle in Resource Allocation Graph → Deadlock possible

For multiple resources of same type: if cycle exists and all processes in cycle blocked → deadlock.

Resource Pool

System has pool of resources available:

Resource Pool:
Printers: 2 units
Memory: 4GB total (allocated to processes)
Disk: 100GB total

Current Allocation:
P1: 1 printer, 1GB memory
P2: 1 printer, 2GB memory
P3: 0 printers, 0.5GB memory

Available:
Printers: 0
Memory: 0.5GB
Disk: 100GB (all available)

Resource Requests and Deadlock

When Request Granted?

If resource available:

  • Resource allocated immediately
  • Process continues

If resource not available:

  • Request goes to queue
  • Process blocked (moved to waiting state)

Why Requests Cause Deadlock?

Combination of factors:

  1. Hold and Wait: Process holds resources while requesting more
  2. No Preemption: Can’t take resources from holding process
  3. Circular Wait: Process 1 waits for P2’s resource, P2 waits for P1’s resource
  4. Mutual Exclusion: Resource used by one at a time

Resource Classes

Preemptible Resources

Can be taken away from process without harm:

Examples:

  • CPU (context switch)
  • Memory (page out to disk)

Non-Preemptible Resources

Can’t be taken away or process fails:

Examples:

  • Printer (if taken mid-job, print corrupted)
  • Database lock (if taken, transaction fails)
  • Device opened in exclusive mode

Multiple Instances of Same Resource

Some systems have multiple units of same resource:

Printer Pool:
- 5 identical printers

CPU Resources:
- 4 CPU cores

Memory:
- 8GB RAM total

Disk Drives:
- 3 disk drives

Database Connections:
- 20 concurrent connections allowed

Allocation becomes more complex:

  • Process may need multiple units
  • Process may request from available pool

Safe and Unsafe States

Safe State

Resource allocation where no deadlock possible:

Resources available: 4
Allocated to processes: P1=2, P2=1, P3=1
Total: 4 (no free resources)

Safe sequence exists: P1 finishes → releases 2 → P2 finishes → releases 1 → P3 finishes → releases 1

Even though allocated equals total, deadlock avoided!

Unsafe State

State where deadlock possible:

Resources: 2
Allocated: P1=1, P2=1
All resources allocated, no free

If both P1 and P2 request more → DEADLOCK
No process can finish to release resources

Banker’s Algorithm

Prevents deadlock by maintaining safe state

Before granting resource request:

  1. Check if granting request keeps system in safe state
  2. If yes, grant request
  3. If no, deny request (process waits)

(Covered in detail in separate topic)

Summary

Resources are components processes need. Two types of allocation: preallocation (inflexible but predictable) and dynamic (flexible but risky). Dynamic allocation with hold-and-wait pattern can cause deadlock if circular wait develops. Resource allocation graphs visualize resource-process relationships. Safe states prevent deadlock. Understanding resource concepts is prerequisite for deadlock prevention strategies. Modern systems must carefully manage resource allocation to ensure both efficiency and correctness.