OS Module 2
OS Module 2
Spawning
Process Termination
•A process terminating from running state includes many causes.
Generally a process terminates when execution is completed.
Some other causes are as follows.
•Time slot expired : When the process execution does not
complete within the "time slice", then the process preempts
from running state. The CPU picks the next job in the ready
queue to execute.
•Memory bound violation : If a process needs more memory
than the available memory, then the process terminates from
running state.
•I/O failure : A process needs an I/O operation at the time of
execution. But if an I/O device is not available at that time, then
process moves to waiting state. If OS does not provides I/O
device in waiting state, then process terminates.
Process Termination
•Parent termination : When the parent process
terminates, the child process also terminates
automatically. The parent process has the authority to
terminate any of its child processes.
•Parent request : If a parent process requests a child
process to terminate, then the child process terminates
automatically.
•Invalid instruction : If a process has illegal instructions and
the CPU fails to execute those instructions, then the
process terminates.
Threads
•A process is divided into number of light weight processes
where, each light weight process is said to be a thread. The
thread has a program counter that keeps track of which
instruction to execute next.
•Because threads have some properties same as process, they
operate in many respects in the same manner as process.
Thread can be in one of the several states viz. ready, blocked,
running, terminated.
•Like process, thread shares the CPU and only one thread at a
time is active or running.
•Threads can create child process and unlike process, threads
are not independent of one another.
Threads
•A thread is said to be in one of the following thread states,
Threads
•Born state : A thread that has just been created.
•Ready state : A thread waiting for CPU.
•Running : CPU is being assigned to the thread and it is
executing.
•Blocked state : A thread is waiting for an I/O device.
•Sleep : A sleeping thread may become ready after the
designated sleep time expires.
•Dead : The execution of the thread is finished.
Process vs. Thread
Process vs. Thread
Multithreading
•A process is divided into number of smaller tasks, each task is
called a 'thread'. Number of threads within a process, executing
simultaneously is called multithreading. Supporting multithreading is
the additional capability of operating system.
•Based on functionality threads are divided into 4 categories as
shown in Fig. 2.8.
Multithreading
Scheduling
•Scheduling refers to the set of policies and mechanisms that an
OS supports for determining the order of execution of
processes. A scheduler is a program that determines the next
pending job to be admitted to the system for execution.
•Goals of Scheduling
To provide fair deal as possible to all pending jobs and
processes.
To optimize the utilization of system resources.
To ensure that more critical processes get priority over others.
Types of Schedulers
•Long term scheduler
•Long term scheduling refers to the OS decision of admitting
the pending process to the system for processing and loading
processes in to main memory and taking them up for
execution.
•Each ready queue contains programmer specified or system
assigned parameters of resource needs, in terms of expected
execution time, memory and I/O requirements. These
specifications enables OS to decide their order of execution.
•The primary goal of long term scheduling is to achieve a
proper mix of I/O bound and CPU bound jobs, so as to keep
the CPU and I/O devices as occupied as possible.
•Long term scheduler controls the degree of
multiprogramming.
Types of Schedulers
•Medium term scheduler
•When a process is suspended after making an I/O request,
it cannot make any progress till the requested I/O is
completed. Such suspended process is removed from main
memory and shifted to secondary memory (HDD)
(swap-out) to make space for another process. This
swapping function is done by medium term scheduler.
•It determines when a swapped out process can be moved
back into the memory (swap-in) and resumed from the
same point, where it was swapped-out.
•Medium term scheduler also reduces the degree of
multiprogramming.
Types of Schedulers
•Short term scheduler
•The function of the short term scheduler is to select a
job\process from the ready queue and give the control of
the CPU to that process with the help of dispatcher.
•A dispatcher is a module, that connects the CPU to the
process selected by the short term scheduler. The main
function of the dispatcher is switching, it means switching
the CPU from one process to another process.
•Short term scheduler selects a process from the ready
queue using CPU scheduling algorithms, such as FCFS, RR,
SJF, Priority.
•Short term scheduler reduces the degree of
multiprogramming.
Types of Scheduling
•Preemptive Scheduling
•Preemptive scheduling allows a process to be interrupted
in the middle of its execution, freeing the CPU and
allocating it to other requesting process. If highest priority
process arrives in the system, CPU's time from currently
executing low priority process is allocated to it.
•Example: Round Robin method, Priority Scheduling
•Non-Preemptive Scheduling
•Non-preemptive scheduling answers that a process
releases the control of CPU only when it finishes with its
current CPU burst/execution time.
•Example - FCFS Method.
Compare
Compare
Long Term Scheduler Medium Term Scheduler Short Term Scheduler
Scheduling Criteria
•The main aim of scheduling is to improve performance by
keeping CPU busy all the time. We can determine which
algorithm is the best algorithm and which is the worst
algorithm using some performance metrics as follows :
•Throughput : Number of jobs completed by CPU within a
time period.
•Waiting time : It is the sum of the periods spent waiting
by a process in the ready queue. CPU scheduling
algorithm having the least average waiting time is said to
be the best algorithm.
•Turnaround time : The time interval between submission
of the process and time of it's completion is the
turnaround time.
Scheduling Criteria
•Response time : It is the time duration between the
submission and first response from the CPU.
•CPU utilization : It is the amount of time the processor
remains busy.
•Fairness : Every process should get equal share of CPU
time.
•Priority : If the OS assigns priorities to processes, scheduler
should favor the higher priority processes first.
Context Switching
•Context of a process is saved in its PCB Switching of CPU's
time from one process to another requires saving of the
state of the old process in its PCB and loading of the saved
state of the new process. This task is performed by OS and
is known as "Context Switching".
The context switching involves the following steps,
Saving the context (state) of the running process in its PCB.
If the running process is being terminated, context
switching will not be required. In that case the process will
be deleted, causing the deletion of its PCB and freeing the
memory occupied by the running process.
Context Switching
Selection of a process from the 'Ready queue' to take
control of the CPU. The state of the newly selected process
will be loaded into the system. This is called as context
loading. The new process will be enabled to run by loading
the address of its next instruction in the program counter
(PC).
During context switch, system does not perform any useful
work. So context switch is a pure overhead on the system.