Operating Systems All Notes
Operating Systems All Notes
Definition
− Operating system is system software that manages computer hardware and software
resources and provides common services for computer programs
− It is the most important software that runs on a computer.
− It manages the computer's memory and processes, as well as all of its software and
hardware.
− It also allows you to communicate with the computer without knowing how to speak the
computer's language.
− All computers and computer-like devices have operating systems eg laptop, tablet,
desktop, smartphone, smartwatch, router etc
− Most software programs are designed to work with just one company's operating system,
like Windows (Microsoft) or macOS (Apple).
− A piece of software will clearly say which operating systems it supports and will get very
specific if necessary.
− For example, a video production software program might say it supports Windows 10,
Windows 8, and Windows 7, but does not support older versions of Windows like
Windows Vista and XP.
− The three most common operating systems for personal computers are Microsoft
Windows, Mac OS X, VMS and Linux.
1
An operating system is a program that acts as an interface between the user and the computer
hardware and controls the execution of all kinds of programs
2
− In multiprogramming environment, the OS decides which process gets the processor when
and for how much time. This function is called process scheduling.
Activities
− Allocates the processor (CPU) to a process.
− De-allocates processor when a process is no longer required.Keeps tracks of processor and
status of process. The program responsible for this task is known as traffic controller.
3. Device Management
− the operating system interacts with the device drivers for peripheral control.
− An Operating System manages device communication via their respective drivers.
Activities
− Decides which process gets the device when and for how much time.
− Allocates the device in the most efficient way.
− De-allocates devices.
− Keeps tracks of all devices. The program responsible for this task is known as the
− I/O controller.
4. File Management
− It organize and keep track of files.
− For example, a hierarchical file system is one that uses directories to organize files into a
tree structure.
− Although the operating system provides its own file management system, you can buy
separate file management systems
Activities
− Decides who gets the resources.
− Allocates the resources.
− De-allocates the resources.
− Keeps track of information, location, uses, status etc. The collective facilities are often
known as file system.
5. Security
3
By means of password and similar other techniques, it prevents unauthorized access
to programs and data
6. Control over system performance
Recording delays between request for a service and response from the system.
7. Job accounting
Keeping track of time and resources used by various jobs and users.
8. Error detecting aids
Production of dumps, traces, error messages, and other debugging and error detecting aids.
9. Coordination between other software and users
Coordination and assignment of compilers, interpreters, assemblers and other
software to the various users of the computer systems.
4
Disadvantages:
− It is very difficult to debug batch systems.
− Lack of interaction between user and operating system.
− Suppose an error occurs in one of the jobs of a batch. Then, all the remaining jobs get
affected i.e; they have to wait until the error is resolved.
− CPU is often idle, because the speed of the mechanical I/O devices is slower than the
CPU.
− Difficult to provide the desired priority.
Time-sharing Operating Systems
− Time-sharing is a technique which enables many people, located at various terminals, to
use a particular computer system at the same time.
− When the Processor's time is shared among multiple users simultaneously is termed as
time-sharing
− As the name itself suggests, in a time-sharing system or multi-tasking system, multiple
jobs can be executed on a system at the same time by sharing the CPU time among them.
− It is considered to be a logical extension of multiprogramming because both does
simultaneous execution but differ in their prime objectives.
− Difference between Multiprogrammed batch systems and time sharing is:
Multiprogrammed batch systems, the objective is to maximize processor
− Time-Sharing Systems, the objective is to minimize response time
− The operating system uses CPU scheduling and multiprogramming to provide each user
with a small portion of a time.
− Computer systems that were designed primarily as batch systems have been modified to
time-sharing systems.
How it works
Multiple jobs are executed by the CPU by switching between them, but the switches occur so
frequently. Thus, the user can receive an immediate response.
For example,
in a transaction processing, the processor executes each user program in a short burst or
quantum of computation. That is, if n users are present, then each user can get a time quantum.
When the user submits the command, the response time is in few seconds at most.
5
Advantages
− Provides the advantage of quick response
− Avoids duplication of software
− Reduces CPU idle time
Disadvantages
− Problem of reliability
− Question of security and integrity of user programs and data
− Data communication must be enabled
Distributed Operating System (DOS)
− These are the systems in which data is stored and processed on many machines which are
connected by some network.
− Distributed systems use multiple central processors to serve multiple real-time
applications and multiple users.
− Data processing jobs are distributed among the processors accordingly.
− To make it more simple, distributed systems are a collection of several separate
(individual) systems which communicate (through a LAN or WAN) and cooperate with
each other (using some software) in order to provide the users, access to various resources
that the system maintains.
− Distributed systems are loosely-coupled i.e; hardware and software may communicate
with each other but they need not depend upon each other.
Eg: Solaris Operating System
Objectives
− Making resources easily available.
− Open
− Scalable.
− Distribution transparency i.e; the fact that the resources are distributed must be hidden.
Advantages
− Reduced load on the host computer.
− Reduced delay in data processing.
− Better service to the users.
6
− With resource sharing facility, a user at one site may be able to use the resources available
at another.
− Speedup the exchange of data with one another via electronic mail.
− Reliable- If one site fails in a distributed system, the remaining sites can potentially
continue operating.
Disadvantages:
− Network Security.
− Network complexity.
− Unpredictability
Network Operating System (NOS)
− A Network Operating System runs on a server and provides the server the capability to
manage data, users, groups, security, applications, and other networking functions.
− The primary purpose of the network operating system is to allow shared file and printer
access among multiple computers in a network, typically a local area network (LAN), a
private network or to other networks.
− Examples of network operating systems include Microsoft Windows Server 2003,
Microsoft Windows Server 2008, UNIX, Linux, Mac OS X, Novell NetWare, and BSD.
− Network Operating System(NOS) is similar to distributed systems but they differ in the
way they access resources.
− An NOS need special functions/protocols to facilitate connectivity & communication
among the systems.
− NOS employs a client-server model where as a DOS employs a master-slave model.
− In NOS, to process the data, it has to be transferred to the server.
Objectives
− Providing Security.
− Facilitating user management i.e; logon & logoff.
− Enabling remote access to servers.
Advantages
− Centralized servers are highly stable.
− Security is server managed.
7
− Upgrades to new technologies and hardware can be easily integrated into the
system.
− Remote access to servers is possible from different locations and types of systems.
− Provides file, print, web & back-up services.
− Authorized access & automatic hardware detection.
Disadvantages
− Expensive as they need to run servers continuously.
− Need for regular maintenance & updates.
− Depends on the center location (server) for most operations.
Real-Time Operating System (RTOS)
− Real time system is defines as a data processing system in which each task has a deadline
to complete.
− Real Time Operating System (RTOS) adheres to this deadline as missing a deadline can
cause affects ranging from undesired to disastrous.
− A real-time system has well-defined, fixed time constraints. otherwise the system will
fail. For example, Scientific experiments, medical imaging systems, industrial control
systems, weapon systems, robots, air traffic control systems, etc.
− The time taken by the system to respond to an input and display of required
updated information is termed as the response time.
− So in this method, the response time is very less as compared to online processing.
− There are two types of real-time operating systems.
Hard real-time systems
− Hard real-time systems guarantee that critical tasks complete on time.
− In hard real-time systems, secondary storage is limited or missing and the data is stored
in ROM.
− In these systems, virtual memory is almost never found.
Soft real-time systems
− Soft real-time systems are less restrictive.
− A critical real-time task gets priority over other tasks and retains the priority until it
completes.
8
− Soft real-time systems have limited utility than hard real-time systems.
− For example, multimedia, virtual reality, Advanced Scientific Projects like undersea
exploration and planetary rovers, etc.
9
File System Manipulation
− A file represents a collection of related information.
− Computers can store files on the disk (secondary storage), for long-term storage purpose.
Examples of storage media include magnetic tape, magnetic disk and optical disk drives
like CD, DVD.
− Each of these media has its own properties like speed, capacity, data transfer rate and data
access methods.
− A file system is normally organized into directories for easy navigation and usage.
− These directories may contain files and other directions.
− major activities of an operating system with respect to file management include
• Program needs to read a file or write a file.
• The operating system gives the permission to the program for operation on file.
• Permission varies from read-only, read-write, denied, and so on.
• Provides an interface to the user to create/delete files.
• Provides an interface to the user to create/delete directories.
• Provides an interface to create the backup of file system.
Communication
− In case of distributed systems which are a collection of processors that do not
share memory, peripheral devices, or a clock, the operating system manages
communications between all the processes. Multiple processes communicate with one
another through communication lines in the network.
− The OS handles routing and connection strategies, and the problems of contention and
security.
− Major activities of an operating system with respect to communication include
• Two processes often require data to be transferred between them.
• Both the processes can be on one computer or on different computers, but
are connected through a computer network)
• Communication may be implemented by two methods, either by Shared
Memory or by Message Passing.
10
Error Handling
− Errors can occur anytime and anywhere.
− An error may occur in CPU, in I/O devices or in the memory hardware.
− Major activities of an operating system with respect to error handling:
• The OS constantly checks for possible errors.
• The OS takes an appropriate action to ensure correct and consistent computing.
Resource Management
− In case of multi-user or multi-tasking environment, resources such as main memory, CPU
cycles and files storage are to be allocated to each user or job.
− Major activities of an operating system with respect to resource management:
• The OS manages all kinds of resources using schedulers.
• CPU scheduling algorithms are used for better utilization of CPU.
Protection
− Considering a computer system having multiple users and concurrent execution of
multiple processes, the various processes must be protected from each other's activities.
− Protection refers to a mechanism or a way to control the access of programs, processes, or
users to the resources defined by a computer system.
− major activities of an operating system with respect to protection:
• The OS ensures that all access to system resources is controlled.
• The OS ensures that external I/O devices are protected from invalid access
attempts.
• The OS provides authentication features for each user by means of passwords.
PROPERTIES OF THE OPERATING SYSTEM
Batch processing
Batch processing is a technique in which an Operating System collects the programs and data
together in a batch before processing starts. An operating system does the following activities
related to batch processing −
• The OS defines a job which has predefined sequence of commands, programs and data as
a single unit.
11
• The OS keeps a number a jobs in memory and executes them without any manual
information.
• Jobs are processed in the order of submission, i.e., first come first served fashion.
• When a job completes its execution, its memory is released and the output for the job gets
copied into an output spool for later printing or processing.
Advantages
• Batch processing takes much of the work of the operator to the computer.
• Increased performance as a new job get started as soon as the previous job is finished,
without any manual intervention.
Disadvantages
• Difficult to debug program.
• A job could enter an infinite loop.
• Due to lack of protection scheme, one batch job can affect pending jobs.
Multitasking
Multitasking is when multiple jobs are executed by the CPU simultaneously by switching
between them. Switches occur so frequently that the users may interact with each program while
it is running. An OS does the following activities related to multitasking −
• The user gives instructions to the operating system or to a program directly, and receives
an immediate response.
• The OS handles multitasking in the way that it can handle multiple operations/executes
multiple programs at a time.
• Multitasking Operating Systems are also known as Time-sharing systems.
• These Operating Systems were developed to provide interactive use of a computer system
at a reasonable cost.
• A time-shared operating system uses the concept of CPU scheduling and
multiprogramming to provide each user with a small portion of a time-shared CPU.
• Each user has at least one separate program in memory.
12
• A program that is loaded into memory and is executing is commonly referred to as a
process.
• When a process executes, it typically executes for only a very short time before it either
finishes or needs to perform I/O.
• Since interactive I/O typically runs at slower speeds, it may take a long time to complete.
During this time, a CPU can be utilized by another process.
• The operating system allows the users to share the computer simultaneously. Since each
action or command in a time-shared system tends to be short, only a little CPU time is
needed for each user.
• As the system switches CPU rapidly from one user/program to the next, each user is given
the impression that he/she has his/her own CPU, whereas actually one CPU is being
shared among many users.
Multiprogramming
Sharing the processor, when two or more programs reside in memory at the same time, is referred
as multiprogramming. Multiprogramming assumes a single shared processor.
Multiprogramming increases CPU utilization by organizing jobs so that the CPU always has one
to execute.
The following figure shows the memory layout for a multiprogramming system.
13
• To accommodate many jobs in memory, memory management is required.
Interactivity
Interactivity refers to the ability of users to interact with a computer system. An Operating system
does the following activities related to interactivity −
• Provides the user an interface to interact with the system.
• Manages input devices to take inputs from the user. For example, keyboard.
• Manages output devices to show outputs to the user. For example, Monitor.
The response time of the OS needs to be short, since the user submits and waits for the result.
Real Time System
Real-time systems are usually dedicated, embedded systems. An operating system does the
following activities related to real-time system activity.
• In such systems, Operating Systems typically read from and react to sensor data.
• The Operating system must guarantee response to events within fixed periods of time to
ensure correct performance.
Distributed Environment
A distributed environment refers to multiple independent CPUs or processors in a computer
system. An operating system does the following activities related to distributed environment −
• The OS distributes computation logics among several physical processors.
• The processors do not share memory or a clock. Instead, each processor has its own local
memory.
• The OS manages the communications between the processors. They communicate with
each other through various communication lines.
Spooling
Spooling is an acronym for simultaneous peripheral operations on line. Spooling refers to putting
data of various I/O jobs in a buffer. This buffer is a special area in memory or hard disk which is
accessible to I/O devices.
An operating system does the following activities related to distributed environment −
• Handles I/O device data spooling as devices have different data access rates.
• Maintains the spooling buffer which provides a waiting station where data can rest while
the slower device catches up.
14
• Maintains parallel computation because of spooling process as a computer can perform
I/O in parallel fashion. It becomes possible to have the computer read data from a tape,
write data to disk and to write out to a tape printer while it is doing its computing task.
Advantages
• The spooling operation uses a disk as a very large buffer.
• Spooling is capable of overlapping I/O operation for one job with processor operations for
another job.
15
OPERATING SYSTEM - PROCESS SCHEDULING
Process
A process is basically a program in execution.
The execution of a process must progress in a sequential fashion.
program, it becomes a process which performs all the tasks mentioned in the program.
When a program is loaded into the memory and it becomes a process, it can be divided into four
sections ─ stack, heap, text and data.
The following image shows a simplified layout of a process inside main memory −
16
Process Life Cycle
When a process executes, it passes through different states.
In general, a process can have one of the following five states at a time.
S.N. State & Description
1 Start
This is the initial state when a process is first started/created.
2 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.
3 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.
4 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.
5 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.
17
Figure: Process States
18
The architecture of a PCB is completely dependent on Operating System and may contain
different information in different operating systems.
The PCB is maintained for a process throughout its lifetime, and is deleted once the process
terminates.
PROCESS SCHEDULING
− Is the act of determining which process is in the ready state, and should be moved to the
running state
− The main aim of the process scheduling system is to keep the CPU busy all the time and
to deliver minimum response time for all programs.
− The scheduler must apply appropriate rules for swapping processes IN and OUT of CPU.
− Process scheduling is an essential part of a Multiprogramming operating systems.
− Such operating systems allow more than one process to be loaded into the executable
memory at a time and the loaded process shares the CPU using time multiplexing.
− Time-division multiplexing (TDM) is a method of transmitting and receiving
independent signals over a common signal path by means of synchronized switches at
each end of the transmission line so that each signal appears on the line only a fraction of
time in an alternating pattern.
Scheduling falls into one of the two general categories:
• Pre-emptive Scheduling:
− When the operating system decides to favour another process, pre-empting the
currently executing process.
− In this scheduling a process switches from running state to ready state or from
waiting state to ready state.
− In pre-emptive scheduling processes can be scheduled
• Non Pre-emptive Scheduling
− It’s when the currently executing process gives up the CPU voluntarily.
− In this scheduling a process terminates or switches from running to waiting for
state.
19
− In Non-preemptive scheduling, processes cannot be scheduled.
−
Scheduling Queues
The OS maintains all PCBs in Process Scheduling Queues.
The OS maintains a separate queue for each of the process states and PCBs of all processes in
the same execution state are placed in the same queue.
When the state of a process is changed, its PCB is unlinked from its current queue and moved to
its new state queue.
The Operating System maintains the following important process scheduling queues −
• Job queue − This queue keeps all the processes in the system.
• Ready queue − This queue keeps a set of all processes residing in main memory, ready
and waiting to execute. A new process is always put in this queue.
• Device queues − Processes waiting for a device to become available are placed in this
queue. There are unique device queues available for each I/O device.
A new process is initially put in the Ready queue. It waits in the ready queue until it is selected
for execution (or dispatched).
Once the process is assigned to the CPU and is executing, one of the following several events can
occur:
• The process could issue an I/O request, and then be placed in the I/O queue.
• The process could create a new subprocess and wait for its termination.
• The process could be removed by force 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.
20
Types of Schedulers
There are three types of schedulers
1. Long Term Scheduler
2. Short Term Scheduler
3. Medium Term Scheduler
Long Term / job Scheduler
− It determines which programs are admitted to the system for processing.
− Process loads into the memory for CPU scheduling.
− Long term scheduler runs less frequently.
− Long Term Schedulers decide which program must get into the job queue.
− From the job queue, selects processes from the queue and loads them into memory for
execution.
− Job Scheduler maintains a good degree of Multiprogramming. An optimal degree of
Multiprogramming means the average rate of process creation is equal to the average
departure rate of processes from the execution memory.
− When a process changes the state from new to ready, then there is use of long-term
scheduler.
Short Term / CPU Scheduler
− It runs very frequently.
− The main aim of this scheduler is to enhance CPU performance and increase process
execution rate.
21
− It is the change of ready state to running state of the process.
− CPU scheduler selects a process among the processes that are ready to execute and
allocates CPU to one of them.
− Short-term schedulers, also known as dispatchers, make the decision of which process to
execute next.
− Short-term schedulers are faster than long-term schedulers.
Medium Term Scheduler
This scheduler removes the processes from memory (and from active contention for the CPU),
and thus reduces the degree of multiprogramming.
the process can be reintroduced into memory and its execution can be continued where it
left from. 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, or because a change in memory
22
Context Switch
A context switch is the mechanism to store and restore the state or context of a CPU in
Process Control block so that a process execution can be resumed from the same point at a later
time.
A context switcher enables multiple processes to share a single CPU.
Context switching is an essential part of a multitasking operating system features.
When the scheduler switches the CPU from executing one process to execute another, the state
from the current running process is stored into the process control block.
After this, the state for the process to run next is loaded from its own PCB and used to set the PC,
registers, etc. At that point, the second process can start executing.
When the process is switched, the following information is stored for later use.
• Program Counter
• Scheduling information
• Base and limit register value
• Currently used register
• Changed State
• I/O State information
• Accounting information
Operations on Process
There are two major operations Process Creation and Process Termination.
Process Creation
Through appropriate system calls, such as fork or spawn, processes may create other processes.
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.
The parent PID (PPID) is also stored for each process.
A child process may receive some amount of shared resources with its parent depending on
system implementation.
To prevent runaway children from consuming all of a certain system resource, child processes
may or may not be limited to a subset of the resources originally allocated to the parent.
There are two options for the parent process after creating the child :
23
• Wait for the child process to terminate before proceeding.
• Run concurrently with the child, continuing to process without waiting. It is also possible
for the parent to run for a while, and then wait for the child later, which might occur in a
sort of a parallel processing operation.
There are also two possibilities in terms of the address space of the new process:
1. The child process is a duplicate of the parent process
2. The child process has a program loaded into it
Process Termination
By making the exit(system call), typically returning an int, processes may request their own
termination.
This int is passed along to the parent if it is doing a wait(), and is typically zero on successful
completion and some non-zero code in the event of any problem.
Processes may also be terminated by the system for a variety of reasons, including :
• The inability of the system to deliver the necessary system resources.
• In response to a KILL command or other unhandled process interrupts.
• A parent may kill its children if the task assigned to them is no longer needed i.e. if the
need of having a child terminates.
• If the parent exits, the system may or may not allow the child to continue without a parent
When a process ends, all of its system resources are freed up, open files flushed and closed, etc.
The process termination status and execution times are returned to the parent if the parent is
waiting for the child to terminate, or eventually returned to init if the process already became an
orphan.
The processes which are trying to terminate but cannot do so because their parent is not waiting
for them are termed zombies. These are eventually inherited by init as orphans and killed off.
Scheduling Criteria
There are many different criterias to check when considering the "best" scheduling algorithm,
they are:
CPU Utilization
To make out the best use of CPU and not to waste any CPU cycle, CPU would be working most
of the time (Ideally 100% of the time).
24
Considering a real system, CPU usage should range from 40% (lightly loaded) to 90% (heavily
loaded.)
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.
Response Time
Amount of time it takes from when a request was submitted until the first response is
produced.
Remember, it is the time till the first response and not the completion of process execution(final
response).
CPU utilization and Throughput are maximized and other factors are reduced for proper
optimization.
Burst Time
Time required by a process for CPU execution.
Arrival Time
Time at which the process arrives in the ready queue.
Completion Time
Time at which process completes its execution.
Turnaround Time
It is the amount of time taken to execute a particular process
Is the interval from time of submission (Arrival time) of the process to the time of completion of
the process(completion time).
Turnaround time = Completion time – Arrival time
Turnaround time = Waiting time + Burst time
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.
Waiting Time = Turn Around Time - Burst Time
25
Load Average
It is the average number of processes residing in the ready queue waiting for their turn to
get into the CPU.
Process scheduling algorithms
• First-Come, First-Served (FCFS) Scheduling
• Shortest-Job-Next (SJN) Scheduling
• Priority Scheduling
• Shortest Remaining Time
• Round Robin(RR) Scheduling
• Multiple-Level Queues Scheduling
These algorithms are either non-preemptive or preemptive.
Non-preemptive algorithms are designed so that once a process enters the running state, it
cannot be preempted until it completes its allotted time, whereas the preemptive scheduling is
based on priority where a scheduler may preempt a low priority running process anytime when a
high priority process enters into a ready state.
First Come First Serve (FCFS)
• Jobs are executed on first come, first serve basis.
• It is a non-preemptive, scheduling algorithm.
• Easy to understand and implement.
• Its implementation is based on FIFO queue.
• Poor in performance as average wait time is high.
Advantages
• Better for long processes
• Simple method (i.e., minimum overhead on processor)
• No starvation
Disadvantages
• Inappropriate for interactive systems
• Large fluctuations in average turnaround time are possible.
• Convoy effect occurs. Even very small process should wait for its turn to come to utilize
the CPU. Short process behind long process results in lower CPU utilization.
• Throughput is not emphasized.
26
Process no Arrival Burst Completion Turnaround Waiting
time(AT) time(BT) time(CT) time(TAT) = CT- time =
AT TAT-BT
1 0 4 4 4 0
2 1 3 7 6 3
3 2 1 8 6 5
4 3 2 10 7 5
5 4 5 15 11 6
27
Example 1
P1 P2 P3
0 24 27 30
28
Average Turn Around time = (Total Turn Around Time) / (Total number of processes)
81 / 3 = 27 ms
Example 3
Gantt chart
P1 P2 P3 P4 P5
0 80 100 110 130 180
29
420/5 = 84 ms
Average Turn Around time = (Total Turn Around Time) / (Total number of processes)
600/5 = 120 ms
30
4 3 2 11 7 5
5 4 8 24 19 11
Preemptive example
Examples
Consider the following table of arrival time and burst time for three processes P0, P1 and P2.
Process Arrival time Burst Time
P0 0 ms 9 ms
P1 1 ms 4 ms
P2 2 ms 9 ms
The pre-emptive shortest job first scheduling algorithm is used. Scheduling is carried out only at
arrival or completion of processes. What is the average waiting time for the three processes?
Priority Scheduling
• Priority scheduling is a non-preemptive algorithm
• Its the most common scheduling algorithms in batch systems.
• Each process is assigned a priority.
• Process with highest priority is to be executed first and so on.
• Processes with same priority are executed on first come first served basis (FCFS manner) .
• Priority can be decided based on memory requirements, time requirements or any other
resource requirement.
• Least burst time is processed first
• Processes with same priority are executed in.
31
Examples
Non Preemptive priority scheduling example
Process no Priority Arrival Burst Completion Turnaround Waiting
time(AT) time(BT) time(CT) time(TAT) time =
= CT-AT TAT-BT
1 2(low) 0 4 4 4 0
2 4 1 2 25 24 22
3 6 2 3 23 21 18
4 10 3 5 9 6 1
5 8 4 1 20 16 15
6 12 (high) 5 4 13 8 4
7 9 6 6 19 13 7
32
Average Turnaround time =
Average Waiting time =
33
Multilevel Queue Scheduling
• Multiple queues are maintained for processes.
• Each queue can have its own scheduling algorithms.
• Priorities are assigned to each queue.
THREADS
is an execution unit which consists of its own program counter, a stack, and a set of registers.
Threads are also known as Lightweight processes.
Threads are popular way to improve application through parallelism.
The CPU switches rapidly back and forth among the threads giving illusion that the threads are
running in parallel.
As each thread has its own independent resource for process execution, multpile processes can be
executed parallel by increasing number of threads.
34
Types of Thread
There are two types of threads :
• User Threads
• Kernel Threads
User threads, - are above the kernel and without kernel support.
- These are the threads that application programmers use in their programs.
Kernel threads - are supported within the kernel of the OS itself.
- All modern OSs support kernel level threads, allowing the kernel to perform
multiple simultaneous tasks and/or to service multiple kernel system calls
simultaneously.
Multithreading Models
The user threads must be mapped to kernel threads, by one of the following strategies.
• Many-To-One Model
• One-To-One Model
• Many-To-Many Model
1. 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.
35
2. One-To-One Model
• The one-to-one model creates a separate kernel thread to handle each and every user
thread.
• Most implementations of this model place a limit on how many threads can be created.
• Linux and Windows from 95 to XP implement the one-to-one model for threads.
3. Many-To-Many Model
• The many-to-many model multiplexes any number of user threads onto an equal or
smaller number of kernel threads,
• It combines the best features of the one-to-one and many-to-one models.
36
• Users can create any number of the threads.
• Blocking the kernel system calls does not block the entire process.
• Processes can be split across multiple processors.
Thread Libraries
Thread libraries provides programmers with API for creating and managing of threads.
Thread libraries may be implemented either in user space or in kernel space. The user space
involves API functions implemented solely within user space, with no kernel support. The kernel
space involves system calls, and requires a kernel with thread library support.
There are three types of thread :
• POSIX Pitheads, may be provided as either a user or kernel library, as an extension to the
POSIX standard.
• Win32 threads, are provided as a kernel-level library on Windows systems.
• Java threads - Since Java generally runs on a Java Virtual Machine, the implementation of
threads is based upon whatever OS and hardware the JVM is running on, i.e. either
Pitheads or Win32 threads depending on the system
Benefits of Multithreading
1. Responsiveness- the response to a call is effective
2. Resource sharing- hence allowing better utilization of resources.
3. Its easier to Create and manage threads
37
4. Scalability - One thread runs on one CPU. In Multithreaded processes, threads can be
distributed over a series of processors to scale.
5. Context Switching is smooth - Context switching refers to the procedure followed by CPU
to change from one task to another.
Multithreading Issues
1. Thread Cancellation.
Thread cancellation means terminating a thread before it has finished working. There can
be two approaches for this, one is Asynchronous cancellation, which terminates the
target thread immediately. The other is Deferred cancellation allows the target thread to
periodically check if it should be cancelled.
2. Signal Handling.
Signals are used in UNIX systems to notify a process that a particular event has occurred.
Now in when a Multithreaded process receives a signal, to which thread it must be
delivered? It can be delivered to all, or a single thread.
3. fork() System Call.
fork() is a system call executed in the kernel through which a process creates a copy of
itself. Now the problem in Multithreaded process is, if one thread forks, will the entire
process be copied or not?
4. Security Issues because of extensive sharing of resources between multiple threads.
There are many other issues that you might face in a multithreaded process, but there are
appropriate solutions available for them. Pointing out some issues here was just to study both
sides of the coin.
38
DEADLOCK
a deadlock occurs when a process or thread enters a waiting state because a requested system
resource is held by another waiting process, which in turn is waiting for another resource held by
another waiting process.
Blocked processes; each holding a resource and waiting to acquire a resource held by another
process.
39
4. Circular Wait
Circular wait can be avoided if we number all resources, and require that processes
request resources only in strictly increasing(or decreasing) order.
Operating system
Following three strategies can be used to remove deadlock after its occurrence.
1. Preemption
By taking a resource from one process and give it to other.
This will resolve the deadlock situation, but sometimes it does cause problems.
2. Rollback
The system can periodically make a record of the state of each process and when deadlock
occurs, roll everything back to the last checkpoint, and restart, but allocating resources
differently so that deadlock does not occur.
3. Kill one or more processes
This is the simplest way, but it works.
What is a Livelock?
There is a variant of deadlock called livelock.
This is a situation in which two or more processes continuously change their state in response
to changes in the other process(es) without doing any useful work.
This is similar to deadlock in that no progress is made but differs in that neither process is
blocked or waiting for anything.
A human example of livelock would be two people who meet face-to-face in a corridor and each
moves aside to let the other pass, but they end up swaying from side to side without making any
progress because they always move the same way at the same time.
40
MEMORY MANAGEMENT
Main Memory refers to a physical memory that is the internal memory to the computer.
Every program we execute and every file we access must be copied from a storage device into
main memory.
All the programs are loaded in the main memeory for execution. Sometimes complete program is
loaded into the memory, but sometimes a certain part or routine of the program is loaded into the
main memory only when it is called by the program, this mechanism is called Dynamic Loading,
this enhance the performance.
Also, at times one program is dependent on some other program. In such a case, rather than
loading all the dependent programs, CPU links the dependent programs to the main executing
program when it’s required. This mechanism is known as Dynamic Linking.
Swapping
A process needs to be in memory for execution. But sometimes there is not enough main memory
to hold all the currently active processes in a timesharing system.
So, excess process are kept on disk and brought in to run dynamically.
Swapping is the process of bringing in each process in main memory, running it for a while and
then putting it back to the disk.
Contiguous Memory Allocation
In contiguous memory allocation each process is contained in a single contiguous block of
memory.
Memory is divided into several fixed size partitions.
Each partition contains exactly one process.
When a partition is free, a process is selected from the input queue and loaded into it.
The free blocks of memory are known as holes.
The set of holes is searched to determine which hole is best to allocate.
Memory Protection
Memory protection is a phenomenon by which we control memory access rights on a computer.
The main aim of it is to prevent a process from accessing memory that has not been allocated to
it. Hence prevents a bug within a process from affecting other processes, or the operating system
41
itself, and instead results in a segmentation fault or storage violation exception being sent to the
disturbing process, generally killing of process.
Memory Allocation
Memory allocation is a process by which computer programs are assigned memory or space. It is
of three types :
1. First Fit
The first hole that is big enough is allocated to program.
2. Best Fit
The smallest hole that is big enough is allocated to program.
3. Worst Fit
The largest hole that is big enough is allocated to program.
Fragmentation
Fragmentation occurs in a dynamic memory allocation system when most of the free blocks are
too small to satisfy any request.
It is generally termed as inability to use the available memory.In such situation processes are
loaded and removed from the memory.
As a result of this, free holes exists to satisfy a request but is non contiguous i.e. the memory is
fragmented into large no. Of small holes. This phenomenon is known as External
Fragmentation.
Paging
A solution to fragmentation problem is Paging.
Paging is a memory management mechanism that allows the physical address space of a process
to be non-contagious. Here physical memory is divided into blocks of equal size called Pages.
The pages belonging to a certain process are loaded into available memory frames.
Page Table
A Page Table is the data structure used by a virtual memory system in a computer operating
system to store the mapping between virtual address and physical addresses.
Virtual address is also known as Logical address and is generated by the CPU. While Physical
address is the address that actually exists on memory.
Segmentation
42
Segmentation is another memory management scheme that supports the user-view of memory.
Segmentation allows breaking of the virtual address space of a single process into segments that
may be placed in non-contiguous areas of physical memory.
Segmentation with Paging
Both paging and segmentation have their advantages and disadvantages, it is better to combine
these two schemes to improve on each. The combined scheme is known as 'Page the Elements'.
Each segment in this scheme is divided into pages and each segment is maintained in a page table.
So the logical address is divided into following 3 parts :
• Segment numbers(S)
• Page number (P)
• The displacement or offset number (D)
Virtual Memory
Virtual Memory is a space where large programs can store themselves in form of pages while
their execution and only the required pages or portions of processes are loaded into the main
memory.
This technique is useful as large virtual memory is provided for user programs when a very small
physical memory is there.
In real scenarios, most processes never need all their pages at once, for following reasons :
• Error handling code is not needed unless that specific error occurs, some of which are
quite rare.
• Arrays are often over-sized for worst-case scenarios, and only a small fraction of the
arrays are actually used in practice.
• Certain features of certain programs are rarely used.
Benefits of having Virtual Memory :
1. Large programs can be written, as virtual space available is huge compared to physical
memory.
2. Less I/O required, leads to faster and easy swapping of processes.
3. More physical memory available, as programs are stored on virtual memory, so they
occupy very less space on actual physical memory.
Demand Paging
43
The basic idea behind demand paging is that when a process is swapped in, its pages are not
swapped in all at once. Rather they are swapped in only when the process needs them (On
demand).
This is termed as lazy swapper, although a pager is a more accurate term.
Initially only those pages are loaded which will be required the process immediately.
The pages that are not moved into the memory, are marked as invalid in the page table. For an
invalid entry the rest of the table is empty. In case of pages that are loaded in the memory, they
are marked as valid along with the information about where to find the swapped out page.
When the process requires any of the page that is not loaded into the memory, a page fault trap is
triggered and following steps are followed,
1. The memory address which is requested by the process is first checked, to verify the
request made by the process.
2. If its found to be invalid, the process is terminated.
3. In case the request by the process is valid, a free frame is located, possibly from a free-
frame list, where the required page will be moved.
4. A new operation is scheduled to move the necessary page from disk to the specified
memory location. ( This will usually block the process on an I/O wait, allowing some
other process to use the CPU in the meantime. )
5. When the I/O operation is complete, the process's page table is updated with the new
frame number, and the invalid bit is changed to valid.
44
6. The instruction that caused the page fault must now be restarted from the beginning.
There are cases when no pages are loaded into the memory initially, pages are only loaded when
demanded by the process by generating page faults. This is called Pure Demand Paging.
The only major issue with Demand Paging is, after a new page is loaded, the process starts
execution from the beginning. Its is not a big issue for small programs, but for larger programs it
affects performance drastically.
Page Replacement
As studied in Demand Paging, only certain pages of a process are loaded initially into the
memory. This allows us to get more number of processes into the memory at the same time. but
what happens when a process requests for more pages and no free memory is available to bring
them in. Following steps can be taken to deal with this problem :
1. Put the process in the wait queue, until any other process finishes its execution thereby
freeing frames.
2. Or, remove some other process completely from the memory to free frames.
3. Or, find some pages that are not being used right now, move them to the disk to get free
frames. This technique is called Page replacement and is most commonly used. We have
some great algorithms to carry on page replacement efficiently.
Basic Page Replacement
• Find the location of the page requested by ongoing process on the disk.
• Find a free frame. If there is a free frame, use it. If there is no free frame, use a page-
replacement algorithm to select any existing frame to be replaced, such frame is known as
victim frame.
• Write the victim frame to disk. Change all related page tables to indicate that this page is
no longer in memory.
• Move the required page and store it in the frame. Adjust all related page and frame tables
to indicate the change.
• Restart the process that was waiting for this page.
FIFO Page Replacement
• A very simple way of Page replacement is FIFO (First in First Out)
• As new pages are requested and are swapped in, they are added to tail of a queue and the
page which is at the head becomes the victim.
45
• Its not an effective way of page replacement but can be used for small systems.
LRU Page Replacement
Below is a video, which will explain LRU Page replacement algorithm in details with an example.
Thrashing
A process that is spending more time paging than executing is said to be thrashing. In other words
it means, that the process doesn't have enough frames to hold all the pages for its execution, so it
is swapping pages in and out very frequently to keep executing. Sometimes, the pages which will
be required in the near future have to be swapped out.
Initially when the CPU utilization is low, the process scheduling mechanism, to increase the level
of multiprogramming loads multiple processes into the memory at the same time, allocating a
limited amount of frames to each process. As the memory fills up, process starts to spend a lot of
time for the required pages to be swapped in, again leading to low CPU utilization because most
of the proccesses are waiting for pages. Hence the scheduler loads more processes to increase
CPU utilization, as this continues at a point of time the complete system comes to a stop.
To prevent thrashing we must provide processes with as many frames as they really need "right
now".
46
FILE SYSTEM
A file can be "free formed", indexed or structured collection of related bytes having meaning only
to the one who created it. Or in other words an entry in a directory is the file. The file may have
attributes like name, creator, date, type, permissions etc.
File Structure
A file has various kinds of structure. Some of them can be :
• Simple Record Structure with lines of fixed or variable lengths.
• Complex Structures like formatted document or reloadable load files.
• No Definite Structure like sequence of words and bytes etc.
Attributes of a File
Following are some of the attributes of a file :
• Name . It is the only information which is in human-readable form.
• Identifier. The file is identified by a unique tag(number) within file system.
• Type. It is needed for systems that support different types of files.
• Location. Pointer to file location on device.
• Size. The current size of the file.
• Protection. This controls and assigns the power of reading, writing, executing.
• Time, date, and user identification. This is the data for protection, security, and usage
monitoring.
File Access Methods
The way that files are accessed and read into memory is determined by Access methods. Usually
a single access method is supported by systems while there are OS's that support multiple access
methods.
Sequential Access
• Data is accessed one record right after another is an order.
• Read command cause a pointer to be moved ahead by one.
• Write command allocate space for the record and move the pointer to the new End Of File.
• Such a method is reasonable for tape.
Direct Access
• This method is useful for disks.
• The file is viewed as a numbered sequence of blocks or records.
47
• There are no restrictions on which blocks are read/written, it can be dobe in any order.
• User now says "read n" rather than "read next".
• "n" is a number relative to the beginning of file, not relative to an absolute physical disk
location.
Indexed Sequential Access
• It is built on top of Sequential access.
• It uses an Index to control the pointer while accessing files.
What is a Directory?
Information about files is maintained by Directories. A directory can contain multiple files. It can
even have directories inside of them. In Windows we also call these directories as folders.
Following is the information maintained in a directory :
• Name : The name visible to user.
• Type : Type of the directory.
• Location : Device and location on the device where the file header is located.
• Size : Number of bytes/words/blocks in the file.
• Position : Current next-read/next-write pointers.
• Protection : Access control on read/write/execute/delete.
• Usage : Time of creation, access, modification etc.
• Mounting : When the root of one file system is "grafted" into the existing tree of another
file system its called Mounting.
•
48
I/O ALLOCATION; DEVICE DRIVERS MANAGEMENT
One of the important jobs of an Operating System is to manage various I/O devices including
mouse, keyboards, touch pad, disk drives, display adapters, USB devices, Bit-mapped screen,
LED, Analog-to-digital converter, On/off switch, network connections, audio I/O, printers etc.
An I/O system is required to take an application I/O request and send it to the physical device,
then take whatever response comes back from the device and send it to the application. I/O
devices can be divided into two categories −
• Block devices − A block device is one with which the driver communicates by sending
entire blocks of data. For example, Hard disks, USB cameras, Disk-On-Key etc.
• Character devices − A character device is one with which the driver communicates by
sending and receiving single characters (bytes, octets). For example, serial ports, parallel
ports, sounds cards etc
Device Controllers
Device drivers are software modules that can be plugged into an OS to handle a particular device.
Operating System takes help from device drivers to handle all I/O devices.
The Device Controller works like an interface between a device and a device driver. I/O units
(Keyboard, mouse, printer, etc.) typically consist of a mechanical component and an electronic
component where electronic component is called the device controller.
There is always a device controller and a device driver for each device to communicate with the
Operating Systems. A device controller may be able to handle multiple devices. As an interface
its main task is to convert serial bit stream to block of bytes, perform error correction as
necessary.
Any device connected to the computer is connected by a plug and socket, and the socket is
connected to a device controller. Following is a model for connecting the CPU, memory,
controllers, and I/O devices where CPU and device controllers all use a common bus for
communication.
49
Synchronous vs asynchronous I/O
• Synchronous I/O − In this scheme CPU execution waits while I/O proceeds
• Asynchronous I/O − I/O proceeds concurrently with CPU execution
Communication to I/O Devices
The CPU must have a way to pass information to and from an I/O device. There are three
approaches available to communicate with the CPU and Device.
• Special Instruction I/O
• Memory-mapped I/O
• Direct memory access (DMA)
Special Instruction I/O
This uses CPU instructions that are specifically made for controlling I/O devices. These
instructions typically allow data to be sent to an I/O device or read from an I/O device.
Memory-mapped I/O
When using memory-mapped I/O, the same address space is shared by memory and I/O devices.
The device is connected directly to certain main memory locations so that I/O device can transfer
block of data to/from memory without going through CPU.
50
While using memory mapped IO, OS allocates buffer in memory and informs I/O device to use
that buffer to send data to the CPU. I/O device operates asynchronously with CPU, interrupts
CPU when finished.
The advantage to this method is that every instruction which can access memory can be used to
manipulate an I/O device. Memory mapped IO is used for most high-speed I/O devices like disks,
communication interfaces.
Direct Memory Access (DMA)
Slow devices like keyboards will generate an interrupt to the main CPU after each byte is
transferred. If a fast device such as a disk generated an interrupt for each byte, the operating
system would spend most of its time handling these interrupts. So a typical computer uses direct
memory access (DMA) hardware to reduce this overhead.
Direct Memory Access (DMA) means CPU grants I/O module authority to read from or write to
memory without involvement. DMA module itself controls exchange of data between main
memory and the I/O device. CPU is only involved at the beginning and end of the transfer and
interrupted only after entire block has been transferred.
Direct Memory Access needs a special hardware called DMA controller (DMAC) that manages
the data transfers and arbitrates access to the system bus. The controllers are programmed with
source and destination pointers (where to read/write the data), counters to track the number of
transferred bytes, and settings, which includes I/O and memory types, interrupts and states for the
CPU cycles.
51
The operating system uses the DMA hardware as follows −
Step Description
1 Device driver is instructed to transfer disk data to a buffer address X.
2 Device driver then instruct disk controller to transfer data to buffer.
3 Disk controller starts DMA transfer.
4 Disk controller sends each byte to DMA controller.
DMA controller transfers bytes to buffer, increases the memory address, decreases the
5
counter C until C becomes zero.
6 When C becomes zero, DMA interrupts CPU to signal transfer completion.
Polling vs Interrupts I/O
A computer must have a way of detecting the arrival of any type of input. There are two ways that
this can happen, known as polling and interrupts. Both of these techniques allow the processor
52
to deal with events that can happen at any time and that are not related to the process it is
currently running.
Polling I/O
Polling is the simplest way for an I/O device to communicate with the processor the processor.
The process of periodically checking status of the device to see if it is time for the next I/O
operation, is called polling. The I/O device simply puts the information in a Status register, and
the processor must come and get the information.
Most of the time, devices will not require attention and when one does it will have to wait until it
is next interrogated by the polling program. This is an inefficient method and much of the
processors time is wasted on unnecessary polls.
Compare this method to a teacher continually asking every student in a class, one after another, if
they need help. Obviously the more efficient method would be for a student to inform the teacher
whenever they require assistance.
Interrupts I/O
An alternative scheme for dealing with I/O is the interrupt-driven method. An interrupt is a signal
to the microprocessor from a device that requires attention.
A device controller puts an interrupt signal on the bus when it needs CPU’s attention when CPU
receives an interrupt, It saves its current state and invokes the appropriate interrupt handler using
the interrupt vector (addresses of OS routines to handle various events). When the interrupting
device has been dealt with, the CPU continues with its original task as if it had never been
interrupted.
53