﻿1
00:00:00,240 --> 00:00:05,760
‫Hello and welcome, everybody, and in this lecture, we're going to implement program termination.

2
00:00:06,480 --> 00:00:08,200
‫So what is program termination?

3
00:00:08,220 --> 00:00:14,300
‫Well, if you have a task manager and kill the process, you just terminated a program.

4
00:00:14,760 --> 00:00:17,250
‫We're going to implement program termination today.

5
00:00:17,250 --> 00:00:23,770
‫We won't be implementing a function to the colonel that allows people to terminate programs.

6
00:00:24,060 --> 00:00:28,350
‫We will, however, be right in the functionality required to terminate a program.

7
00:00:28,560 --> 00:00:32,220
‫And later on in the course, we can implement program crashes.

8
00:00:32,520 --> 00:00:38,670
‫So if there's a unhandled page fault, for example, then we can terminate the program forcefully.

9
00:00:39,420 --> 00:00:41,650
‫So, yeah, we're going to implement that today.

10
00:00:42,210 --> 00:00:45,750
‫So first, let's explain how termination works.

11
00:00:45,960 --> 00:00:49,270
‫We remove the task from the task list.

12
00:00:49,320 --> 00:00:54,860
‫OK, so it's no longer there that ensures that it no longer runs any code of the process.

13
00:00:55,440 --> 00:01:00,630
‫Once we've done that, we can then safely delete the memory of the process.

14
00:01:00,820 --> 00:01:02,340
‫OK, so that's the next step.

15
00:01:02,970 --> 00:01:05,850
‫We delete the memory of the process and the memory at the task.

16
00:01:06,570 --> 00:01:12,080
‫Finally, we remove the process from the process list and then there's no more instance of it.

17
00:01:12,960 --> 00:01:18,060
‫There's literally no more instance of processes, all of of that task.

18
00:01:18,090 --> 00:01:19,160
‫It's completely gone.

19
00:01:19,890 --> 00:01:21,900
‫It's been unloaded from memory.

20
00:01:22,200 --> 00:01:27,120
‫And because it's had the task removed from the list and the process is removed from the list, we are

21
00:01:27,120 --> 00:01:30,470
‫now sure that the program can't run anymore.

22
00:01:30,690 --> 00:01:35,130
‫We we successfully terminate the program now in a more better implementation.

23
00:01:35,130 --> 00:01:42,330
‫You could call some sort of handler to let the process know that we were about to kill it and then we

24
00:01:42,330 --> 00:01:44,310
‫can kill it in the next cycle or something.

25
00:01:44,320 --> 00:01:44,590
‫Right.

26
00:01:44,640 --> 00:01:46,830
‫Although all like within a second or something.

27
00:01:47,130 --> 00:01:49,820
‫But we're not going to go that far in depth, at least for now.

28
00:01:50,520 --> 00:01:53,400
‫So we're just going to focus on program termination.

29
00:01:53,730 --> 00:01:54,760
‫OK, let's begin.

30
00:01:55,320 --> 00:02:00,570
‫So we're going to start this by going to task and process start see.

31
00:02:01,680 --> 00:02:11,250
‫And just by here, we're going to say in it process, terminate, process, process.

32
00:02:12,430 --> 00:02:17,430
‫OK, so we're going to terminate the process.

33
00:02:17,430 --> 00:02:18,720
‫We're going to return zero here.

34
00:02:19,980 --> 00:02:22,380
‫So let's see what we need to free.

35
00:02:22,410 --> 00:02:23,850
‫We need to free the task.

36
00:02:24,840 --> 00:02:26,790
‫We need to free our stack.

37
00:02:26,790 --> 00:02:29,580
‫The point, all the arguments.

38
00:02:29,580 --> 00:02:31,250
‫So there's quite a lot to go here, guys.

39
00:02:31,260 --> 00:02:36,480
‫OK, so the first thing we should do is terminate the process allocations.

40
00:02:36,480 --> 00:02:37,590
‫OK, so we're going to go in.

41
00:02:37,590 --> 00:02:42,870
‫It equals zero rest equals process, terminate allocations.

42
00:02:42,870 --> 00:02:48,060
‫That's the very first thing we're going to do, which will terminate all the allocations of the process.

43
00:02:48,060 --> 00:02:49,410
‫And we're going to return Rassi here.

44
00:02:49,410 --> 00:02:50,040
‫OK, guys.

45
00:02:50,400 --> 00:02:54,000
‫So we're going to go in a process, terminate allocations.

46
00:02:54,270 --> 00:03:05,700
‫You can make that static if you want struct process process four inch equals zero I below max process

47
00:03:06,270 --> 00:03:07,350
‫allocations.

48
00:03:07,350 --> 00:03:08,640
‫Well program allocations.

49
00:03:08,640 --> 00:03:21,720
‫I mean I plus plus and now we literally just, we, we just go process free and we pass in the process

50
00:03:21,720 --> 00:03:23,100
‫and the allocation.

51
00:03:24,600 --> 00:03:32,250
‫Now this is a slow way of doing it because process free well get the allocation by address and then

52
00:03:32,250 --> 00:03:35,040
‫it will unmap the memory and join it.

53
00:03:35,040 --> 00:03:36,030
‫And then Kifri.

54
00:03:37,140 --> 00:03:43,200
‫Now I'm, I'm using process free in case you decide to do anything special with process free in the

55
00:03:43,200 --> 00:03:43,710
‫future.

56
00:03:43,710 --> 00:03:49,440
‫OK, but if you really want to gain a speed advantage here, you can completely remove process free

57
00:03:49,440 --> 00:03:52,880
‫and just put and pass in the allocation pointer.

58
00:03:52,890 --> 00:03:56,100
‫OK, the allocation pointer.

59
00:03:56,130 --> 00:03:58,140
‫So just do a pointer here.

60
00:03:58,170 --> 00:04:01,470
‫I forgot to do that at that point because that's what we're free.

61
00:04:01,920 --> 00:04:09,420
‫And by doing that you will essentially ensure that you have the fastest solution possible because by

62
00:04:09,420 --> 00:04:12,690
‫just passing a K free here, we just delete the memory straight away.

63
00:04:12,990 --> 00:04:16,380
‫Process free does all sorts of things done, maps the memory.

64
00:04:16,560 --> 00:04:20,820
‫You know, if we terminate in the process, we don't need to go through all of that difficulty because

65
00:04:20,820 --> 00:04:22,260
‫we're not going to use the process again.

66
00:04:22,650 --> 00:04:27,270
‫But like I said, I'm going to keep process free here in case you decide to adapt that in the future,

67
00:04:27,510 --> 00:04:28,890
‫work more and process free.

68
00:04:28,890 --> 00:04:32,310
‫And maybe it does something that is important and does need to be called.

69
00:04:32,640 --> 00:04:35,010
‫So sometimes it's best quality.

70
00:04:35,670 --> 00:04:37,050
‫Overcoached speed, guys.

71
00:04:37,050 --> 00:04:40,050
‫OK, so here we're going to return zero down here.

72
00:04:40,050 --> 00:04:44,520
‫We're just going to say that that was acceptable because process for returns void anyway.

73
00:04:45,360 --> 00:04:52,560
‫Uh, OK, so here we're going to say if recist is below zero, return address, OK.

74
00:04:52,710 --> 00:04:55,590
‫Or we can even just go to our go to I we'll go to our.

75
00:04:56,850 --> 00:04:59,340
‫OK, so that's step one of process CHURMAN.

76
00:04:59,570 --> 00:05:03,710
‫And we terminate all the allocations, they couldn't be bothered to terminate themselves.

77
00:05:04,120 --> 00:05:09,550
‫So basically, any time they used Mallak and they didn't use free, we, the kernel, are responsible

78
00:05:09,550 --> 00:05:11,490
‫for cleaning up after them.

79
00:05:11,890 --> 00:05:13,310
‫They're like a messy teenager.

80
00:05:13,900 --> 00:05:16,140
‫OK, let's see what else we need to free now.

81
00:05:16,150 --> 00:05:19,480
‫So we're going to go there and we can see we need to free the stack.

82
00:05:19,480 --> 00:05:20,540
‫So that's the next thing.

83
00:05:20,560 --> 00:05:21,580
‫So what do we do?

84
00:05:21,580 --> 00:05:24,400
‫We go, OK, free process stack.

85
00:05:25,840 --> 00:05:35,680
‫Free the process, stack memory, and now we can't just go and do a Kifri on the pointer, right, because,

86
00:05:36,130 --> 00:05:38,910
‫well, we already don't really know what it is.

87
00:05:38,920 --> 00:05:41,550
‫It could be an ELF program.

88
00:05:41,560 --> 00:05:43,330
‫It could be a binary program.

89
00:05:43,330 --> 00:05:43,540
‫Right.

90
00:05:43,560 --> 00:05:49,090
‫So we need to do something like process free data.

91
00:05:50,090 --> 00:05:58,930
‫OK, and then we pass that in or we can even call it program data free program data or program data

92
00:05:58,930 --> 00:06:01,030
‫free, you know, whatever you want.

93
00:06:01,030 --> 00:06:04,480
‫And don't you start to process my back just to process like that.

94
00:06:04,960 --> 00:06:12,370
‫OK, and we're going to go, rascal's that and we're going to say if we're below zero, go to out.

95
00:06:14,480 --> 00:06:24,240
‫OK, so now let's do in it process free program data, struct process process and we're just going to

96
00:06:24,280 --> 00:06:33,940
‫return zero here and it rasika zero return address and we're going to say switch process type.

97
00:06:36,930 --> 00:06:45,140
‫File type, process, file type, and we're going to say case process, file type binary, OK?

98
00:06:45,390 --> 00:06:51,900
‫And what this will do is it will just do a Kifri on process.

99
00:06:53,490 --> 00:06:58,500
‫Pointer, and we should even make a separate function, really, so we're going to stay in it process

100
00:06:58,500 --> 00:07:08,080
‫free binary data, struct process process and then we just do a Kifri process pointer and then we're

101
00:07:08,100 --> 00:07:09,360
‫just going to return zero.

102
00:07:09,420 --> 00:07:18,840
‫OK, and then here we'll just go rest's equals process free binary data and we'll pass in the process

103
00:07:18,840 --> 00:07:19,140
‫there.

104
00:07:20,220 --> 00:07:21,380
‫OK, nice.

105
00:07:21,390 --> 00:07:22,140
‫Not bad.

106
00:07:22,670 --> 00:07:30,840
‫So here we're going to go, case process file type elf and we're going to give a break and then we're

107
00:07:30,840 --> 00:07:34,200
‫going to say REST's equals elf free.

108
00:07:36,320 --> 00:07:42,530
‫We'll know it'll be processed, free health data, process, free health data, and we'll pass in the

109
00:07:42,530 --> 00:07:43,370
‫process there.

110
00:07:44,160 --> 00:07:51,620
‫Now for the faults, we're just going to set the rest to minus invalid argument, OK?

111
00:07:52,280 --> 00:07:56,270
‫Because the process has a file type that we don't support.

112
00:07:56,780 --> 00:08:01,410
‫OK, and now we're going to do is we're going to create the process free of data.

113
00:08:01,420 --> 00:08:06,560
‫So we're going to go in it process, free of data, struct process process.

114
00:08:08,150 --> 00:08:14,120
‫And we're just going return zero here and we're just going to do an error free, essentially our free

115
00:08:14,120 --> 00:08:16,880
‫process, our file.

116
00:08:17,290 --> 00:08:19,580
‫OK, and it is as simple as that.

117
00:08:19,580 --> 00:08:25,310
‫We need to create our free now and that will basically free the memory of the old files.

118
00:08:25,330 --> 00:08:31,380
‫So just go to Loda offloads, see, and just to our float.

119
00:08:32,300 --> 00:08:33,170
‫We've already got it.

120
00:08:33,170 --> 00:08:36,250
‫We've got LTH clothes, that's what we named it, elf clothes.

121
00:08:36,530 --> 00:08:40,150
‫So what that does is a freeze of memory and freeze the file itself.

122
00:08:40,490 --> 00:08:45,740
‫So yeah, just go back here and instead of free just use elf clothes and that's perfect.

123
00:08:45,740 --> 00:08:48,030
‫So that'll close the file and free the memory.

124
00:08:48,350 --> 00:08:48,740
‫All right.

125
00:08:48,740 --> 00:08:49,580
‫That's really good.

126
00:08:49,940 --> 00:08:56,000
‫So now you can see by breaking things down into many different functions, as much cleaner to write

127
00:08:56,000 --> 00:09:02,630
‫code as you're already aware, and just one little call to process free program data.

128
00:09:02,840 --> 00:09:08,390
‫And it doesn't matter if it's binary or ELF, the program will figure it out and free the data correctly.

129
00:09:09,620 --> 00:09:16,110
‫So, yeah, once we freed the stack memory, we now need to basically free the task.

130
00:09:16,130 --> 00:09:20,930
‫OK, so we're going to go task free process task.

131
00:09:21,090 --> 00:09:23,840
‫OK, and have we created tasks free.

132
00:09:23,870 --> 00:09:24,950
‫Yes, we have.

133
00:09:24,950 --> 00:09:25,520
‫Perfect.

134
00:09:25,530 --> 00:09:27,530
‫So we've actually already done task free.

135
00:09:27,860 --> 00:09:32,810
‫So that removes the page directory and removes it from the task list.

136
00:09:32,820 --> 00:09:33,530
‫So excellent.

137
00:09:34,130 --> 00:09:40,080
‫OK, so at this point in time we freed the entire process and the task, however the process pointer

138
00:09:40,100 --> 00:09:44,750
‫is still in the process is array, which is not good.

139
00:09:46,040 --> 00:09:48,910
‫We absolutely need to remove that from that list.

140
00:09:50,030 --> 00:09:55,700
‫So what we can say is, is essentially we can just go something like this, right?

141
00:09:55,730 --> 00:10:03,290
‫We can scroll down here and we can just go static, void process, unlink struct process process.

142
00:10:04,670 --> 00:10:11,600
‫And what we can then do is we can go process is process ID equals zero zero.

143
00:10:12,470 --> 00:10:17,240
‫So that successfully removes that process from the processes array.

144
00:10:17,360 --> 00:10:18,730
‫OK, so that's perfect.

145
00:10:19,160 --> 00:10:25,700
‫We do need to do one of the thing we need to say, if the current process is equal to the process or

146
00:10:25,720 --> 00:10:28,660
‫unthinking, then we need to change the current process.

147
00:10:28,670 --> 00:10:29,910
‫So that's very important.

148
00:10:29,930 --> 00:10:30,330
‫OK.

149
00:10:30,470 --> 00:10:37,070
‫So what we're going to say is process switch to any OK, just like that.

150
00:10:37,520 --> 00:10:42,730
‫And this function where we haven't made yet and it will basically switch to any process it finds, OK,

151
00:10:43,700 --> 00:10:47,400
‫we've removed our process here so we know that it won't pick that one.

152
00:10:47,810 --> 00:10:52,700
‫So just by here, then we're going to say, uh, void process, switch to any

153
00:10:56,000 --> 00:10:57,800
‫struct process process.

154
00:10:57,810 --> 00:11:02,450
‫Oh, no, no, no, no, no, no struct process process just like that.

155
00:11:02,930 --> 00:11:13,880
‫And we're just going to loop through it for I equals zero I below ptw max processes I plus plus.

156
00:11:14,960 --> 00:11:15,240
‫Okay.

157
00:11:15,320 --> 00:11:24,980
‫And we're going to say if the process is I then process which process is.

158
00:11:24,980 --> 00:11:27,590
‫I just like that.

159
00:11:27,590 --> 00:11:32,950
‫OK, and then that will ensure that we switch to that process there.

160
00:11:32,960 --> 00:11:36,650
‫OK, and here we're just going to return.

161
00:11:37,430 --> 00:11:42,080
‫OK, and by here we're going to say panic.

162
00:11:43,160 --> 00:11:45,740
‫No processes to switch to.

163
00:11:46,380 --> 00:11:53,480
‫This is very important because if we don't have a process to switch to, then that can be a bit dangerous.

164
00:11:53,510 --> 00:11:55,870
‫OK, we kind of always want to process there.

165
00:11:55,880 --> 00:12:02,510
‫OK, so in this scenario you can either start a process again, maybe some sort of shell or you can

166
00:12:02,510 --> 00:12:04,850
‫panic and we're just going to panic for the sake of it.

167
00:12:04,850 --> 00:12:10,070
‫OK, essentially, if we have a bunch of no pointers around the place, you know, it could lead to

168
00:12:10,070 --> 00:12:14,510
‫bugs, you know, people trying to get the current process and it returns null.

169
00:12:14,720 --> 00:12:15,470
‫You know what I mean?

170
00:12:15,740 --> 00:12:17,150
‫The same for tasks, OK?

171
00:12:17,360 --> 00:12:22,910
‫There needs to be a running task at all times, really, to be honest, for our system to work properly.

172
00:12:23,630 --> 00:12:27,410
‫So in this situation, ideally, you would just want to shell.

173
00:12:27,680 --> 00:12:29,630
‫OK, well, we'll just panic for now.

174
00:12:30,470 --> 00:12:34,300
‫And if you do decide that we want to share, then make a separate function for.

175
00:12:34,970 --> 00:12:41,180
‫This load shell, for example, it's much cleaner right now, now we'll just keep the panic because

176
00:12:41,180 --> 00:12:44,550
‫it's educational and it's good that it's good that you can see what's going on.

177
00:12:44,840 --> 00:12:45,980
‫So we're going to build that.

178
00:12:47,700 --> 00:12:49,660
‫You see that bill's absolutely fine.

179
00:12:50,310 --> 00:12:56,460
‫OK, so that should be it now for our termination functionality.

180
00:12:56,530 --> 00:13:01,350
‫OK, we do actually have to call process Suddenlink actually, which we haven't done.

181
00:13:01,350 --> 00:13:02,430
‫We've only made the function.

182
00:13:02,440 --> 00:13:03,870
‫So just to the bottom here, right.

183
00:13:03,870 --> 00:13:06,870
‫Just after we free the tasks, just go process, unlink.

184
00:13:07,710 --> 00:13:09,360
‫That's all we need to do now, really.

185
00:13:09,690 --> 00:13:11,220
‫And we'll pass in the process.

186
00:13:11,230 --> 00:13:11,530
‫Right.

187
00:13:12,000 --> 00:13:12,720
‫And there we go.

188
00:13:12,750 --> 00:13:18,090
‫OK, so yeah, that's pretty much it for termination that I'll terminate any process you pass to it.

189
00:13:18,090 --> 00:13:19,980
‫It'll free it from its list.

190
00:13:20,250 --> 00:13:26,670
‫It will to lead the process, it'll free all the memory and terminate all applications.

191
00:13:26,670 --> 00:13:29,380
‫That process made in its lifetime that it didn't free.

192
00:13:29,790 --> 00:13:31,770
‫So yeah, we now have process termination.

193
00:13:31,770 --> 00:13:37,650
‫Let's just put that in the header file process terminate, uh, just so we have access to it.

194
00:13:39,560 --> 00:13:44,810
‫And there we go, so now in the next couple of lectures, we'll actually start using process terminate,

195
00:13:45,170 --> 00:13:46,730
‫we'll handle crashes.

196
00:13:47,600 --> 00:13:48,860
‫You know, what is a crash?

197
00:13:48,860 --> 00:13:50,460
‫A crash is when a program does something.

198
00:13:50,460 --> 00:13:52,610
‫It's not supposed to access memory.

199
00:13:52,610 --> 00:13:56,180
‫It shouldn't, for example, rights to memory that's read only.

200
00:13:56,360 --> 00:14:01,610
‫You know, these types of things call cause page faults, general protection faults.

201
00:14:01,850 --> 00:14:04,190
‫And as you know, that throws an exception.

202
00:14:04,490 --> 00:14:10,340
‫So we can handle that by calling a process crash function or something, which basically just calls

203
00:14:10,340 --> 00:14:13,880
‫process terminate, which then terminates the program.

204
00:14:14,090 --> 00:14:21,770
‫And you might want to even start another program when the process crashes and passed the IP, the program

205
00:14:21,770 --> 00:14:27,260
‫counter address of way of where the program crashed, you know, a bit like in Windows when it says

206
00:14:27,260 --> 00:14:31,820
‫this program, it's not responding and then it crashes, you know, a little like that.

207
00:14:31,820 --> 00:14:35,990
‫Or like in Linux when you're in the terminal and it says segmentation fault, you know, we can alert

208
00:14:35,990 --> 00:14:42,740
‫the user to it's perfectly possible we just create a process crash function, which calls process terminate

209
00:14:43,640 --> 00:14:45,650
‫when there's an interrupt exception.

210
00:14:46,040 --> 00:14:52,940
‫And then we just basically launch another program that shows the crash information.

211
00:14:52,970 --> 00:14:53,750
‫It's very easy.

212
00:14:54,290 --> 00:14:59,060
‫We pass it to it through its aragvi, to its program arguments, you know, and let's give it to those

213
00:14:59,060 --> 00:14:59,710
‫new lines.

214
00:14:59,720 --> 00:15:03,590
‫This needs to be a bit tidier, right, for the task.

215
00:15:05,240 --> 00:15:09,790
‫Unlink the process from the array.

216
00:15:10,310 --> 00:15:14,060
‫OK, perfect from the processor has to be right.

217
00:15:15,020 --> 00:15:16,130
‫OK, perfect guys.

218
00:15:16,130 --> 00:15:17,270
‫So thanks for watching.

219
00:15:17,270 --> 00:15:19,190
‫And stay tuned to the next lectures now.

