Process
A process is a program in execution and it is more than a program code called as
text section and this concept works under all the operating system because all the
task perform by the operating system needs a process to perform the task
The process executes when it changes the state. The state of a process is defined by
the current activity of the process.
Process can be described:
1. I/O Bound Process- spends more time doing I/O then computation.
2. CPU Bound Process- spends more time doing computation.
Each process may be in any one of the following states:
● New − The process is being created.
● Running − In this state the instructions are being executed.
● Waiting − The process is in waiting state until an event occurs like I/O
operation completion or receiving a signal.
● Ready − The process is waiting to be assigned to a processor.
● Terminated − the process has finished execution.
It is important to know that only one process can be running on any processor at
any instant. Many processes may be ready and waiting.
State diagram of these process states: -
Explanation
Step 1 − Whenever a new process is created, it is admitted into ready state.
Step 2 − If no other process is present at running state, it is dispatched to running
based on scheduler dispatcher.
Step 3 − If any higher priority process is ready, the uncompleted process will be
sent to the waiting state from the running state.
Step 4 − Whenever I/O or event is completed the process will send back to ready
state based on the interrupt signal given by the running state.
Step 5 − Whenever the execution of a process is completed in running state, it will
exit to terminate state, which is the completion of process.
Process/Task Control Block (PCB):
In the OS, each process is represented by its PCB (Process Control Block). The
PCB, generally contains the following information:
● Process State: A process, from its creation to completion goes through
different states. Generally, a process may be present in one of the 5
states during its execution:
● Process ID: When a new process is created by the user, the operating system
assigns a unique ID i.e. a process-ID to that process. This ID helps the
process to be distinguished from other processes existing in the system.
● Program Counter (PC) value: The counter indicates the address of the next
instruction to be executed or this process.
● Process Priority: Process priority is a numeric value that represents the
priority of each process. The lesser the value, the greater the priority of that
process. This priority is assigned at the time of the creation of the PCB and
may depend on many factors like the age of that process, the resources
consumed, and so on. The user can also externally assign a priority to the
process.
● Register values: The registers vary in number and type, depending on the
computer architecture. They include accumulators, index registers, stack
pointers, and general-purpose registers, plus any condition-code information.
Along with the program counter, this state information must be saved when
an interrupt occurs, to allow the process to be continued correctly afterward.
● Context Switching: Context switching is a process that involves switching
the CPU from one process or task to another. It is the process of storing the
state of a process so that it can be restored and resume execution at
a later point. This allows multiple processes to share a single CPU and is an
essential feature of a multitasking operating system.
● Memory Management Information (page tables, base/bound registers etc.):
● Processor Scheduling Information (priority, last processor burst time etc.)
● I/O Status Info (outstanding I/O requests, I/O devices held, etc.)
● List of Open Files: As the name suggests, it contains information on all the
files that are used by that process. This field is important as it helps the
operating system to close all the opened files at the termination state of the
process.
● Accounting Info: This information includes the amount of CPU and real
time used, time limits, account numbers, job or process numbers, and so on.
Operations on process:
A.Process Creation: - Parent process create children processes, which, in turn
create other processes, forming a tree of processes.
1. Resource sharing
● Parent and children share all resources
● Children share subset of parent’s resources
● Parent and child share no resources
2. Execution
● Parent and children execute concurrently
● Parent waits until children terminate
B. Process Termination
1. Process executes last statement and asks the operating system to delete it
(exit)
● Output data from child to parent (via wait)
● Process’ resources are deallocated by operating system
2. Parent may terminate execution of children processes (abort)
● Child has exceeded allocated resources
● Task assigned to child is no longer required
● If parent is exiting
Some operating system do not allow child to continue if its
parent terminates.
Process Address Space:
● The process address space consists of the linear address range presented to
each process. Each process is given a flat 32- or 64-bit address space, with
the size depending on the architecture. The term "flat" describes the fact that
the address space exists in a single range. (As an example, a 32-bit address
space extends from the address 0 to 429496729.)
● Some operating systems provide a segmented address space, with addresses
existing not in a single linear range, but instead in multiple segments.
Modern virtual memory operating systems generally have a flat memory
model and not a segmented one.
● A memory address is a given value within the address space, such as
4021f000. The process can access a memory address only in a valid memory
area. Memory areas have associated permissions, such as readable, writable,
and executable, that the associated process must respect. If a process
accesses a memory address not in a valid memory area, or if it accesses a
valid area in an invalid manner, the kernel kills the process with the dreaded
"Segmentation Fault" message.
● Memory areas can contain all sorts of goodies, such as
A memory map of the executable file's code, called the text section.
A memory map of the executable file's initialized global variables,
called the data section.
A memory map of the zero page (a page consisting of all zeros, used
for purposes such as this) containing uninitialized global variables,
called the bss section
A memory map of the zero page used for the process's user-space
stack (do not confuse this with the process's kernel stack, which is
separate and maintained and used by the kernel)
An additional text, data, and bss section for each shared library, such
as the C library and dynamic linker, loaded into the process's address
space.
Any memory mapped files
Any shared memory segments
Any anonymous memory mappings, such as those associated with
malloc().
Process Identification Information
● Process Identifier (process ID or PID) is a number used by most operating
system kernels (such as that of UNIX, Mac OS X or Microsoft Windows) to
temporarily uniquely identify a process.
● This number may be used as a parameter in various function calls allowing
processes to be manipulated, such as adjusting the process's priority or
killing it altogether.
● In Unix-like operating systems, new processes are created by the fork()
system call. The PID is returned to the parent enabling it to refer to the child
in further function calls. The parent may, for example, wait for the child to
terminate with the waitpid() function, or terminate the process with kill().
Threads and their Management
Thread: -
A thread is a basic unit of CPU utilization, consisting of a program counter, a stack,
and a set of registers.
● Traditional (heavyweight) processes have a single thread of control - There
is one program counter, and one sequence of instructions that can be carried
out at any given time.
● multi-threaded applications have multiple threads within a single process,
each having their own program counter, stack and set of registers, but
sharing common code, data, and certain structures such as open files.
Types of Threads
Threads are mainly classified based on how they are managed and scheduled in an
operating system. There are two primary types of threads.
● User Level Thread
● Kernel Level Thread
User-Level Threads (ULTs)
● Managed entirely in user space using a thread library; the kernel is unaware
of them.
● Switching between ULTs is fast since only program counter, registers, and
stack need to be saved/restored.
● Do not require system calls for creation or management, making them
lightweight.
● If one thread makes a blocking system call, the entire process (all threads) is
blocked.
● Scheduling is done by the application itself, which may not be as efficient as
kernel-level scheduling.
● Cannot fully utilize multiprocessor systems because the kernel schedules
processes, not individual user-level threads.
Kernel-Level Threads (KLTs)
● Managed directly by the operating system kernel; each thread has an entry in
the kernel’s thread table.
● The kernel schedules each thread independently, allowing true parallel
execution on multiple CPUs/cores.
● Handles blocking system calls efficiently; if one thread blocks, the kernel
can run another thread from the same process.
● Provides better load balancing across processors since the kernel controls all
threads.
● Context switching is slower compared to ULTs because it requires switching
between user mode and kernel mode.
● Implementation is more complex and requires frequent interaction with the
kernel.
● Large numbers of threads may add extra load on the kernel scheduler,
potentially affecting performance.
Benefits of Threads:
● Responsiveness. Multithreading an interactive application may allow a
program to continue running even if part of it is blocked or is performing a
lengthy operation.
● Resource sharing. Threads share the memory and the resources of the
process to which they belong. The benefit of sharing code and data is that it
allows an application to have several different threads of activity within the
same address space.
● Economy. Allocating memory and resources for process creation is costly.
Because threads share resources of the process to which they belong, it is
more economical to create and contextswitch threads.
● Utilization of multiprocessor architectures. The benefits of multithreading
can be greatly increased in a multiprocessor architecture, where threads may
be running in parallel on different processors. A single threaded process can
only run on one CPU; no matter how many are available. Multithreading on
a multi-CPU machine increases concurrency.
Multithreading Models (Management of Threads):
Threads may be provided either at the user level, for user threads, or by the kernel,
for kernel threads. User threads are supported above the kernel and are managed
without kernel support, whereas kernel threads are supported and managed directly
by the operating system. There must exist a relationship between user threads and
kernel threads. There are three common ways of establishing this relationship.
1. Many-to-Many Model:
● Multiple user threads map to multiple kernel threads.
● If one thread blocks, others can continue.
● Provides high concurrency and is the most efficient model.
2. Many-to-One Model:
● Multiple user threads map to a single kernel thread.
● Blocking in one thread blocks the entire process.
● Efficient user-level management but poor multiprocessing utilization.
3. One-to-One Model:
● Each user thread maps to a unique kernel thread.
● Multiple threads can run on multiple processors.
● Blocking in one thread does not affect others.
● Overhead is higher because each user thread requires a corresponding kernel
thread.