Search icon CANCEL
Arrow left icon
Explore Products
Best Sellers
New Releases
Books
Videos
Audiobooks
Learning Hub
Conferences
Free Learning
Arrow right icon
Arrow up icon
GO TO TOP
Mastering Linux Kernel Development

You're reading from   Mastering Linux Kernel Development A kernel developer's reference manual

Arrow left icon
Product type Paperback
Published in Oct 2017
Publisher Packt
ISBN-13 9781785883057
Length 354 pages
Edition 1st Edition
Tools
Arrow right icon
Author (1):
Arrow left icon
CH Raghav Maruthi CH Raghav Maruthi
Author Profile Icon CH Raghav Maruthi
CH Raghav Maruthi
Arrow right icon
View More author details
Toc

Table of Contents (12) Chapters Close

Preface 1. Comprehending Processes, Address Space, and Threads FREE CHAPTER 2. Deciphering the Process Scheduler 3. Signal Management 4. Memory Management and Allocators 5. Filesystems and File I/O 6. Interprocess Communication 7. Virtual Memory Management 8. Kernel Synchronization and Locking 9. Interrupts and Deferred Work 10. Clock and Time Management 11. Module Management

Process status and termination

During the lifetime of a process, it traverses through many states before it ultimately terminates. Users must have proper mechanisms to be updated with all that happens to a process during its lifetime. Linux provides a set of functions for this purpose.

wait

For processes and threads created by a parent, it might be functionally useful for the parent to know the execution status of the child process/thread. This can be achieved using the wait family of system calls:

#include <sys/types.h>
#include <sys/wait.h>
pid_t wait(int *status);
pid_t waitpid(pid_t pid, int *status, intoptions);
int waitid(idtype_t idtype, id_t id, siginfo_t *infop, int options)

These system calls update the calling process with the state change events of a child. The following state change events are notified:

  • Termination of child
  • Stopped by a signal
  • Resumed by a signal

In addition to reporting the status, these APIs allow the parent process to reap a terminated child. A process on termination is put into zombie state until the immediate parent engages the wait call to reap it.

exit

Every process must end. Process termination is done either by the process calling exit() or when the main function returns. A process may also be terminated abruptly on receiving a signal or exception that forces it to terminate, such as the KILL command, which sends a signal to kill the process, or when an exception is raised. Upon termination, the process is put into exit state until the immediate parent reaps it.

The exit calls the sys_exit system call, which internally calls the do_exit routine. The do_exit primarily performs the following tasks (do_exit sets many values and makes multiple calls to related kernel routines to complete its task):

  • Takes the exit code returned by the child to the parent.
  • Sets the PF_EXITING flag, indicating process exiting.
  • Cleans up and reclaims the resources held by the process. This includes releasing mm_struct, removal from the queue if it is waiting for an IPC semaphore, release of filesystem data and files, if any, and calling schedule() as the process is no longer executable.

After do_exit, the process remains in zombie state and the process descriptor is still intact for the parent to collect the status, after which the resources are reclaimed by the system.

You have been reading a chapter from
Mastering Linux Kernel Development
Published in: Oct 2017
Publisher: Packt
ISBN-13: 9781785883057
Register for a free Packt account to unlock a world of extra content!
A free Packt account unlocks extra newsletters, articles, discounted offers, and much more. Start advancing your knowledge today.
Unlock this book and the full library FREE for 7 days
Get unlimited access to 7000+ expert-authored eBooks and videos courses covering every tech area you can think of
Renews at $19.99/month. Cancel anytime