Avishai Wool lecture 2 - 1 Introduction to Systems Programming Lecture 2 Processes & Scheduling.

Slides:



Advertisements
Similar presentations
CPU Scheduling.
Advertisements

 Basic Concepts  Scheduling Criteria  Scheduling Algorithms.
Silberschatz, Galvin and Gagne ©2009 Operating System Concepts – 8 th Edition Chapter 5: CPU Scheduling.
Chap 5 Process Scheduling. Basic Concepts Maximum CPU utilization obtained with multiprogramming CPU–I/O Burst Cycle – Process execution consists of a.
Chapter 5 CPU Scheduling. CPU Scheduling Topics: Basic Concepts Scheduling Criteria Scheduling Algorithms Multiple-Processor Scheduling Real-Time Scheduling.
CPU Scheduling CS 3100 CPU Scheduling1. Objectives To introduce CPU scheduling, which is the basis for multiprogrammed operating systems To describe various.
Chapter 3: CPU Scheduling
Silberschatz, Galvin and Gagne ©2009 Operating System Concepts – 8 th Edition, Chapter 5: CPU Scheduling.
CS 311 – Lecture 23 Outline Kernel – Process subsystem Process scheduling Scheduling algorithms User mode and kernel mode Lecture 231CS Operating.
Scheduling in Batch Systems
Chapter 6: CPU Scheduling. 5.2 Silberschatz, Galvin and Gagne ©2005 Operating System Concepts – 7 th Edition, Feb 2, 2005 Chapter 6: CPU Scheduling Basic.
Silberschatz, Galvin and Gagne  Operating System Concepts Chapter 6: CPU Scheduling Basic Concepts Scheduling Criteria Scheduling Algorithms.
What we will cover…  CPU Scheduling  Basic Concepts  Scheduling Criteria  Scheduling Algorithms  Evaluations 1-1 Lecture 4.
Chapter 5-CPU Scheduling
02/11/2004CSCI 315 Operating Systems Design1 CPU Scheduling Algorithms Notice: The slides for this lecture have been largely based on those accompanying.
Chapter 5: CPU Scheduling. 5.2 Silberschatz, Galvin and Gagne ©2005 Operating System Concepts Chapter 5: CPU Scheduling Basic Concepts Scheduling Criteria.
Silberschatz, Galvin and Gagne  Operating System Concepts Chapter 6: CPU Scheduling Basic Concepts Scheduling Criteria Scheduling Algorithms.
Chapter 5: CPU Scheduling. 5.2 Silberschatz, Galvin and Gagne ©2005 Operating System Concepts – 7 th Edition, Feb 2, 2005 Basic Concepts Maximum CPU utilization.
Chapter 6: CPU Scheduling
CS212: OPERATING SYSTEM Lecture 3: Process Scheduling 1.
Avishai Wool lecture 2 1 Introduction to Systems Programming Lecture 2 Processes & Scheduling.
Silberschatz, Galvin and Gagne  Operating System Concepts Chapter 6: CPU Scheduling Basic Concepts Scheduling Criteria Scheduling Algorithms.
Scheduling. Alternating Sequence of CPU And I/O Bursts.
Silberschatz and Galvin  Operating System Concepts Module 5: CPU Scheduling Basic Concepts Scheduling Criteria Scheduling Algorithms Multiple-Processor.
Alternating Sequence of CPU And I/O Bursts. Histogram of CPU-burst Times.
CPU Scheduling Basic Concepts Scheduling Criteria Scheduling Algorithms Thread Scheduling Multiple-Processor Scheduling Operating Systems Examples Algorithm.
Silberschatz, Galvin and Gagne ©2009 Operating System Concepts – 8 th Edition, Chapter 5: CPU Scheduling.
CE Operating Systems Lecture 7 Threads & Introduction to CPU Scheduling.
Silberschatz, Galvin and Gagne  Operating System Concepts Chapter 6: CPU Scheduling Basic Concepts Scheduling Criteria Scheduling Algorithms.
Silberschatz, Galvin and Gagne ©2009 Operating System Concepts – 8 th Edition, Lecture 7: CPU Scheduling Chapter 5.
Chapter 5: CPU Scheduling. 5.2 Silberschatz, Galvin and Gagne ©2005 Operating System Concepts Chapter 5: CPU Scheduling Basic Concepts Scheduling Criteria.
Chapter 5: Process Scheduling. 5.2 Silberschatz, Galvin and Gagne ©2005 Operating System Concepts Basic Concepts Maximum CPU utilization can be obtained.
1 11/29/2015 Chapter 6: CPU Scheduling l Basic Concepts l Scheduling Criteria l Scheduling Algorithms l Multiple-Processor Scheduling l Real-Time Scheduling.
Chapter 5: CPU Scheduling. 5.2 Silberschatz, Galvin and Gagne ©2005 Operating System Concepts – 7 th Edition, Feb 2, 2005 Chapter 5: CPU Scheduling Basic.
Silberschatz and Galvin  Operating System Concepts Module 5: CPU Scheduling Basic Concepts Scheduling Criteria Scheduling Algorithms Multiple-Processor.
6.1 CPU Scheduling Basic Concepts Scheduling Criteria Scheduling Algorithms Multiple-Processor Scheduling Real-Time Scheduling Algorithm Evaluation.
Purpose of Operating System Part 2 Monil Adhikari.
1 CPU Scheduling Basic Concepts Scheduling Criteria Scheduling Algorithms Multiple-Processor Scheduling Real-Time Scheduling.
Introduction to Operating System Created by : Zahid Javed CPU Scheduling Fifth Lecture.
1 Uniprocessor Scheduling Chapter 3. 2 Alternating Sequence of CPU And I/O Bursts.
Lecture 4 CPU scheduling. Basic Concepts Single Process  one process at a time Maximum CPU utilization obtained with multiprogramming CPU idle :waiting.
CPU Scheduling G.Anuradha Reference : Galvin. CPU Scheduling Basic Concepts Scheduling Criteria Scheduling Algorithms Multiple-Processor Scheduling Real-Time.
CPU scheduling.  Single Process  one process at a time  Maximum CPU utilization obtained with multiprogramming  CPU idle :waiting time is wasted 2.
1 Module 5: Scheduling CPU Scheduling Scheduling Algorithms Reading: Chapter
Basic Concepts Maximum CPU utilization obtained with multiprogramming
1 Lecture 5: CPU Scheduling Operating System Fall 2006.
Lecturer 5: Process Scheduling Process Scheduling  Criteria & Objectives Types of Scheduling  Long term  Medium term  Short term CPU Scheduling Algorithms.
Chapter 5: CPU Scheduling. 5.2 Silberschatz, Galvin and Gagne ©2005 Operating System Concepts Chapter 5: CPU Scheduling Basic Concepts Scheduling Criteria.
1 Chapter 5: CPU Scheduling. 2 Basic Concepts Scheduling Criteria Scheduling Algorithms.
Silberschatz, Galvin and Gagne  Operating System Concepts Chapter 6: CPU Scheduling Basic Concepts Scheduling Criteria Scheduling Algorithms.
Chapter 6: CPU Scheduling
Dan C. Marinescu Office: HEC 439 B. Office hours: M, Wd 3 – 4:30 PM.
Chapter 5a: CPU Scheduling
CPU Scheduling.
Chapter 6: CPU Scheduling
Process management Information maintained by OS for process management
Chapter 5: CPU Scheduling
CPU Scheduling G.Anuradha
Chapter 6: CPU Scheduling
Module 5: CPU Scheduling
3: CPU Scheduling Basic Concepts Scheduling Criteria
Chapter5: CPU Scheduling
Chapter 6: CPU Scheduling
Chapter 5: CPU Scheduling
Operating System , Fall 2000 EA101 W 9:00-10:00 F 9:00-11:00
Chapter 6: CPU Scheduling
Module 5: CPU Scheduling
Chapter 6: CPU Scheduling
CPU Scheduling: Basic Concepts
Module 5: CPU Scheduling
Presentation transcript:

Avishai Wool lecture Introduction to Systems Programming Lecture 2 Processes & Scheduling

Avishai Wool lecture Multi-Programming A computer can do “several things at once” –Browse the net –Write an –Listen to net radio –Print a document … but CPU only does one thing at a time CPU switches between running programs

Avishai Wool lecture What is a process? A process is an executing program When we double click on icon linked to “iexplore.exe”, the OS creates a new process: –Allocates some memory for the program –loads instructions from the disk into RAM –Does some book-keeping –Gives control to the new process

Avishai Wool lecture What does a process have? 1.Resources –Address space: the RAM allocated to the program –Program image: machine instructions –file handles, child processes, accounting info… 2.Thread of execution –Instruction address (program counter) –Registers and CPU state –Stack of called procedures

Avishai Wool lecture Analogy: Baking a Cake Baker Recipe instructions Ingredients (flour, sugar,…) Cake Program != Process  CPU  Program  Input  Output

Avishai Wool lecture Analogy cont. Child is injured Baker marks place in recipe Switches to “medical help” Return to recipe in same place  Interrupt  Save process state  higher priority process  Resume process

Avishai Wool lecture Which processes are running? Unix: the ps command shows the active processes. To see all processes type “ps –aux ” (linux) or “ ps –ef ” (Sun) Lots of output so use “ps –aux | more” Alternative: the “top” command Windows 2000 / XP: –Alt+Ctrl+Del  Task Manager

Avishai Wool lecture What the OS knows about a process Process Control Block (PCB): a data structure in OS. Static info: PID, owner, file… Resources in use (devices, files) Memory maps CPU state: (place for content of) registers Accounting (CPU usage, memory usage...)

Avishai Wool lecture How are processes created? An existing process issues a system call to create a new process. The creating process can be: –An OS process (Windows Explorer, Desktop process that handles “double click”, the “cmd” shell,….) –A regular user process

Avishai Wool lecture Process Management System Calls Create a process –allocate initial memory –allocate process ID –loads initial binary image –optionally: send arguments to process Destroy a process –terminate the process –free resources associated with it –optionally: return value Wait for a process: block until specified process terminates, optionally obtain returned value

Avishai Wool lecture Process management in Unix and Windows

Avishai Wool lecture Unix Processes Parent process creates children processes (recursively: get a tree of processes) OS maintains information about which process is the parent of each process Execution –Parent and children execute concurrently. –Parent waits until children terminate (sort of).

Avishai Wool lecture Unix Process Creation Address space –Child duplicate of parent. –Child has a program loaded into it. Two system calls needed –fork system call creates new process –exec system calls replaces process’ memory space with a new program.

Avishai Wool lecture Unix Process Management fork : (return TWICE if successful) –Create a copy of the current process –Return 0 to the “child” process –Return child’s pid to the “parent” process exec(file,argv) : (NEVER return if successful) –Make current process run file with given arguments argv exit(code) : (NEVER return if successful) –Terminate current process with given return code (0 means OK) waitpid(pid, &code, options) : (return only when appropriate) –wait until child exits

Avishai Wool lecture int pid, ret_val; // pid == process ID... if ((pid = fork()) == 0) { // pid is 0  only child process exec(command, parameters, …); } else { // pid is not 0  only parent reaches here waitpid(-1, &ret_val, 0); } Simple Example Wait for any child to exit Child’s return value

Avishai Wool lecture Fork Example: Chain #include void main(void) { int i; for (i = 0; i < 6; i++) { if (0 != fork()) break; // parent exits the loop } fprintf(stderr, “Process: %ld Parent: %ld\n", (long)getpid(), (long)getppid()); sleep(1); }

Avishai Wool lecture Fork Example: Fan #include void main(void) { int i; for (i = 0; i < 6; i++) { if (0 == fork()) break; // child exits the loop } fprintf(stderr, “Process: %ld Parent: %ld\n", (long)getpid(), (long)getppid()); sleep(1); }

Avishai Wool lecture Win32 processes In Windows there is a huge library of “system” functions called the Win32 API. Not all functions result in system calls (many are not executed in kernel mode) No Parent-Child relationship between processes.

Avishai Wool lecture Win32 Process Management API CreateProcess() – Create a new process (fork+exec) ExitProcess() – Terminate Execution WaitForSingleObject() – Wait for termination –Can wait for many other events There is no separation into fork and exec in Win32

Avishai Wool lecture Process scheduling

Avishai Wool lecture current process runs, until either –an interrupt occurs, or –it makes a system call, or –it runs for too long (interrupt: timer expired) OS gets control, handles the interrupt/syscall …  … But OS does NOT immediately “return” to the process that was running before ! Process Scheduling Basics - 1

Avishai Wool lecture OS maintains several lists of processes. OS saves current process’ registers in PCB Place (PCB of) current process in appropriate list Dispatcher/scheduler (a part of the OS): –chooses new “current process” from the Ready list –loads its registers from PCB – activates the process context switch Process Scheduling Basics - 2

Avishai Wool lecture Schematic Scheduling

Avishai Wool lecture Process Life Cycle event: an interrupt, or a signal (system call) from another process new readyrunning terminated waiting admitted interrupt/yield scheduled wait for event event occurrence exit, kill

Avishai Wool lecture When does CPU scheduling occur? The OS makes a CPU scheduling decision when: 1.A process switches from running to ready state. (interrupt) 2.A process switches from running to waiting state. (syscall) 3.A process switches from waiting to ready. (interrupt) 4.A process terminates. (syscall)

Avishai Wool lecture The Dispatcher A module in OS to execute scheduling decisions. This is done by: –switching context (loading stored register values from PCB) –switching to user mode –jumping to the proper location in the user program to restart that program Small, tricky to write, assembly function

Avishai Wool lecture Scheduling Policies Who’s turn is it now?

Avishai Wool lecture To Preempt or not to Preempt? Preemptive: A Process can be suspended and resumed Non-preemptive: A process runs until it voluntarily gives up the CPU (wait for event or terminate). Most modern OSs use preemptive CPU scheduling, implemented via timer interrupt. Non-preemptive is used when suspending a process is impossible or very expensive: e.g., can’t “replace” a flight crew in middle of flight.

Avishai Wool lecture Typical CPU burst distribution

Avishai Wool lecture Scheduling Criteria Abstract scheduling terminology –CPU == Server –CPU burst == job System view: –Utilization: percentage of the time server is busy –Throughput: number of jobs done per time unit Individual job view: –Turnaround time: how long does it take for a job to finish –Waiting time: turnaround time minus “solo” execution time

Avishai Wool lecture Example: FCFS Scheduling First Come, First Served. Natural, fair, simple to implement Assume non-preemptive version Example: Jobs are P 1 (duration: 24 units); P 2 (3); and P 3 (3), arriving in this order at time 0 Gantt Chart for FCFS schedule: Average waiting time: ( )/3 = 17 P1P1 P2P2 P3P

Avishai Wool lecture FCFS: continued What if execution order is P 2, P 3, P 1 ? Gantt chart: Average waiting time: ( )/3 = 3 Much better! Convoy effect: many short jobs stuck behind one long job P1P1 P3P3 P2P

Avishai Wool lecture Scheduling policy: SJF “Shortest Job First”: –Assume each job has a known execution time –Schedule the shortest job first Can prove: SJF ensures minimal average waiting time! In pure form, mostly theoretical: OS does not know in advance how long the next CPU burst will last

Avishai Wool lecture Preemptive Scheduling: Round Robin Each process gets a small unit of CPU time (time quantum), usually milliseconds. After this time has elapsed, the process is preempted and added to the end of the ready queue. Then the process at head of queue is scheduled.

Avishai Wool lecture Performance of Round Robin n processes in the ready queue time quantum is q, 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. Performance –If q too large  FCFS –But q must be large with respect to context switch, otherwise overhead is too high.

Avishai Wool lecture Round Robin: Example ProcessBurst Time P 1 53 P 2 17 P 3 68 P 4 24 Gantt chart for time quantum 20: Typically, higher average turnaround than SJF, but better response. P1P1 P2P2 P3P3 P4P4 P1P1 P3P3 P4P4 P1P1 P3P3 P3P

Avishai Wool lecture SJF for CPU Scheduling Associate with each process the (estimated) length of its next CPU burst. Use these lengths to schedule the process with the shortest time. if a new process arrives with CPU burst length less than remaining time of current executing process, preempt. A.k.a. Shortest-Remaining- Time-First (SRTF).

Avishai Wool lecture Estimating length of CPU burst Idea: use length of previous CPU bursts Heuristic: use exponential averaging (aging).

Avishai Wool lecture Exponential Averaging Expanding the formula, we get:  n+1 =  t n +(1 -  )  t n-1 + … +(1 -  ) j  t n-j + … +(1 -  ) n  0 Since both  and (1 -  ) are less than or equal to 1, each successive term has less weight than its predecessor.  =0 –  n+1 =  n = … =  0 –Recent history does not count.  =1 –  n+1 = t n –Only the actual last CPU burst counts.

Avishai Wool lecture Exponential Averaging: Example  =0.5

Avishai Wool lecture Priority Scheduling Idea: Jobs are assigned priorities. Always, the job with the highest priority runs. Note: All scheduling policies are priority scheduling! Question: How to assign priorities? priority 1 priority 2 priority M

Avishai Wool lecture Example for Priorities Static priorities can lead to starvation!

Avishai Wool lecture Dynamic Priorities Example: multilevel feedback

Avishai Wool lecture Concepts for review Process Process Control Block (PCB) Unix process creation: fork / exec Context switch Dispatcher Preemption CPU Burst FCFS SJF Round-Robin Exponential aging Starvation