Operating System Concepts Sunbeam Institute of Information & Technology, Hinjwadi, Pune & Karad
Operating System Concepts Sunbeam Institute of Information & Technology, Hinjwadi, Pune & Karad
SunBeam Institute of
Information & Technology,
Hinjwadi, Pune & Karad.
* Introduction:
- Why there is need of an OS?
- What is an OS?
- Booting process in brief
- Functions of an OS
* Process Management
- What is Process & PCB?
- States of the process
- CPU scheduling & CPU scheduling algorithms
- Inter Process Communication: Shared Memory Model & Message
Passing Model
* Process Management
- Process Synchronization/Co-ordination
- Deadlocks & deadlock handling methods
* Memory Management
- Swapping
- Memory Allocation Methods
- Internal Fragmentation & External Fragmentation
- Segmentation
- Paging
- Virtual Memory Management
* File Management
- What is file?
- What is filesystem & filesystem structure?
- Disk space allocation methods
- Disk scheduling algorithms
Q. What is a Software?
- Software is a collection of programs.
Q. What is a Program?
- Program is a finite set of instructions written in any programming language
(either low level or high level programming language) given to the machine to do
specific task.
- 3 types of programs are there:
1. "user programs": programs defined by the programmer user/developers
e.g. main.c, hello.java, addition.cpp etc....
2. "application programs": programs which comes with an OS/can be installed later
e.g. MS Office, Notepad, Compiler, IDE's, Google Chrome, Mozilla Firefox, Calculator,
Games etc....
3. "System Programs": programs which are inbuilt in an OS/part of an OS.
e.g. Kernel, Loader, Scheduler, Memory Manager etc...
# Functions of an OS:
Basic minimal functionalities/Kernel functionalities – must be
supported by any OS – compulsory functionalities :
1. Process Management
2. Memory Management
3. Hardware Abstraction
4. CPU Scheduling
5. File & IO Management
Extra utility functionalities/optional:
6. Protection & Security
7. User Interfacing
8. Networking
# Process Management
- When we say an OS does process management it means an OS is
responsible for process creation, to provide environment for
an execution of a process, resource allocation, scheduling,
resources management, inter process comminication,
process coordination, and terminate the process.
Q. What is a Program?
- User view: Program is a finite set of instructions written in any
programming language given to the machine to do specific task.
- System view: Program is an executable file in HDD which
divided logically into sections like exe header, bss section, data
section, rodata section, code section, symbol table.
Q. What is a Process?
User view:
- Program in execution is called as a process.
- Running program is called as a process.
- When a program gets loaded into the main memory it is reffered as a process.
- Running instance of a program is reffered as a process.
System view:
- Process is a program loaded into the main memory which has got PCB into the
main memory inside kernel space and program itself into the main memory
inside user space has got bss section, rodata section, code section, and
two new sections gets added for the process:
stack section: contains function activation records of called functions.
heap section: dynamically allocated memory
- As a kernel, core program of an a OS runs continuosly into the main memory, part
of the main memory which is occupied by the kernel reffered as kernel
space and whichever part is left is reffered as an user space, so main memory
is divided logically into two parts: kernel space & user space.
- User programs gets loaded into the user space only.
- When we execute a program or upon submission of a process very first one structure
gets created into the main memory inside kernel space by an OS in which all the
information which is required to control an execution of that process can be kept, this
structure is reffered as a PCB: Process Control Block, is also called as a Process
Descriptor.
- Per process one PCB gets created and PCB remains inside the main memory
throughout an execution of a program, upon exit PCB gets destroyed from the main
memory.
- PCB mainly contains: PID, PPID, PC, CPU sched information, memory management
information, information about resources allocated for that process, execution context
etc...
# Process States:
- Throughout execution, process goes through different states out of which at a
time it can be only in a one state.
- States of the process:
1. New state: upon submission or when a PCB for a process gets created into
the main memory process is in a new state.
2. Ready state: after submission, if process is in the main memory and waiting
for the CPU time, it is in a ready state.
3. Running state: if currently the CPU is executing any process then state of
that process is considered as a running state.
4. Waiting state: if a process is requesting for any i/o device then state of that
process is considered as a waiting state.
5. Terminated state: upon exit, process goes into terminated state and its PCB
gets destroyed from the main memory.
# Process States:
- To keep track on all running programs, an OS maintains few data structures reffered as
kernel data structures:
1. Job queue: it contains list of PCB’s of all submitted processes.
2. Ready queue: it contains list of PCB’s of processes which are in the main memory and
waiting for the CPU time.
3. Waiting queue: it contains list of PCB’s of processes which are requesting for that
particular device.
# Context Switch:
- As during context-switch, the CPU gets switched from an execution context of one
process onto an execution context of another process, and hence it is reffered as
"context-switch".
- context-switch = state-save + state-restore
- state-save of suspended process can be done i.e. an execution context of
suspended process gets saved into its PCB.
- state-restore of a process which is scheduled by the CPU scheduler can be done
by the dispacther, dispather copies an execution context of process scheduled by
the cpu scheduler from its PCB and restore it onto the CPU registers.
- When a high priority process arrived into the ready queue, low priority process
gets suspended by means of sending an interrupt, and control of the CPU gets
allocated to the high priority process, and its execution gets completed first, then
low priority process can be resumed back, i.e. the CPU starts executing suspended
process from the point at which it was suspended and onwards.
4. Priority Scheduling
- In this algorithm, process which is having highest priority gets control of the CPU
first, each process is having priority in its PCB.
- priority for a process can be decided by two ways:
1. internally – priority for process can be decided by an OS depends on no. of resources
required for it.
2. externally – priority for process can be decided by the user depends on requirement.
- Minimum priority value indicates highest priority.
- This algorithm is purely preemptive.
- Due to the very low priority process may gets blocked into the ready queue and
control of the CPU will never gets allocated for such a process, this situation is reffered as a
starvation or indifinite blocking.
- Ageing: it is a technique in which, an OS gradually increments priority of blocked
process, i.e. priority of blocked process gets incremented after some fixed time interval by
an OS, so that priority of blocked process becomes sufficient enough to get control of the
CPU, and starvation can be avoided.
Synchronization Tools:
1. Semaphore: there are two types of semaphore
i. Binary semaphore: can be used when at a time resource can be
acquired by only one process.
- It is an integer variable having either value is 0 or 1.
ii. Counting / Classic semaphore: can be used when at a time resource
can be acquired by more than one processe
2. Mutex Object: can be used when at a time resource can be acquired by
only one process.
- Mutex object has two states: locked & unlocked, and at a time it can be
only in a one state either locked or unlocked.
- Semaphore uses signaling mechanism, whereas mutex object uses
locking and unlocking mechanism.
Deadlock:
- There are four neccessary and sufficient conditions to occure
deadlock / characteristics of deadlock:
1. Mutual Exclusion: at a time resource can be acquired by only
one process.
2. No Preemption: control of the resource cannot be taken away
forcefully from any process.
3. Hold & Wait: every process is holding one resource and waiting
for the resource which is held by another process.
4. Circular Wait: if process P1 is holding resource and waiting for
the resource held by another process P2, and process P2 is also
holding one resource and waiting for the resource held by process P1.
3. Deadlock Recovery:
- System can be recovered from the deadlock by two ways:
1. Process termination: in this method randomly any one
process out of processes causes deadlock gets selected and
terminated forcefully to recover system from deadlock.
- Process which gets terminated forcefully in this method is
reffered as a victim process.
2. Resource preemption: in this method control of the
resource taken away forcefully from a process to recover system
from deadlock.
# Memory Management
Q. Why there is a need of memory (main
memory )management?
- As main memory is must for an execution of any program and it is a
limited memory, hence an OS manages main memory.
- To achieve maximum CPU utilization, an OS must support multi-
tasking, and to support multi-tasking multiple porcesses must be
submitted into the system at a time i.e. it must support multi-
programming, but as main memory is limited to support multi-
programming an OS has to do memory management to complete an
execution of all submitted processes.
- Memory space of one process should gets protected from another
process.
# Swapping:
- Swap area: it is a portion of the hard disk drive ( keep reserved while
installation of an OS) can be used by an OS as an extension of the main
memory in which inactive running programs can be kept temporarily and as
per request processes can be swapped in and swapped out between swap
area and the main memory.
- In Linux swap area can be maintained in the form of swap partition,
whereas in Windows swap area can be maintained in the form of swap
files.
- Conventionally size of the swap area should be doubles the size of
the main memory, i.e. if the size of main memory is 2 GB then size of
swap area should be 4 GB, if the size of main memory is 4 GB then size of
swap area should be 8 GB and so on.
# Swapping:
- Swapping done by the system program of an OS named as
Memory Manager, it swap ins active running programs into the
main memory from swap area and swap outs inactive running
programs from the main memory and keep them temporarily into
the swap area.
- there are two variants of swapping: swap in & swap out.
Memory Allocation:
- When a process is requesting for the main memory, there are two methods by
which memory gets allocated for any process
1. Contiguos Memory Allocation
2. Non-contiguos Memory Allocation
# Advantages:
- This method is simple to implement
# Disadvantages:
- Internal fragmentation: if memory remains unused which is
internal to the partition.
- Degree of multi-programming is limited to the number of
partitions in the main memory.
- Maximum size of a process is limited to max size
partition in the main memory.
- To overcome limitations/disadvantages of fixed size partitioning
method, variable/dynamic size partitioning method has been
designed.
1. Compaction: shuffling of main memory can be done in such a way that all used
partitions can be shifted to one side and all free partitions can be shifted to other
side and contiguos large free partition will be made available for the new processes.
- Compaction is practically not feasible as there is need to do recalculations of
addresses every time.