CS510 Operating System Foundations. Jonathan Walpole

Size: px
Start display at page:

Download "CS510 Operating System Foundations. Jonathan Walpole"

Transcription

1 CS510 Operating System Foundations Jonathan Walpole

2 Project 3 Part 1: The Sleeping Barber problem - Use semaphores and mutex variables for thread synchronization - You decide how to test your code!! We re not providing the sample output for this one! Part 2: The Gaming Parlor problem - Use monitors and condition variables for synchronization - Avoid deadlock when handling resource requests - Sample output given in handout 2

3 Scheduling 3

4 Process State Model New Process Ready Running Termination Blocked 4

5 CPU Scheduling Criteria CPU Utilization how busy is the CPU? Throughput how many jobs finished/unit time? Turnaround Time how long from job submission to job termination? Response Time how long does it take to get a response Missed deadlines 5

6 Broad Classes of Scheduler Priority-based? - Threads have priorities - Priorities can be assigned statically or dynamically Preemptive? - Threads can be switched at any time 6

7 Example Scheduling Policies First-Come, First Served (FIFO) Shortest Job First (non-preemptive) Shortest Job First (with preemption) Round-Robin Scheduling Priority Scheduling Real-Time Scheduling 7

8 First-Come, First-Served (FIFO) Start jobs in the order they arrive (FIFO queue) Run each job until completion 8

9 First-Come, First-Served (FIFO) Arrival Processing Process Time Time 9

10 First-Come, First-Served (FIFO) Arrival Processing Turnaround Process Time Time Delay Time 10

11 First-Come, First-Served (FIFO) Arrival Processing Turnaround Process Time Time Delay Time Arrival Times of the Jobs 11

12 First-Come, First-Served (FIFO) Arrival Processing Turnaround Process Time Time Delay Time 12

13 First-Come, First-Served (FIFO) Arrival Processing Turnaround Process Time Time Delay Time 13

14 First-Come, First-Served (FIFO) Arrival Processing Turnaround Process Time Time Delay Time 14

15 First-Come, First-Served (FIFO) Arrival Processing Turnaround Process Time Time Delay Time 15

16 First-Come, First-Served (FIFO) Arrival Processing Turnaround Process Time Time Delay Time 16

17 First-Come, First-Served (FIFO) Arrival Processing Turnaround Process Time Time Delay Time 17

18 First-Come, First-Served (FIFO) Arrival Processing Turnaround Process Time Time Delay Time 18

19 First-Come, First-Served (FIFO) Arrival Processing Turnaround Process Time Time Delay Time 19

20 First-Come, First-Served (FIFO) Arrival Processing Turnaround Process Time Time Delay Time 20

21 First-Come, First-Served (FIFO) Arrival Processing Turnaround Process Time Time Delay Time 21

22 First-Come, First-Served (FIFO) Arrival Processing Turnaround Process Time Time Delay Time 22

23 First-Come, First-Served (FIFO) Arrival Processing Turnaround Process Time Time Delay Time 23

24 First-Come, First-Served (FIFO) Arrival Processing Turnaround Process Time Time Delay Time

25 First-Come, First-Served (FIFO) Arrival Processing Turnaround Process Time Time Delay Time

26 Shortest Job First q Select the job with the shortest (expected) running time q Non-Preemptive 26

27 Shortest Job First Arrival Processing Turnaround Process Time Time Delay Time Same Job Mix 27

28 Shortest Job First Arrival Processing Turnaround Process Time Time Delay Time 28

29 Shortest Job First Arrival Processing Turnaround Process Time Time Delay Time 29

30 Shortest Job First Arrival Processing Turnaround Process Time Time Delay Time 30

31 Shortest Job First Arrival Processing Turnaround Process Time Time Delay Time 31

32 Shortest Job First Arrival Processing Turnaround Process Time Time Delay Time 32

33 Shortest Job First Arrival Processing Turnaround Process Time Time Delay Time 33

34 Shortest Job First Arrival Processing Turnaround Process Time Time Delay Time 34

35 Shortest Job First Arrival Processing Turnaround Process Time Time Delay Time 35

36 Shortest Job First Arrival Processing Turnaround Process Time Time Delay Time 36

37 Shortest Job First Arrival Processing Turnaround Process Time Time Delay Time 37

38 Shortest Job First Arrival Processing Turnaround Process Time Time Delay Time 38

39 Shortest Job First Arrival Processing Turnaround Process Time Time Delay Time 39

40 Shortest Job First Arrival Processing Turnaround Process Time Time Delay Time

41 Shortest Job First Arrival Processing Turnaround Process Time Time Delay Time

42 Shortest Remaining Time Preemptive version of SJF Arrival Processing Turnaround Process Time Time Delay Time Same Job Mix 42

43 Shortest Remaining Time Arrival Processing Turnaround Process Time Time Delay Time 43

44 Shortest Remaining Time Arrival Processing Turnaround Process Time Time Delay Time 44

45 Shortest Remaining Time Arrival Processing Turnaround Process Time Time Delay Time 45

46 Shortest Remaining Time Arrival Processing Turnaround Process Time Time Delay Time 46

47 Shortest Remaining Time Arrival Processing Turnaround Process Time Time Delay Time 47

48 Shortest Remaining Time Arrival Processing Turnaround Process Time Time Delay Time 48

49 Shortest Remaining Time Arrival Processing Turnaround Process Time Time Delay Time 49

50 Shortest Remaining Time Arrival Processing Turnaround Process Time Time Delay Time 50

51 Shortest Remaining Time Arrival Processing Turnaround Process Time Time Delay Time 51

52 Shortest Remaining Time Arrival Processing Turnaround Process Time Time Delay Time 52

53 Shortest Remaining Time Arrival Processing Turnaround Process Time Time Delay Time 53

54 Shortest Remaining Time Arrival Processing Turnaround Process Time Time Delay Time 54

55 Shortest Remaining Time Arrival Processing Turnaround Process Time Time Delay Time 55

56 Shortest Remaining Time Arrival Processing Turnaround Process Time Time Delay Time

57 Shortest Remaining Time Arrival Processing Turnaround Process Time Time Delay Time

58 Round-Robin Scheduling Goal: Enable interactivity Limit the amount of CPU that a thread can have at one time. Time quantum Amount of time the OS gives a thread before intervention Sometimes called the time slice Typically: 1 to 100ms Not necessarily the same as the timer interrupt frequency! 58

59 Round-Robin Scheduling Arrival Processing Process Time Time 59

60 Round-Robin Scheduling Arrival Processing Process Time Time Ready List: 60

61 Round-Robin Scheduling Arrival Processing Process Time Time Ready List: 61

62 Round-Robin Scheduling Arrival Processing Process Time Time Ready List: 62

63 Round-Robin Scheduling Arrival Processing Process Time Time Ready List: 63

64 Round-Robin Scheduling Arrival Processing Process Time Time Ready List: 64

65 Round-Robin Scheduling Arrival Processing Process Time Time Ready List: 65

66 Round-Robin Scheduling Arrival Processing Process Time Time Ready List: 66

67 Round-Robin Scheduling Arrival Processing Process Time Time Ready List: 67

68 Round-Robin Scheduling Arrival Processing Process Time Time Ready List: 68

69 Round-Robin Scheduling Arrival Processing Process Time Time Ready List: 69

70 Round-Robin Scheduling Arrival Processing Process Time Time Ready List: 70

71 Round-Robin Scheduling Arrival Processing Process Time Time Ready List: 71

72 Round-Robin Scheduling Arrival Processing Process Time Time Ready List: 72

73 Round-Robin Scheduling Arrival Processing Process Time Time Ready List: 73

74 Round-Robin Scheduling Arrival Processing Process Time Time Ready List: 74

75 Round-Robin Scheduling Arrival Processing Process Time Time Ready List: 75

76 Round-Robin Scheduling Arrival Processing Process Time Time Ready List: 76

77 Round-Robin Scheduling Arrival Processing Process Time Time Ready List: 77

78 Round-Robin Scheduling Arrival Processing Process Time Time Ready List: 78

79 Round-Robin Scheduling Arrival Processing Process Time Time Ready List: 79

80 Round-Robin Scheduling Arrival Processing Process Time Time Ready List: 80

81 Round-Robin Scheduling Arrival Processing Process Time Time Ready List: 81

82 Round-Robin Scheduling Arrival Processing Process Time Time Ready List: 82

83 Round-Robin Scheduling Arrival Processing Process Time Time Ready List: 83

84 Round-Robin Scheduling Arrival Processing Process Time Time Ready List: 84

85 Round-Robin Scheduling Arrival Processing Process Time Time 85

86 Round-Robin Scheduling Arrival Processing Turnaround Process Time Time Delay Time

87 Round-Robin Scheduling Effectiveness of round-robin depends on - The number of threads, and - The size of the time quantum. Large # of threads means that the time between scheduling of a single thread increases - Slow responses Larger time quantum means that the time between the scheduling of a single thread also increases - Slow responses Smaller time quantum means higher processing rates but also more overhead! 87

88 General Purpose Schedulers 88

89 Priority Scheduling Assign a priority (number) to each thread Schedule threads based on their priority Higher priority threads get more CPU time Starvation is possible! Managing priorities Can use nice to voluntarily reduce your priority Scheduler can periodically adjust a process priority - Prevents starvation of a lower priority process - Can improve performance of I/O-bound processes by basing priority on fraction of last quantum used 89

90 Multi-Level Queue Scheduling Multiple queues, each with its own priority Equivalently: each priority level has its own ready queue Round-robin scheduling is used within each queue Simplest approach uses statically assigned priorities High priority Low priority CPU 90

91 Multi-Level Feedback Scheduling Problem: with fixed priorities I/O-bound processes are disadvantaged Can we fix this with dynamic priorities? 91

92 Multi-Level Feedback Scheduling Solution: Let the amount of CPU that a thread used in the last quantum determine its scheduling priority for the next Expired time quantum à decrease priority Unexpired time quantum à increase priority Rationale: if a thread didn t use all of its time (because it blocked for I/O) the system owes it some more CPU time Also, if a thread is compute-bound, raising its priority risks it starving the other threads 92

93 Multi-Level Feedback Scheduling N priority levels, round-robin scheduling within a level Should Quanta increase as priority decreases? Are high priority threads more likely to be interactive? What stops starvation??? High priority CPU Low priority 93

94 Proportional Share Scheduling The amount of CPU a thread gets is a separate concern from how urgently it must run Latency and throughput are distinct scheduling concerns

95 Lottery Scheduling A kind of proportional share scheduling by chance Scheduler gives each thread some lottery tickets To select the next process to run... - The scheduler randomly selects a lottery number - The winning process gets to run Example Thread A gets 50 tickets Thread B gets 15 tickets Thread C gets 35 tickets There are 100 tickets outstanding 95

96 Lottery Scheduling A kind of proportional share scheduling Scheduler gives each thread some lottery tickets. To select the next process to run... - The scheduler randomly selects a lottery number - The winning process gets to run Example Thread A gets 50 tickets 50% of CPU Thread B gets 15 tickets 15% of CPU Thread C gets 35 tickets 35% of CPU There are 100 tickets outstanding 96

97 Proportion-Period Scheduling No need for randomization Threads should be able to get throughput and latency guarantees Necessary in time-sensitive applications

98 Real-Time Scheduling Typically real-time systems involve several steps that are not in traditional systems Admission control - All processes must ask for resources ahead of time - If sufficient resources exist, the job is admitted into the system. Resource allocation - Upon admission, the appropriate resources need to be reserved for the task. Resource enforcement - Carry out the resource allocations properly 98

99 Real-Time Scheduling Assume processes are relatively periodic - Fixed amount of work per period (e.g. sensor systems or multimedia data) - Allows for specification of requirements 99

100 Real-Time Scheduling Two main types of schedulers... Rate-Monotonic Schedulers - Assign a fixed, unchanging priority to each process based on its requirements specification - No dynamic adjustment of priorities Earliest-Deadline-First Schedulers - Assign dynamic priorities based upon deadlines 100

101 Rate Monotonic Schedulers For preemptable, periodic processes (tasks) Assigns a fixed priority to each task T = The period of the task C = The amount of processing per task period Process P1 In RMS scheduling, the question to answer is... T = 1 second C = 1/2 second / period - What priority should be assigned to a given task? 101

102 Rate Monotonic Schedulers P1 P2 102

103 Rate Monotonic Schedulers P1 P2 P1 PRI > P2 PRI P2 PRI > P1 PRI 103

104 Rate Monotonic Schedulers Which is best? P1 P2 P1 PRI > P2 PRI P2 PRI > P1 PRI 104

105 Rate Monotonic Schedulers Assign shortest period tasks to the highest priorities Admission control is difficult... < 69% There is a standard formula > 69% It may be possible, but need to do more complex analysis 105

106 Rate Monotonic Schedulers Assumptions: - Processes complete (yield) within their period - Independent processes - Same CPU requirements per burst - Other non-periodic processes have no deadlines - Instantaneous preemption with no overhead 106

107 Earliest Deadline First When processes do not have periodic execution or constant CPU requirements... When processes have deadline specifications... Unlike RMS, EDF uses dynamic priorities (based upon earliest deadline first) (+) 100% processor utilization (-) Need to keep track of deadlines Admission Control Just check to see if 100% processor utilization. Sum the C i /T i s and see if less than or equal to 1 What about overhead? 107

108 Quiz What are the main tasks of the scheduler? What is the difference between preemptive and non-preemptive scheduling? What is the advantage of a shorter scheduling quantum? What is the advantage of a longer scheduling quantum? Why is feedback scheduling useful for interactive jobs? Are these scheduling policies subject to starvation? - Shortest Job First scheduling? - Round Robin scheduling? - First Come First Served scheduling? - Priority scheduling? - Earliest Deadline First scheduling? 108