Lecture_11-CSCIU511-Jahangir_Majumder-Spring_2025

CSCI U511 01 Operating Systems

  • Instructor: AKM Jahangir A Majumder, PhD

  • Date: February 18, 2025

  • Note: Some slides are adapted from previous instructors and include textbook figures.

Review and Learning Outcomes

  • Finish coverage on Synchronization

  • Introduce Scheduling:

    • Resource Contention: Deadlock

      • Conditions for Deadlock

      • Deadlock Prevention, Avoidance, and Detection Strategy

      • Prediction: Banker’s Algorithm

    • Scheduling Techniques:

      • Uniprocessor: FIFO, SJF, RR, MFQ

      • Multiprocessor: Affinity and Gang

  • Homework 3 posted on Blackboard, due today

  • Upcoming Quiz 3 on Thursday, Feb. 20, covering lectures 6-10

  • Exam 1 on Tuesday, February 25, covering lectures 1-10

  • Sample Exam 1 will be posted on Blackboard soon

Review: Deadlock

  • Definition: Permanent blocking of processes competing for system resources or communicating.

  • A set of processes is deadlocked when each process waits for an event triggered by another blocked process.

  • Characteristics:

    • Permanent as no events are triggered.

    • No efficient general case solution exists.

Deadlock Examples

Bridge Crossing Example

  • Traffic problem: Two cars on a bridge, each occupies a segment and needs to cross.

  • If deadlocked, resolved if one car backs up, potentially requires multiple cars to back up.

  • Risk of starvation exists.

Train Example

  • Circular Dependency: Each train wants to turn right but gets blocked by others.

  • Solution involves extending grid and forcing ordering of channels:

    • Follow east-west first, then north-south (dimension ordering).

Dining Philosophers Problem

  • Five philosophers need two chopsticks to eat, leading to potential deadlocks if all attempt to grab chopsticks simultaneously.

  • Solutions include ensuring at least one philosopher gives up a chopstick to prevent deadlock.

  • Preventive measure: A philosopher cannot take the last chopstick if another philosopher has two chopsticks.

Review: Deadlock Approaches

  • Deadlock Avoidance:

    • Do not grant resource requests if it might lead to deadlock.

  • Deadlock Detection:

    • Grant requests when possible but periodically check for deadlock.

  • No single effective strategy for all deadlock types; common approaches include:

    • Prevention: Disallow one of the three necessary conditions for deadlock.

Deadlock Prevention Strategies

  • Preventing Deadlock:

    • Exploitation: Limit behavior to avoid conditions leading to deadlock.

    • Prediction: Use Banker’s Algorithm to predict potential deadlocks before they occur.

    • Detection/Recovery: Rollback a thread to resolve deadlocks when they occur.

Conditions for Deadlock

  1. Mutual Exclusion: Only one process can use a resource at a time.

  2. Hold-and-Wait: Processes can hold allocated resources while waiting for others.

  3. No Pre-emption: Resources cannot be forcibly taken from processes.

  4. Circular Wait: Closed chain of processes exists, each holding at least one resource needed by another.

Deadlock Prevention Strategies

  • Designing Systems: Exclude deadlock possibility by disallowing necessary conditions:

    • Adjust mutual exclusion policies.

    • Ensure processes request all needed resources at once.

    • Implement rules for resource preemption if a request is denied.

    • Define a linear ordering for resource types to avoid circular wait.

Deadlock Avoidance

  • Permits necessary conditions but makes judicious choices to avoid deadlock.

  • Dynamic decisions based on current resource requests.

  • Requires knowledge of future requests: Banker’s Algorithm ensures system maintains safe state.

Banker’s Algorithm

  • Grant requests only if system leads to a safe state:

    • Total resources available - allocated >= max remaining needed by thread to finish.

  • State of the System:

    • Safe: Possible to grant all future requests without deadlock.

    • Unsafe: Some requests can lead to deadlock.

    • Doomed: All computations lead to deadlock.

Deadlock Dynamics

  • States:

    • Safe State: Possible to fulfill all future requests.

    • Unsafe State: Potential for deadlock exists.

    • Doomed State: All paths lead to deadlock.

Deadlock Avoidance Advantages

  • No need for process preemption and rollback as in deadlock detection strategies.

  • Less restrictive than deadlock prevention approaches.

Deadlock Strategies

  • Preventive Strategies: Restrictive, limit resource access.

  • Detection Strategies: Grant requests whenever possible, check for deadlock periodically.

Example of Thread Locks

  • Two threads using locks: careful handling to avoid nested waiting.

    • Condition variables (CV) wait and signal mechanics by threads is crucial to avoid deadlock situations.

Example Thread Operations

  • Thread 1: Acquire resources in sequence. Careful handling recommended to avoid blocking scenarios.

  • Thread 2: Similar operations, needs careful sequencing to prevent deadlock.

Deadlock Detection Example

  • Handle single instance and multiple instances of resources.

  • System allows deadlock occurrence and then employs detection algorithms for recovery by preempting or terminating processes.

Summary

  • Covered Multi-Object Synchronization and ready to transition into Scheduling.