Operating Systems CSE 411 CPU Management Sept. 15 2006 - Lecture 5 Instructor: Bhuvan Urgaonkar.

Slides:



Advertisements
Similar presentations
 Basic Concepts  Scheduling Criteria  Scheduling Algorithms.
Advertisements

Silberschatz, Galvin and Gagne ©2009 Operating System Concepts – 8 th Edition Chapter 5: CPU Scheduling.
Operating Systems Chapter 6
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.
Operating Systems CPU Scheduling. Agenda for Today What is Scheduler and its types Short-term scheduler Dispatcher Reasons for invoking scheduler Optimization.
WELCOME TO THETOPPERSWAY.COM
CSC 501 Lecture 2: Processes. Von Neumann Model Both program and data reside in memory Execution stages in CPU: Fetch instruction Decode instruction Execute.
Chapter 3: CPU Scheduling
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.
02/04/2008CSCI 315 Operating Systems Design1 CPU Scheduling Algorithms Notice: The slides for this lecture have been largely based on those accompanying.
Silberschatz, Galvin and Gagne  Operating System Concepts Chapter 6: CPU Scheduling Basic Concepts Scheduling Criteria Scheduling Algorithms.
Chapter 5-CPU Scheduling
Silberschatz, Galvin and Gagne ©2009 Operating System Concepts – 8 th Edition, Chapter 3: Processes.
02/11/2004CSCI 315 Operating Systems Design1 CPU Scheduling Algorithms Notice: The slides for this lecture have been largely based on those accompanying.
1 Wednesday, June 14, 2006 "Beware of bugs in the above code; I have only proved it correct, not tried it." - Donald Knuth.
Modified from Silberschatz, Galvin and Gagne ©2009 Lecture 8 Chapter 5: CPU Scheduling.
Chapter 5: CPU Scheduling. 5.2 Silberschatz, Galvin and Gagne ©2005 Operating System Concepts Chapter 5: CPU Scheduling Basic Concepts Scheduling Criteria.
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.
Chapter 6 CPU 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.
Computer Architecture and Operating Systems CS 3230: Operating System Section Lecture OS-1 Process Concepts Department of Computer Science and Software.
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 3: Processes.
Silberschatz, Galvin and Gagne  2002 Modified for CSCI 399, Royden, Operating System Concepts Operating Systems Lecture 15 Scheduling Read Ch.
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.
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 ©2009 Operating System Concepts – 8 th Edition, Chapter 3: Processes.
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.
Operating Systems CMPSC 473 CPU Scheduling September 07, Lecture 5 Instructor: Bhuvan Urgaonkar.
Operating Systems CSE 411 CPU Management Sept Lecture 6 Instructor: Bhuvan Urgaonkar.
Silberschatz and Galvin  Operating System Concepts Module 5: CPU Scheduling Basic Concepts Scheduling Criteria Scheduling Algorithms Multiple-Processor.
1 CS.217 Operating System By Ajarn..Sutapart Sappajak,METC,MSIT Chapter 5 CPU Scheduling Slide 1 Chapter 5 CPU Scheduling.
6.1 CPU Scheduling Basic Concepts Scheduling Criteria Scheduling Algorithms Multiple-Processor Scheduling Real-Time Scheduling Algorithm Evaluation.
Operating Systems CSE 411 Process Management Sept Lecture 4 Instructor: Bhuvan Urgaonkar.
Silberschatz, Galvin and Gagne ©2009 Operating System Concepts – 8 th Edition, Chapter 3: Processes.
Operating Systems CMPSC 473 Processes (contd.) September 01, Lecture 4 Instructor: Bhuvan Urgaonkar.
Chapter 3: Processes. 3.2 Silberschatz, Galvin and Gagne ©2005 Operating System Concepts - 7 th Edition, Feb 7, 2006 Chapter 3: Processes Process Concept.
Purpose of Operating System Part 2 Monil Adhikari.
Chapter 3: Processes. 3.2 Silberschatz, Galvin and Gagne ©2005 Operating System Concepts Chapter 3: Processes Process Concept Process Scheduling Operations.
1 CPU Scheduling Basic Concepts Scheduling Criteria Scheduling Algorithms Multiple-Processor Scheduling Real-Time Scheduling.
1 Uniprocessor Scheduling Chapter 3. 2 Alternating Sequence of CPU And I/O Bursts.
Chapter 4 CPU Scheduling. 2 Basic Concepts Scheduling Criteria Scheduling Algorithms Multiple-Processor Scheduling Real-Time Scheduling Algorithm Evaluation.
Lecture 4 CPU scheduling. Basic Concepts Single Process  one process at a time Maximum CPU utilization obtained with multiprogramming CPU idle :waiting.
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.
1 Chapter 5: CPU Scheduling. 2 Basic Concepts Scheduling Criteria Scheduling Algorithms.
1 Module 3: Processes Reading: Chapter Next Module: –Inter-process Communication –Process Scheduling –Reading: Chapter 4.5, 6.1 – 6.3.
Chapter 3: Processes.
Topic 3 (Textbook - Chapter 3) Processes
Chapter 3: Processes.
Chapter 3: Processes.
So far…. Firmware identifies hardware devices present
Chapter 5: CPU Scheduling
Chapter 5: CPU Scheduling
CPU Scheduling: Basic Concepts
CPU Scheduling.
Chapter 5: CPU Scheduling
Presentation transcript:

Operating Systems CSE 411 CPU Management Sept Lecture 5 Instructor: Bhuvan Urgaonkar

Today Quickly revisit –Timer interrupt, system clock –CPU management related data structures –Process creation and termination Start CPU scheduling Quiz 1

Timer Interrupt, System Clock System clock drives the CPU circuit, no interrupt processing Timer interrupt handled by OS, less frequent CPU TSC reg.++ Oscillator IRQ3 IRQ2 IRQ1 IRQ0 CLK tick tick (Set by OS at bootup … coming soon) Slower clocks to other components (e.g., memory, PCI bus) jiffies++ (Linux)

Today Quickly revisit Timer interrupt, system clock  CPU management related data structures –Process creation and termination Start CPU scheduling Quiz 1

Data Structure #1: PCB Can PCBs be swapped out? –Depends on the OS design.. so sometimes YES Process id Program Counter … Other registers Process state Ptr to linked list Main Memory (RAM) OS Processes

Data Structure #2: Linked Lists based on Process States

Ready Waiting Running Disk Lock

Ready Waiting Running Disk Lock Timer interrupt

Ready Waiting Running Disk Lock

Ready Waiting Running Disk Lock I/O call

Ready Waiting Running Disk Lock OS (scheduler) Hmm.. Who should I pick to run?

Ready Waiting Running Disk Lock OS (scheduler) Lets pick the second process in the ready queue

Today Quickly revisit Timer interrupt, system clock CPU management related data structures  Process creation and termination Start CPU scheduling

Process Creation When would a new process be created? –When a user of the computer asks one to be created That is, when an existing process asks for one to be created –Therefore, there is going to be a parent-child relation among processes –There must be a first process, then - the OS creates it during boot-up Who is going to create it? The OS What does the OS need to do? –Needs to create a PCB Assign a unique id to the new process –Bring in the code and data for the new process into RAM –Move the new process to the ready queue, so the OS can schedule it on the CPU

Process Creation fork() Recall: Processes use system calls to request services from the OS OS provides a system call called fork() that a process can use to request the creation of a new process OS creates a new PCB when fork() is called with a unique id for the new process The calling process is the parent, the new process is the child Then adds the child’s PCB to the ready queue The child process is ready to go! What code is the child going to execute?

Specifying the Code the Child should execute exec() Recall: Processes use system calls to request services from the OS OS provides a system call called exec() that a process can use to tell the OS what code it would like to execute Once the code and data are loaded into memory, the OS sets the PC (in the PCB) to the beginning of the child’s code What happens to the parent now???

Process Creation: What happens to the parent? The parent could wait for the child to finish –This is what a shell does Or it could go about its business

Process Creation: Parent wants to wait Again, the parent process has to request the OS via a system call The wait() system call The OS moves the parent to a waiting queue It would be moved to ready again when the child terminates

Process Creation: Parent doesn’t want to wait Both the parent and the child would run whenever they are ready

Process Creation Why/when are processes created? 1.A user wants to run a program 2.An existing process needs another process to run the same code  Why? More parallelism, make use of CPU even when it gets moved to the waiting state So most OSes initialize the child process to have the same state as the parent Except the process id and a few other things

Id=2000 State=ready PCB of parent RAM OS Processes Parent’s memory Process calls fork Id= PCB with new id created 2. Memory allocated for child Initialized by copying over from the parent Child’s memory 3. If parent had called wait, it is moved to a waiting queue 4. If child had called exec, its memory overwritten with new code & data 5. Child added to ready queue, all set to go now! State=ready PCB of child

#include using namespace std; int globalVariable = 2; main() { string sIdentifier; int iStackVariable = 20; pid_t pID = fork(); if (pID == 0) // child { // Code only executed by child process sIdentifier = "Child Process: "; globalVariable++; iStackVariable++; } else if (pID < 0) // failed to fork { cerr << "Failed to fork" << endl; exit(1); // Throw exception } else // parent { // Code only executed by parent process sIdentifier = "Parent Process:"; } // Code executed by both parent and child. cout << sIdentifier; cout << " Global variable: " << globalVariable; cout << " Stack variable: " << iStackVariable << endl; } Compile: g++ -o ForkTest ForkTest.cpp Run: ForkTest Parent Process: Global variable: 2 Stack variable: 20 Child Process: Global variable: 3 Stack variable: 21

Process Creation and Termination

C Program Forking Separate Process int main( ) { pid_t pid; /* fork another process */ pid = fork( ); if (pid < 0) { /* error occurred */ fprintf(stderr, "Fork Failed"); exit(-1); } else if (pid == 0) { /* child process */ execlp("/bin/ls", "ls", NULL); } else { /* parent process */ /* parent will wait for the child to complete */ wait (NULL); printf ("Child Complete"); exit(0); }

Process Termination Process executes last statement and asks the operating system to delete it (exit) –Output data from child to parent (via wait) –Process’ resources are deallocated by operating system Parent may terminate execution of children processes (abort) –Child has exceeded allocated resources –Task assigned to child is no longer required –If parent is exiting Some operating system do not allow child to continue if its parent terminates –All children terminated - cascading termination

Today Quickly revisit Timer interrupt, system clock CPU management related data structures Process creation and termination  Start CPU scheduling Quiz 1

Ready Waiting Running Disk Lock OS (scheduler) Hmm.. Who should I pick to run?

First-Come, First-Served Scheduling (FCFS) Process Run Time P 1 24 P 2 3 P 3 3 Suppose that the processes arrive in the order: P 1, P 2, P 3 The Gantt Chart for the schedule is: Waiting time for P 1 = 0; P 2 = 24; P 3 = 27 Average waiting time: ( )/3 = 17 P1P1 P2P2 P3P

FCFS Scheduling (Cont.) Suppose that the processes arrive in the order P 2, P 3, P 1 The Gantt chart for the schedule is: Waiting time for P 1 = 6; P 2 = 0 ; P 3 = 3 Average waiting time: ( )/3 = 3 Much better than previous case Convoy effect short process behind long process P1P1 P3P3 P2P

Shortest-Job-First (SJF) Scheduling 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 for avg. waiting time – gives minimum average waiting time for a given set of processes –In class: Compute average waiting time for the previous example with SJF –Prove it (Homework 1, Will be handed out next Friday) Two schemes: –nonpreemptive – once CPU given to the process it cannot be preempted until completes its CPU burst –preemptive – if a new process arrives with CPU burst length less than remaining time of current executing process, preempt. This scheme is know as the Shortest-Remaining-Time-First (SRTF)

Choosing the Right Scheduling Algorithm/Scheduling Criteria CPU utilization – keep the CPU as busy as possible Throughput – # 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 (for time-sharing environment) Fairness

When is the scheduler invoked? 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 Scheduling only under 1 and 4: nonpreemptive scheduling All other scheduling is preemptive

Dispatcher Dispatcher module gives control of the CPU to the process selected by the short-term scheduler; this involves: –switching context –switching to user mode –jumping to the proper location in the user program to restart that program Dispatch latency – time it takes for the dispatcher to stop one process and start another running

Example from Linux 2.6.x asmlinkage void __sched schedule(void) { [... ] prepare_arch_switch(rq, next); prev = context_switch(rq, prev, next); barrier(); finish_task_switch(prev); [... ] } task_t * context_switch(runqueue_t *rq, task_t *prev, task_t *next) { struct mm_struct *mm = next->mm; struct mm_struct *oldmm = prev->active_mm; /* Here we just switch the register state and the stack. */ switch_to(prev, next, prev); return prev; } #define switch_to(prev,next,last) \ asm volatile(SAVE_CONTEXT \ "movq %rsp,%P[threadrsp](%[prev])\n\t" /* saveRSP */ \ "movq %P[threadrsp](%[next]),%rsp\n\t" /* restore RSP */ \ "call __switch_to\n\t" \ ".globl thread_return\n" \ "thread_return:\n\t" \ "movq %gs:%P[pda_pcurrent],%rsi\n\t" \ "movq %P[thread_info](%rsi),%r8\n\t" \ LOCK "btr %[tif_fork],%P[ti_flags](%r8)\n\t" \ "movq %rax,%rdi\n\t" \ "jc ret_from_fork\n\t" \ RESTORE_CONTEXT \ : "=a" (last) \ : [next] "S" (next), [prev] "D" (prev), \ [threadrsp] "i" (offsetof(struct task_struct, thread.rsp)), \ [ti_flags] "i" (offsetof(struct thread_info, flags)),\ [tif_fork] "i" (TIF_FORK), \ [thread_info] "i" (offsetof(struct task_struct, thread_info)), \ [pda_pcurrent] "i" (offsetof(struct x8664_pda, pcurrent)) \ : "memory", "cc" __EXTRA_CLOBBER)

Shortest Remaining Time First (SRTF)

Priority-based Scheduling UNIX

Deadline-based Algorithms

Proportional-Share Schedulers

Lottery Scheduling Project 1

Work Conservation

Reservation-based Schedulers

Rate Regulation

Hierarchical Schedulers

Problem introduced by I/O-bound processes

Scheduler Considerations: Context-Switch Overhead

Scheduler Considerations: Quantum Length

Scheduler Considerations: Setting Parameters

Scheduler Considerations: CPU Accounting

Scheduler Considerations: Time and Space Requirements

Algorithm Evaluation

A Look at the Linux CPU Scheduler Show a timer interrupt, blocking due to I/O etc.

Threads

Thread Libraries

Thread or Process?

Event-driven Programming Synchronous vs NS I/O

Multi-processor Scheduling

CPUs with Hyper-threading

Process Synchronization

Inter-process Communication

Deadlocks

System Boot-up

Booting a Linux Kernel

Next: Memory Management