A2 Basic 6 Folder Collection
After playing the video, you can click or select the word to look it up in the dictionary.
Report Subtitle Errors
So in the previous video, we looked at how we can build it operated on that can switch between multiple processes to create the illusion that they're running at the same time.
And we saw with the devil we got here.
We had this model that enabled us to conceive how we had different processes running at the same time.
So we always have one that's running, and we could have other processes of the programs that are either blocked waiting for something to happen from the operating system, say, waiting for user input or network io or disk I or something we never others that are ready to run, but it just waiting.
If you want your the video, we can see that we can then switch the ones that are running to be run, herbal and so on, so that we can then get another one running.
And if we do that fast enough, we do.
It's a every 2/100 of a second.
We could give the illusion that these processes are all running at the same time, and occasionally they become a blocked on that can then be ready to be winnable until one of them finishes.
I know somebody put her comment on the previous video saying, Would it be possible to go directly from create to run a ble?
And in fact, that's probably what you do.
If you look at the source code for UNIX, say then yet when the press is created does end up in winnable, but it's put in the position.
So the next time you contact switch, which won't go look at today, it will become running.
And so we said when we looked at this video that these three things we need we need this model so we know how we're going to switch them on which ones we can switch until we need a way of knowing which process we're gonna move from rentable to running.
At which point in time on the final thing we need is some physical way that we can make the CPU stop running one program and then start running the other.
That's what we got today.
It's called what's called Contact Switching because we're switching from the context which one programs running, switching to the context that another program is running.
So to understand how context which it works we need to think about how I CPU excuse problem.
So that's actually have a look at a very simple program.
I'm gonna write it using our machine code, because the context which it does happen at the machine code level doesn't happen a higher up as we use a compartment high level language.
So think about a very simple program that is going to load in a string.
I'm gonna use our machine K for this example.
So we're gonna load in the address.
I was straying into our zero.
So let's call this a string and then we're going Thio, print it out on dhe.
I use the development environment for developing our machine code should you into the University of Manchester, which allows me to call the equivalent of the operating system using swine three to print something out that's gotta print something out, will define eight.
String up here, tow, have a hello world in it, and then let's do something.
Let's print out all the numbers from 1 to 10 so we'll move into our warn the valley of zero, and then we'll start a loop we can add.
Won't come.
Are one number one we should move that into the register are zero.
And they will call another suitor operating system function to print on interview.
So it have you want to the value we put in there.
So because from 0 to 1 by one onto it with them move into another register.
And then we called up into the printed out.
Well, then compare our value in our war with 10 on Will branch if it's less than 10.
So Branch was left on 10 to leap, and then we'll print out something else s again.
We set our 02 point to our string and we'll call this ice during for want of a better name that will call that Suy three don't know that for the rest of the program.
So we got a simple arm machine code program here.
We'll just use arm for the example.
Could do exactly the same.
While the x 86 a m.
D 64 chip with exactly the same of the 6 8000 ship A 652 Doesn't matter.
We're just using our example.
So we talked about this before a computer file with the way this program works is every time we execute instruction somewhere inside the CPU.
That's what would be even worse to see.
We have a set of registers are zero are one, are two and so on on.
These could just store a temporary value.
So as we run through the program, this one says, put the address of eight string in our zero.
So we work out what it is, and we want a value in there.
Then we call this and this cause off into our suitor operating system and, well, print out whatever is that it's doing certainly use that value and probably manipulate the registers well.
Then come to the next line, which moves the value off zero into there.
And then we come to the next line which gets the value out of the register.
Our war, which is adds one onto it, which is one yet and then stores the result back in I want without the next line which gets the value are one which is one and stores it in our moves it into ours there with and call swat for which prints it out.
We come here, we compare our one which is one with 10 it's less done.
So we go around the loop and then we do the same.
We get the value of our wall, which is well when we add one to it, which is to, and we update the value.
So I've now got two in our wall.
So that's the model that RCP runs the program on dhe.
To understand how contact switching is, we need Thio.
Remember that this instruction only ever looks at the value of our warm.
We're interested in this yellow instruction hit for it to work.
The only thing it needs is the value that's currently in our one.
It doesn't care how that value got there.
It could have got there because we've moved zero into it here, which is why I was the first time.
Or it could have got there because we've added one into it previously.
It doesn't make any difference to the operation of that instruction as long as the value of our want has the same value, and then it will do exactly the same thing.
So if it has a really one in it, it will always add one had become too has value zero, and it always become one, and that store wanted it.
So So it doesn't matter how we got there, and that is the key to understanding how context, which it works.
What happens when the context, which is We have one program running and he's executing lots and lots of different code to make things happen.
At some point, that prime gets interrupted either because it's called the operating system or because it's an interrupt in the computer, which has hit the operating system of the operators.
Okay, you've had enough time on this.
I'm going to switch to something else.
The things that we talked about briefly in the previous video, let's say we are here.
We've got to this point in our program.
We got interrupt happening here on We're going to context, which now the operating system without context, which is needs to make sure that when it comes back here, this problem continues as if nothing had happened.
So just so that we could be absolutely clear on this.
So when you say contacts, which in this is switching jelly babies in our precinct?
So this is switching from one program running to the other program running so going from this one to this one and then back to this one as if nothing had happened when this one had run.
So we need to switch from this world.
So this is the 1st 1 on.
We've got to start winning this one.
So at this point, we need to store the state or the contacts of this program so that we can.
Then after we've run this one, switch it back to running this program instead on this can continue as if nothing had happened.
And as we've seen, the reason we can do that is because, as almost the values in our registers have exactly the same values and as long as the value in memory have exactly the same values, then the throne we'll continue to execute in the same thing.
Each of these instructions only depends on the values on the registers, Not how they got there.
So we need to do is make sure whatever we switch from one program to another, that we keep track of the state of the program, its context, so that when we come to load a different one, we can switch and its state and it will carry So how do we go about storing the state of the problem we need to store?
So the things we still the value of the registers on any other state that is responsible for keeping the program running now, things in memory.
We can hide quite easily.
Modern operating system support, virtual memory.
And so we can just map one person's memory out map another process.
Memory into that preserves the state of the memory very easily.
The Maur involved part comes in store in the state of the registers and storing the state with CPU.
But actually that ends up being relatively straightforward.
What we do is we have a process control Bach for each process in operating system.
On its part of that, we store the value to the registers and a few other things in that if we start off by having a look at our prices control box, we have a register pointing to that, and we can use our 13 for this.
They were point into it at a certain point.
Now think about the armed ship.
Is it to make interests fast?
It actually has registers that that can overlay of the registers so I'm using our 13 here, but this is the register that exists in supervisor mode inside the CPU.
It's not the same register that we use when we're writing a code that runs in user.
May we talked about that in another video.
So the first thing I gotta do is store multiple registers into memory of the dress, pointed to buy our 13 and I'm actually gonna take all the registers are zero.
Through our 14 I'm dumping the registers into memory.
So these get written in Here are zero R wand.
That's just copying the values in.
And that's just that's just that's just normal Round that's been allocated is part of the operation we calling this bit?
We've set things up so pointing of that, but we need to be careful because of the state of our CPU isn't just stored in the registers that we can access is another register the status register, which contains the flags that decide whether we can branch because if things have been compared in their equal or not equal or less than their greater them.
So we also need to store that the way we do that is removed that status register into our zero like so.
So we copying that register into there, and then we can store that into memory below here.
So we're storing again relative to our 13 the value of our zero, which is now the value of status register.
So that goes in below where we were before and also the value in our 14.
Because of the way the armpits, that will be the return address our prey away.
We want to continue running from.
So we store that in that.
What do we want to do next?
Well, next we want to get the process control Brock of the next process you want to do on the way the arm suggests you do that is that you load are 13 with the value in memory pointed to by the register art.
Well, again that would have been set up, buy something else, and then you had floor onto the end so that you then putting it expresses, probably want to check here that you're not pointing it at no point test.
Because if you are things that crash and so if we're not, then what we do is we load multiple registers alone d M d B, if not equal to zero from our 30 which have now reported it.
But remember, we're still pointing at the same thing, actually were pointing down here now, So we load them back into our zero andare 14.
We then move back into the status register that we had before were just simplify things a bit for the point of this video, the value.
And so I said in the state is ready to get back up and then we move.
The registers that were at all are 13 back into our 0% to our 14.
But of course these are the registers from the other process.
Because we changed here, which process were pointing out, and the last thing we need to do is we just need to wait a bit.
We really restriction, which doesn't do anything.
And we then set up the program counter sewing room into the program, counter the value which we put in our 14.
But because of the way the point line works on the arm CP used, we have to subtract for as if we execute that block of code, we can preserve the contents of the current process.
Copying it's registered in your state, especially that's the important bit and then load in the value of the previous programs.
Status register on values.
So then we could just go back to our program and continue running out instructions as if nothing had happened, because all the registers have the same values or the memory addresses have the same value on the status register.
Of course, which we didn't have before also has the same value that construction like this.
Preserving the status really wouldn't have made any difference.
But if I switch, it happened here after the compare instruction.
But if that got corrupted, then the branch wouldn't do as we'd expect.
So context, which is relatively easy to implant yourself to preserve all the registers of one process on, then load back in the preserved registers for the new process.
Same thing could be done to start a new process.
We just set up a dummy process control block, which contains empty values for those registers, zero say, And then we could just loaded into move it from renewable back to running to get things started.
So that's the sort of mechanics about how we save one process and then moving another one.
The only other thing we need to do is decide when we do this well, that's where things get slightly trickier.
So we said that there's two places we could do it.
We could either do it when on operating system call is made.
When he's asked the operating system to do something, we could then make a switch, and that's whether to go straight for the operation gets to the end of whatever it was it was doing for the process, and it could then make a context which show it moves to the different process.
So you just execute the code like that and do that, and that's fine.
The other time we can do is by using an interrupt.
What we do is we set up the hard drive.
Our system on this could either be a dedicated hard piece of hardware is generates this interrupt or the sudden CPS, and they enable you to do that on the actual CPU itself.
He generated interrupt at a regular piece of time because the secretaries all got integrated into the system.
We set up that interrupt happen regularly.
Let's say every 2/100 of a second.
And when that fires, we can stop whatever process is running because we're now in the operating system code so it can execute the contact switch to stop it running and then move in to another process and it will continue as it is.
The only time you have to be careful is if you actually running operating system code of that point when the interrupts happened, you probably don't want to switch out in the middle of an operating system call.
So probably what you do in that situation is different until the operating system called finished and then switch at that point.
Otherwise, you get some weird things happening, which you might be halfway through setting up the network.
This is for something, and then you switch out and things could get slightly quickly.
So you have to be slightly careful when you do it.
What happens when a programme relies on, say, time or the clock?
How does that work?
I mean, is that something you don't have to factor into code or s O.
So, for example, if you wanted a problem that say was drawing a real time clock on screen.
How do you make sure that, well, this again comes down to things about scheduling, which will look at another video, is that you probably weren't certain.
    You must  Log in  to get the function.
Tip: Click on the article or the word in the subtitle to get translation quickly!


OS Context Switching - Computerphile

6 Folder Collection
林宜悉 published on March 28, 2020
More Recommended Videos
  1. 1. Search word

    Select word on the caption to look it up in the dictionary!

  2. 2. Repeat single sentence

    Repeat the same sentence to enhance listening ability

  3. 3. Shortcut


  4. 4. Close caption

    Close the English caption

  5. 5. Embed

    Embed the video to your blog

  6. 6. Unfold

    Hide right panel

  1. Listening Quiz

    Listening Quiz!

  1. Click to open your notebook

  1. UrbanDictionary 俚語字典整合查詢。一般字典查詢不到你滿意的解譯,不妨使用「俚語字典」,或許會讓你有滿意的答案喔