Professional Documents
Culture Documents
Os - Module-1
Os - Module-1
Os - Module-1
ADVANCED OPERATING
SYSTEM
MOD UL E- 1
C HAPTER-1
WH AT I S A N O P E R ATING SYST E M?
• Computer-system operation
• Storage structure
• I/O structure
CO MP UT ER-SYSTEM O R GA NI ZATI ON CO NT. .
• Each device controller has a local buffer.The input and output data can be
stored in these buffers.
• The data is moved from memory to the respective device buffers by the CPU
for I/O operations and then this data is moved back from the buffers to
memory.
• The device controllers use an interrupt to inform the CPU that I/O
operation is completed.
• The device driver understands the device controller and provides the rest of
the operating system with a uniform interface to the device
CO MP UT ER-SYSTEM O R GA NI ZATI ON CO NT. .
Storage structure
• Main memory – only large storage media that the CPU can access directly
• Random access
• Typically volatile
• Secondary storage – extension of main memory that provides large
nonvolatile storage capacity
• Magnetic disks – rigid metal or glass platters covered with magnetic recording
material
• Disk surface is logically divided into tracks, which are subdivided into
sectors
• The disk controller determines the logical interaction between the device
and the computer
CO MP UT ER-SYSTEM O R GA NI ZATI ON CO NT. .
Storage structure
Storage structure
Volatile -Loses its
contents
When power is removed
I/O Structure
• Storage is only one of many types of I/O devices within a computer.
• A large portion operating system code is dedicated to I/O
management, because of its importance to the reliability and
performance of system and because of varying nature of devices.
• A general purpose computer system consists of CPUs and multiple
device controllers that are connected through a common bus.
• Each device controller is in charge of specific device.
• Device controller maintains both local buffer storage and special
purpose registers.
• OS have a device driver for each device controller
• This device driver understands the controller and presents a uniform
interface to the device to the rest of the OS.
CO MP UT ER-SYSTEM O R GA NI ZATI ON CO NT. .
M E M O RY L AYO U T F O R M U LT I P R O G R AM M I N G S YS T E M
OPE RATING SYSTE M STRUCTURE CONT..
• Timer is used to prevent user program entering infinite loop or not calling
system services and never returning control to the operating system
• Timer can be set to interrupt the computer after a specified period
• Operating system sets the counter.
• Every time the clock ticks, the counter is decremented.
• When the counter reaches zero, an interrupt occurs.
• A process includes:
• Stack-The process Stack contains the temporary data such as method/function parameters,
return address and local variables
• Heap -This is dynamically allocated memory to a process during its run time.
• Text-This includes the current activity represented by the value of Program Counter
• Data-This section contains the global and static variables.
• Program is passive entity, process is active entity
• Program becomes process when executable file loaded into memory
• Execution of program started via GUI mouse clicks, command line entry of its name,
etc.
• One program can be several processes
• Consider multiple users executing the same program
P RO CESS CO NCEP T CO NT. .
Each process is represented in operating system by the process control block (PCB), its also
known as task control block
Information associated with each process
• Process state. The state may be new, ready running, waiting, halted, and so on.
• Program counter. The counter indicates the address of the next instruction to be
executed for this process.
• CPU registers. 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
• CPU-scheduling information. This information includes a process priority, pointers to
scheduling queues, and any other scheduling parameters.
P RO CESS CO NT ROL BLO CK (P CB)
• Job queue - As processes enter the system, they are put into a job queue,
which consists of all processes in the system.
• Ready queue - The processes that are residing in main memory and are
ready and waiting to execute are kept on a list called the ready queue.
• The system also includes other queues. When a process is allocated the CPU, it executes for a while
and eventually quits, is interrupted, or waits for the occurrence of a particular event, such as the
completion of an I/0 request.
• Suppose the process makes an I/O request to a shared device, such as a disk. Since there are many
processes in the system, the disk may be busy with the I/0 request of some other process. The
process therefore may have to wait for the disk.
• Device queue - The list of processes waiting for a particular I/0 device is called a device queue.
Each device has its own device queue
• The process control block contains all the necessary information for representing a process,
including the state of the process, memory-management information, and pointers to the process's
parent and any of its children.
• Each rectangular box represents a queue.
• The circles represent the resources that serve the queues, and the arrows indicate the flow of
processes in the system.
SCHEDULING QUEUES (CONT.)
• Process scheduling can be represented using queuing diagram
SCH E DULING Q UE UES (CO NT.)
• There are two types of queues : the ready queue and a set of device queues.
• A new process is initially put in the ready queue. It waits there until it is selected for
execution, or is dispatched. Once the process is allocated the CPU and is executing, one of
several events could occur:
• The process could issue an I/0 request and then be placed in an I/0 queue.
• The process could create a new sub-process and wait for the sub-process's termination.
• 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.
SCH E DULERS
• A process migrates among the various scheduling queues throughout its lifetime.
• The operating system must select processes from these queues in some fashion. The selection of
the process is carried out by the appropriate scheduler.
• There are two types of schedulers:
• Long-term scheduler - The long-term scheduler is also known as job scheduler. Its job is to
select processes from the job pool and loading them into memory for execution.
• Short-term scheduler - The short-term scheduler is also known as CPU scheduler. It selects
the processes that are ready to execute from ready queue and allocates the CPU to one of them.
• The main difference between these two schedulers lies in frequency of execution.
• The short-term scheduler must select a new process for the CPU frequently. The short-term
scheduler must be fast.
• The long-term scheduler executes much less frequently. The long-term scheduler controls the
degree of multiprogramming (the number of processes in memory). It must maintain the balance
between process creation and process leaving the system. Thus, the long-term scheduler may need
to be invoked only when a process leaves the system.
SCH E DULERS (CO NT. )
Processes can be described as either:
• I/O-bound process – spends more time doing I/O than computations, many short CPU bursts
• CPU-bound process – spends more time doing computations; few very long CPU bursts
It is important that the long-term scheduler select a good process mix of I/O-bound and CPU-bound
processes
Some operating systems, such as time-sharing systems, may use intermediate level of scheduling, known as
medium-term scheduler
Medium-term scheduler –it is used to remove processes from memory to reduce the degree of
multiprogramming.
Swapping - The removed process can be reintroduced into memory, and its execution can be continued
where it left off.This scheme is called swapping.
The process is swapped out, and is later swapped in, by the medium-term scheduler. Swapping may be
necessary to improve the process mix
Schedulers (Cont.)
• For instance,a running process may become suspended if it makes an I/O request.
• A suspended processes cannot make any progress towards completion.
• 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.
• The context of a process is represented in the Process Control Block(PCB) of a process. It
includes the value of the CPU registers, the process state and memory-management information.
• When a context switch occurs, the Kernel saves the context of the old process in its PCB and loads
the saved context of the new process scheduled to run.
• Context switch time is pure overhead, because the system does no useful work while switching.
• Its speed varies from machine to machine, depending on the memory speed, the number of registers
that must be copied.
• Context Switching has become such a performance bottleneck that programmers are using new
structures(threads) to avoid it whenever and wherever possible
O P E RAT ION O N P RO CESS
• Process can created and deleted dynamically, they can execute concurrently
• Two types of operations can be performed on process
• Process creation
• Process termination
Process creation
Processes need to be created in the system for different operations.
Process creation can be done by the following events −
• User request for process creation
• System initialization
• Execution of a process creation system call by a running process
O P E RAT ION O N P RO CESS CO NT..
• Process creation cont..
• A process may be created by another process using fork().
• The creating process is called the parent process and the created process is the child process.
• A child process can have only one parent but a parent process may have many children.
• Each process is given an integer identifier, termed as process identifier, or PID. The parent PID
(PPID) is also stored for each process.
• Resource Sharing - In general, a process will need certain resources (CPU time, memory, files,
I/0 devices) to accomplish its task. When a process creates a sub-process, that sub-process may be
able to obtain its resources directly from the OS, or it may be constrained to a subset of the
resources of the parent process.
• The parent may have to partition its resources among its children,
• or it may be able to share some resources (such as memory or files) among several of its children.
O P E RAT ION O N P RO CESS CO NT..
• Process creation-
• Execution - When a process creates a new process, two possibilities exist in
terms of execution:
• The parent continues to execute concurrently with its children, competing equally
for the CPU.
• The parent waits until some or all of its children have terminated.
• There are also two possibilities in terms of the address space of the new
process:
• The child process is a duplicate of the parent process (it has the same program and
data as the parent, an exact clone).
• The child process has a new program loaded into it.
Operation on Process cont..
PROCESS CREATION
Operation on Process cont..
• Process Termination
• A process terminates when it finishes executing its final statement and asks the OS to delete it by
using the exit() system call
• All the resources of the process -including physical and virtual memory, open files, and I/O buffers-
are deallocated by the OS.
• Termination can occur in other circumstances as well. A process can cause the termination of
another process via an appropriate system call.
• Usually, such a system call can be invoked only by the parent of the process that is to be terminated.
• Note that a parent needs to know the identities of its children.Thus, when one process creates a
new process, the identity of the newly created process is passed to the parent.
Operation on Process cont..
• Process Termination
• A parent may terminate the execution of one of its children for a variety of reasons,
such as these:
• The child has exceeded its usage of some of the resources that it has been allocated. (To
determine whether this has occurred, the parent must have a mechanism to inspect the state of
its children.)
• The task assigned to the child is no longer required.
• The parent is exiting, and the operating system does not allow a child to continue if its parent
terminates.
• Some systems do not allow a child to exist if its parent has terminated. In such systems,
if a process terminates (either normally or abnormally), then all its children must also
be terminated.This phenomenon, referred to as cascading termination, is normally
initiated by the operating system.
INTERPROCESS
COMMUNICATION
• Selects from among the processes in ready queue, and allocates the CPU to one
of them:
• Queue may be ordered in various ways
• CPU scheduling decisions may take place when a process:
P1 P2 P3
0 24 27 30
P2 P3 P1
0 3 6 30
• Example-3 Consider the set of processes whose arrival and burst time are given
below,
• Calculate the average waiting time and turnaround time using FCFS algorithm.
Process ID Arrival Time Burst Time
P1 4 5
P2 6 4
P3 0 3
P4 6 2
P5 5 4
F IRS T COME F IRS T S E RVE (F CF S)
P3 P1 P5 P2 P4
0 3 4 9 13 17 19
F IRS T COME F IRS T S E RVE (F CF S)
• This algorithm is associated with each process the length of the process’s next CPU burst
• When the CPU is available, it is assigned to the process that has the smallest next CPU burst.
• This is also known as shortest next CPU Burst algorithm
• This is a non-preemptive, pre-emptive scheduling algorithm.
• It is sometimes called Shortest-remaining-time-first scheduling, because scheduling depends
on the length of the next CPU burst of the process rather than its total length
• Best approach to minimize waiting time.
• Impossible to implement in interactive systems where required CPU time is not known.
• The processer should know in advance how much time process will take.
SH O RTEST J O B F I R ST (NO N-PR EEMP TIVE)
Example-1
Calculate the average waiting time of the problem using SJF algorithm whose process
Id and Burst time are given below:
ProcessArriva l Time Burst Time
P1 0.0 6
P2 2.0 8
P3 4.0 7
P4 5.0 3
SHORTEST JOB FIRST (NON-PREEMPTIVE)
P4 P1 P3 P2
0 3 9 16 24
P1 P2 P4 P1 P3
0 1 5 10 17 26
P1=(10-1-0)=9ms
P2=(1-0-1)=0ms
P3=(17-0-2)=15ms
P4=(5-0-3)=2ms
P2 P5 P1 P3 P4
0 1 6 16 18 19
In the Example, there are 7 processes P1, P2, P3, P4, P5, P6 and P7. Their
priorities, Arrival Time and burst time are given in the table. Calculate turn
around time and Average waiting time.
Process ID Priority Arrival Time Burst Time
1 2 0 3
2 6 2 5
3 3 1 4
4 5 4 2
5 7 6 9
6 4 5 4
7 10 7 10
PRIORITY B ASED SCHEDULING CONT..
1 2 0 3 3 3 0
2 6 2 5 18 16 11
3 3 1 4 7 6 2
4 5 4 2 13 9 7
5 7 6 9 27 21 12
6 4 5 4 11 6 2
7 10 7 10 37 30 20
• Example-1 Consider the following set of processes that arrive at time 0, with the
length of the CPU burst given in milliseconds and time quantum is of 4 milliseconds,
calculate the average waiting time.
• Gantt Chart
P1 P2 P3 P1 P1 P1 P1 P1
0 4 7 10 14 18 22 26 30
ROU ND-ROBIN S CH E DULING CONT..
• 0 4 7 10 14 18 22 26 30
P1 P2 P3 P1 P1 P1 P1 P1
• Method 2
• Waiting time= Last start Time - Arrival Time-
Proces Burst Waiting
• (preemption X Time quantum) s Time Time
P1 24 26-0-
(5X4)=6
• Average waiting Time= (6+4+7)/3=5.66ms P2 3 4-0-(0X4)=4
P3 3 7-0-(0X4)=7
DEADLOCK S SYSTEM M ODEL
91
DEADLOCK S SYS TEM M ODEL
93
DE ADLOCK E XAMPLE
94
Deadlock characterization
• Mutual Exclusion
• There should be a resource that can only be held by one process at a time. In the
diagram below, there is a single instance of Resource 1 and it is held by Process 1
only.
95
DE ADLOCK CHARACTE RI ZATI ON
96
D E AD LOCK CHARACT E RI ZAT I ON
• No Preemption
97
D E AD LOCK CHARACT E RI ZAT I ON
Circular Wait
• A process is waiting for the resource held by the second
process, which is waiting for the resource held by the third
process and so on, till the last process is waiting for a resource
held by the first process.
• This forms a circular chain.
• For example: Process 1 is allocated Resource2 and it is
requesting Resource 1.
• Similarly, Process 2 is allocated Resource 1 and it is requesting
Resource 2.
• This forms a circular wait loop
98
D E AD LOCK CHARACT E RI ZAT I ON
99
End of module 1