 All right good afternoon So quiz three was set last night, so I can tell you what's kind of on it So I assume people are interested in that Yes, okay so it will be like a Scheduling multiple choice, which will be fairly quick and simple So that will be a multiple choice on scheduling then there's going to be like 10 multiple choice virtual memory ish related ones Then a kind of short written answer about scheduling So here's a scenario probably like what's a benefit what's a drawback and then there'll be a page replacement question Where you should probably know optimal least recently used or FIFO depending on your luck and Then there is my fun paging question Which shouldn't be a surprise given last lecture and the one before that So you can expect multi-level page tables. You can expect that it was somewhat nice that it was nice to you and didn't make you have to write in Binary so as long as you somewhat know hex you should be good. Yeah Well, the first scheduling one will be multiple choice and then the other one. Yeah, the other scheduling one like what's good? What's bad? And then page replacement you need to do the full-blown thing Yeah, so how many page faults, you know, here's what's in memory at what given time what gets replaced? Yeah, oh, can you do that in corpus the questions if I can like write a PDF and You can email Ash and I haven't heard of that Yeah, do you have a question? So like you just submit a random PDF at the end and it's supposed to match You can email Ash because he'd have to set it I assume and I have to commute so my internet will not be good in probably until you start. Yeah, don't worry about Who said that oh so when you get into the implementation of it sometimes the pages in memory or Data on disk will be inconsistent dirty just means that there's a modification. It means you need to write it back out So like say you have the page already Like it's been on disk and you swap it back into memory one thing you can do is just keep it around in disk and When you bring it into memory if you only read from it and you never write to it Well, you don't have to write back out to disk because it's already there and it's not modified at all So you could just not bother but we didn't go into that much detail and yeah for the quiz if a Clock would take longer than all the other ones probably combined so You can assume if you get randomly assigned one having clock when Would kind of suck if you were the unlucky soul that got that one No, you wouldn't because it would be super unfair to whoever got assigned clock has to spend like you know 30 minutes of their life doing that Yeah, you just do straight-up lease recently used even if it would be super expensive to implement Well, you can do it on paper because it's like four pages or something like that All right any other quick quiz questions and it's it's 40 points instead of 30 So it's not like quiz one, but so It's somewhere in between Yeah, for what algorithm so if you're so questions about the scheduling and if a new process comes in Do you need to consider it? So like at time zero Process a is running and then B comes in Well, it depends what algorithm it is if it's like that dynamic example. I went through well It would be scheduled first because there's no choice But then now when B arrives if B is an absolute higher priority It's gonna get scheduled in that time slice if it's like that dynamic feedback one If be at a higher priority it would get scheduled right until it goes to sleep if it was round robin That doesn't line up any more whatever if it was round robin B would just be added to the back of the queue and it would depend on What however long your quantum length is as to when a gets added back to it? So if the column length of three it's going to go like a a a and then this is our queue It's just B and then at time three a would get re-added to the back And there would be like BBB and wouldn't execute immediately because it still has time on its length if it's Yeah, it'd be in round robin be added to the queue immediately. Yeah Yeah, yeah, so if there's a tie here and B or a gets re-queued while B arrives You always put the new process first because you may as well It's not going to really affect the waiting time of a so you may as well good good response time for being put in first Yeah, well like here here in the middle Sorry B The net effect is the same if you assume it just gets out of that to it would get added before a gets Re-queued anyways, so it's the same effect. It doesn't matter Yeah, they'd have some arrival order. So that'd be told to you or like just assume they're arriving this order Yeah, so response time is the amount of time it takes in order for you to start executing at all Wait time is the total amount of time you're waiting before you're done Yeah, it's excluding it. So So so if you arrived say you arrived at time one your process B and then Something else is scheduled here. Something else is scheduled here and then you do be be and then Something else something else be and then say bees done here At this point, this is when B arrives. So it would have two for its response So it just takes two time in so it starts executing and then it's wait time. It's just Between it starts and it finishes how much time is it not executing for so in this case? It would be for yeah, so it excludes it. So the wait Wait would be for in this case So easy way to do it if you don't want to count all of them It's just like how long it took from start to finish minus how long it actually took to run and That's how much time is waiting for yeah That's kind of what you did in your lab Your lab right you had to pick which thread to run in lab three So whenever the timer up happened you got control back and then you can fiddle around with it and do whatever you want Then you pick yeah, yeah, it do the same thing so It would have a timer interrupt every like quantum length or whatever Or that process if it calls exit and you're the kernel you get control back because you get a system call you execute code So it's good block. You could make it so like every system call it Did you call or they call you just like terminate and let it do something else? Yeah, response times two in this case Yeah, yeah arrival till first execution Okay, so let's I guess quickly go over that and then we kind of bleed into office hours So this is a bit of a weird one. We'll talk about hard disk drives Has anyone does anyone even use hard disk drives anymore any data hoarders in the house? No, okay, so this Might should probably be cut from future offerings because no one knows what the hell these are But anyways, it looks kind of like this So this is like old-school storage where it's not actually flash RAM or anything like that It's like actual mechanical spinning pieces of metal so in a hard disk driver and HDD like the big chunky things The inside of them will look like this all the data is stored on metal disks and they're called platters And then within if you took apart the hard drives it's going to be multiple this or platters in it and then there's going to be like this giant arm assembly that Has a magnetic head on it that can move back and forth and it can read You know the magical magnetic bits that are set on that disk so There's going to be multiple platters multiple read and write heads that can read the magnetic information off it and then if you want to actually address things so hard drives kind of work like pages so they have like a smallest unit and it's typically called a sector and they'll have a set size so a sector is Something on a track and a track you can think of is just everything on the same radius So you have the same radius that's everything on the same track and then that track will be made up of different sectors Which is essentially like the degree into that track and then how it works is The read write heads there. They don't move back and forth They just move up and down so they can select what track they're reading and to select a sector The whole platter has to rotate so there's a big motor in it that actually physically rotates the disk So if you want to read some information You adjust the head to the right track and then you have to rotate the disk around until that sector comes under the head And then you can actually read the data or write the data Sorry No, typically the disk will just keep on spinning. They might slow down a little bit, but If it completely stopped then it would be like start stop start stop and it would like need breaks or something It'd be kind of weird. Yeah Yeah, so there could be more sectors the closer you get to the inner size or they could be just wider to To fit as much the same amount of information Yeah, that gets Yeah, it could because it's closer to the center So it would probably be less sectors in the middle and probably faster to spin So yeah, so it depends how tricky you get so there's some optimization so you can do We're not even going to do them But yeah, that's things you could do if you're the kernel and you have absolute control over it Maybe you want to put things you care about more closer to the center because that would might be faster Or if there's more sectors on the outside edge since it's rotating anyways That will actually go faster or no that will go slower. Yeah, so put on the inner So you could do that. That's just kind of what a hard drive looks like so That's kind of this good point the access speed depends on locality and like where it's actually Located on the disc So sectors, you know that are located together in the same track and be read continuously because the read-write head doesn't have to move and As it's rotating it's just rotating so it can just completely just read everything Read or write everything as it's just spinning and it can do that continuously But it will be really really slow if you deviate from that So if you need to switch tracks that head has to move back and forth Which will take some time because it's a mechanical thing and then Yeah, and then it also needs to rotate so if you want to optimize sing the the Old style way, which is like really really old now to address these things is you essentially get three coordinates So you get cylinder head sector So when you are accessing the disc you specify each of those coordinates So you tell the disc which platter you want which is like what cylinder so which revolving platter Then which track so which lane there's which is basically just picking the radius And then what sector you want, which is the degree again So historically that will only let you address things up to an 8 gigabyte limit Because it assumes that there's like 500 up to 512 bytes in a sector 63 sectors on a track 500 or 255 heads So that's how many like tracks per cylinder you have and then up to 1024 cylinders And then with that they're like wow, that's too complicated. Let's simplify that so then something came along called logical block addressing and that just uses a simple index number to address a sector and Then you can just address as many as you want But you don't know actually where it's physically located now. You're hoping the vendor actually did something sensible So along with something sensible So if you are a data hoarder one of the technologies for these spinning discs is called shingled magnetic recording So the read write heads kind of only right in the center of the track and has some unused padding So most hard drives look like this where it writes little strips and the gray is the actual data and The white is just some padding. So because it's essentially magnets You might interfere with you know your neighboring track So there's some padding between them so that you don't overlap and while you're writing information to one track You don't disturb another one So that's typically what you have but with this shingled magnetic recording It allows you to kind of write over the padding if you do things in order So if you do it sequentially so on the right there that shingled part it might only interfere at the bottom so if you Write these kind of like shingles So you write the first track and then the track below that and then the track below that you won't disturb What's above you, but you would disturb what's below you? So if you write them in the right order you can fit them closer together and get more density But it's also going to be slower depending on how you use it because say you shingled those three things You have to write the top one then the middle one then the bottom one and if you wanted to change data Just in the top one well if you just change data in the top one on this one You would disturb the track below you so you have to if you want to modify it You have to write the one at the top and then rewrite the one in the middle and then rewrite the one at the bottom So it's really really slow but you can pack you can pack information closer together and funny story because This is much much slower and annoying the first time a company used it They didn't tell customers that they used it and they bought it and the performance was awful because the Colonel didn't know about it Either and they sued them Because the performance was so terrible So fun story But for most conventional hard drives All their latencies are going to actually depend on the distance travel Because the thing's spinning and you have to move ahead back and forth So typically it's going to have two types of delays. There's rotational delay So to physically rotate that this to get to the correct sector and this is where we actually know the numbers for milliseconds So this makes sense why it's so slow because it actually has to spin So typically it'll be like four to eight milliseconds to actually rotate the disc once around and The average delay is going to be like half of a rotation if you're assuming, you know, it's random and fairly uniform Then the seek time is moving the disc arm to get to the track Which is faster than spinning the whole platter But still kind of slow where it's like half a millisecond to two milliseconds and then the transfer time Is how long it takes to read bytes from the disc Assuming everything is sequential and it's as fast as possible and for these drives your maximum transfer speed Probably going to be around like 125 megabytes a second While your NVMe drives are probably pushing if you have a new one like 7,000 megabytes a second So comparatively much much slower, but hey, it's much cheaper too So if you're to calculate the transfer rate, they're fairly self-explanatory Formulas so you take the total time and the total time is going to be equal to all the moving parts So you have to include the rotational delay the seek time to move the head back and forth and also the transfer time that takes to actually transfer that data and Then once you have that your transfer rate is just equal to the size of the transfer However much data you transferred divided by how much time it took and Then we can answer questions. Well, what's the transfer rate of large sequential accesses and then small random accesses? And then we'll see why these things are actually decent for hoarding videos and terrible for putting your kernel on it Yeah Yeah, sorry by sequentially I mean on the same track So while it's spinning the head doesn't have to move and the disc is already spinning Yeah Yeah, so all the heads are aligned so they'd be one head per platter So you do it by you access information by sector So if you had an array it probably all be on one sector because they're like each sector is 512 bytes So it deals in sectors and blocks and things like that that are gonna be bigger units Yeah, I'm the same track. Yeah, so This is to illustrate to use Hard drive sequentially whenever possible. So here's two versions of dis so the rotational speed on consumer devices You'll hit like 7200 rpm, which is 7200 rotations per minute of that spinning thing and then That will translate to a rotational latency of like on average 4.2 milliseconds average seek time would be like nine max transferred be 105 megabytes a second then have four platters and you don't have to worry about the interface and then like hard drive to would be a more Enterprise e1 that goes at 1500 rotations per minute. So almost twice or more than twice as fast Which would mean its rotational latency is only two milliseconds It's average seek time would probably be more than twice as fast to although they might not be related So be like four milliseconds which a max transfer speed That's not that much different like only 125 megabytes a second Then if you actually compute these things Well for sequential accesses you can assume that the rotational Time and the seek time are zero. So in this Scenario, you're essentially given the transfer rate and then given how much data That's transferred. So you just have to calculate t So you just take the data divide it by the transfer rate and then that's how much time it takes So for hard drive one That hundred megabyte transfer would take like 950 milliseconds for hard drive to 800 milliseconds, which is a bit faster, but not that much Then The case where you would want the enterprise drive is because it's way better for random accesses Although both of them kind of suck So if you're just to read a random four kilobyte page, so say that was you swapping something to disk Well, if you're just Do accessing a random four kilobyte page? Well, even if we assume the transfer time is instant just to make our lives easier that it's zero So the total time Just assuming a random access would have to be the rotational time for hard drive one, which is 4.2 Plus the seek time, which is nine So nine plus four point two is thirteen point two So that would be your time and then we would be transferring four kilobytes And then if we figured out the rate based off those two numbers We would get point three megabytes a second, which is horribly slow, which is also why you don't really want paging Well, you don't sorry you don't want to sorry you don't want paging So you don't want to swap to disk especially back in the day So this would be better now that you have SSDs that will Touch on next lecture because SSDs are a bit weird But this shows kind of the big difference between them. Yeah Yeah, but you'd also have to know the layout of the disk and kind of optimize it if you use like the Just index to you just get a random sector based off the device you won't know Yeah, the newer way by just having an index you have no idea and also hard drives historically fail so Sectors go bad all the time even without telling you and you can still use the device So even if they lined up before they might not line up anymore Because hard drives will take a bad sector and just remap it somewhere else without telling you about it Yeah, you can't rely on ordering Yeah, but typically if they're sequential to try and keep it sequential ish Yeah, by bad, so I haven't taken an electro magnetics in a long time So I don't know the the mechanics of it, but essentially Something the magnets fail. I don't know so defragmentation hard drive We'll see more when we get to file systems, but not much, but we know what the word fragmentation means now, right? No, we know what what's fragmentation me? There's like some gaps in between right so if we have a spinning hard drive and we want everything all together in a single row Well when we're accessing files, it just essentially deals with big like sectors, which you can think of as just blocks So as you're using it you might create little holes here and there Where your file gets chopped up and it's stored on different blocks and it's not actually all sequential So if you defrag your hard drive it will take try and make sure that all the blocks that represent your file are actually sequential Yeah, random is horrible. So if you use these devices as sequential, they're not too bad but they're still not as fast as SSDs, but they're still Way cheaper Yeah, yeah everyone watch breaking bad when they went in away, I won't swell I think but anyways Like back in the days you can destroy computers by just bringing a magnet close to them You know, we don't have magnetic drives Yeah Yeah, so someone takes a high-powered magnet and sticks it on your hard drive Well because it's storing it magnetically it would screw it up So your data just gets random stuff and then you don't have data anymore So so like your iPads or whatever. I mean, I can't if you take a magnet to that It's fine. Even the accessories are magnetic so So back in the day we weren't allowed to have magnetic accessories because we'd blow up our devices So if you have an old iPod or whatever that the old the original iPods had the spinning disc in them So if you brought it too close to a magnet, there go all your songs Yeah Damn, I feel old now All right, someone was just walking around in their home with magnets No Okay, people like playing with magnets. All right learning something new every day Okay, so with the logical mapping so when everything is just numbers what you would hope to do is Place all the numbers so that they're actually sequential So if you access everything, you know sequentially in terms of the numbers they're laid out sequentially in terms of the disc So if the disc spun watch in this way counterclockwise Yeah, so you would expect oh no clockwise The heads there you would expect, you know, if you're reading Zero then one's next two three four five six seven all the way to 11 So you could read that sequentially get your max access speed and then You would want it such that when you switch from 11 to 12 That's the slowest pot or that's as fast as possible So if you're designing the hard drive, you probably want to take into account the time It takes to move the head one track forward while it's rotating so everything lines up So you at least get an access as quick as possible So you line up 12 so that it's kind of in line with when the head settles down and then so you can access 11 to 12 as quick as possible and then 1314 all the way to 19 would be all the row and then same thing with 20 So you want to lay it out so that it's the most efficient where you kind of minimize the amount of Time that heads moving So the pros of that is it's really simple to program you just access things sequentially in terms of indexes as much as you can It's the default mapping aims to reduce reduce seek time for sequential accesses But the cons are kind of like what you said the file system can't inspect to try and optimize anything And like really truly try and lay it out or try and you know have data under The same data that's accessed together under multiple heads at the same time It's never going to be able to figure out that and if you try to reverse it It's also going to be a pain So maybe you can keep track of you know the time between two sectors is longer than the time between mostly other sectors So it's probably changing tracks there So maybe I can figure that out But like I said before sectors go bad all the time and the disk can just silently remap them So just because you figured out maybe where the track changes that might not always be the case The OS wouldn't know about typically it was able to it kind of detected it was going bad Like it was getting weaker So it would just copied the data to a new sector and then just remapped it and then it stops Yeah, it would do it So if it failed at doing it it would tell you and then report to the operating system. Oh crap. You've lost data but if It silently remapped it be like yeah, no, it's fine. Don't worry about Yeah, and then it would never use a bad sector again Yeah No reverse the mapping means you try and figure out where things are located just based off kind of the index numbers Like you're trying to reverse engineer Whatever mapping they used So what we could do is like we always do just throw cash onto it So this is another cash we can talk about so this will have some internal memory on them that would be like a flash so for like Western digital red ones they have like 40 or 64 megabytes of cash and basically what they do is Since the data is under the head anyways It would just keep on reading it and filling up that cash And if the kernel decides it wants to access one of those pages Well, it would have already been in a buffer and already read So it could just send that straight to the kernel from the memory without having to read it again Since it's essentially just reading it for free So you can do that and that will kind of minimize kind of hide that and you get some nice speed-ups for free you could also Do some write caching with volatile memory like going to disk So you could just say you have a page on disk That's supposed to map to a sector on the hard drive that might represent a file or something like that Well, if you modify that file it can just instead the curl can just you know modify that page on disk and then The idea would be that it only writes it to back to disk whenever it pages that out or whenever that file You know you close that program and that memory gets deallocated or something. So that would be called a write back So whenever you modify a file, it doesn't actually physically write to disk It just stays in memory until you the close up file or get remapped or something else happens to it But if you do that it's going to look fast because all your writes are going to actually be to memory instead of disk So that's going to look really fast but because Ram is volatile if you have a power failure and you wrote modified a file And you just modified it in memory and then actually physically write it to disk if your power goes out all your changes are now gone so So for normal caches, that's all volatile anyway, so if the power goes out it doesn't matter Right your CPU volatile rams volatile, you know Yeah, so this would be hey, I modified a file. It didn't actually get modified on disk It's just waiting to write it out because writing slow you want to like accumulate all the changes Or just do it all in one big go at the end so Right back With right back if your power goes out before you actually write it to disk then you actually lose data So if you're designing a file system or something like that You would want to know if you have right back or not or if you have a database a program representing a database You might really care about the data and you might want to make sure it's actually written to disk So that's something called right through where you tell the kernel Hey, I mean it write this to disk right now and let me know about it So then if there's a power failure, you're not completely screwed Because you know it's actually physically written to disk Yeah, right through means skip the cache write directly to the disk Because I actually care about this data if the power goes out now. I don't want to lose things Yeah, so like processes There's gonna be requests for sectors and if you're the kernel you get to schedule them Because you're telling you issue commands to the disk and say what sectors you want to write so We don't escape scheduling So if you just have a list of sectors you need to access Well, there's different ways to schedule that you can do first come first serve. That's always an option So that's gonna be fair But it's essentially going to be fairly random, especially if you have a multiprocessor machine that every Program is using different sectors So you're gonna spend a lot of time Waiting for the disk to rotate and waiting for it to seek because all the sectors are going to be located in different spots So if you were a bit More clever, you could do something like shortest seek time first Which is just to handle the nearest cylinder or or sector next to try and reduce the Amount of mark arm movement, but that can be unfair and you starve some requests So the idea behind that is oh you'd only access the next if you're reading a sector The sector you would access next would be the one that you can make it to in the fastest time So if there's just a cluster say and the inside of your disk and there's one sector on the outside Well, if you have a bunch of accesses that were just to the inside It would prioritize those and only stay reading sectors on the inside and keep the head down by the bottom And never read the thing that is in far away, which would be another case of starvation and Starvation is an issue for any scheduler Especially, you know the ones we already saw before so what you can do for a different scheduling algorithm is something called an elevator or AK a scan and all it does is basically sweep across the disk and keeps the seek head moving in the same direction So like an elevator if it's going up It's only going up. It doesn't go back down until it's done everything so in this case if we have requests for sectors 0 12 and 20 it would Even no matter what order those requests came in we would start our elevator at the bottom So start at the outer track. We would read sector 0 Then move it up one track then read sector 12 then move it up one track and then read sector 20 and then with this to so say we also had a request for Sector 6 as well. Well with this algorithm It wouldn't be terribly smart Because it would read sector 0 then wait for the disk to spin halfway around Then it would read 6 and then it would reposition the head to the inner track or to one track in and then wait For the disk to spin back halfway around to make it to 12 again, and then it would read 20. Yeah Yeah, so the way the elevator works. So even if I read Zero and then I went to 12 If I moved the head to the 12 track and I request came in for like one two and three I wouldn't move back down until I've made it to the top of the elevator and then it comes back down So that's just a way to make it fair Otherwise you would have that starvation if you just kept it on the same track or went back down So that's one of the things this tries to avoid So the elevator sometimes it's called sure to seek time first because it's trying to minimize the head Actually moving back and forth So the optimal thing to do that has that starvation issue So it's kind of like doing shortest remaining time first when we're talking about processes So shortest positioning time first would be the best strategy Which would take into account both rotational and seek time and try and minimize that to actually go to the next sector That is the absolute closest but in that case the Colonel and the disc would need to work together to implement it Which if you don't know what the mapping is that's going to be impossible to implement any ways Yeah Yeah, so in this case You know if we had all these accesses plus six If we wanted to be super fast the quickest thing to do would probably be read 0 1220 and then let it and then move the head back to the outer track and let it rotate just half a rotation and then read the six But elevators going to wait it's going to read the zero then rotate halfway read the six Wait for another half rotation to read the 12 and then the 20 So the question is why can't we just have a head at every sector because that a Track edit then you wouldn't need to spin But if you have one at every sector you just read the One at every track Yes, so question is why don't we just have a head at every track? I Believe the heads are way bigger than the track and are actually fairly complicated and expensive Yeah, although I personally haven't made any hard drives myself But I imagine that's probably why yeah, so so You optimizing you might be able to optimize a hard drive, but it's going to be like ridiculously Expensive probably and still lose to an SSD now that we'll talk about SSDs Next class because apparently we run around with magnets, but no tape drives are yeah, but so It's like a big Cassette Yeah, but they can hold like terabytes like So Google's archives use big cassette magnetic tapes and each tape is like really cheap and Holds like terabytes But you actually have to like if you want to get data You should actually you can probably find this on YouTube where like you see it just as a wall of tapes and to read A tape has to go get the tape pull the tape out put it in the tape reader and do all that crap and most so it's mostly for archival stuff, so if If you detected something that's like in their archive Well, it probably give you a response That's like no I don't have that and then in the background it could go grab the tape and then you know read it and then And then you know make it all live again and do whatever so the next time you Do that the next day it works But it's like first stuff they want to keep around yeah short of seek time first So just the head it doesn't care about rotation. Yeah, I've not I do not operate at that scale So if when you get an internship at Google and you can like Somehow get into the data center, which they probably don't let interns do that then you can probably see these things Yeah, yeah You think who was gonna let you have a magnet in their debt that a center Yeah, you think your mom yell that you just Wait till your mom gets armed All right, so that wraps up so discs are things that enable persistence And that's one of the like three major topics of operating systems So we saw the spinning discs today like basically to them like the enterprise There's like enterprise versions of things. There's the shingled magnetic recording versions of things But basically they all really poor random access time they need some scheduling because you'll be accessing sectors and The best scheduling would be to do something like shortest positioning time first But that's gonna have starvation issues and not Really be too feasible So just remember I'm pulling for you wrong this to get