Operating System
Operating System
Operating System
A computer is a general purpose device that can execute sequences of instructions presented in a
formal format to perform numerical calculations and other tasks.
Read: Newell, A., Perlis, A. J. and Simon, H. I. 1967. Is ‘computer science’ science or engineering?
Science, 167(3795): 1373-1374.
Computer hardware is the collection of all physical elements of the computer system.
Computer software is the collection of all programs stored in and executed by a computer system.
An operating system is a piece of software that manages all the resources of a computer system,
both hardware and software, and provides an environment in which the user can execute his/her
programs in a convenient and efficient manner.
Operating systems exist because they offer a reasonable way to solve the problem of creating a
usable computer system.
An operating system –
- manages the computer hardware
- facilitates execution of application programs
- acts as an intermediary between the user and the computer hardware
- designed to be convenient and efficient
User
Application programs
Operating system
Computer hardware
The operating system provides the means for proper use of the resources in the operation of the
computer system.
Design goals –
- convenience (ease of use) - personal computers
- efficiency (proper resource allocation) - high performance computers
- energy conservation - handheld devices
- minimal user interference - embedded devices
2 Bedtime Stories on Operating Systems
Multiprogramming increases CPU utilization by keeping multiple jobs (code and data) in the
memory so that the CPU always has one to execute.
Job 1
Job 2
Job 3
Operating system
A kernel is that part of the operating system which interacts directly with the hardware and
performs the most crucial tasks.
A microkernel is much smaller in size than a conventional kernel and supports only the core
operating system functionalities.
A shell, also known as a command interpreter, is that part of the operating system that receives
commands from the users and gets them executed.
A system call is a mechanism using which a user program can request a service from the kernel for
which it does not have the permission to perform.
User programs typically do not have permission to perform operations like accessing I/O devices and
communicating other programs.
A user program invokes system calls when it requires such services.
System calls provide an interface between a program and the operating system.
System calls are of different types.
E.g. – fork, exec, getpid, getppid, wait, exit.
Dual-mode operation –
- User mode
- Kernel mode / supervisor mode / system mode / privileged mode
Mode bit– 0: kernel, 1: user
Request using a system call
An Introduction to Operating Systems 3
A real-time operating system (RTOS) has well-defined and fixed time constraints which have to be
met or the system will fail.
An RTOS is used when rigid time constraints have been placed on the operation of processes or flow
of data.
4 Bedtime Stories on Operating Systems
Booting is the process of starting the computer and loading the kernel.
When a computer is turned on, the power-on self-tests (POST) are performed.
Then the bootstrap loader, which resides in the ROM, is executed.
The bootstrap loader loads the kernel or a more sophisticated loader.
http://amturing.acm.org –
- Kenneth Lane Thompson
- Frederick Phillips Brooks, Jr.
- Barbara Liskov
Chapters: 1 and 2.
Sections: 1.1, 1.4 – 1.9, 1.11, 2.1 – 2.5, 2.7 and 2.10.
UNIT 2
PROCESS AND PROCESS SCHEDULING
A process comprises of –
- text section containing the program code
- current activity represented by the values of the program counter and other registers
- program stack
- data section containing global variables
- heap
Stack max
↑
Heap
Data
Text 0
Each process is internally represented by the operating system by a process control block (PCB) also
called task control block.
PCB contains all information associated with the process –
- Process state
- Values of program counter and other registers
- CPU scheduling information - priority, pointer to scheduling queue, etc.
- Accounting information - process id, CPU- and real- time used, time limits, etc.
- I/O status information - list of i/o devices allocated, list of open files, etc.
6 Bedtime Stories on Operating Systems
P0 P1
Process scheduling is selecting one process for execution out of all the ready processes.
The objective of multiprogramming is to have some process running at all times so as to maximize
CPU utilization.
The objective of multitasking is to switch the CPU among the processes so frequently that the user
can interact with each process while it is running.
To meet these objectives the process scheduler selects one of the available processes for execution.
A queuing diagram shows how the processes migrate among the various scheduling queues.
Process and Process Scheduling 7
A long-term scheduler (job scheduler) selects processes from those submitted by the user and loads
them into the memory.
The long-term scheduler controls the degree of multiprogramming which is represented by the
number of processes in the memory.
It is invoked less frequently.
A short-term scheduler (CPU scheduler) selects one of the processes in the memory and allocates
the CPU to it.
The short-term scheduler is invoked frequently and should be very fast.
The long-term scheduler should select a proper mix of CPU-bound processes and i/o-bound
processes.
A CPU-bound process spends most of its time doing computations.
An i/o-bound process spends most of its time doing i/o.
Some multitasking operating systems, like Unix and Windows, do not use long-term schedulers.
All new processes are put in the memory for the perusal of the short-term scheduler.
A medium-term scheduler removes processes from the memory and from the competition for the
CPU, thus reducing the degree of multiprogramming.
The processes can be later reintroduced in the memory and their executions can be resumed.
This scheme is called swapping.
Swapping may be used to improve process mix and to free up some memory in uncontrollable
circumstances.
A parent process may continue executing with its children processes or may wait for them to
complete.
A process may be a duplicate of its parent process (same code and data) or may have a new program
loaded into it.
Typically, the kernel is the first process to be created, is the ancestor of all other processes and is at
the root of the process tree.
A zombie process is a process that has terminated but its PCB still exists because its parent has not
yet accepted its return value.
Interprocess communication –
- Reasons –
- information sharing
- computational speedup
- modularity
- convenience
- Models –
- shared memory
- message passing [send(P,message) and receive(id,message)]
The execution of a process consists of alternate CPU bursts and i/o bursts, starting and ending with
CPU bursts.
In preemptive scheduling, a process can be forced to leave the CPU and switch to the ready queue.
E.g. – Unix, Linux, Windows 95 and higher.
CPU scheduling is optional for conditions 2 and 3, but necessary in the other two conditions.
A dispatcher is the module of the operating system that gives control of the CPU to the process
selected by the CPU scheduler.
Steps –
- switching context
- switching to user mode
- jumping to the proper location in the user program
Dispatch latency is the time taken to stop a process and start another.
Dispatch latency is a pure overhead.
Scheduling criteria –
- ↑ CPU utilization - percentage
- ↑ Throughput - number of processes completed per unit time
- ↓ Turnaround time - time from submission to completion (time spent in different
queues + time spent in CPU + time spent in different i/o
devices)
- ↓ Waiting time - time spent in ready queue (only)
- ↓ Response time - time from submission to first response
Variance in response time should be minimal.
Exercise 1.
Process Arrival time (ms) CPU burst time (ms)
P1 0 24
P2 1 3
P3 2 3
Calculate throughput, average turnaround time and average waiting time.
Exercise 2.
Process Arrival time (ms) CPU burst time (ms)
P1 0 8
P2 1 4
P3 2 9
P4 3 5
Calculate throughput, average turnaround time and average waiting time for (a) non-preemptive
and (b) preemptive scheduling.
Priority scheduling –
- CPU is allocated to the process with the highest priority
- priority range be 0 to 7 (say), with 0 representing the highest or the lowest priority
- priority may depend on internal factors (time limit, memory requirement, number of open
files, etc.) and external factors (user, department, etc.)
- may be preemptive or non-preemptive
- SJF is a special case of priority scheduling, with priority inversely proportional to predicted
next CPU burst length
- may cause starvation, i.e. indefinite blocking of processes
- aging: gradually increase the priority of a process waiting for a long time
- priority inversion: a low-priority process gets the priority of a high-priority process waiting
for it
Process and Process Scheduling 11
Exercise 3.
Process Arrival time (ms) CPU burst time (ms) Priority
P1 0 8 7
P2 2 4 5
P3 4 6 0 (highest)
P4 6 4 1
Dispatch latency = 1 ms.
Calculate CPU utilization, throughput, average turnaround time and average waiting time for (a)
non-preemptive and (b) preemptive scheduling.
Exercise 4.
Process Arrival time (ms) CPU burst time (ms)
P1 0 18
P2 2 4
P3 4 6
P4 6 12
Calculate throughput, average turnaround time and average waiting time for (a) time quantum = 8
ms and (b) time quantum = 2 ms.
FCFS
RR
RR
FCFS
FCFS
12 Bedtime Stories on Operating Systems
Exercise 5.
Process Arrival time (ms) CPU burst time (ms) Queue
P1 0 18 Q2
P2 2 4 Q2
P3 4 6 Q1 (highest-priority)
P4 6 12 Q2
Q1: FCFS
Q2: RR, time quantum = 2 ms
Inter-queue: preemptive priority scheduling
Calculate throughput, average turnaround time and average waiting time.
Exercise 6.
Process Arrival time (ms) CPU burst time (ms)
P1 0 32
P2 2 10
P3 4 16
P4 6 30
Calculate throughput, average turnaround time and average waiting time.
Comparison –
☺
First come first served Simple Inefficient (high turnaround
scheduling time, waiting time and
response time)
Shortest job first scheduling Most efficient Impossible to implement
Priority scheduling Low waiting time for high Indefinite blocking of low
priority processes priority processes
Round robin scheduling Efficient and no indefinite Too much context switching
blocking of processes
Multilevel queue scheduling Low waiting time for high Complex
priority processes
Multilevel feedback queue Fast turnaround for short Complex
scheduling processes
Process and Process Scheduling 13
Chapters: 3 – 5.
Sections: 3.1 – 3.4, 4.1, 4.2 and 5.1 – 5.4.
UNIT 3
PROCESS SYNCHRONIZATION
A cooperating process is one that can affect or be affected by other processes executing in the
system.
P0 P1
… …
i++; i--;
… …
R1 i R2 i
R1 R1 + 1 R2 R2 - 1
i R1 i R2
Initially, i = 10
P0: R1 = 10
P1: R2 = 10
P0: R1 = 11
P1: R2 = 9
P0: i = 11
P1: i = 9
Race condition is a situation where several processes access and manipulate the same data
concurrently and the outcome of the execution depends on the particular order in which the
accesses take place.
To avoid such situations, it must be ensured that only one process can manipulate the data at a
given time.
This can be done by process synchronization.
A solution to the critical section problem must satisfy the following properties –
- Mutual exclusion
- Progress - If no process is in its critical section and some processes want to enter
their critical sections, then the processes which are in their entry
sections or exit sections decide which process will enter its critical
section next. This selection cannot be postponed indefinitely.
- Bounded waiting - There is a limit on the number of times other processes are allowed to
enter their critical sections after a process has made a request to enter
its critical section and before that request is granted.
A semaphore is an integer variable that, apart from initialization, is accessed only through two
atomic operations called wait() and signal().
wait (S) signal (S)
{while (S <= 0) {S++;
; // busy waiting }
S--;
}
Only one process can access a semaphore at a time.
Binary semaphore –
- also called mutex lock
- used to implement solution of critical section problem with multiple processes
- initialized to 1
do { wait (mutex);
critical section
signal (mutex);
remainder section
} while (true);
Counting semaphore –
- used to control access to a resource that has multiple instances
- initialized to n
Process Synchronization 17
Semaphores –
chopsticks[5];
all initialized to 1
Pi
do { wait (chopstick[i]);
wait (chopStick[(i+1)%5]);
// eat
signal (chopstick[i]);
signal (chopstick[(i+1)%5]);
// think
} while (true);
If all philosophers get hungry simultaneously then there will be a deadlock.
initialization_code()
{for (int i=0; i < 5; i++)
state[i] = THINKING;
}
Pi
…
dp.pickup(i);
…
// eat
…
dp.putdown(i);
…
Chapter: 6.
Sections: 6.1 – 6.7.
UNIT 4
DEADLOCKS
In a multiprogramming system, several processes may compete for a finite number of resources.
A process requests for resources, and if the resources are not available at the time then the process
enters the waiting state.
Sometimes, a process will wait indefinitely because the resources it has requested for are being held
by other similar waiting processes.
Deadlock is a situation in which two or more processes are waiting indefinitely because the
resources they have requested for are being held by one another.
The resources are partitioned into several types, each of which has several identical instances.
E.g. – memory spaces, registers, i/o devices, etc.
If a process requests for an instance of the resource type, then the allocation of any one instance of
the resource type will satisfy the process.
A process utilizes a resource in the following sequence: request use release.
If there is a cycle, then there may be a deadlock (necessary but not sufficient condition).
If there is no cycle, then there is not deadlock.
If each resource type has one instance, then a cycle implies that there is a deadlock (necessary and
sufficient condition).
If each resource type in a cycle has one instance, then there is a deadlock (necessary and sufficient
condition).
- Deadlock avoidance - Each process informs the operating system about the
resources it will require in its lifetime. The operating
system allocates the resources to the processes in a
sequence that will not lead to a deadlock.
- Deadlock detection and recovery
Deadlock prevention –
1. Mutual exclusion
- Mutual exclusion is necessary for non-sharable resources, like printer and speaker.
- Mutual exclusion can be prevented from sharable resources, like read-only files.
2. Hold and wait
- Ensure that when a process requests for resources it is not holding some other resources.
- Protocol 1: Request and get all the resources in the beginning.
- Protocol 2: Release current resources before requesting other resources.
- Disadvantages – low resource utilization, starvation of processes requiring several resources.
3. No preemption
- If a process requests for some resources and they cannot be allocated right now, then the
resources that the process is holding are preempted.
- Resources that can be saved and later restored – registers and files.
- Resources that cannot be preempted – printer.
4. Circular wait
- Arrange the resource types as R1, R2, R3 … Rm.
- Protocol 1: Request resources in increasing order.
- Protocol 2: If a process requests for Ri, then it must release Rj for all j>i.
- All instances of a resource type should be allocated together.
- Prove: The protocols can prevent deadlock.
- Disadvantages – low resource utilization, reduced throughput.
Deadlock avoidance –
- A safe state is one in which the system can allocate resources to each process up to the
maximum in some order and still avoid deadlock.
- A system is in a safe state if there is a safe sequence.
- A sequence of processes <P1, P2, P3 … Pn> is safe if for each Pi the resources that Pi may still
request can be satisfied by the currently available resources plus all resources held by all Pj
with j<i.
- Pi may need to wait for one or more Pjs.
deadlock
safe
unsafe
When a process requests a set of resources, the system must determine whether allocating these
resources will leave the system in a safe state.
If yes, then the resources may be allocated to the process.
If no, then the process must wait till other processes release enough resources.
n processes
m resource types
Data structures –
- Available[m] - currently available instances
- Max[nxm] - maximum demand
- Allocation[nxm] - currently allocated
- Need[nxm] = Max - Allocation
These data structures may vary in size with time.
For Pi: Max[i], Allocation[i], Need[i].
Exercise 1.
0 1 1 0 0 1
A system has four processes, viz. P1 to P4, and three resource types. The system is in a safe state with
6 2 4 4 2 3
Available = 1 1 1, Max = and Allocation = . Which of the following requests should be
4 3 5 3 3 3
1 2 1 0 2 1
Solution.
0 1 0
(a) Resource-request algorithm -
2 0 1
Request = [1 0 1], Available = [1 1 1] and Need =
1 0 2
1 0 0
0 0 1 0 1 0
Since Request < Need[3] and Request < Available, pretend to allocate resources
4 2 3 2 0 1
Available = [0 1 0], Allocation = , and Need =
4 3 4 0 0 1
0 2 1 1 0 0
Safety algorithm -
Initially - Work: [0 1 0] Finish: [F F F F]
P1 selected - Work: [0 1 1] Finish: [T F F F]
P3 selected - Work: [4 4 5] Finish: [T F T F]
P4 selected - Work: [4 6 6] Finish: [T F T T]
P2 selected - Work: [8 8 9] Finish: [T T T T]
Safe sequence: <P1, P3, P4, P2>
Safe state: Request should be granted.
0 1 0
(b) Resource-request algorithm -
2 0 1
Request = [1 0 1], Available = [1 1 1] and Need =
1 0 2
1 0 0
0 0 1 0 1 0
Since Request < Need[2] and Request < Available, pretend to allocate resources
5 2 4 1 0 0
Available = [0 1 0], Allocation = , and Need =
3 3 3 1 0 2
0 2 1 1 0 0
Safety algorithm -
Initially - Work: [0 1 0] Finish: [F F F F]
P1 selected - Work: [0 1 1] Finish: [T F F F]
Unsafe state: Request should not be granted.
Deadlock detection –
If all resource types have single instances, then we can detect deadlocks using a wait-for graph.
A cycle in the wait-for graph denotes that there is a deadlock and all the processes in the cycle are
deadlocked.
Time complexity of cycle detection algorithm = O(n2).
Deadlocks 25
A modification of the banker’s algorithm can be used to detect deadlock in a system that has
resource types with multiple instances.
Request[nxm] represents the requests.
1. Let Work[m] and Finish[n] be vectors
Work = Available
For i = 0, 1, 2 … n-1, if (Allocation[i] != 0) then Finish[i] = false else Finish[i] = true
2. Find an i such that (Finish[i] == false && Request[i] <= Work)
If no such i exists, then go to step 4
3. Work = Work + Allocation[i]
Finish[i] = true
Go to step 2
4. If Finish[i] == false for some i, then there is a deadlock and Pi is deadlocked
Time complexity = O(mxn2).
Chapter: 7.
Sections: 7.1 – 7.7.
UNIT 5
MEMORY MANAGEMENT
Object module
Execution time
Process in memory absolute address (runtime)
Swapping –
- swap-out, swap-in
- priority scheduling: roll-out, roll-in
- relocation is helpful
Advantages –
- size of process independent of page size
- no external fragmentation
Disadvantage –
- internal fragmentation
Research topic –
- dynamic page size
Effective access time = hit ratio x (TLB access time + memory access time)
+ (1 - hit ratio) x (TLB access time + 2 x memory access time)
Shared pages
Reentrant code = pure code = non-self-modifying code = not changed during execution.
Pages common to multiple processes can be shared if they contain reentrant code.
ed1
ed1 ed1 ed1 ed2
ed2 ed2 ed2 ed3
ed3 ed3 ed3 data1
data1 data2 data3 data2
P1 P2 P3 data3
Three-level paging: <p1, p2, p3, d> - second outer page table, first outer page table, inner page table.
Too many memory accesses.
30 Bedtime Stories on Operating Systems
Segmentation is a memory management scheme where the linear memory is logically partitioned
into segments each having unique purpose.
Address: <segment_number, offset>.
Chapter: 8.
Sections: 8.1 – 8.6.
UNIT 6
VIRTUAL MEMORY MANAGEMENT
Virtual memory is a technique that allows the execution of processes that are not completely in
memory.
physical
memory
v (valid): page is in memory
i (invalid): page is not in memory
If the CPU wants to access a memory-resident page, then the execution continues normally.
If the CPU wants to access a page that is not in memory, then a page fault trap occurs.
(trap: highest priority non-maskable external-hardware interrupt)
page
(3) locate page on disk
(5) update page table table
disk
memory
34 Bedtime Stories on Operating Systems
Pure demand paging is an extreme case of demand paging where the execution of a process is
started with none of its pages in memory.
Starting is fast and response time is low.
Takes advantage of locality of references for both code and data.
Page replacement is the process of replacing one page by another in the memory when there is no
free frame.
How to load a page in memory when there is no free frame?
1. Save the content of the page currently in memory on disk.
2. Load new page.
3. Update page table.
Avoid saving unmodified pages –
- use modify bit / dirty bit
number of
page faults
number of frames
Belady’s anomaly
Number of page faults increases with number of frames allocated.
- e.g. –
- reference string: 1, 2, 3, 4, 1, 2, 5, 1, 2, 3, 4, 5.
- frames = 3, page faults = 9.
- frames = 4, page faults = 10.
Allocation of frames –
Minimum number of frames – otherwise too many page faults
Frame allocation algorithms –
- equal allocation
- proportional allocation (proportional to the virtual memory of the process)
Local page replacement – a process can control its page fault rate
Global page replacement – better throughput, commonly used
Thrashing is a situation when a system is spending more time in servicing page faults than executing.
If a process does not have the number of frames it needs to store the pages in active use, then it will
page fault frequently.
Since all the pages in memory are in active use, the page that will be replaced will be needed again.
This will bring down the CPU utilization and the system will load more processes in the memory in
order to increase the degree of multiprogramming.
This will trigger a chain reaction of page faults.
This situation when the CPU utilization diminishes due to high paging activity is called thrashing.
Virtual Memory Management 37
Avoiding thrashing –
- Working-set model
- based on locality of references
- working set of a process = pages referenced in ∆ most recent references
- e.g. –
∆ = 10.
reference string: 2 6 1 5 7 7 7 7 5 1 6 2 3 4 1 2 3 4 4 3 4 3 3 4 4 4 3 3.
- WSSi = working-set size of Pi
- total demand of frames, D = Σ WSSi
- let, m = number of frames
- if D > m, then there will be thrashing
- we swap out one or more processes
- this keeps degree of multiprogramming as high as possible and optimizes CPU
utilization
- keeping track of working-sets is difficult
- Page fault frequency
Chapter: 9.
Sections: 9.1, 9.2 and 9.4 – 9.6.
UNIT 7
STORAGE MANAGEMENT
The operating system abstracts from the physical properties of its storage devices to define a logical
storage, the file.
A file is a named collection of related information that is recorded on secondary storage.
A file is the smallest allotment of logical secondary storage.
Data cannot be written to secondary storage unless they are within a file.
Files store both code and data.
File formats – magic number.
To provide efficient and convenient access to the disk, the operating system imposes a file system to
allow data to be stored, located and retrieved easily.
Design issues –
- how the file system should look to the user?
- how to map logical files onto the physical disk?
The file system is typically implemented as a multi-leveled system.
Application programs
↓
Logical file system [Manages metadata]
↓ logical block address
File organization module [Allocation management, free space management]
↓ physical block address
Basic file system
↓ high-level generic command
I/o control [Device driver, interrupt]
↓ low-level device specific command
Disk
Example of disk address: drive 1, track 2, sector 1.
Allocation methods –
- contiguous
- linked
- indexed
Transfer rate: rate at which data is transferred between the bus and the disk.
Seek time: time taken to move the arm to the cylinder.
Rotational latency: time taken to rotate the disk so that the head comes over the sector.
Positioning time (random access time) = seek time + rotational latency.
Performance of a disk depends on transfer rate and positioning time.
Disk formatting –
- physical formatting or low-level formatting
- fills the disk with a special data structure for each sector
- the data structure has header, data area and trailer
- contains sector number, error correcting code, etc.
- logical formatting
- creates a file system
Chapters: 10 to 12.
Sections: 10.1, 10.3, 11.1, 11.2, 11.4, 11.5, 12.1, 12.2, 12.4 and 12.5.
UNIT 8
(A) I/O MANAGEMENT AND (B) SYSTEM PROTECTION AND SECURITY
The i/o subsystem of the kernel separates the rest of the kernel from the complexities of managing
the i/o devices.
The device drivers provide a uniform device access interface to the i/o subsystem.
Kernel
Kernel i/o subsystem Software
Device driver 1 Device driver 2 … Device driver n
Device controller 1 Device controller 2 … Device controller n Hardware
↕ ↕ …
↕
Device 1 Device 2 Device n
Types of devices –
- storage devices (disk, USB drives)
- transmission devices (modem)
- human-interface devices (keyboard, mouse, display)
- specialized devices (microscope, telescope, case of flight control computer)
Hardware –
- port - a connection point where a device is connected
- bus - set of wires to transfer data and control information
- controller - controls a port or a device
Complementary devices –
- teletype = keyboard + display
- photocopier = scanner + printer
Virtual devices –
- PDF printer
Properties of devices –
44 Bedtime Stories on Operating Systems
Access matrix
Object O1 O2 O3 O4
Domain
D1 R R
D2 RW R RW
D3 R E
Threats –
- Trojan horse
- attractive and harmless cover program
- harmful hidden program
- used as virus carrier
- Trap door
- hidden door
- Logic bomb
- initiate attack under specific situation
- Virus
- self-replicating and infectious
- modify and destroy files
- Linux is not susceptible to virus because it does not allow modifying executable
programs
- Worm
- infection programs spread through networks
hook
(1) attack
(2) request
worm worm
(3) copy
Infected system Target system