CPU Scheduling Concepts
Transcript
>> Look. So CPU scheduling is something that we have been talking about since the beginning of the semester. And I've been talking about CPU scheduling and you now understand -- -- what the scheduler does. Now, we already know the what, -- -- what the scheduler does,-- -- but now we are about to study the how. How does the -- So, what does the scheduler do? >> Decides what process gets the CPU?
>> Yeah, exactly, it's that part -- that important part of the operating system that decides which process gets less CPU -- if there is one CPU, or which process gets which CPU if we have multiple CPUs. Generally speaking, we have multiple CPUs and the scheduler decides which process gets which CPU and for how long. Now, in the beginning, in the early part of this chapter, we will assume a single CPU to understand certain important concepts. Then, we will discuss -- -- multiprocessor scheduling. We'll discuss the issues involved when we do scheduling for multiprocessors. So in the beginning, we will assume a single CPU.
Okay. So first some basic concepts -- -- which in fact, you already know. So we already know the concept of the CPU burst and the I/O burst. And we have -- in fact, we have done some simulation of operating system operation, with CPU bursts and I/O burst. And most students understand this very well.
So a CPU burst is any operation -- a CPU burst -- it consists of all the operations that do not involve I/O or do not involve system calls requesting something from the system. So anything -- any arithmetic operation, like add, multiply, or even memory operation, like load, and store; these are all considered CPU operations. But when you do something like read from a file -- -- or read from keyboard, this is something that process requires operating system intervention for -- so it's done via a system core. So this operation marks the end of a CPU burst.
This means that this CPU burst has ended. And now this starts an I/O burst. And the process will be waiting for I/O until that service is complete. Then the state of the process will change from waiting to what? >> To end [phonetic]. To end? So the process will never get the CPU immediately, right? We will have to put it in the ready state and then let the scheduler choose which process to get the CPU next,-- -- okay?
So loads and stores or memory operations, even though they are memory operations, they are not -- -- you know, when a burst is performing in a memory operation, the CPU may be idle -- may be actually idle depending on the architecture. But during the execution of a load or a store, the CPU may be idle and the memory system is doing the work. But even in that state, from operating system point of view, that's considered CPU time. So it's -- a process has the CPU, whether it's doing arithmetic operations or memory operations. From operating system point of view, the process has the CPU and may not be utilizing the CPU well and it -- you know, there may be --
-- a memory operation that will cause the CPU to stall. But from operating system point of view that counts as your CPU time for that process -- -- okay? What is not CPU time is when the process is waiting for I/O. So -- -- when a process is doing any of these operations, it's in the running state. [ Silence ] Okay, so now we're looking to -- in this --
-- histogram that shows the distribution of CPU bursts. And this is just you know -- the horizontal axis here is the duration -- the CPU length. And the vertical axis is how frequently does this -- how frequently this occurs. And we've seen that the most common or the most frequent CPU burst length is about two milliseconds.
While CPU bursts that are longer or take longer than eight milliseconds are rare. It's rare to have something, you know, longer than eight milliseconds. And why is that? Why is it -- why are CPU bursts that are longer than eight milliseconds rare? >> Long time to go without an I/O? >> Yeah, exactly. So it's a long, it's very -- -- unusual for a process to go for eight milliseconds -- -- without I/O.
I/O is what? I/O is reading from input -- reading from keyboard, writing to the screen, or accessing a file, right? So can you imagine a process going for two milliseconds? Now, two milliseconds -- how long is two milliseconds? How many operations will a process do in two milliseconds? >> A lot. >> What's a lot? Give me some numbers? >> So like millions. >> Two million?
>> And the milliseconds? >> Yeah. >> Yeah. But yeah, so there will be millions of seconds. So again, it's very important that you have a sense of, you know, these numbers. And how long is a two millisecond time period in the context of a computer system? If we are talking about a one gigahertz processor -- one gigahertz processor, that means a billion cycles per second; 1 billion or 10 power line cycles per second?
And how many operations does that mean? Well, it depends on the operation. If we are talking about an add operation, that's one cycle. If we're talking about the multiply, that's a few cycles. If we're talking about the load on a store, that can take up to hundreds of cycles. This is in all levels of cache and goes to main memory. So this load misses in the cache and has to go to main memory, then that's hundreds of cycles, okay? So the number of cycles for an operation depends on what kind of operation that is. And on average, you may assume that 10 cycles is a good average, or at most 100. A hundred is too long. And 10 is a little bit too short for an average number of cycles per operation. But if we use 10, then this is like --
-- 10 power eight operations per second. So that's 100,000 operations per second. Which means that in one millisecond, -- -- you will have what? 10 power 5 operations in a millisecond. So 10 power 5 is hundreds of thousands. So this -- a few milliseconds is hundreds of thousands of operations. And can you imagine a process doing hundreds of thousands of operations without doing I/O -- without reading from input? Or writing to the screen? Or reading from a file? Or writing to a file? It's -- that's why it's -- -- long CPU bursts are very rare, okay?
Okay. Now we will talk about the CPU Scheduler. Our focus here is on the short-term scheduler, which looks at the ready queue and picks one process from the ready queue, and gives it the CPU. Now, what's that ready queue? The ready queue -- In fact, in operating system terminology, they use the term queue even though that the ready queue is not necessarily implemented as a queue. Could be implemented using any data structure, including first in-first out queue or priority queue, or even a tree.
So -- -- in fact, as we will see later, all modern operating systems have the notion of a priority. So there's the notion of a priority or real -- realistic scheduling algorithms are priority-based. And when it's priority-based, you need some kind of priority queue or a tree to implement the ready queue. So it's the data standard in which -- -- the system maintains the already processed.
Decisions will have to be made when a process switches from running to waiting by requesting I/O. When a process switches from running to ready, while in fact, the process itself will not switch, it's the system that will preempt the process or interrupt the process via the time interrupt. And take the CPU away from the process and give it to another process via the time interrupt. So when we have this happening, a process getting interrupted with the time interrupt, we call this pre-emptive scheduling. And this is what we focus on; preemptive scheduling.
Then, of course, it requires hardware support. It requires a timer. If you don't have a timer, you cannot implement premium scheduling as an operating system. So this is an example of a hardware feature that an operating system needs in order to -- -- implement this -- -- functionality -- this operating system functionality, which is preemption. Now, if the system is non-pre-emptive, if it's non-pre-emptive, it's not going to interrupt any process. A process would get the CPU until the process itself releases the CPU. Or, you know, releasing the CPU by -- maybe by in the question of it. So the system will be making decisions when a process gets switched from running to ready. Always, when a process switches from waiting to ready
and gets switched from quick to ready when, for example, I/O completes or when a process terminates. So now we've talked about the dispatcher. So the dispatcher is that part of the operating system that basically implements the scheduling decisions. So the scheduler makes a decision to give the CPU to process X. And the dispatcher implements this by doing the -- -- necessary work to implement this like context switching, -- -- saving and restoring states, saving the state of the process that lost the CPU, and restoring the state of the process that is about to get the CPU.
Other things that the dispatcher does is switching to user mode. This is very important. When the system gets the CPU to a process, it's extremely critical to switch the user mode, otherwise what will happen? Like, say, if there is a bug in the in the OS, and the OS fails to switch to user mode before it gives the CPU to a process, what will happen? >> The user programs can execute privilege. That's -- >> Yeah, exactly then the user process may execute privileged processes, which is a disaster. So now the user-process will have all the privileges of the kernel. And then jumping to that proper location in the process or in the program that we are about to resume or restart.
And the time taken to do all of this is called the dispatch latest. When we evaluate different scheduling algorithms, there are multiple criteria that we can look at for evaluating scheduling algorithms. And this -- you know, this chapter is going to be mostly about evaluating different scheduling algorithms.
So the criteria that we can look at include CPU utilization, which is something that we would like to maximize, obviously. We would like to maximize the utilization of the CPU. There is throughput, which is -- you know, throughput means given -- -- a limited time period or a certain time period, what's the number of operations that will complete within this given time period? So this is the throughput. Turnaround time is the time from start to finish or from the -- -- submission of a certain task to the system, to the completion of that task.
Waiting time is the time that a process spends in the ready queue. And of course, you know, -- -- waiting time is something that we'd like to minimize. Turnaround time is something that we'd like to minimize. Throughput is something that we'd like to minimize or maximize, so that's right. And there is also response time, which is the amount of time between submission of a task to first response, okay? So these are related.
So usually in most cases these are related, and a good system will do a good job at you know maximizing the criteria that must be maximized and minimizing the criteria that must be minimized. But sometimes there are -- -- conflicts or tradeoffs. So sometimes optimizing one criteria may have negative -- a negative impact on another criteria. So for example, if I do shorter -- -- time quantum.
If I do shorter time quantum, will this affect the response time positively or negatively? Was that -- will this have a positive or a negative impact on response time? >> Positive. >> Yeah, positive because I'm making the time quantum short. So I'm allowing multiple processes to make progress concurrently. And the users of these processes are feeling shorter delays. So the system feels more responsive when I make the time quantum short.
But will making the time quantum shorter have a positive impact or a negative impact on the turnaround time -- the total time needed to complete a process? Will that be longer or shorter? It will be shorter-term quantum. >> No, longer. >> It will be longer? So it will have a negative impact on turnaround time. And why is that? Why would the total time needed to complete a process be shorter -- longer?
We will make the time quantum shorter? Yes? >> The kernel is using more of the CPU to switch. >> Yeah, exactly, because we will be spending more time in the kernel. And what the kernel will be doing is context switching. So we will be spending or wasting if you will -- wasting more time in the kernel doing context switching. So shorter time quantum will have a positive impact on response time but a negative impact on turnaround time. And how about throughput -- will it have a positive or a negative impact on throughput? >> Negative. >> Negative as well. So it's -- the time -- so basically you're getting less work done. With shorter time quantum you're actually getting less work done. But you are giving the user the feel that the system --
-- is making progress or it is responsive. So the user is feeling a responsive system but you are actually getting less work done. >> It seems like what the user wants and what the programmer wants conflict. So how do you -- -- decide how your scheduler should operate when you make -- >> Yeah, so what -- the system will have to balance. Sometimes there are tradeoffs, right? The system will have to balance all of this. So there are user requirements and there are system requirements. So if I -- you know, if I make the time quantum too short, then the user will be happy, but I will be -- I will not be using the resources --
-- efficiently. So it will be an inefficient use of resources or poor resource utilization. So the system will have to balance all of these factors. Okay. Now, in this chapter, we will be studying multiple scheduling algorithms. And the main criteria that we'll be evaluating or using to evaluating the algorithms is going to be the waiting time. Or more specifically, the average waiting time.
Okay, it's because usually when we optimize, we optimize the average measure. But sometimes it may make sense to optimize the minimum or the maximum; like minimizing the maximum response time. This is to make the system more interactive. [ Silence ] [ Music ]

Ask anything
We've read this whole study set, so questions about it get the best answers.