‫Hello and welcome back, everybody.
‫In the last lecture, we started implementing process functionality, we found ourselves needing to
‫change the paging functionality and add new functions.
‫In this lecture, we are going to continue implementing the process functionality, as well as finishing
‫this paging functionality that we did in the last lecture.
‫So in the last lecture, we got up to this point.
‫We now need to implement the paging map range function.
‫So we're going to do that now, simply go in it.
‫Paging Map Range Unit 32 T and this is a pointer.
‫So the directory.
‫I avoid virtual address, avoid physical address in accounts, and instead of flags down here, we're
‫going to go in and rescue zero return address.
‫We're going to go for an IQ zero I below count I plus plus.
‫And now we're going to go rest equals paging map, and we haven't created this function yet, directory
‫virtual physical flags.
‫And I was going to say if the rest is equal to zero, then break.
‫I'll explain this to you in a moment.
‫And now we're going to go virtual address.
‫Plus equals page size, paging page size, physical address.
‫Plus equals paging page size.
‫So your function should look like this.
‫OK, so let's explain this then.
‫This function takes a page directory, a virtual address and a physical address.
‫Now, this is the starting virtual address and this is the starting physical address.
‫Now the count is how many pages we want to map.
‫So essentially what this function is going to do if you pass zero x 1000 for the virtual address, zero
‫x 5000 for the physical address and let's say count of two, it will maps you X thousand to zero x 5000
‫and it will map as zero 2000 to zero x 6000 because that count was two.
‫So that's what that does.
‫We provide a starting virtual address and the physical address for that virtual address and then we
‫provide how many pages we wish to map.
‫So that's what that function does.
‫OK, and this function does something a little similar to page a map to a from it takes physical and
‫address and it calculates the number of pages that it will need and then calls paging map range.
‫So that's what we're basically doing here, guys.
‫OK, so that's what that function does.
‫So we loop through the can we map the virtual address to the physical address?
‫We then increase the virtual address by a page size and we increase the physical address by a page size.
‫OK, so we now need to create the paging map function.
‫This is a very easy function, so we're just going to go in it.
‫Paging Map Unit 32 T and this is a pointer.
‫So the directory we can avoid virtual address, avoid physical address and the flags.
‫OK, so what I want to now do is I wanted to go if unsigned in a virtual address while V.I. Viotti process.
‫No, no, no, sorry, paging page size, OK, or unsigned in it.
‫Physical address, page size and just put some brackets around these guys, keep them separate.
‫OK, we're going to want to put it another bracket here.
‫OK, so if either one of these is true, then we want to return invalid argument.
‫OK, essentially, we're checking that the virtual address and physical address are page aligned, if
‫they're not, then the caller is passing a valid argument to this function.
‫We're now going to go return paging set directory, virtual address, unit 32 to physical.
‫So just that.
‫And we're going to draw on the flags just like that.
‫OK, so that's how it should look.
‫So I'll page a map.
‫We'll take in our virtual address, our physical address in the pen, the flags to the physical address
‫and then call paging set.
‫So that's what the paging map function does.
‫OK, so now let's put these function prototypes in the header files.
‫Just copy and paste those.
‫We're going to put them in paging Dr. Hajj, so we're going to put paging map to.
‫We're going to put paging map range.
‫And we're going to put paging map.
‫OK, so that should be heds, so if we just go back to process, start see, we can now continue.
‫But the mapping of the binary so we can see that all works fine now we pass in the first directory entry
‫here.
‫Well, the directory entry array, I should say, on the page for gigabyte chunk right here, the directory
‫entry rate, we pass that in on paging map to we pass in the program's virtual address and we pass in
‫the process points up the process pointer is the pointer in physical memory that we have loaded the
‫program to.
‫So here we then say, OK, the end address is, the program points up, plus the program size.
‫And then obviously we align that to the the next page up.
‫OK, and we set the paging is present paging access from all and the paging is right to Bobette so that
‫our user program can write to its own memory, which is what we want, because like I said, there's
‫going to be global variables and so on.
‫So yeah, this functions now.
‫Correct.
‫So we can see the virtual addresses of 400000.
‫So what basically say in map the address, the virtual address, 400000 to the physical address where
‫we've actually loaded the information, loaded the program, I should say.
‫Now when the user program accesses Iraq's 400000, he's actually accessing the address that the process
‫was loaded in that we loaded in process load data by here.
‫So you can see we did Tasiilaq here.
‫Right.
‫And then we go process pointer equals equals the program data point.
‫Right.
‫So that's what that's doing, guys.
‫We're giving access.
‫So they use the program to this memory that we've allocated here and we're allowing it to access this
‫memory by using address 2x 400000, OK, because that's the address we specify for the program.
‫Virtual address here.
‫OK, perfect.
‫So we've we've successfully got the mapping working correctly now so we can carry on now.
‫We've done process slow for slot.
‫Let's make a process load function which will find the free slot for us.
‫OK, so we're just going to go in it, process a load koncz filename struct process and.
‫Yeah, that'll just be it.
‫OK, so here we're going to go in at risk equals zero return address in a process slot equals process
‫get free slot.
‫We haven't created that function yet.
‫We will if the process slot is below zero then rest sequel's NMM go to out and we're going to have the
‫out label by here guys.
‫Otherwise we're going to say rest's equals process.
‫Load for slot.
‫FileName process.
‫Process slot.
‫So, yeah, we can see how that works, right?
‫So we find a free process slot when we find that we call process low for slot, which then loads it.
‫OK, so now let's create the process, get free slot functions.
‫So in that process, get free slot.
‫And this is going to be pretty easy.
‫Guys, Frenched equals zero i.e. below max processes ie plus plus.
‫And then we're going to say if processes ie is equal to zero return I otherwise here we're going to
‫return NMM.
‫Oh no that would be wrong wouldn't it.
‫Will return e e is taken.
‫We'll do that because there's no more process is available.
‫Right.
‫So that's what we'll do.
‫And if we couldn't find the free slot, we'll just change that that is taken as well.
‫OK, so yes, process load, if it can't load any more processes because the array is full.
‫We only have enough room for 12 processes currently, then it will terminate.
‫If you wanted infinite processes, then you could use a linked list.
‫However, I think the arrays perfectly acceptable in this training.
‫In this training course, you could use an extremely large array if you wanted like an array of 5000
‫processes or something.
‫Tried to make it a line to the page for performance reasons.
‫But anyway, the point I'm making is you can be very flexible with how many processes your system can
‫load.
‫So that shouldn't be any problem at all.
‫OK, so we just implemented the process load function now and the process kept slot.
‫OK, ok, so I now want to go to test hatcheck and we are just going to go at the top here and we're
‫going to go struct process, OK, because we want the test to be aware of the process now.
‫So we're going to go through the process process and the process of the task.
‫OK, because we're just changing this test functionality now.
‫Remember I told you we were going to do this, right?
‫So the task and it here needs to change as well.
‫We're now going to accept another argument.
‫Task well, structure of process, OK, in the task in it.
‫And we're just going to go TASC process equals process.
‫And you want to copy and paste this guys here?
‫Oh, we haven't actually spotted that one.
‫That's fine.
‫What?
‫We have exported it, but we we we won't allow this to be used outside.
‫So that's fine.
‫Skrull up to the top here because we need to replace this prototype.
‫OK, like that.
‫OK, taskin a structured task struct process process good.
‫Marianao test new.
‫We're going to do the same here, we're going to go struct process process.
‫OK, and now you'll see on line three two here taskin we need to now pass the process.
‫OK, I copy and paste the test new prototype and we're going to override the one in the header file.
‫Excellent.
‫OK, so that that's pretty good.
‫Now now tasks are aware of processes, and we just want to include the process in this in the source
‫files.
‫So just going through process thought and now imposes the Hajj.
‫Scroll down here.
‫And when we see tests new by here, we've already done it, brilliant.
‫We passed that in earlier on.
‫So you can see test new now perhaps the process, and that's perfectly accepted.
‫OK, let's now put process starts.
‫See?
‫Into the makefile and make sure that compiles, so go to the Makefile, we're going to scroll down here.
‫And we see Testudo just copy and paste that above and we're just going to go process Stuto, just paste
‫that in here, in here and in here, as we've done countless times before.
‫Copy and paste the build object file here, scroll to the top and we're just going to put that anywhere
‫in the files at the top here so you know the drill guys.
‫So we're now going to go make a clean bill to the just as we've done many times before.
‫Are we going to see that there's an error on process line line eight?
‫That's no problem.
‫Go to process Stazi.
‫We've included the heap incorrectly.
‫So I see what's happened here.
‫We should have done memory heap keep.
‫So just do that memory heap keep.
‫And I we're going to try and build that again.
‫We can see it now.
‫Bolts that one Brillion.
‫We've made the same mistake with paging so that should be memory paging.
‫Paging.
‫Well that again.
‫And we can see there's a few compile issues with process, get that, because I made some mistakes,
‫we want to return a process, not an integer.
‫Secondly, this should return now, OK, rather than a negative value if there was a problem.
‫OK, so you can see that now that we've corrected that it returns null if the process is invalid, otherwise
‫it returns the process pointer perfect.
‫That's clear and build again.
‫And we can see some more errors here.
‫OK, so implicit declaration of function, paging a line address, OK, we still need to implement that
‫one.
‫Not to worry, we can implement that later.
‫We have an error on line 115.
‫So let's go there.
‫And we see declaration of process, load for slot's, that's no problem.
‫That's because this this we need to put this in the head of file like copy and paste process, load
‫for slot and just pasted under the structure here.
‫OK, because because it hasn't seen it at this point that we call it, because it's beneath us to understand
‫so that that solves that problem.
‫And we can see passing argument, one of process memory from incompatible point of type.
‫So go to one six seven in here.
‫And yes, we can see what's happened here.
‫I have passed the process here rather than the process we made.
‫So this is the one we were setting once we've completed this result, once we've built the process.
‫This is the one we're creating.
‫OK, so back here, this should be underscore process.
‫OK, that's what I'm trying to say.
‫And the reason for that is, is because this structure process is a multidimensional pointer, which
‫means they're providing the address for pointed to us so that we can set their points to the point where
‫we've created here.
‫So that's the whole point of that.
‫Right now, this one dimensional pointer here is the one that we're actually working with.
‫So that was the bug there, guys.
‫So I used process instead of underscore process.
‫OK, so I'm just going to clear now and build that.
‫And we can see the only areas left now are the paging ones, which will fix in a minute.
‫That's no big deal.
‫We just need to create paging align address and string and copy.
‫So let's just go to String Dot C and I see the problem.
‫We only have a string copy, we don't have a string and copy.
‫So that's the issue right there, guys.
‫That's why there's a problem.
‫So we can fix that.
‫It's not a big deal.
‫Let me just find that again.
‫So that's nine one five four.
‫We're basically just copying the file name.
‫OK, so we can fix that.
‫There's no big deal.
‫Go to String Dot C and we're going to create the string and copy functions.
‫So we're going to go cha string and copy sha dest constant source and cowens.
‫So when I was just going to go for an IQ zero, I, i below count I plus plus.
‫And we're just going to say, if source I equals zero, no Terminator, then we break otherwise just
‫I ecosoc I.
‫OK, and it's as simple as that.
‫And that should copy the string.
‫And as a matter of fact it should be count minus one because we're also going to add another Terminator
‫down here.
‫So to do that just go Dast.
‫I know.
‫Yeah.
‫D'Asti equals zero zero zero and we're going to have Ibai here.
‫OK, so what will happen is it'll go through here, go through the count minus one.
‫If there's no Terminator it will break, otherwise it will set the D'Asti to the source.
‫I at this point in time the I should be pointing at the very end of the string and then we add on the
‫Terminator.
‫OK, so we're going to copy and paste that into here into the header file and that is that problem now
‫resolved.
‫So if we build now, we can now see the only issue left is the paging and we just need to make it a
‫line.
‫The address of got paging Dot C and we're going to do that now.
‫So we're just going to go in page and see here.
‫We're just going to go void's pointer, paging a line address.
‫So we're returning a void point basically, which we are returning and the lines address.
‫Basically, we're going to accept an argument provided to US code pointer and now we're now going to
‫check if it's already a line.
‫That's the first thing, because we don't need to process it if it's already aligned.
‫OK, so if it's already aligned, then we'll just return the pointer, OK, down by here.
‫However, this if statement here checks if it's not aligned.
‫OK, because if it's aligned, this will be zero.
‫Right.
‫But if it's not aligned, it will be greater than zero.
‫So this statement will run if the point are provided is not aligned.
‫So all we do here is we go return void's.
‫So we cast and then guys.
‫Unit 32, three pointer.
‫Plus paging page size, minus unit 30 to 40 points modulus, paging page size, so that should work
‫absolutely fine.
‫So just let me show you how that's working so you can understand we're going to open a python here.
‫We're going to go four nine six modulus four nine six.
‫That gives to zero.
‫Perfect.
‫So this statement's never run.
‫We just for 10 points up.
‫Now we're going to go four nine seven, which is no aligned modulus.
‫Four nine six.
‫We see one.
‫OK, so this if this if statement now gets run.
‫So let's do the equation here.
‫Four nine six plus page size.
‫No, sorry, the value is four nine seven plus the page size of four nine six.
‫OK, so that gives us eight one nine three eight one nine three minus four nine seven modulus page size
‫of four nine six gives us eight one nine two, which is aligned.
‫So that's how that works, guys.
‫OK, because four nine seven modulars four nine six is one.
‫OK, so that's how that works.
‫That guarantees that we can align that address.
‫So I've written a bit more math here to break it down for you.
‫Essentially, we're aligning to the next page.
‫OK, so if we if we are even one byte over the page, we need to align to the next page up because otherwise
‫we can't accommodate for that one extra byte because the paging has to be aligned.
‫As I've said, we can't pass in the address four nine seven.
‫This is not allowed.
‫So this function here, paging a line address will align it to the next page up.
‫If you provide for nine five, it's going to align it to four nine six.
‫If you provide four nine six, everything's fine.
‫If you provide four nine seven, it's going to align it to eight one nine two.
‫It needs to align it and even if it's one byte out, has to align to the next page because unfortunately
‫you can't compute that address.
‫And aligning a size less than the size you provided means that we would cap data and we don't want that.
‫So we have to align to the next page, guys.
‫OK, so that's how that's working, because we can't just provide unaligned addresses.
‫The paging model doesn't work like that.
‫If it did if you did do that, you'd be overriding flags and in the paging and things would get corrupted.
‫OK, then copy and paste this now in the header file paging align address and we're going to try to
‫build that again.
‫OK, good.
‫Now we see a problem on String one, 08.
‫And that's absolutely fine, we just forgot to return Dast, so just return desk by their guys, OK,
‫because it expects you to return at.
‫Run that again.
‫And now we can see final issue on paging Dr. C, line 73.
‫Oh, and that's the division by zero that should be paging page size, not not the PTO asshole.
‫OK, so that should be that.
‫All right.
‫And we can see everything compiled successfully now.
‫We now run that.
‫We can see we still see the messages, OK, perfect, so I know that was quite a complicated lecture.
‫I know we ran into a few issues.
‫I hope that you solve them all correctly.
‫If you run into problems, please let me know.
‫And I can assist further with with this lecture.
‫However, when now at the stage where we have process functionality and tasks, functionality, we're
‫almost ready now to start creating our very own process and loading that into memory and executing it.
‫So that should be interesting.
‫We can see that we have a maximum of 12 processes currently in the system.
‫However, this can be adjusted.
‫We can see and test and see the tasks are linked list so we can have an infinite amount of tasks.
‫Thanks for watching and I hope you enjoyed this lecture.