Threads, CPU Scheduling and Deadlocks - PowerPoint PPT Presentation

About This Presentation
Title:

Threads, CPU Scheduling and Deadlocks

Description:

Chapter 3 Threads, CPU Scheduling and Deadlocks – PowerPoint PPT presentation

Number of Views:146
Avg rating:3.0/5.0
Slides: 23
Provided by: Dei78
Category:

less

Transcript and Presenter's Notes

Title: Threads, CPU Scheduling and Deadlocks


1
Chapter 3
  • Threads, CPU Scheduling and Deadlocks

2
Objectives
  • After finish this chapter, you will understand
  • CPU Scheduling Concept
  • CPU Scheduling Criteria
  • Scheduling Algorithms
  • What is Deadlock Problem?
  • Deadlock Characterization
  • Methods for Handling Deadlocks
  • Deadlock Prevention
  • Deadlock Avoidance

3
I-Basic Concepts of CPU Scheduling
  • CPU scheduling is the basis of multiprogrammed
    operating systems (Maximum CPU utilization)
  • By switching the CPU among processes, the,
    operating system can make the computer more
    productive
  • The success of CPU scheduling depends on the
    following observed property of processes Process
    execution consists of a cycle of CPU execution
    and I/O wait. CPUI/O Burst Cycle or Process
    execution consists of a cycle of CPU execution
    and I/O wait.

4
Alternating Sequence of CPU And I/O Bursts
5
CPU Scheduler
  • Whenever the CPU becomes idle, the operating
    system must select one of the processes in the
    ready queue to be executed.
  • The selection process is carried out by the
    short-term scheduler (or CPU scheduler).
  • The scheduler selects from among the processes in
    memory that are ready to execute, and allocates
    the CPU to one of them.

6
  • CPU scheduling decisions may take place when a
    process
  • 1. Switches from running to waiting state.
  • 2. Switches from running to ready state.
  • 3. Switches from waiting to ready.
  • 4. Terminates.

7
Scheduling Criteria
  • CPU utilization keep the CPU as busy as
    possible
  • Throughput Number of processes that complete
    their execution per time unit
  • Turnaround time amount of time to execute a
    particular process
  • Waiting time amount of time a process has been
    waiting in the ready queue
  • Response time amount of time it takes from when
    a request was submitted until the first response
    is produced, not output.

8
Optimization Criteria
  • Maximized CPU utilization
  • Maximized throughput
  • Minimized turnaround time
  • Minimized waiting time
  • Minimized response time

9
Scheduling Algorithms
  • First Come First Serve (FCFS)
  • Shortest Job First (SJF)
  • Round Robin (RR)
  • ...

10
Scheduling AlgorithmsFirst-Come, First-Served
(FCFS) Scheduling
  • Process Burst Time
  • P1 24
  • P2 3
  • P3 3
  • Suppose that the processes arrive in the order
    P1 , P2 , P3 The Gantt Chart for the schedule
    is
  • Waiting time for P1 0 P2 24 P3 27
  • Average waiting time (0 24 27)/3 17

11
FCFS Scheduling (Cont.)
  • Suppose that the processes arrive in the order
  • P2 , P3 , P1 .
  • The Gantt chart for the schedule is
  • Waiting time for P1 6 P2 0 P3 3
  • Average waiting time (6 0 3)/3 3
  • Much better than previous case. (Shortest Job
    First)

12
Priority Scheduling (Shortest Job First)
  • Associate with each process the length of its
    next CPU burst. Use these lengths to schedule
    the process with the shortest time.
  • SJF is optimal gives minimum average waiting
    time for a given set of processes.
  • SJF is a priority scheduling.

13
Shortest Job First (SJF)
  • Suppose that the processes arrive in the order
  • P2 , P3 , P1 .
  • The Gantt chart for the schedule is
  • Waiting time for P1 6 P2 0 P3 3
  • Average waiting time (6 0 3)/3 3
  • Much better than previous case. (Shortest Job
    First)

14
Round Robin (RR)
  • Each process gets a small unit of CPU time (time
    quantum), usually 10-100 milliseconds. After
    this time has elapsed, the process is preempted
    and added to the end of the ready queue.
  • If there are n processes in the ready queue and
    the time quantum is q, then each process gets 1/n
    of the CPU time in chunks of at most q time units
    at once. No process waits more than (n-1)q time
    units.

15
Example of RR with Time Quantum 20
  • Process Burst Time
  • P1 53
  • P2 17
  • P3 68
  • P4 24
  • The Gantt chart is
  • Typically, higher average turnaround than SJF,
    but better response.

16
II-The Deadlock Problem
  • A set of blocked processes each holding a
    resource and waiting to acquire a resource held
    by another process in the set.
  • Example
  • System has 2 tape drives.
  • P1 and P2 each hold one tape drive and each needs
    another one.
  • Example
  • semaphores A and B, initialized to 1
  • P0 P1
  • wait (A) wait(B)
  • wait (B) wait(A)

17
Bridge Crossing Example
  • Traffic only in one direction.
  • Each section of a bridge can be viewed as a
    resource.
  • If a deadlock occurs, it can be resolved if one
    car backs up (preempt resources and rollback).
  • Several cars may have to be backed up if a
    deadlock occurs.

18
Deadlock Characterization
  • Deadlock can arise if four conditions hold
    simultaneously.
  • Mutual exclusion only one process at a time can
    use a resource.
  • Hold and wait a process holding at least one
    resource is waiting to acquire additional
    resources held by other processes.
  • No preemption a resource can be released only
    voluntarily by the process holding it, after that
    process has completed its task.
  • Circular wait there exists a set P0, P1, ,
    P0 of waiting processes such that P0 is waiting
    for a resource that is held by P1, P1 is waiting
    for a resource that is held by
  • P2, , Pn1 is waiting for a resource that is
    held by Pn, and P0 is waiting for a resource
    that is held by P0.

19
Deadlock Prevention
Restrain the ways request can be made.
  • Mutual Exclusion not required for sharable
    resources must hold for nonsharable resources.
  • Hold and Wait must guarantee that whenever a
    process requests a resource, it does not hold any
    other resources.
  • Require process to request and be allocated all
    its resources before it begins execution, or
    allow process to request resources only when the
    process has none.
  • Low resource utilization starvation possible.

20
Deadlock Prevention (Cont.)
  • No Preemption
  • If a process that is holding some resources
    requests another resource that cannot be
    immediately allocated to it, then all resources
    currently being held are released.
  • Preempted resources are added to the list of
    resources for which the process is waiting.
  • Process will be restarted only when it can regain
    its old resources, as well as the new ones that
    it is requesting.

21
Deadlock Avoidance
Requires that the system has some additional a
priori information available.
  • Simplest and most useful model requires that each
    process declare the maximum number of resources
    of each type that it may need.
  • The deadlock-avoidance algorithm dynamically
    examines the resource-allocation state to ensure
    that there can never be a circular-wait
    condition.
  • Resource-allocation state is defined by the
    number of available and allocated resources, and
    the maximum demands of the processes.

22
Review Questions
  • Do exercise in CPU Process Scheduling.
  • snµt Process enAkñúg Queue List dUcxageRkam³
  • Process Burst Time
  • P1 3
  • P2 5
  • P3 3
  • P4 7
  • P5 8
  • k-cUlKUs Gantt Chat tMNageGay Process nImYy²tam
    FCFS, SJF, RR (time quantum3)?
  • x-cUlKNna Average Waiting Time rbs Process
    nImYy²tam FCFS, SJF, RR (Time quantum3)?
Write a Comment
User Comments (0)
About PowerShow.com