1 00:00:00,450 --> 00:00:06,900 In this lecture, we will talk about the general process of program exit. In our system, 2 00:00:06,960 --> 00:00:09,210 we use function exit and wait to do that. 3 00:00:10,320 --> 00:00:12,390 When a program exits, 4 00:00:12,390 --> 00:00:14,100 the function exit is called 5 00:00:14,100 --> 00:00:18,270 and the system call function sys exit in this case receives the request, 6 00:00:18,900 --> 00:00:23,490 then it will append the process in the killed list and then switch to other processes. 7 00:00:24,390 --> 00:00:31,110 When the init process gets to run, it will call function wait to do the cleanup. 8 00:00:31,270 --> 00:00:36,920 In this system, the init process is the process responsible for cleaning up other processes when they exit. 9 00:00:37,560 --> 00:00:39,690 Ok, let’s see it in the process file. 10 00:00:46,130 --> 00:00:52,010 As you see, we retrieved the process control and get the current process, then we change the state to 11 00:00:52,010 --> 00:00:57,230 proc killed and append it in the killed list. So we add a new list 12 00:01:00,420 --> 00:01:03,140 In the structure here. 13 00:01:04,680 --> 00:01:10,860 Then we call wake up function. The reason we call wake up function is that the init process 14 00:01:10,860 --> 00:01:16,140 which is the first process in this system is constantly calling wait function to do the cleaning up. 15 00:01:16,710 --> 00:01:18,620 And it could be in sleep state, 16 00:01:19,200 --> 00:01:23,310 so we wake it up before we switch process. The argument we pass 17 00:01:23,320 --> 00:01:27,510 is 1 which means we will wake up the process with pid 1. 18 00:01:28,610 --> 00:01:31,760 Remember the first process is assigned with pid 1. 19 00:01:32,700 --> 00:01:35,340 Ok then we jump to scheduler and we are done. 20 00:01:36,290 --> 00:01:38,090 Next, we move to the wait function. 21 00:01:39,180 --> 00:01:44,640 The first thing we are going to do is we are going to get the killed list. And after that, we will be running in the infinite loop 22 00:01:44,640 --> 00:01:49,200 to constantly find exit process and do the cleanup. 23 00:01:50,390 --> 00:01:56,920 So we check the killed list to see if it is empty, if it is, we just sleep and pass 1 to it. 24 00:01:57,320 --> 00:01:59,450 Here 1 is the pid of this process. 25 00:02:00,020 --> 00:02:05,840 Just as we talked about in the exit function, it will wake up the process when a process exits 26 00:02:05,840 --> 00:02:06,350 . 27 00:02:07,800 --> 00:02:14,310 If we have elements in the list, we get the elements and do the cleanup, such as free the kernel stack page 28 00:02:14,310 --> 00:02:21,510 and free vm for that process. Then we clear the process structure so that it becomes unused in the process table 29 00:02:21,510 --> 00:02:22,380 . 30 00:02:23,250 --> 00:02:25,220 OK, that's it for the process module. 31 00:02:26,280 --> 00:02:31,800 The Next thing we are going to do is we are going to do add the system calls in the syscall file. 32 00:02:38,150 --> 00:02:41,390 You can see we add sysexit and syswait here. 33 00:02:43,080 --> 00:02:46,080 In the functions, we simply exit or wait functions. 34 00:02:47,160 --> 00:02:53,280 And also, at this point, the checks should allow more values passing in for the index number. 35 00:02:54,350 --> 00:02:59,660 Alright, with kernel module prepared, let’s rebuild the library since we need two more functions. 36 00:03:03,410 --> 00:03:04,790 In the syscall file, 37 00:03:08,880 --> 00:03:10,200 we write exitu, 38 00:03:12,240 --> 00:03:13,920 the index number is 2. 39 00:03:15,670 --> 00:03:19,570 Because it passes 0 arguments, we don’t allocate space on the stack. 40 00:03:20,640 --> 00:03:28,070 So we save 0 to rdi which means no arguments. Then we int 80. 41 00:03:29,560 --> 00:03:35,950 After we return from kernel, we can return to caller. Normally we will never return from sys exit 42 00:03:35,980 --> 00:03:39,100 as we have seen in the exit function. 43 00:03:40,530 --> 00:03:42,960 Ok next function is waitu. 44 00:03:45,410 --> 00:03:52,870 The index number is 3 and it doesn’t have arguments either. So we move rdi 0. 45 00:03:55,560 --> 00:03:57,070 And int 80 46 00:03:58,050 --> 00:04:00,630 After returning from the kernel, we simply return. 47 00:04:01,620 --> 00:04:05,100 And don't forget to global exitu 48 00:04:08,500 --> 00:04:09,290 and waitu 49 00:04:11,450 --> 00:04:13,250 now let’s build the library. 50 00:04:25,960 --> 00:04:26,420 OK. 51 00:04:26,750 --> 00:04:30,390 Also, we add the declaration of the functions in lib.h 52 00:04:32,760 --> 00:04:37,830 Next we test the exit function in the user2 program, since the first program calls wait function 53 00:04:37,830 --> 00:04:40,860 to do the cleanup for exit programs. 54 00:04:42,180 --> 00:04:49,950 Exit out a process is simple, in the start.asm of user2, all we need to do is add exitu 55 00:04:49,950 --> 00:04:52,500 after we return from main function. 56 00:04:52,830 --> 00:04:57,210 Since exitu is not defined here, we add keyword extern. 57 00:05:00,520 --> 00:05:02,340 In the main.c file, 58 00:05:06,090 --> 00:05:07,380 we get rid of loop 59 00:05:08,800 --> 00:05:13,870 and it will return from main function and exit. Ok let’s move to user1. 60 00:05:15,080 --> 00:05:20,960 Since the library and start.o are the same as in the user1. We copy and paste it here. 61 00:05:23,590 --> 00:05:24,940 And in the main function, 62 00:05:29,180 --> 00:05:35,210 we call waitu function in a loop. The user1 program is used to call the wait function to do the cleanup in the system. 63 00:05:35,210 --> 00:05:38,390 Let's build the user program. 64 00:05:43,810 --> 00:05:49,720 In this example, we also need one more program, because both user1 and user2 could call function sleep, 65 00:05:49,720 --> 00:05:53,230 if that happens, we will have no process to run. 66 00:05:55,560 --> 00:06:01,530 And you see, the assertion in the schedule will fail. So we add user3 in the system 67 00:06:01,770 --> 00:06:05,610 which will be the only process available if no other processes to run. 68 00:06:06,750 --> 00:06:08,690 I have copied one in the last lecture. 69 00:06:10,550 --> 00:06:12,660 The only change we make in this file 70 00:06:14,960 --> 00:06:19,610 is the message process 3. Let’s build the project. 71 00:06:25,190 --> 00:06:28,040 Now we can copy the 3 programs in the boot folder. 72 00:06:55,190 --> 00:07:02,480 In the build script, we write the user3 from sector 126. In the loader file, 73 00:07:07,910 --> 00:07:15,590 we load the user3 into address for example, 40000 from sector 126. 74 00:07:17,230 --> 00:07:18,580 So in the process, 75 00:07:20,730 --> 00:07:25,320 we add one more element in the array the address 40000 76 00:07:26,910 --> 00:07:28,620 and change the count to 3. 77 00:07:29,760 --> 00:07:31,160 OK, let's test it out. 78 00:07:40,970 --> 00:07:46,130 As you see, the only process is message printed from process3. 79 00:07:47,780 --> 00:07:49,100 That's it for this lecture. 80 00:07:49,130 --> 00:07:50,420 See you in the next video.