Processs Consept All in One
Processs Consept All in One
Processes
Carnegie Mellon
Processes
Processes
A process is the OS's abstraction for execution
▪ A process represents a single running application on the system
Process has three main components:
1. Address space
▪ The memory that the process can access
▪ Consists of various pieces: the program code, static variables, heap, stack, etc.
2. Processor state
▪ The CPU registers associated with the running process
▪ Includes general purpose registers, program counter, stack pointer, etc.
3. OS resources
▪ Various OS states associated with the process
▪ Examples: open files, network sockets, etc.
Process Address Space
Carnegie Mellon
program Stack
Stack
▪ the data of the running pointer
▪ New
▪ Ready
▪ The ready process exists in the main memory. It waits for the CPU to be
assigned to it. There may be many processes present in the ready state
Carnegie Mellon
▪ Ready
▪ Any process in a ready state can be taken by the OS and assigned to the CPU
for execution through CPU scheduling algorithms
o If there is only one CPU in a system, then only one process will be in execution at a time
o If there are N number of CPUs in the system, then N number of processes can run simultaneously
Carnegie Mellon
▪ Waiting
▪ From the Running state, a process can go to the waiting state, depending upon
Scheduling algorithm (i.e., time quantum)
▪ When a process requests an I/O, the OS blocks the running process and assigns
the CPU to the next processes
Carnegie Mellon
▪ Terminated
Process Scheduling
The objective of multiprogramming is to
have some process running at all times to
maximize CPU utilization
The objective of time sharing is to switch a
CPU core among processes so frequently that
users can interact with each program while it is
running.
To meet these objectives, the process
scheduler selects an available process (possibly
Multicore systems can run multiple
from a set of several available processes) for
processes at one time
program execution on a core
Each CPU core can run one process at a
time
Carnegie Mellon
Process Scheduling
Processes can be described as either I/O bound or CPU bound
o A CPU-bound process, in contrast, generates I/O requests infrequently, using
more of its time doing computations. These tasks involve complex
calculations, data processing, and algorithmic operations that heavily utilize the
CPU
o An I/O-bound process spends more time doing I/O than doing computations.
These operations involve interactions with external resources such as databases,
file systems, network requests, and more. I/O-bound tasks typically involve
reading or writing data from or to external sources
Carnegie Mellon
Context Switching
Processes are managed by a shared chunk of OS code called the kernel
▪ Important: the kernel is not a separate process, but rather runs as part of some user
process
Control flow passes from one process to another via a context switch
Carnegie Mellon
Context Switching
Switching the CPU to another process by
saving the current state of a running
process in PCB.
Context Switching
Diagram showing context switch from process to process
Concurrent Processes
Each process is a logical control flow.
Two processes run concurrently (are concurrent) if their flows overlap in time
Otherwise, they are sequential
Examples (running on single core):
o Concurrent: A & B, A & C
o Sequential: B & C
User View of Concurrent Processes
Carnegie Mellon
o process creation
o process termination
Process Creation
Memory concerns
OS aggressively tries to share memory between processes
o Especially processes that are fork()'d copies of each other
Copies of a parent process do not actually get a private copy of the
address space...
o ... though that is the illusion that each process gets.
o Instead, they share the same physical memory, until one of them makes a change (COW:
copy-on-write)
Terminating Processes
A process terminates when it finishes executing its final statement
and asks the operating system to delete it by using the exit() system
call
At that point, the process may return a status value (typically an
integer) to its waiting parent process (via the wait() system call)
All the resources of the process including physical and virtual
memory, open files, and I/O buffers are deallocated and reclaimed by
the operating system
Carnegie Mellon
Terminating Processes
Process becomes terminated for one of three reasons:
o Receiving a signal whose default action is to terminate (more later)
o Returning from the main routine
o Calling the exit function
Terminating Processes
▪ Parent may terminate the execution of children processes using the
abort() system call. Some reasons for doing so:
o Child has exceeded allocated resources
o Task assigned to child is no longer required
o The parent is exiting and the operating systems does not allow a child to
continue if its parent terminates
o returns a nonzero value if the child process terminated normally with exit or _exit.
int WEXITSTATUS (int status)
o If WIFEXITED is true of status, this macro returns the low-order 8 bits of the exit status value from the child process.
int WIFSIGNALED (int status)
o returns a nonzero value if the child process terminated because it received a signal that was not handled
int WTERMSIG (int status)
o If WIFSIGNALED is true of status, this macro returns the signal number of the signal that terminated the child process.
int WCOREDUMP (int status)
o Returns a nonzero value if the child process terminated and produced a core dump.
int WIFSTOPPED (int status)
o If WIFSTOPPED is true of status, this macro returns the signal number of the signal that caused the child process to stop.
Carnegie Mellon
init [1]
Daemon …
e.g. httpd Login shell Login shell
Grandchild Grandchild
Note: you can view the hierarchy using the Linux pstree command
Carnegie Mellon
Other Processes
▪ Orphan Process
o Orphan implies parentless process
o When we run a program or application, the parent process for the application is shell. When we
create a process using fork(), the parent of all the processes is init process (Process ID → 1)
o What happens if the parent process exits before the child process?
o The result is, the child process now becomes the orphan process
▪ Zombie Process
o Zombie process is also known as "dead" process
o Ideally, when a process completes its execution, its entry from the process table should
be removed but this does not happen in the case of zombie a process
o Of course, the zombie process is cleaned up after the parent process becomes ready