OS Chapter 2
OS Chapter 2
• 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.
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
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…
• 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
• Efficient communication.
Disadvantages
• Multithreaded application cannot take advantage of
multiprocessing
Kernel Level Thread
• 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.
TED
M
INTERRUPT IT
IT
EX
T
ED
RUNNIN
READY G
I/O
T
EN
CO O
M RE SCHEDULAR DISPATCH
T EV
PL V
W OR
ET EN
IO T
I/O
N
AI
Process Scheduling[1] WAITING
Why process scheduling?
Bill desk
CPU
Although another desk is available people are waiting for their turn
1. The scheduler must apply appropriate rules for swapping processes IN and
OUT of CPU
Non Pre-emptive Scheduling: When the currently executing process gives up the
CPU voluntarily.
Billing Desk
Non Pre-emptive Scheduling
JOB’S IN READY
QUEUE JOB IN
EXECUTION
CPU
All jobs in ready queue has to wait till Job in execution completes its task.
Pre-emptive Scheduling
JOB’S IN READY
QUEUE JOB IN
EXECUTION
priority CPU
Billing Desk
Pre-emptive Scheduling
JOB’S IN READY
QUEUE
JOB IN
EXECUTION
priority CPU
4. A new process is initially put in the Ready queue. It waits in the ready queue
until it is selected for execution(or dispatched).
Scheduling Queues
1.The process could issue an I/O request, and then be placed in the I/O queue.
2.The process could create a new sub process and wait for its termination.
3.The process could be removed forcibly from the CPU, as a result of an interrupt, and
be put back in the ready queue.
In the first two cases, the process eventually switches from the waiting state to the
ready state, and is then put back in the ready queue.
A process continues this cycle until it terminates, at which time it is removed from all
queues and has its PCB and resources deallocated.
Types of Schedulers
Schedulers
• Long Term Schedulers decide which program must get into the job
queue.
• From the job queue, the Job Processor, selects processes and loads
them into the memory for execution.
• During extra load, this scheduler picks out big processes from the
ready queue for some time, to allow smaller processes to execute,
thereby reducing the number of processes in the ready queue.
2. Throughput
• It is the total number of processes completed per unit time or rather
say total amount of work done in a unit of time.
• This may range from 10/second to 1/hour depending on the specific
processes.
Scheduling Criteria
3. Turnaround time
It is the amount of time taken to execute a particular process, i.e. The
interval from time of submission of the process to the time of
completion of the process(Wall clock time).
4. Waiting time
The sum of the periods spent waiting in the ready queue amount of time
a process has been waiting in the ready queue to acquire get control on
the CPU.
Scheduling Criteria
5. Load average
•It is the average number of processes residing in the ready queue waiting
for their turn to get into the CPU.
6. Response time
•Amount of time it takes from when a request was submitted until the first
response is produced.
Operation on Process
1. Process Creation:
The process which creates other process, is termed the parent of the
other process, while the created sub-process is termed its child.
Each process is given an integer identifier, termed as process identifier,
or PID.
2. Process Termination:
By making the exit(system call), processes may request their own
termination.
CPU Scheduling
•CPU scheduling is a process which allows one process to use the CPU while the
execution of another process is on hold(in waiting state) due to unavailability of any
resource like I/O etc, thereby making full use of CPU.
•The aim of CPU scheduling is to make the system efficient, fast and fair.
•Whenever the CPU becomes idle, the operating system must select one of the
processes in the ready queue to be executed.
•The selection process is carried out by the short-term scheduler (or CPU scheduler). The
scheduler selects from among the processes in memory that are ready to execute, and
allocates the CPU to one of them.
Scheduling Algorithms
• To decide which process to execute first and which process to
execute last to achieve maximum CPU utilization, computer
scientists have defined some algorithms, they are:
• Shortest-Job-First(SJF) Scheduling
• Priority Scheduling
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
1
2
3
4
5
6
7
8
9
Ready queue
FCFS: Example
• Consider the processes P1, P2, P3, P4 given in the below
table, arrives for execution in the same order, with
different Arrival Time, and given Burst Time.
10
11
12
0
1
2
3
4
5
6
7
8
9
Ready queue
Problems in FCFS
• It is Non Pre-emptive algorithm, which means the process
priority doesn't matter.
• Not optimal Average Waiting Time.
• Resources utilization in parallel is not possible, which leads
to Convoy Effect, and hence poor resource(CPU, I/O etc)
utilization.
• Solution:
• Aging Gradually increase the priority of processes
that wait in the system for a long time.
Round Robin Scheduling
• Each process gets a small unit of CPU time (time quantum q),
usually 10-100 milliseconds. After this time has elapsed, the
process is preempted and added to the end of the ready queue.
• Context switching is used to save states of preempted processes.
• If there are n processes in the ready queue and the time
quantum is q, then each process gets 1/n of the CPU time in
chunks of at most q time units at once. No process waits more
than (n-1)q time units.
• Timer interrupts every quantum to schedule next process
Time Quantum (q) = 5
• Performance
• q large FIFO
• q small q must be large with respect to context
switch, otherwise overhead is too high
• q should be large compared to context switch time
• q usually 10ms to 100ms, context switch < 10 μsec
Response Time = Start Time of Process – Arrival Time
Multilevel Queue Scheduling
• A multi-level queue scheduling algorithm partitions the ready
queue into several separate queues.
• foreground (interactive)
• background (batch)
• The processes are permanently assigned to one queue, generally
based on some property of the process, such as memory size,
process priority, or process type.
• Each queue has its own scheduling algorithm.
• foreground – RR
• background – FCFS
• In addition, there must be scheduling among the queues, which is
commonly implemented as fixed-priority pre-emptive scheduling.
• Each queue has absolute priority over lower-priority
queues. No process in the batch queue, for example, could
run unless the queues for system processes, interactive
processes, and interactive editing processes were all empty.
• Scheduling
• A new job enters queue Q0 which is served FCFS
When it gains CPU, job receives 8 milliseconds
If it does not finish in 8 milliseconds, job is moved to queue Q1
• At Q1 job is again served FCFS and receives 16 additional
milliseconds
If it still does not complete, it is preempted and moved to queue Q2
References
[1] Silberschatz, A., Galvin, P. B., & Gagne, G. (2005). Operating system concepts.
Hoboken, NJ: J. Wiley & Sons.
[2] Stallings, W. (2018). Operating systems: Internals and design principles.
Prentice-Hall
[3] Tanenbaum, A. (2014). Modern operating systems. Harlow: Pearson.
[4] Nutt, G. J. (2004). Operating systems: A modern perspective. Boston: Pearson/
Addison Wesley.
[5] Bower T. Operating System Structure. K–State Polytechnic.
http://faculty.salina.k-state.edu/tim/ossg/Introduction/struct.html
[6] Bower T. Basic Operating System Concepts. K–State Polytechnic.
http://faculty.salina.k-state.edu/tim/ossg/Introduction/OSrole.html
[7] Operating System Generations. Tutorialspoint.
https://www.tutorialspoint.com/operating-system-generations
www.paruluniversity.ac.in