Operating System
CHAPTER-2
Processes, Thread & Process Scheduling
Syllabus
Process
• A process is a program in execution.
• Process is not as same as program code but a lot more than it.
• A process is an 'active' entity as opposed to program which is
considered to be a 'passive' entity. Attributes held by process
include hardware state, memory, CPU etc.
Main OS Process Related Goals
• Interleave the execution of existing processes to maximize
processor utilization
• Provide reasonable response time
• Allocate resources to processes
• Support inter-process communication (and synchronization)
and user creation of processes
How are these goals achieved ?
• Schedule and dispatch processes for execution by the processor
• Implement a safe and fair policy for resource allocation
to processes
• Respond to requests by user programs
• Construct and maintain tables for each process managed by
the operating system
Process Creation
?When is a new process created
• System initialization (Daemons)
• Execution of a process creation system call by a running process
• A user request to create a process
• Initiation of a batch job
Process Termination
?When does a process terminate
• Normal exit (voluntary)
• Error exit (voluntary)
• Fatal error (involuntary)
• Killed by another process (involuntary)
Contd…
• Programs are executable(.exe) files generally
stored in Secondary Memory.
• In order to execute program it must be loaded in
main memory.
• When a program is loaded into the memory and
it becomes process.
• Then process memory can be divided into four
sec ons ─ stack, heap, text and data.
Process Stack :Google
Contd..
• The Text section is madeup of the compiled program code , read in from
non-volatile storage when the program is launched.
• The Data section is made up the global and static variables, allocated and
initialized prior to executing the main.
• The Heap Section is used for the dynamic memory allocation, and is
managed via calls to new, delete, malloc, free, etc.
• The Stack is used for local variables. Space on the stack is reserved for
local variables when they are declared.
Process Vs. Program
Process State
• As a process executes, it changes state.
• The state of a process is defined in part by the current activity of that
process. The Process state is an indicator of the nature of the current
activity in a Process.
• Processes in the operating system can be in any of the following states:
¬NEW- The process is being created.
¬READY- The process is waiting to be assigned to a processor.
¬RUNNING- Instructions are being executed.
¬WAITING- The process is waiting for some event to occur(such as
an I/O completion or reception of a signal).
¬TERMINATED- The process has finished execution.
Process State & Description
Start
This is the initial state when a process is first started/created.
Ready
The process is waiting to be assigned to a processor. Ready
processes are waiting to have the processor allocated to them by
the operating system so that they can run.
Process may come into this state after Start state or while running
it by but interrupted by the scheduler to assign CPU to some other
process.
Contd…
Running
Once the process has been assigned to a processor by the OS
scheduler, the process state is set to running and the processor
executes its instructions.
Waiting
Process moves into the waiting state if it needs to wait for a
resource, such as waiting for user input, or waiting for a file to
become available.
For example the process may be waiting for keyboard input, disk
access request, inter-process messages, a timer to go off, or a child
process to finish.
Contd..
Terminated or Exit
Once the process finishes its execution, or it is terminated by the
operating system, it is moved to the terminated state where it waits
to be removed from main memory.
Process State Transitions
Process State[ 1 ]
Process Transitions
Ready --> Running
When it is time, the dispatcher selects a new process to run
Running --> Ready
the running process has expired his time slot the running process gets
interrupted because a higher priority process is in the ready state
Contd…
Running --> Blocked
When a process requests something for which it must wait
a service that the OS is not ready to perform an access to a resource not
yet available initiates I/O and must wait for the result waiting for a process
to provide input (IPC).
Blocked --> Ready
When the event for which it was waiting occurs
Process Suspension
• Many OS are built around (Ready, Running, Blocked) states. But
there is one more state that may aid in the operation of an OS -
suspended state.
• When none of the processes occupying the main memory is in a
Ready state, OS swaps one of the blocked processes out onto to
the Suspend queue.
• When a Suspended process is ready to run it moves into “Ready,
Suspend” queue. Thus we have two more state:
Blocked_Suspend, Ready_Suspend.
Contd…
• Blocked_suspend: The process is in the secondary memory and
awaiting an event.
• Ready_suspend: The process is in the secondary memory but is
available for execution as soon as it is loaded into the main
memory.
• Observe on what condition does a state transition take place? What
are the possible state transitions?
Process Suspension : Google
Process Control Block
• A Process Control Block is a data structure
maintained by the Operating System for
every process.
• The PCB is identified by an integer process
ID (PID).
• A PCB keeps all the information needed to
keep track of a process
Process Control Block :Google
Contd…
1 Process State : The current state of the process i.e., whether it is ready,
running, waiting, or whatever.
2 Process privileges : This is required to allow/disallow access to system
resources.
3 Process ID : Unique identification for each of the process in the operating
system.
4 Pointer : A pointer to parent process.
Table : 2.1 Process States Attributes
Contd…
5 Program Counter : Program Counter is a pointer to the address of the
next instruction to be executed for this process.
6 CPU registers : Various CPU registers where process need to be stored for
execution for running state.
7 CPU Scheduling Information : Process priority and other scheduling
information which is required to schedule the process.
8 Memory management information :information of page table, memory
limits, Segment table depending on memory used by the OS
9 Accounting information :This includes the amount of CPU used for
process execution, time limits, execution ID etc.
10 IO status information : list of I/O devices allocated to the process.
Table : 2.2 Process States Attributes
Contd…
The PCB is maintained for a process throughout its lifetime,
and is deleted once the process terminates.
Context
Switching
• Switching the CPU to another process requires saving the
state of the old process and loading the saved state for
the new process. This task is known as a Context Switch.
• A context switch is a procedure that a computer's CPU
(central processing unit) follows to change from one
task (or process) to another while ensuring that the tasks
do not conflict.
• Effective context switching is critical if a computer is to
provide user-friendly multitasking.
Example
Context Switch : Google
Example
CPU Switch from Process to Process [1]
Contd…
• A context switch is the mechanism to store and restore the
state or context of a CPU in Process Control block so
that a process execution can be resumed from the same
point at a later time.
• Using this technique, a context switcher enables multiple
processes to share a single CPU. Context switching is an
essential part of a multitasking operating system features.
Contd…
Context switch time is pure overhead, because the system does
no useful work while switching.
Context Switching has become such a performance bottleneck
that programmers are using new structures(threads) to
avoid it whenever and wherever possible.
Thread
• A thread is a single sequence stream within a process.
• They are sometimes called lightweight processes.
• In a process, threads allow multiple executions of streams.
• Thread is an execution unit which consists of its own program counter, a
stack, and a set of registers.
• As each thread has its own independent resource for process execution,
multiple processes can be executed parallel by increasing number of threads.
• Each thread belongs to exactly one process and no thread can exist outside a
process.
Threads
Single and Multithreaded Processes [1]
Advantages of Thread
• Threads minimize the context switching time.
• Use of threads provides concurrency within a process.
• Efficient communication.
• It is more economical to create and context switch
threads.
• Threads allow utilization of multiprocessor architectures
to a greater scale and efficiency.
Process Vs. Thread
Types of Thread
• User Level Threads − User managed threads.
• Kernel Level Threads − Opera ng System managed
threads acting on kernel, an operating system core.
between threads, for
saving and restoring thread
User Level Thread
• In this case, the thread management kernel is not aware of the
existence of threads.
• The thread library contains code for creating
and destroying
• The application starts with a single thread.
Contd…
User Level Thread :Google
Advantages and Disadvantages
Advantages
• Thread switching does not require Kernel mode privileges.
• User level thread can run on any operating system.
• Scheduling can be application specific in the user level
• thread. User level threads are fast to create and manage.
Disadvantages
• Multithreaded application cannot take advantage
of multiprocessing
Kernel Level Thread
• In this case, thread management is done by the Kernel.
• There is no thread management code in the application area.
• Kernel threads are supported directly by the operating system.
• The Kernel performs thread creation, scheduling and management
in Kernel space.
Advantages
• Kernel can simultaneously schedule multiple threads from the
same process on multiple processes.
• If one thread in a process is blocked, the Kernel can schedule
another thread of the same process.
Disadvantages
• Kernel threads are generally slower to create and manage
than the user threads.
• Transfer of control from one thread to another within the
same process requires a mode switch to the Kernel.
Multithreading
• Ability of operating system to execute multiple threads.
• Some operating system provide a combined user level thread
and Kernel level thread facility.
• Multithreading Models
1. Many to One Model
2. One to One Model
3. Many to Many Model
Many to one Model
• In the many to one model, many user-level threads are
all mapped onto a single kernel thread.
• Thread management is handled by the thread library in
user space, which is efficient in.nature
Many to One Model [1]
One to One Model
• The one to one model creates a separate kernel thread to handle each
and every user thread.
• Most implementations of this model place a limit on how many threads
can be created.
• Linux and Windows from 95 to XP implement the one-to-one model for
threads
One to One Model [1]
Many to Many Model
• The many to many model multiplexes any number of user threads onto an
equal or smaller number of kernel threads, combining the best features
of the one-to-one and many-to-one models.
• Users can create any number of the threads.
• Blocking the kernel system calls does not block the entire process.
• Processes can be split across multiple processors.
Many to Many Model[1]
Benefits of
Multithreading
• Responsiveness
• Resource sharing, hence allowing better utilization of resources.
• Economy: Creating and managing threads becomes easier.
• Scalability: One thread runs on one CPU. In Multithreaded
processes, threads can be distributed over a series of processors to
scale.
• Context Switching is smooth. Context switching refers to the
procedure followed by CPU to change from one task to
another.
User Level Thread Vs. Kernel Level Thread