xv6, line by line
user/kill.c

user/kill.c

C · 17 lines · annotated 100% · user program / library · upstream

About this file

kill pid ... asks the kernel to terminate each process whose PID (process ID) is given. xv6 has no ps command, but you can see the PIDs by typing Ctrl-P at the console, which makes the kernel print its process table (procdump).

“Kill” in xv6 is a request, not an instant stop. sys_kill calls kkill, which only sets the victim’s killed flag and, if the victim is sleeping, makes it runnable. The victim notices the flag itself: usertrap checks it at the start of every system call (kernel/trap.c:57) and after handling any trap, just before returning to user space (kernel/trap.c:81), and long waits such as sys_pause, consoleread and kwait check it after waking. The victim then calls kexit with status -1 (exit status). This way the process never dies in the middle of a kernel operation, while holding locks or halfway through a file system update.

There are no signals in xv6: kill cannot be caught, ignored, or used to send anything other than “terminate”.

Read before: user/zombie.c. Read next: kernel/proc.c (kkill).

5int
6main(int argc, char **argv)
7{
8 int i;
10 if (argc < 2) {
11 fprintf(2, "usage: kill pid...\n");
12 exit(1);
13 }
14 for (i = 1; i < argc; i++)
16 exit(0);