Skip to content

Processes

Timothy Burchfield edited this page May 3, 2018 · 2 revisions

Processes can be created similarly to in Linux, by using the system calls fork and exec. Fork takes no arguments, and returns 0 to the child, and the pid of the child to the parent. Exec takes the path to the program to run, an array of string arguments, and the number of arguments. These arguments must be under 255 characters in length, and are placed above the process stack at the time the process is created.

The first process is created with sys_process_run, which takes similar arguments as exec. (There is an analogous system call process_run, but this will likely be removed in favor of fork and exec.)

Processes contain a process id (pid) as well as a process parent id (ppid). The pid is allocated by a next fit search through an array of 1024 available ids. This array also serves to allow easy lookup of processes by id.

A process is killed in any of three cases:
1. It performs the exit system call.
2. It is killed by another process by the process_kill system call.
3. Its parent is killed.

When a process is killed, it is removed from any process queue it might be in (i.e. the ready queue or an I/O queue) and placed in the grave queue. Additionally, any child of that process is killed (as well as any of their children, etc.). A process in the grave queue is not cleaned up immediately, however.

An immediate parent of a process can get the exit status of a process by using the process_wait system call. This will return the exit records of the first of that process' dead children in the grave queue. process_wait takes a timeout argument, and will wait for as long as the timeout before returning with no process dead. process_wait also takes a pointer to a process_info struct. The process_info struct has three members - the pid of the waited on process, its exit code, and its exit reason. The exit code is set when the process calls exit, and is set to 0 when a process is killed without exiting normally. The exit reason is set to PROCESS_EXIT_NORMAL (0) when a process calls exit, and is set to PROCESS_EXIT_KILLED (1) when a process is killed another way.

process_wait does not clean the records of a process. Instead, process_reap must be called to discard a process completely. When a process is reaped, its windows' reference counters are decreased, its pagetable and kernel object are freed, and its pid is opened for reuse. process_reap takes in a specific pid to reap. Currently, any process can reap another process, even if they are not a parent of the reaped process.