Presentation is loading. Please wait.

Presentation is loading. Please wait.

CS444/544 Operating Systems II Scheduler

Similar presentations


Presentation on theme: "CS444/544 Operating Systems II Scheduler"— Presentation transcript:

1 CS444/544 Operating Systems II Scheduler
Yeongjin Jang 05/28/19

2 Concurrency Lab 2 Tasks Please ‘fork’ the repository to start…
Resolve deadlock issues Deadlock q1, q2, q3, q4 Implement a simple thread-safe array Please ‘fork’ the repository to start… Due: 6/10

3 Quiz 2 This week Attend any lab session to take, but please do not take more than 1 time.. Please study sample quiz before taking the quiz..

4 Recap: Deadlock Theory
Deadlocks can only happen if threads are having Mutual exclusion Hold-and-wait No preemption Circular wait We can eliminate deadlock by removing such conditions…

5 How to Remove Mutual Exclusion
Do not use lock Use atomic operations instead Replace locks with atomic primitives compare_and_swap(uint64_t *addr, uint64_t prev, uint64_t value); if *addr == prev, then update *addr = value; lock cmpxchg in x86.. void add (int *val, int amt) { do { int old = *value; } while(!CompAndSwap(val, old, old+amt); } void add (int *val, int amt) { Mutex_lock(&m); *val += amt; Mutex_unlock(&m); }

6 Hold-and-Wait Definition
Threads hold resources allocated to them (e.g., locks they have already acquired) while waiting for additional resources (e.g., locks they wish to acquire). Mutex_lock(&setA->lock); Mutex_lock(&setB->lock);

7 How to Remove Hold-and-Wait
Strategy: Acquire all locks atomically once Can release locks over time, but cannot acquire again until all have been released How to do this? Use a meta lock, like this: lock(&meta); lock(&L1); lock(&L2); unlock(&meta); // Critical section code unlock(…);

8 Remove Hold-and-Wait set_t *set_intersection (set_t *s1, set_t *s2) {
Mutex_lock(&meta_lock) Mutex_lock(&s1->lock); Mutex_lock(&s2->lock); Mutex_unlock(&s2->lock); Mutex_unlock(&s1->lock); Mutex_unlock(&meta_lock); }

9 No Preemption lock(A); lock(B); … Definition
Resources (e.g., locks) cannot be forcibly removed from threads that are holding them. lock(A); lock(B); In case if B is acquired by other thread All other threads must wait for acquiring A

10 How to Remove No Preemption
Release the lock if obtaining a resource fails… top: lock(A); if (trylock(B) == -1) { unlock(A); goto top; } Can’t acquire B, then Release A!

11 Circular Wait holds wanted wanted by by holds Definition Thread 1
Lock A Definition There exists a circular chain of threads such that each thread holds a resource (e.g., lock) being requested by next thread in the chain. wanted by wanted by Thread 2 Lock B holds

12 How to Remove Circular Wait

13 How to Remove Circular Wait
Lock variable is mostly a pointer, then provide a correct order of having a lock e.g., if(l1 > l2) { Mutex_lock(l1); Mutex_lock(l2); } else {

14 Scheduler An algorithm that decides which process to run at certain moment in the system’s execution When the scheduler switches the program’s execution? Which process to run for the next cycle? Goal of scheduler Efficiency, fairness, etc. Problems to avoid Starvation

15 Scheduler An algorithm that decides which process to run at certain moment in the system’s execution When the scheduler switches the program’s execution? Which process to run for the next cycle? Goal of scheduler Efficiency, fairness, etc. Problems to avoid Starvation

16 Multi-Programming Run multiple programs on a single CPU simultaneously
Why? Increase CPU utilization / job throughput I/O operations are slow Recv() returns! Recv() JOB 1 Waiting for I/O ops JOB 1 JOB 2 A better CPU Utilization

17 Scheduler An algorithm in OS that determines which job (processes, threads, etc.) to run at certain moment Scheduler runs when: Interrupt occurs (preemptive) A job surrenders its execution rights (non-preemptive) A new job has created

18 Non-preemptive/preemptive
In non-preemptive systems, the scheduler waits until a scheduled process surrenders its execution rights Voluntary context switch E.g., switch if the program calls recv/read, etc. This means that no read/recv, no yield, then the process could run forever… In preemptive systems, the scheduler can interrupt a running process and take back the execution rights Timer Interrupt? Any other I/O interrupts, etc. This is more responsive

19 Goals of a Scheduler Maximize CPU utilization
Maximize job throughput ( # jobs finished / time unit) Supporting a responsive execution (minimize Tfinish – Tstart) Minimize average wait time Maximize average response time Goals depends on the system’s purpose Batch process of a big amount of data Job throughput is important Interactive systems for many small actions Low latency is important

20 First-In-First-Out (FIFO) Scheduler
A non-preemptive scheduler Schedule jobs for their arrival time JOB 1 JOB 1 arrives JOB 2 arrives JOB 3 arrives JOB 2 JOB 3

21 First-In-First-Out (FIFO) Scheduler
A non-preemptive scheduler Schedule jobs for their arrival time Think about the real-world scenario Grocery cashier line Drive-thru JOB 1 JOB 1 arrives JOB 2 arrives JOB 3 arrives JOB 2 JOB 3

22 FIFO Scheduler Pros Cons A fair rule
“Come earlier if you wish to get scheduled…” Cons Long average wait time if long jobs and short jobs are mixed…

23 FIFO Scheduler JOB 1 (10) JOB 2(10) Arrival seq: 1,2,3,4,5 JOB 3 (2)
JOB 1 waits 0 seconds JOB 2 waits 10 seconds JOB 3 waits 20 seconds JOB 4 waits 22 seconds JOB 5 waits 24 seconds JOB 4 (2) JOB 5 (2) AVG: 15.2 seconds of wait time

24 What if we schedule like..
JOB 1 (10) JOB 2(10) Arrival seq: 3,4,5,1,2 JOB 3 (2) JOB 4 (2) JOB 5 (2) JOB 3 (2) JOB 4 (2) JOB 5 (2) JOB 3 waits 0 seconds JOB 4 waits 2 seconds JOB 5 waits 4 seconds JOB 1 waits 6 seconds JOB 2 waits 16 seconds JOB 1 (10) JOB 2 (10) 5.6 vs 15.2 AVG: 5.6 seconds of wait time

25 Shortest Job First (SJF)
Always schedule the job that finishes earlier than others… Also called as Shortest Remaining Job First (SRJF)

26 SJF 5.6 vs 15.2 JOB 1 (10) JOB 2(10) Arrival seq: 3,4,5,1,2 JOB 3 (2)
JOB 3 waits 0 seconds JOB 2 waits 2 seconds JOB 3 waits 4 seconds JOB 4 waits 6 seconds JOB 5 waits 16 seconds JOB 1 (10) JOB 2 (10) 5.6 vs 15.2 AVG: 5.6 seconds of wait time

27 Problems: Job Finish Time
How do we know the job finish time? User program asserts the time What if they extends that???? A simple cheating is possible Set the finish time as 1 Extend that during the execution (another 1) Schedule again and again…

28 Problems: Starvation I am a job that requires 10 seconds to finish. Let me be scheduled! Before its scheduling, one hundred of 1 second job has come… JOB 1 (10) J (1) J (1) J (1) J (1) J (1) J (1) J (1) J (1) J (1) J (1) J (1) J (1) JOB 1 (10) 100….

29 Problems: Starvation After finishing 99 of such small jobs,
Another one hudreds of 1 second job has come… JOB 1 (10) J (1) J (1) J (1) J (1) J (1) J (1) J (1) J (1) J (1) J (1) J (1) J (1) JOB 1 (10) 100….

30 SJF: Starvation A longer job might not be scheduled forever…

31 Round-Robin - Preemptive
A more fair scheduler Each tasks will get a fixed period time (a quantum) for an execution If task does not complete, it goes back to the line Required to pick a time quantum What happens if a quantum is too long, say, 10 seconds? What happens if a quantum is too short, say, 1 nanoseconds?

32 Round-Robin Example JOB 1 (10) JOB 2(10) J1 JOB 3 (2) JOB 4 (2) JOB 5
Fin J2 Fin Fin J1 J2 J1 J2 J1 J2

33 Round-Robin Pros Fair! No starvation Cons Many context-switch


Download ppt "CS444/544 Operating Systems II Scheduler"

Similar presentations


Ads by Google