‫Hello and welcome, everybody, and in this lecture, we're going to implement program termination.
‫So what is program termination?
‫Well, if you have a task manager and kill the process, you just terminated a program.
‫We're going to implement program termination today.
‫We won't be implementing a function to the colonel that allows people to terminate programs.
‫We will, however, be right in the functionality required to terminate a program.
‫And later on in the course, we can implement program crashes.
‫So if there's a unhandled page fault, for example, then we can terminate the program forcefully.
‫So, yeah, we're going to implement that today.
‫So first, let's explain how termination works.
‫We remove the task from the task list.
‫OK, so it's no longer there that ensures that it no longer runs any code of the process.
‫Once we've done that, we can then safely delete the memory of the process.
‫OK, so that's the next step.
‫We delete the memory of the process and the memory at the task.
‫Finally, we remove the process from the process list and then there's no more instance of it.
‫There's literally no more instance of processes, all of of that task.
‫It's completely gone.
‫It's been unloaded from memory.
‫And because it's had the task removed from the list and the process is removed from the list, we are
‫now sure that the program can't run anymore.
‫We we successfully terminate the program now in a more better implementation.
‫You could call some sort of handler to let the process know that we were about to kill it and then we
‫can kill it in the next cycle or something.
‫Right.
‫Although all like within a second or something.
‫But we're not going to go that far in depth, at least for now.
‫So we're just going to focus on program termination.
‫OK, let's begin.
‫So we're going to start this by going to task and process start see.
‫And just by here, we're going to say in it process, terminate, process, process.
‫OK, so we're going to terminate the process.
‫We're going to return zero here.
‫So let's see what we need to free.
‫We need to free the task.
‫We need to free our stack.
‫The point, all the arguments.
‫So there's quite a lot to go here, guys.
‫OK, so the first thing we should do is terminate the process allocations.
‫OK, so we're going to go in.
‫It equals zero rest equals process, terminate allocations.
‫That's the very first thing we're going to do, which will terminate all the allocations of the process.
‫And we're going to return Rassi here.
‫OK, guys.
‫So we're going to go in a process, terminate allocations.
‫You can make that static if you want struct process process four inch equals zero I below max process
‫allocations.
‫Well program allocations.
‫I mean I plus plus and now we literally just, we, we just go process free and we pass in the process
‫and the allocation.
‫Now this is a slow way of doing it because process free well get the allocation by address and then
‫it will unmap the memory and join it.
‫And then Kifri.
‫Now I'm, I'm using process free in case you decide to do anything special with process free in the
‫future.
‫OK, but if you really want to gain a speed advantage here, you can completely remove process free
‫and just put and pass in the allocation pointer.
‫OK, the allocation pointer.
‫So just do a pointer here.
‫I forgot to do that at that point because that's what we're free.
‫And by doing that you will essentially ensure that you have the fastest solution possible because by
‫just passing a K free here, we just delete the memory straight away.
‫Process free does all sorts of things done, maps the memory.
‫You know, if we terminate in the process, we don't need to go through all of that difficulty because
‫we're not going to use the process again.
‫But like I said, I'm going to keep process free here in case you decide to adapt that in the future,
‫work more and process free.
‫And maybe it does something that is important and does need to be called.
‫So sometimes it's best quality.
‫Overcoached speed, guys.
‫OK, so here we're going to return zero down here.
‫We're just going to say that that was acceptable because process for returns void anyway.
‫Uh, OK, so here we're going to say if recist is below zero, return address, OK.
‫Or we can even just go to our go to I we'll go to our.
‫OK, so that's step one of process CHURMAN.
‫And we terminate all the allocations, they couldn't be bothered to terminate themselves.
‫So basically, any time they used Mallak and they didn't use free, we, the kernel, are responsible
‫for cleaning up after them.
‫They're like a messy teenager.
‫OK, let's see what else we need to free now.
‫So we're going to go there and we can see we need to free the stack.
‫So that's the next thing.
‫So what do we do?
‫We go, OK, free process stack.
‫Free the process, stack memory, and now we can't just go and do a Kifri on the pointer, right, because,
‫well, we already don't really know what it is.
‫It could be an ELF program.
‫It could be a binary program.
‫Right.
‫So we need to do something like process free data.
‫OK, and then we pass that in or we can even call it program data free program data or program data
‫free, you know, whatever you want.
‫And don't you start to process my back just to process like that.
‫OK, and we're going to go, rascal's that and we're going to say if we're below zero, go to out.
‫OK, so now let's do in it process free program data, struct process process and we're just going to
‫return zero here and it rasika zero return address and we're going to say switch process type.
‫File type, process, file type, and we're going to say case process, file type binary, OK?
‫And what this will do is it will just do a Kifri on process.
‫Pointer, and we should even make a separate function, really, so we're going to stay in it process
‫free binary data, struct process process and then we just do a Kifri process pointer and then we're
‫just going to return zero.
‫OK, and then here we'll just go rest's equals process free binary data and we'll pass in the process
‫there.
‫OK, nice.
‫Not bad.
‫So here we're going to go, case process file type elf and we're going to give a break and then we're
‫going to say REST's equals elf free.
‫We'll know it'll be processed, free health data, process, free health data, and we'll pass in the
‫process there.
‫Now for the faults, we're just going to set the rest to minus invalid argument, OK?
‫Because the process has a file type that we don't support.
‫OK, and now we're going to do is we're going to create the process free of data.
‫So we're going to go in it process, free of data, struct process process.
‫And we're just going return zero here and we're just going to do an error free, essentially our free
‫process, our file.
‫OK, and it is as simple as that.
‫We need to create our free now and that will basically free the memory of the old files.
‫So just go to Loda offloads, see, and just to our float.
‫We've already got it.
‫We've got LTH clothes, that's what we named it, elf clothes.
‫So what that does is a freeze of memory and freeze the file itself.
‫So yeah, just go back here and instead of free just use elf clothes and that's perfect.
‫So that'll close the file and free the memory.
‫All right.
‫That's really good.
‫So now you can see by breaking things down into many different functions, as much cleaner to write
‫code as you're already aware, and just one little call to process free program data.
‫And it doesn't matter if it's binary or ELF, the program will figure it out and free the data correctly.
‫So, yeah, once we freed the stack memory, we now need to basically free the task.
‫OK, so we're going to go task free process task.
‫OK, and have we created tasks free.
‫Yes, we have.
‫Perfect.
‫So we've actually already done task free.
‫So that removes the page directory and removes it from the task list.
‫So excellent.
‫OK, so at this point in time we freed the entire process and the task, however the process pointer
‫is still in the process is array, which is not good.
‫We absolutely need to remove that from that list.
‫So what we can say is, is essentially we can just go something like this, right?
‫We can scroll down here and we can just go static, void process, unlink struct process process.
‫And what we can then do is we can go process is process ID equals zero zero.
‫So that successfully removes that process from the processes array.
‫OK, so that's perfect.
‫We do need to do one of the thing we need to say, if the current process is equal to the process or
‫unthinking, then we need to change the current process.
‫So that's very important.
‫OK.
‫So what we're going to say is process switch to any OK, just like that.
‫And this function where we haven't made yet and it will basically switch to any process it finds, OK,
‫we've removed our process here so we know that it won't pick that one.
‫So just by here, then we're going to say, uh, void process, switch to any
‫struct process process.
‫Oh, no, no, no, no, no, no struct process process just like that.
‫And we're just going to loop through it for I equals zero I below ptw max processes I plus plus.
‫Okay.
‫And we're going to say if the process is I then process which process is.
‫I just like that.
‫OK, and then that will ensure that we switch to that process there.
‫OK, and here we're just going to return.
‫OK, and by here we're going to say panic.
‫No processes to switch to.
‫This is very important because if we don't have a process to switch to, then that can be a bit dangerous.
‫OK, we kind of always want to process there.
‫OK, so in this scenario you can either start a process again, maybe some sort of shell or you can
‫panic and we're just going to panic for the sake of it.
‫OK, essentially, if we have a bunch of no pointers around the place, you know, it could lead to
‫bugs, you know, people trying to get the current process and it returns null.
‫You know what I mean?
‫The same for tasks, OK?
‫There needs to be a running task at all times, really, to be honest, for our system to work properly.
‫So in this situation, ideally, you would just want to shell.
‫OK, well, we'll just panic for now.
‫And if you do decide that we want to share, then make a separate function for.
‫This load shell, for example, it's much cleaner right now, now we'll just keep the panic because
‫it's educational and it's good that it's good that you can see what's going on.
‫So we're going to build that.
‫You see that bill's absolutely fine.
‫OK, so that should be it now for our termination functionality.
‫OK, we do actually have to call process Suddenlink actually, which we haven't done.
‫We've only made the function.
‫So just to the bottom here, right.
‫Just after we free the tasks, just go process, unlink.
‫That's all we need to do now, really.
‫And we'll pass in the process.
‫Right.
‫And there we go.
‫OK, so yeah, that's pretty much it for termination that I'll terminate any process you pass to it.
‫It'll free it from its list.
‫It will to lead the process, it'll free all the memory and terminate all applications.
‫That process made in its lifetime that it didn't free.
‫So yeah, we now have process termination.
‫Let's just put that in the header file process terminate, uh, just so we have access to it.
‫And there we go, so now in the next couple of lectures, we'll actually start using process terminate,
‫we'll handle crashes.
‫You know, what is a crash?
‫A crash is when a program does something.
‫It's not supposed to access memory.
‫It shouldn't, for example, rights to memory that's read only.
‫You know, these types of things call cause page faults, general protection faults.
‫And as you know, that throws an exception.
‫So we can handle that by calling a process crash function or something, which basically just calls
‫process terminate, which then terminates the program.
‫And you might want to even start another program when the process crashes and passed the IP, the program
‫counter address of way of where the program crashed, you know, a bit like in Windows when it says
‫this program, it's not responding and then it crashes, you know, a little like that.
‫Or like in Linux when you're in the terminal and it says segmentation fault, you know, we can alert
‫the user to it's perfectly possible we just create a process crash function, which calls process terminate
‫when there's an interrupt exception.
‫And then we just basically launch another program that shows the crash information.
‫It's very easy.
‫We pass it to it through its aragvi, to its program arguments, you know, and let's give it to those
‫new lines.
‫This needs to be a bit tidier, right, for the task.
‫Unlink the process from the array.
‫OK, perfect from the processor has to be right.
‫OK, perfect guys.
‫So thanks for watching.
‫And stay tuned to the next lectures now.