#os #system-design #engineering
Process is the unit of work in a computer system, it's managed by the [Operating System].
It's a much wider concept than a program, we can say program is part of a process. A process includes:
A child process will inherit all the resources from its parent process, and it will copy all memories of the parent.
In multiprogramming environment, the CPU switch back and forth between processes, and it has to save and load all of the process's information in the progress, this is called context switch.
A process is represented by a data structure called Process Control Block (PCB) or Process Descriptor, it contains the following informations:
In Linux, a process is represented by a struct called
it can be found in <linux/sched.h>.
There are 5 main states of a process, but there are also many transistion states in between, the following diagram show every possible states and their transistions.
A process terminates when it finished its execution or being forced to termination. When this happen, its resources will be returned to the system, and its PCB will be erased and return to the free memory pool.
There are some termination reasons:
The wait() system call returns the ID of a terminated child process, so the parent can tell which and when the child process has been terminated.
The program must be aware of and know how to handle these termination signals for various resons. The following are some common signals:
SIGTERM: This signal can be blocked, handled or ignored, and
this is the polite way to ask a program to terminate.
use this signal.
SIGINT: "program interrupt" signal is sent when the user types
INTR character (Ctrl + C).
SIGQUIT: Similar to SIGINT, except it's the
(Ctrl + \), this will produces a core dump when it terminate the
process, like an error signal.
If a program creates temporary files or something, it should not be deleted when handling this signal, so the user can examine them afterwards.
SIGKILL: This cause immediate termination, it cannot be handled
or ignored, therefore always fatal. This is what
kill -9 does. One
should not use SIGKILL before trying other methods like SIGTERM or
Ctrl + C.
SIGHUP: This to report that the user's terminal is disconencted.
If the parent process terminates, the child processes will become a zombie onced it terminated. Why?
When a process is terminated, the OS release most of its resources and information, but still keep some data and the termination status, because the parent process might be interested in these.
When a child process is terminated, parent process will receives a SIGCHLD signal, so it will be able to collect these data (done via wait() syscall). When this collection is done, the operating system release those last bits of information, and remove the pid from the process table. If the parent process is failed to do so, the system has to keep these data in the process table indefinitely.
Terminated process whose data has not been collected is called zombie process.
So, best practices is, program that spawn child processes must handle it properly to prevent the formation of zombies from its child processes.
To create a process from a program on disk, the OS must load all the program static data and program code into the memory which has been allocated to that process. Some OSes does this eagerly, some modern OSes does this lazily.
Then, the OS will initialize the program's stack, allocate some memory for program's heap, do some I/O related things, for example, on UNIX systems, each process will have 3 file descriptors open for STDIN, STDOUT, STDERR.
The process will starts to run when the OS jump in to the
routine of the process, at this point, it will transfers the control
of the CPU to that process.
Operating System Notes
#os #system-design #engineering Components of an Operating System Not all systems are the same, but most will have these components: Process…
Welcome! Look like you've found my personal notebook. This is the place where you can take a peek into my mind to see what I've been…
If you think this note resonated, be it positive or negative, please feel free to send me an email and we can talk.