Process Termination
Terminating a process
When a process terminates, the kernel releases the resources owned by the process and notifies the child’s parent.
A Process can be terminated in 2 ways:
- Self induced when it calls
exit()
- Initiated when it receives a signal or exception
In both cases, the termination is done via the code block do_exit()
- It sets the
PF_EXITING
flag in theflags
member of thetask_struct
. - It calls
del_timer_sync()
to remove any kernel timers. - If BSD process accounting is enabled,
do_exit()
callsacct_update_integrals()
to write out accounting information. - It calls
exit_mm()
to release themm_struct
held by this process. If no other process is using this address space—if the address space is not shared—the kernel destroys it. - It calls
exit_sem()
. If the process is queued waiting for an IPC semaphore, it is dequeued here. - It then calls
exit_files()
andexit_fs()
to decrement the usage count of objects related to file descriptors and filesystem data. If either usage counts reach zero, the object is no longer in use by any process, and it is destroyed. - It sets the task's exit code, stored in the
exit_code
member of the task_struct, to the code provided byexit()
or whatever kernel mechanism forced the termination.The exit code is stored here for optional retrieval by the parent. - It calls
exit_notify()
to send signals to the task’s parent, reparents any of the task’s children to another thread in their thread group or the init process, and sets the task’s exit state, stored inexit_state
in the task_struct structure, toEXIT_ZOMBIE
. do_exit()
callsschedule()
to switch to a new process.do_exit()
never returns
The task is not runnable (and no longer has an address space in which to run)
and is in the EXIT_ZOMBIE
exit state
The only memory it occupies is its kernel stack, the thread_info
structure, and the task_struct
structure.The task exists solely to provide information to its parent. After the parent retrieves the information, or notifies the kernel that it is uninterested, the remaining memory held by the process is freed and returned to the system for use.