OS Notes Module 2
OS Notes Module 2
A process needs certain resources, including CPU time, memory, files, and I/O
devices, to accomplish its task.
3.1.1The Process
A program is a passive entity i.e. a file containing a list of instructions stored on disk
called as executable file.
A process is an active entity with a program counter specifying the next instruction to
execute and a set of associated resources.
Entering the name of the executable file on the command line(ex prog.exe or
a.out)
A process includes:
Process in memory
Two processes may be associated with same program but they are not considered as
separate processes e.g. many copies of one mail program.
The state of a process is defined in part by the current activity of that process.
Process state
Program counter
CPU registers
Memory-management information
Accounting information
1. Process state
2. Program counter
The counter indicates the address of next instruction to be executed for this
process.
3. CPU registers
Along with the program counter, this state information must be saved when
an interrupt occurs, to allow the process to be continued correctly afterward.
This includes a process priority, pointers to scheduling queues, and any other
scheduling parameters.
5. Memory-management information
6. Accounting information
Amount of CPU and real time used, time limits, account numbers , job or
process numbers.
3.2.1Scheduling Queues:
Job queue
Ready queues
Device queues
Ready queue
Set of all processes residing in main memory, ready and waiting to execute.
Header of the queue contains the pointers to first and last PCB.
A new process initially put in ready queue, where it waits for CPU for execution.
The process issues an I/O request, and then be placed in an I/O device queue.
The process creates a new subprocess and waits for its termination.
A process migrates between the various scheduling queues throughout its life time.
The operating system must select the processes from or to the scheduling queues.
Selects process from ready queue which should be executed next and allocates
CPU.
Medium-term scheduler
Swap in the process from secondary storage into the ready queue.
I/O-bound process – spends more time doing I/O than computations, many
short CPU bursts may be needed.
CPU-bound process – spends more time doing computations; long CPU bursts
are required.
System should have mix of both type of processes so I/O waiting queue and ready
queue will have equal and balanced work load.
Context Switch
Many users processes and system processes run simultaneously.
The system must save the state of the old process and load the saved state of the new
process.
Context-switch time is overhead and the system does no useful work while switching.
Switching speed varies from machine to machine depending upon memory, number of
registers and hardware support.
User data
Program counter
System stack
PCB
Contents:
Overview
Multithreading Models
Overview
Multithreaded Processes
If one application should be implemented as a set of related units of execution. Two
possible ways:
Multiple processes
Heavy weight
Less efficient
Multiple threads
A thread
shares a code section, data section, open files etc. with other threads belonging
to the same process.
Multithreaded Processes
A traditional (or heavy weight) process has a single thread of control.
If a process can have multiple threads, it can do more than one task.
Multiple threads
A multithreaded process contains several different flows of control within the same
address space.
A single PCB & user space are associated with the process and all the threads belong
to this process.
Each thread has its own thread control block (TCB) having following information:
Why multithreading
Use of traditional processes incurs high overhead due to process switching.
Unavoidable overhead of saving the state of running process and loading the
state of new process.
Use of threads splits the process state into two parts – resource state remain with the
process while execution state is associated with a thread.
This state needs to be saved and restored while switching between threads of a
process. The resource state remains with the process.
Resource state is saved only when the kernel needs to perform switching
between threads of different processes.
Examples
Web browser
Word processor
Displaying graphics
Benefits of Multithreading
Responsiveness
Resource Sharing
Economy
Utilization of MP Architectures
User threads are supported above the kernel and are managed without kernel support.
Examples:
POSIX Pthreads
Mach C-threads
Solaris threads
If a thread performs a blocking system call, the kernel can schedule another kernel
thread.
Windows XP
Solaris
Mac OS X
Tru64 UNIX
Linux
Multithreading Models
There must exist a relationship between user threads and kernel threads
Many-to-One
One-to-One
Many-to-Many
Many-to-One Model
Many user-level threads are mapped to single kernel thread.
It is fast, efficient.
Drawback: If one user thread makes a system call and only one kernel thread is
available to handle, entire process will be blocked.
Dept of ISE, DSCE Page 12
Course Name: Operating System Code: IPCC22IS34
One-to-One Model
Each user-level thread is mapped to one kernel thread
If blocking call from one user thread to one kernel thread, another kernel thread can
be mapped to other user thread.
Drawback: Creating a user thread requires the creation of corresponding kernel thread
which is overhead.
Windows 95/98/NT/2000/XP
Linux
Many-to-Many Model
Many user-level threads are mapped to a smaller or equal number of kernel threads.
Examples
IRIX
HP-UX
Tru64 UNIX
Scheduling Algorithms
Program
1
Program
P1 P2 P1 P2 2
P1 P2
Basic Concepts
How to obtain maximum CPU utilization with multiprogramming?
CPU scheduling is the task of selecting a waiting process from the ready queue and
allocating the CPU to it.
CPU Scheduler
CPU Scheduler (short term scheduler) selects one process from the processes in
memory that are ready to execute, and allocates the CPU.
Ready queue may be implemented as FIFO queue, a tree or an ordered link where
PCBs are linked waiting for CPU to be available.
Terminates.
Nonpreemptive
No forceful switching from running state to waiting process.
Once CPU has been allocated to a process, the process keeps the CPU, process
switches from running state to waiting state only due to:
I/O request
Some OS service
Preemptive
Forceful switching from running state to waiting process.
Dispatcher
Dispatcher module gives control of the CPU to the process selected by the short-term
scheduler; this involves:
Switching context
Jumping to the proper location in the user program to restart that program
Dispatch latency
It is the time taken by the dispatcher to stop one process and start another running.
Scheduling Criteria
Different CPU scheduling algorithms have different properties and choice of one
algorithm depends on different criteria.
CPU utilization – Aim is to keep the CPU as busy as possible (lightly loaded: 40%,
heavily loaded: 90%)
(High)
Throughput – number of processes that complete their execution per unit time (very
long process: 1 process/hour, short: 10 processes/sec)
(High)
Turnaround time – amount of time to execute a particular process (from submission to
completion, includes execution time, I/O time and waiting time)
(Least)
Waiting time – amount of time a process has been waiting in the ready queue (CPU
scheduling algorithm does not affect execution time and time required for I/O, it can
only reduce waiting time)
(Least)
Response time – amount of time it takes from when a request was submitted until the
first response is produced, not output (for time-sharing environment)
(Least)
Optimization Criteria for scheduling
Max CPU utilization
Max throughput
Scheduling Algorithms
First-Come, First-Served (FCFS)
Shortest-Job-First (SJF)
Priority Scheduling
When CPU is free, it is allotted to the process at the head of the queue.
It is non-preemptive.
P1 , P2 , P3
Gantt Chart for the schedule
P P P
0 2 2 3
Waiting time for P1 = 0
P2 = 24
P3 = 27
Average waiting time: (0 + 24 + 27)/3 = 51/3 = 17
P1 24
P2 3
P3 3
P2 , P3 , P1 .
P2 P3 P1
0 3 6 30
Waiting time for P1= 6; P2 = 0; P3 = 3
Convoy effect
Thus average waiting time in FCFS vary substantially if CPU burst time vary greatly.
All the processes wait for the one big process to get off the CPU.
CPU is assigned to the process with the shortest next CPU burst time.
If two processes have the same length CPU burst, FCFS is used to break the tie.
SJF is optimal and gives minimum average waiting time for a given set of processes.
Example of SJF
Process Burst Time
P1 6
P2 8
P3 7
P4 3
Gantt Chart for the schedule
P4 P1 P2
P3
0 3 9 1 2
Once CPU is given to the process it cannot be preempted until completes its
CPU burst.
Preemptive SJF
If a new process arrives with CPU burst length less than remaining time of
current executing process, current process is preempted. This scheme is know
as
Shortest-Remaining-Time-First (SRTF).
P1 P3 P2 P4
0 7 8 12 16
Average waiting time = (0 + (7-4) + (8-2) + (12-5))/4 = 16/4 = 4
P1 P2 P3 P2 P4 P1
0 11 1
2 4 5 7
Average waiting time = (9 + 1 + 0 +2)/4 = 12/4 = 3ms
Priority Scheduling
A priority number (integer) is associated with each process.
The CPU is allocated to the process with the highest priority (smallest integer
highest priority).
Preemptive
Nonpreemptive
P1 10 3
P2 1 1
P3 2 4
P4 1 5
P5 5 2
Scheduler will preempt the CPU if the priority of newly arrived process is higher than
priority of running process.
P1 6 3 0
P2 1 1 2
P3 2 4 3
P4 1 5 5
P5 3 2 6
Priority Scheduling
Problem is Starvation i.e. low priority processes may wait for indefinite time or never
execute.
(In a heavily loaded system, a steady stream of higher priority process can prevent a
low priority process from ever getting the CPU)
Solution of starvation is Aging i.e. technique of gradually increasing the priority of
processes that wait in the system for a long time.
Each process gets a small unit of CPU time (time quantum), usually 10-100
milliseconds.
After this time elapses, the process is preempted and added to the end of the ready
queue.
CPU scheduler goes around the ready queue allocating the CPU to each process for a
time interval of up to 1 time quantum.
CPU scheduler picks the first process from the ready queue, sets a timer to interrupt
after 1 time quantum and dispatches the process.
After 1 time quantum, process either will finish the execution or preempted and
scheduler will schedule other process from head of the queue.
P1 14
P2 10
P3 8