CS3210: Processes and switching 1. Taesoo Kim

Similar documents
CS3210: Processes and Switching

CS3210: Multiprocessors and Locking

CS5460/6460: Operating Systems. Lecture 11: Locking. Anton Burtsev February, 2014

INF1060: Introduction to Operating Systems and Data Communication. Pål Halvorsen. Wednesday, September 29, 2010

Problem Set 2. CS347: Operating Systems

Advanced Operating Systems (CS 202) Scheduling (1)

High level scheduling: Medium level scheduling: Low level scheduling. Scheduling 0 : Levels

238P: Operating Systems. Lecture 14: Process scheduling

Computer Systems Laboratory Sungkyunkwan University

CSE 451: Operating Systems Winter Lecture 7 Synchronization. Steve Gribble. Synchronization. Threads cooperate in multithreaded programs

CSCI-GA Operating Systems Lecture 3: Processes and Threads -Part 2 Scheduling Hubertus Franke

CS 318 Principles of Operating Systems

ECE 7650 Scalable and Secure Internet Services and Architecture ---- A Systems Perspective. Part I: Operating system overview: Processes and threads

Preview. Process Scheduler. Process Scheduling Algorithms for Batch System. Process Scheduling Algorithms for Interactive System

Synchronization I. Jo, Heeseung

CSE 153 Design of Operating Systems

CS 571 Operating Systems. Midterm Review. Angelos Stavrou, George Mason University

CSE 451: Operating Systems Winter Lecture 7 Synchronization. Hank Levy 412 Sieg Hall

Scheduling. CS 161: Lecture 4 2/9/17

Course Syllabus. Operating Systems

Building Concurrency Primitives

CS3210: Crash consistency

Process management. Scheduling

CS3210: Coordination. Tim Andersen

CS A320 Operating Systems for Engineers

CS 153 Design of Operating Systems Winter 2016

Context Switching & CPU Scheduling

Scheduling, part 2. Don Porter CSE 506

Process Scheduling. Copyright : University of Illinois CS 241 Staff

CSE 120 Principles of Operating Systems Spring 2017

10/17/ Gribble, Lazowska, Levy, Zahorjan 2. 10/17/ Gribble, Lazowska, Levy, Zahorjan 4

Processes. Overview. Processes. Process Creation. Process Creation fork() Processes. CPU scheduling. Pål Halvorsen 21/9-2005

CS A331 Programming Language Concepts

CSL373: Lecture 5 Deadlocks (no process runnable) + Scheduling (> 1 process runnable)

Midterm Exam Amy Murphy 19 March 2003

Scheduling. Scheduling 1/51

CS370 Operating Systems

CSE120 Principles of Operating Systems. Prof Yuanyuan (YY) Zhou Scheduling

CS 326: Operating Systems. CPU Scheduling. Lecture 6

INF1060: Introduction to Operating Systems and Data Communication. Operating Systems: Processes & CPU Scheduling. Thursday 22 September 16

CPU Scheduling. CSE 2431: Introduction to Operating Systems Reading: Chapter 6, [OSC] (except Sections )

SMD149 - Operating Systems

Lecture Topics. Announcements. Today: Uniprocessor Scheduling (Stallings, chapter ) Next: Advanced Scheduling (Stallings, chapter

CSL373: Lecture 6 CPU Scheduling

Operating Systems. Synchronization

Advanced Operating Systems (CS 202) Scheduling (1) Jan, 23, 2017

OS 1 st Exam Name Solution St # (Q1) (19 points) True/False. Circle the appropriate choice (there are no trick questions).

Operating Systems: Quiz2 December 15, Class: No. Name:

Scheduling. Scheduling 1/51

Lecture 9: Midterm Review

Comparison of soft real-time CPU scheduling in Linux kernel 2.6 series with Solaris 10

Scheduling policy. Reference: ULK3e 7.1. Scheduling in Linux 1 / 20

CSE 120 Principles of Operating Systems

CS370 Operating Systems

Scheduling - Overview

CS 318 Principles of Operating Systems

Operating Systems ECE344. Ding Yuan

Midterm Exam. October 20th, Thursday NSC

Recap. Run to completion in order of arrival Pros: simple, low overhead, good for batch jobs Cons: short jobs can stuck behind the long ones

Last Class: CPU Scheduling! Adjusting Priorities in MLFQ!

Chapter 6: CPU Scheduling

CS3210: Virtual memory. Taesoo Kim w/ minor updates K. Harrigan

CS3210: Virtual memory applications. Taesoo Kim

[537] Locks. Tyler Harter

CSE 120 Principles of Operating Systems

CPU Scheduling. The scheduling problem: When do we make decision? - Have K jobs ready to run - Have N 1 CPUs - Which jobs to assign to which CPU(s)

Synchronization I. Jin-Soo Kim Computer Systems Laboratory Sungkyunkwan University

Processes. CS 475, Spring 2018 Concurrent & Distributed Systems

W4118: advanced scheduling

CPU Scheduling. Operating Systems (Fall/Winter 2018) Yajin Zhou ( Zhejiang University

Concurrent Programming

Precept 2: Non-preemptive Scheduler. COS 318: Fall 2018

Chapter 5: CPU Scheduling

CSCE Operating Systems Scheduling. Qiang Zeng, Ph.D. Fall 2018

Scheduling. CSC400 - Operating Systems. 7: Scheduling. J. Sumey. one of the main tasks of an OS. the scheduler / dispatcher

Threads. Concurrency. What it is. Lecture Notes Week 2. Figure 1: Multi-Threading. Figure 2: Multi-Threading

Linux process scheduling. David Morgan

Review. Preview. Three Level Scheduler. Scheduler. Process behavior. Effective CPU Scheduler is essential. Process Scheduling

Starting the Threads

CPU Scheduling. Daniel Mosse. (Most slides are from Sherif Khattab and Silberschatz, Galvin and Gagne 2013)

Operating system concepts. Task scheduling

Operating Systems. Process scheduling. Thomas Ropars.

CS3733: Operating Systems

Chapter 5: CPU Scheduling

CS 537 Lecture 11 Locks

ò mm_struct represents an address space in kernel ò task represents a thread in the kernel ò A task points to 0 or 1 mm_structs

OS Structure. User mode/ kernel mode (Dual-Mode) Memory protection, privileged instructions. Definition, examples, how it works?

CSE 120. Fall Lecture 8: Scheduling and Deadlock. Keith Marzullo

Scheduling. Don Porter CSE 306

Concurrent Programming. Implementation Alternatives. Content. Real-Time Systems, Lecture 2. Historical Implementation Alternatives.

Lecture 5 / Chapter 6 (CPU Scheduling) Basic Concepts. Scheduling Criteria Scheduling Algorithms

Last Class: Processes

CS 471 Operating Systems. Yue Cheng. George Mason University Fall 2017

CS370 Operating Systems

Scheduling Mar. 19, 2018

Synchronization Spinlocks - Semaphores

Concurrency: Mutual Exclusion (Locks)

NuttX Realtime Programming

CS370 Operating Systems

SFO The Linux Kernel Scheduler. Viresh Kumar (PMWG)

Transcription:

1 CS3210: Processes and switching 1 Taesoo Kim

2 Administrivia (Mar 17) Team Proposal Day (just slides, 3-5 min/team) Problem statement Idea Demo plan (aka evaluation) Timeline DUE : submit slides (as a team) by 10 pm, Mar 16 NOTE : if you submit one by 10 pm, Mar 15, we will give you early feedback near midnight (Mar 15)

3 Summary of last lectures Power-on BIOS bootloader kernel user programs OS: abstraction, multiplexing, isolation, sharing Design: monolithic (xv6) vs. micro kernels (jos) Abstraction: process, system calls Isolation mechanisms: CPL, segmentation, paging

4 Today's plan A few notes on locking in xv6 (cont.) About process For multiplexing (e.g., more processes than CPUs) In particular, switching and scheduling

5 Locks Mutual exclusion : only one core can hold a given lock concurrent access to the same memory location, at least one write example: acquire(l); x = x + 1; release(l);

6 Example: why do we need a lock? 00 struct file* filealloc(void) { 01 struct file *f; 02 03 acquire(&ftable.lock); 04 for(f = ftable.file; f < ftable.file + NFILE; f++){ 05 if(f->ref == 0){ 06 f->ref = 1; 07 release(&ftable.lock); 08 return f; 09 } 10 } 11 release(&ftable.lock); 12 return 0; 13 }

7 Locks Mutual exclusion : only one core can hold a given lock concurrent access to the same memory location, at least one write example: acquire(l); x = x + 1; release(l); Atomic execution : hide intermediate state another example: transfer money from account A to B put(a + 100) and put(b - 100) must be both effective, or neither

8 A different way to think about locks Locks help operations maintain invariants on a data structure assume the invariants are true at start of operation operation uses locks to hide temporary violation of invariants operation restores invariants before releasing locks Q: put(a + 100) and put(b - 100)?

9 Strawman: locking 01 struct lock { int locked; }; 02 03 void acquire(struct lock *l) { 04 for (;;) { 05 if (l->locked == 0) { // A: test 06 l->locked = 1; // B: set 07 return; 08 } 09 } 10 } 11 12 void release(struct lock *l) { 13 l->locked = 0; 14 }

10 Problem: concurrent executions on line 05 // process A // process B if (l->locked == 0) if (l->locked == 0) l->locked = 1; l->locked = 1; Recall: $ while true; do./count 2 10 grep 10 ; done cpu = 2, count = 10...

11 Relying on an atomic operation 01 struct lock { int locked; }; 02 03 void acquire(struct lock *l) { 04 for (;;) { 05 if (xchg(&l->locked, 1) == 0) 06 return; 07 } 08 } 09 10 void release(struct lock *l) { 11 // Q? 12 xchg(&l->locked, 0); 13 }

12 Spinlock in xv6 Pretty much same, but provide debugging info 01 struct spinlock { 02 uint locked; // Is the lock held? 03 04 // Q? 05 char *name; // Name of lock. 06 struct cpu *cpu; // The cpu holding the lock. 07 uint pcs[10]; // The call stack (an array of program counters) 08 // that locked the lock. 09 };

13 acquire() in xv6 01 void acquire(struct spinlock *lk) { 02 // Q1? 03 pushcli(); 04 // Q2? 05 if (holding(lk)) 06 panic("acquire"); 07 08 while (xchg(&lk->locked, 1)!= 0) 09 ; 10 11 lk->cpu = cpu; 12 getcallerpcs(&lk, lk->pcs); 13 }

14 release() in xv6 01 void release(struct spinlock *lk) { 02 // Q1? 03 if (!holding(lk)) 04 panic("release"); 05 06 // Q2? 07 lk->pcs[0] = 0; 08 lk->cpu = 0; 09 10 xchg(&lk->locked, 0); 11 12 // Q3? 13 popcli(); 14 }

15 Why spinlocks? Q: don't they waste CPU while waiting? Q: why not give up the CPU and switch to another process, let it run? Q: what if holding thread needs to run; shouldn't you yield CPU?

16 Spinlock guidelines hold for very short times don't yield CPU while holding lock (un)fairness issues: FIFO ordering? NOTE "blocking" locks for longer critical sections waiting threads yield the CPU but overheads are typically higher (later)

17 Problem 1: deadlock (e.g., double acquire) Q: what happens in xv6? 01 struct spinlock lk; 02 initlock(&lk, "test lock"); 03 acquire(&lk); 04 acquire(&lk);

18 Problem 2: interrupt (preemption) Race in iderw() ( ide.c) sti() after acquire() cli() before release()

19 Q: iderw() Q: what goes wrong with adding sti/cli in iderw? Q: what ensures atomicity between processors Q: what ensures atomicity within a single processor?

20 What about racing in file.c Race in filealloc() ( file.c) Q: ftable.lock? sti() after acquire() cli() before release()

21 Q: filealloc() Q: could the disk interrupt handler run while interrupts are enabled? Q: does any any interrupt handler grab the ftable.lock? Q: what interrupt could cause trouble?

22 Scheduling Which process to run? Pick one from a set of RUNNABLE processes (or env in jos) Q: what have you seen from lab? (next lecture) Switching/scheduling in detail

23 Scheduling: design space Q: Preemptive vs. cooperative? Q: Global queue vs. per-cpu queue?

24 Scheduling: design space Scalability: w/ many runnable processes? Granularity (timeslice, quantum): 10ms vs 100ms? (dynamic? tickless?) Fairness: time quota, epoch (inversion? group?) QoS: priority? (e.g., nice) Constraints: realtime, deadlines (e.g., airplane) etc: resource starvation, performance consolidation (e.g., cloud)

25 Scheduling: difficult in practice No perfect/universal solution/policy Contradicting goals: maximizing throughput vs. minimizing latency minimizing response time vs. maximizing scalability maximizing fairness vs. maximizing scalability

26 Example: round-robin scheduling Simple: assign fixed time unit per process Starvation-free (no priority)

27 Complexity in real scheduling algorithms Linux?

28 Complexity in real scheduling algorithms Linux kernel/sched/*.c: 17k LoC with 7k lines of comments vs. your RR in jos? 10 LoC? 01 for (j = 1; j <- NENV; j++) { 02 k = (j + i) % NENV; 03 if (envs[k].env_status == ENV_RUNNABLE) 04 env_run(&envs[k]); 05 }

Summary (Wikipedia) 29

30 Example: available options in Linux $ sudo sysctl -A grep "sched" grep -v "domain" kernel.sched_child_runs_first = 0 kernel.sched_latency_ns = 18000000 kernel.sched_migration_cost_ns = 500000 kernel.sched_min_granularity_ns = 2250000 kernel.sched_rr_timeslice_ms = 30 kernel.sched_rt_period_us = 1000000 kernel.sched_rt_runtime_us = 950000 kernel.sched_shares_window_ns = 10000000 kernel.sched_time_avg_ms = 1000 kernel.sched_wakeup_granularity_ns = 3000000... $ less /proc/sched_debug $ less /proc/[pid]/sched

31 Characterizing processes CPU-bound vs IO-bound Interactive processes (e.g., vim, emacs) Batch processes (e.g., cronjob) Real-time processes (e.g., audio/video players)

32 Scheduling policies in Linux SCHED_FIFO : first in, first out, real time processes SCHED_RR : round robin real time processes SCHED_OTHER : normal time/schedule sharing (default) SCHED_BATCH : CPU intensive processes SCHED_IDLE : Very low prioritized processes

33 Example Q: count.c? $ sudo./count 3 1000000000 8522: runs 8524: runs 8523: runs 8523: 2.05 sec 8522: 2.34 sec 8524: 2.49 sec

34 Example: available policies $ chrt -m SCHED_OTHER min/max priority : 0/0 SCHED_FIFO min/max priority : 1/99 SCHED_RR min/max priority : 1/99 SCHED_BATCH min/max priority : 0/0 SCHED_IDLE min/max priority : 0/0

35 Example: FIFO (real time scheduling) $ sudo./count 10 1000000000 "chrt -f -p 99"...

36 References Intel Manual UW CSE 451 OSPP MIT 6.828 Wikipedia The Internet