You are on page 1of 129

Operating Systems

Code: CSPC-303
By
Dr K P Sharma

Department of Computer Science and Engineering


Dr. B.R. Ambedkar National Institute of Technology
1
Jalandhar Punjab-144011
Course Syllabus
 DEPARTMENT: COMPUTER SCIENCE AND ENGINEERING
COURSE CODE: CSPC-214
 COURSE TITLE: OPERATING SYSTEMS
 COURSE DESIGNATION: REQUIRED
 PRE-REQUISITES: NONE
 CONTACT HOURS/CREDIT SCHEME: (L-T-P-C: 2-0-0-2)
COURSE OUTCOMES
After the course completion, the students would be able to:
 1. Analyze the working of an operating system and its components.
 2. Define and analyze the synchronization in processes.
 3. Identify the working methodology of various subcomponents of the operating system.
 4. Compare and analyze different approaches for memory management techniques. .
Course Program outcomes
Outcomes

PO1 PO2 PO3 PO4 PO5 PO6 PO7 PO8 PO9 PO10 PO11 PO12
CSPC-214
CO 1 L H L M
CO 2 M L L
CO 3 M H M
CO 4 M M H M 2
TOPICS TO BE COVERED
 Operating systems objectives, services and functions: Characteristics of Modern Operating
Systems, Characteristics of Batch and multiprogramming operating systems. Comparisons
between real time systems and time-sharing systems, Operating system services and kernel
features.
 I/O management, I/O devices: Introduction to I/O management, I/O devices, Concepts of
threading, Organization of I/O functions, polling, various modes of data transfer,
Hardware/Software interface, I/O buffering.
 Processes and Process scheduling policies: Introduction to processes management, operating
system views of processes, various process transition states, Introduction to Processor
scheduling, Introduction to various types of schedulers, Performance criteria in scheduling
algorithms, Concept of FCFS scheduling algorithm, Concept of priority scheduling algorithm
like SJF, Concept of non-preemptive and preemptive algorithms, Concept of round-robin
scheduling algorithm, , Concept of multi-level queues, feedback queues.
 Concurrency control schemes: Various approaches to concurrency control schemes, Concept
of prouder/consumer problem, Mutual Exclusion, Concept of mutual exclusion first and second
algorithm, Concept of mutual exclusion third algorithm including introduction and
characteristics of semaphores, Introduction to Mutual exclusion with semaphores, Introduction
to Interprocess Communication and Synchronization, Critical regions and Conditional critical
regions in a Semaphore. Introduction to monitors, various modes of monitors.

3
 Dead Locks: Concept of Deadlocks, issues related to its prevention, avoidance and
detection / recovery, Concept of deadlock prevention and its avoidance, Concept of
deadlock detection and recovery.
 Memory Management: Need of Memory management and its requirements, paging,
segmentation, concept of fragmentation. Characteristics of contiguous & non-contiguous
allocation techniques, Detail study of fragmentation, Virtual memory management,
introduction to page-replacement, Need of various page-replacement policies, Concept of FIFO
and optimal page-replacement algorithms, Concept of LRU approximation and its page-
replacement algorithm, Concept of allocation algorithms.
 File management System: Need of file management, its requirements, User’s and operating
system’s view of file system, Concept of file directories and file sharing, secondary memory
management.
TEXT BOOKS, AND/OR REFERENCE MATERIAL
 Peterson and Silberschatz, “Operating System Concepts”, Addison-Wesley 6th Edition 2006.
 Milenkoviac, “Operating Systems Concepts and Design”, Tata McGraw-Hill 2005.
 Andrews S. Tanenbaum, “Modern Operating Systems”, Pearson Education, 3nd edition 2016.
 W Stallings, “ Opertating Systems” Pearson, 6th Edition 2012
 Dhamdhere, D M, “Introduction to Systems Software”, Tata Mc-Graw Hill 2015
 N Chauhan, “Principles of Operating Systems” 1st ed. 2018 4
What is an Operating System?

A program that acts as an


intermediary between a user of a
computer and the computer
hardware
Goals of OS
 Execute user programs and make solving user problems easier.
 Make the computer system convenient to use.
 Use the computer hardware in an efficient manner.
 Manages all resources.
 Controls execution of programs to prevent errors and improper
use of the computer
Four Components of a Computer System
Operating System – Definition:
1) An operating system is a program that controls the execution of
application programs and acts as an interface between the user of a
computer and the computer hardware.

2) A more common definition is that the operating system is the


one program running at all times on the computer (usually called the
kernel), with all else being application programs.

3) Syllabus defining definition: An operating system is concerned


with the allocation of resources and services, such as memory,
processors, devices and information. The operating system
correspondingly includes programs to manage these resources, such
as a traffic controller, a scheduler, memory management module,
I/O programs, and a file system.
Operating Systems

An operating system is similar to a


government. Like a government, it
performs no useful function by itself. It
simply provides an environment within
which other programs can do useful
work.
Where does the OS fit in?
 The layer between the
hardware and the user
program (application User programs
programs). Operating system interface
Operating system
 OS is a software system
that directly interacts with Hardware interface
the hardware. Hardware
 Hardware
• CPU, registers, disks, monitors, etc
 Hardware interface
• the instruction set
• other things like interrupt – anything that a programmer needs to
know in order to write programs that use the hardware.
 Operating system
• Implements the OS interface + resource management
 OS interface
• The enhanced instruction set: hardware instruction set + special
instructions called “traps” or “system calls”.
 User programs:
• Instructions in the enhanced instruction set + data
 Question: How many of you have dealt with the
OS (enhanced) interface before?
• One more layer between a typical user program and the
OS interface, the programming environment (compiler +
run time library).
• cout << “hello world.\n”  write(1, “hello world.\n”, 13);

User programs
programming environment
Operating system interface
Operating system
Hardware interface
Hardware
What does the OS achieve?
 Make it easy to write programs
• Add more powerful instructions to the hardware instruction set.
• What kind of instructions are added?
– Common functions used by many different applications.
– E.g. write (fileno, buf, len);
• Resource virtualization
• E.g. the illusion of Infinite memory.
 Make it easy to run programs
• How does a program run on the raw machine?
• The program is in memory, starting from program counter (pc),
run one instruction, goto the next instruction, run until the
halt instruction.
• How do you run a program?
• g++ helloworld.cpp and then a.out.
 The OS must fill the gap between how you run a program
and how the hardware runs a program. The OS must be able
to:
• Take the user command (g++, a.out, etc) – shell
• Find the executable file (g++ or a.out) – File system
• Load the executable into memory – memory management
• Set the registers properly (e.g. pc = starting address of the
executable)
• When there are multiple programs running, the OS must make each
program feel like it solely owns the whole machine (CPU, memory,
registers) – virtual machine for each process
• Manage processes.
 OS functionality: Implements the OS interface +
resource management
What does an OS achieve?
 It hides the complexity and limitations of hardware (hardware
interface) and creates a simpler, more powerful abstraction (OS
interface).
Hardware reality vs. OS abstraction

Reality Abstraction
A single CPU Multiple CPUs
Limited RAM capacity Infinite capacity
Mechanical disk Memory speed access
Insecure and Reliable and secure
unreliable networks
Many physical A single machine
machines
Operating System Services
An Operating System provides services to both the users and to the
programs.
 It provides programs an environment to execute.
 It provides users the services to execute the programs in a convenient manner.
Some common services of OS
• Program execution
• I/O operations
• File System manipulation
• Communication
• Error Detection
• Resource Allocation
• Protection
17
Program Execution
 Operating systems handle many kinds of activities from user
programs to system programs like printer spooler, name servers,
file server, etc. Each of these activities is encapsulated as a process.
 Following are the major activities of an OS with respect to program
management −
• Loads a program into memory.
• Executes the program.
• Handles program's execution.
• Provides a mechanism for process synchronization.
• Provides a mechanism for process communication.
• Provides a mechanism for deadlock handling.

18
I/O Operation
 An I/O subsystem comprises of I/O devices and their
corresponding driver software. Drivers hide the peculiarities of
specific hardware devices from the users.
 An Operating System manages the communication between user
and device drivers.
• I/O operation means read or write operation with any file or
any specific I/O device.
• Operating system provides the access to the required I/O
device when required.

19
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.
 For ease of use OS provides an easy manageable view of file system in the form
of directories and file icons.
 Following are the major activities of an operating system with respect to file
management −
• 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.
• Operating System provides an interface to the user to create/delete files.
• Operating System provides an interface to the user to create/delete directories.
• Operating System provides an interface to create the backup of file system.
20
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.
 Following are the major activities of an operating system with respect to
communication −
• 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.

21
Error handling
 Errors can occur anytime and anywhere. An error may occur in
CPU, in I/O devices or in the memory hardware. Following are the
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.

22
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. Following are the 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.
• Deadlock prevention and avoidance
• Error handling due to sharing of common recourses by multiple
processes

23
Protection
 Example: Protection
• Applications should not crash one another
• Address space: all memory addresses that an application can touch.
• Address space for one process is separated from address space for another
process and from the OS.
• Applications should not crash the OS
• Dual-mode operations
– Kernel mode: some instructions can only be executed by the OS (must be
executed with a kernel mode).
– User mode: an application can only access its own address space
– Examples of kernel mode instructions?
Kernel mode or Privileged Instructions Non-Kernel or non Privileged Instructions
• I/O instructions and Halt instructions • Reading the status of Processor
• Turn off all Interrupts • Reading the System Time
• Set the Timer • Generate any Trap Instruction
• Context Switching
• Sending the final printout of Printer
• Clear the Memory or Remove a process from
the Memory
• Modify entries in Device-status table
Resources managed by OS

 CPU: process management


 Memory: memory management
 Storage: storage and file management
 I/O devices: I/O management
Who needs OS?
 OS makes a computer easier to use
• All general purpose computers need OS.
 A better question: Who does not need OS?
• Some very specialized systems that usually do one thing (OS can be
embedded in the application).
• Microwave oven control
• MP3 players, etc.
Computer-System Organization
Major Components of a Computer System

 Processor (CPU)
• Runs program instructions
 Main Memory
• Storage for running programs and current data
 Secondary Storage
• Long-term program & data storage (hard disk, CD, etc)
 Input Devices
• Communication from the user to the computer(e.g.
keyboard, mouse)
 Output Devices
• Communication from the computer to the user (e.g.
monitor, printer, speakers)
Computer Components: Top Level View
The CPU
TheCPU is a silicon chip that contains millions of tiny electrical
components.

The CPU’s three main parts are:


 Control Unit
 Arithmetic Logic Unit (ALU)
Performs
 Registers calculations and
Arithmetic / Logic Unit
decisions

Coordinates
Control Unit
processing steps

Small, fast
Registers storage areas
for
instructions
and data
Registers
 Registers are small, fast memory within the CPU
 Different registers hold different things like instructions and addresses of
instructions, data (operands) and results of operations
These are of two types

1. Special Purpose Registers contain specific information the CPU needs.


 Instruction Register (IR) contains the actual instruction which is currently
being executed by the CPU.
 The Program Counter (PC) contains the address of the next instruction to
be executed by the program.

2. General Purpose Registers hold:


 the operands for arithmetic and logical operations (ie. the values on
which the operation will be performed)
 the results of such operations
 So General Purpose Registers are used for holding and manipulating
data used by the CPU
Memory
Computer Memory : millions/billions of on/off charges
 Bits: 0 or 1
 Bytes: Groups of 8 bits, A byte is the smallest unit of storage.
(Can hold one text character)
 Words: Groups of bits/bytes (8, 16, 32, 64-bits)
Storage is usually too large to be expressed in bytes or words.
 Kilobyte (KB) = 1024 bytes (210 bytes)

 Megabyte (MB) = 1024 x 1024 bytes or one million bytes (220 bytes)

 Gigabyte (GB) = 1024 x 1024 x 1024 bytes or 1 trillion bytes (230 bytes)

 Terabyte (TB) = 1024 x 1024 x 1024 x 1024 bytes one quadrillion bytes (240 bytes)
MEMORY CLASSIFICATION
Registers
2ns
Cache (I, II, III)
Volatile SRAM
DRAM
Primary SDRAM
(Semiconductor – RAM EDRAM
chip). Main Memory
EDO
FLASH RAM
Memory Types &
Storage Devices PROM
Non-volatile EPROM
ROM EEPROM
Tape
Magnetic memory HD, Zip Disk
Secondary FDD
(Devices)
Optical
CD-ROM, CD-R, CD-RW
Memory DVD-ROM, DVD-R 5ms
DVD- RW
PRIMARY MEMORY: RAM AND ROM
 RAM (Random Access Memory) is volatile (temporary). Programs
and data can be written to and erased from RAM as needed. This
means that RAM does not retain its bit configuration when the
power is turned off, but ROM does.
• SRAM: static RAM
• No need to be refreshed, made up of transistors to construct flip flops,
costly and fast, used as cache memory
• DRAM: dynamic RAM
• Need to be refreshed periodically, Made up of capacitors and transistors
• Main memory

 ROM (Read Only Memory) is nonvolatile (permanent). The


contents in locations in ROM cannot be changed. It holds
instructions that run the computer when it is first turned on (BIOS)
 The CPU accesses each location in memory by using a unique
number, called a memory address.
Main Memory

Each memory cell 5248 Each memory cell stores a


has a numeric 5249 10011010 set number of bits (some
address, which 5250 computers use 8 bits/one
uniquely identifies 5251 byte, others use words)
its location 5252
5253 A word is stored in
5254 consecutive
5255 memory bytes.
5256
Magnetic Disks
 A read/write head travels across a spinning magnetic
disk, retrieving or recording data

The organization
of a magnetic disk
CPU and Memory
 CPU can interact with main memory in two ways:
• It can write a byte/word to a given memory location.
• The previous bits that were in that location are
destroyed
• The new bits are saved for future use.
• It can read a byte/word from a given memory location.
• The CPU copies the bits stored at that location and
stores them in a CPU register
• The contents of the memory location are NOT
changed.
Main Memory Characteristics

 Very closely connected to the CPU.


 Contents are quickly and easily changed.
 Holds the programs and data that the
processor is actively working with.
 Interacts with the processor millions of times
per second.
 Nothing permanent is kept in main memory.
Secondary Storage Characteristics

 Connected to main memory through a bus and a


device controller.
 Contents are easily changed, but access is very
slow compared to main memory.
 Only occasionally interacts with CPU.
 Used for long-term storage of programs and data.
 Much larger than main memory (GBs vs. MBs).
Cache Memory

 But as CPU speeds became faster, the main


memory couldn’t provide the CPU with the
instructions at a fast enough rate.
 So even faster memory ( cache memory) is now
placed between the CPU and main memory to
provide the instructions at an quicker rate to the
CPU.
Cache Memory

When an instruction or data is accessed from main memory, it is placed in


the cache. Second and subsequent use of the same instruction/data will
then be faster, since it is accessed directly from the cache.
Primary and Secondary
Cache Memory
 Most modern CPUs now have a cache memory (L1),
on the same silicon wafer as the CPU, to provide
the CPU with instructions at the same clock speed
as the CPU.

 An additional off-the-chip secondary cache (L2) may


also interact with the CPU at a slower speed.
L1 and L2 Cache Memory
Computer Memory Hierarchy

44
Program Instructions

 Programs instructions are stored in secondary storage


(hard disks, CD-ROM, DVD).
 To process data, the CPU requires a working area
 Uses Main Memory
 Also called: RAM (random access memory),
primary storage, and internal memory.
 Before a program is run, instructions must first be copied
from the slow secondary storage into fast main memory
 Provides the CPU with fast access to instructions to
execute.
Instructions
 An instruction is a sequence of bits.
 A simple instruction format may consist of an operation
code (op code) and an address or operands.

Op Code Operands / Address

 Instructions tell the computer’s CPU what to do.


Instructions
 The operation code specifies the operation the
computer is to carry out (add, compare, etc)
 The operand/address area can store an operand or an
address
• An operand is a specific value or a register number
• An address allows the instruction to refer to a location
in main memory

 The CPU runs each instruction in the program, starting


with instruction 0, using the fetch-decode-execute
cycle.
Fetch-Decode-Execute Cycle
 During the fetch part, the CPU fetches the next
instruction from the address contained in the Program
Counter and places the instruction in the Instruction
Register.
• When a program starts, the program counter contains 0, so the
instruction at address 0 is fetched.

 As soon as an instruction is fetched, the CPU adds 1


word to the contents of the Program Counter, so that it
will contain the address of the next sequential
instruction.
Fetch-Decode-Execute Cycle

 The decode unit within the CPU deciphers the


instruction in the Instruction Register and determines
what operations need to be done and what type of
operands will be used (decode part).
 During the execution part, the specified operation is
performed (add, compare, etc).
 After execution of the instruction has been completed
the cycle starts all over again (unless the instruction
terminates the program).
Instruction Cycle
 Two steps:
• Fetch
• Execute
Fetch Cycle
 Program Counter (PC) holds address of next instruction to fetch
 Processor fetches instruction from memory location pointed to by
PC
 Increment PC
• Unless told otherwise
 Instruction loaded into Instruction Register (IR)
 Processor interprets instruction and performs required actions
Execute Cycle
 Processor-memory
• data transfer between CPU and main memory
 Processor I/O
• Data transfer between CPU and I/O module
 Data processing
• Some arithmetic or logical operation on data
 Control
• Alteration of sequence of operations
• e.g. jump
 Combination of above
A Hypothetical Machine

53
Example of Program Execution
Interrupt Cycle
 Added to instruction cycle
 Processor checks for interrupt
• Indicated by an interrupt signal
 If no interrupt, fetch next instruction
 If interrupt pending:
• Suspend execution of current program
• Save context
• Set PC to start address of interrupt handler routine
• Process interrupt
• Restore context and continue interrupted program
Transfer of Control via Interrupts
Instruction Cycle with Interrupts
Access to Instructions

The hard disk is too slow to provide instructions to the CPU.


So programs are first loaded into main memory, which is much faster.
The CPU can then access the instructions more quickly.
User view of Computer Systems

Applications
e.g. Word, Netscape, etc

Operating System –
- the user interface

Software that
controls the
hardware devices
How Programs Are Run

 The operating system presents an interface to the user


(e.g. Windows Desktop)
 The user double clicks on an icon to run a program (e.g.
Microsoft Word)
• The operating system copies the program (or at least the first part of
it) from the hard disk into main memory
• The CPU runs the instructions in the program, and presents the
initial Word screen
 Within Word, the user uses the menu to open a document
• The application software (Word) asks the Operating System to open
the file.
• The Operating System communicates with the hardware to open the
file on the hard disk.
Different Types of Operating Systems

 Batch Operating System- Sequence of jobs in a program on a


computer without manual interventions.
 Time sharing operating System- allows many users to share
the computer resources.(Max utilization of the resources).
 Distributed operating System- Manages a group of different
computers and make appear to be a single computer.
 Network operating system- computers running in different
operating system can participate in common network (It is used
for security purpose).
 Real time operating system – meant applications to fix the
deadlines.
Batch Operating System –
 This type of operating system do not interact with the computer
directly.

 There is an operator which takes similar jobs having same


requirement and group them into batches.

 It is the responsibility of operator to sort the jobs with similar


needs.
 Advantages of Batch Operating System:
• Multiple users can share the batch systems
• The idle time batch system is very less
• It is easy to manage large work repeatedly in batch systems

 Disadvantages of Batch Operating System:


• The computer operators should be well known with batch
systems
• Batch systems are hard to debug
• It is sometime costly
• The other jobs will have to wait for an unknown time if any
job fails
Time-Sharing Operating Systems –
 Each task has given some time to execute, so that all the tasks work
smoothly.

 Each user gets time of CPU as they use single system. These
systems are also known as Multitasking Systems.

 The task can be from single user or from different users also.

 The time that each task gets to execute is called quantum. After this
time interval is over OS switches over to next task.
 Advantages of Time-Sharing OS:
• Each task gets an equal opportunity
• Less chances of duplication of software
• CPU idle time can be reduced

 Disadvantages of Time-Sharing OS:


• Reliability problem
• One must have to take care of security and integrity of user programs and
data
• Data communication problem
Distributed Operating System –

 Various autonomous interconnected computers communicate each


other using a shared communication network.

 Independent systems possess their own memory unit and CPU.


These are referred as loosely coupled systems or distributed
systems.
 Advantages of Distributed Operating System:
• Failure of one will not affect the other network communication, as all
systems are independent from each other
• Since resources are being shared, computation is highly fast and durable
• Load on host computer reduces
• These systems are easily scalable as many systems can be easily added to
the network
• Delay in data processing reduces

 Disadvantages of Distributed Operating System:


• Failure of the main network will stop the entire communication
• To establish distributed systems the language which are used are not well
defined yet
• These types of systems are not readily available as they are very expensive.
Not only that the underlying software is highly complex and not understood
well yet
Network Operating System –
 These systems runs on a server and provides the capability to
manage data, users, groups, security, applications, and other
networking functions.

 These type of operating systems allows shared access of files,


printers, security, applications, and other networking functions over
a small private network.

 One more important aspect of Network Operating Systems is that


all the users are well aware of the underlying configuration, of all
other users within the network, their individual connections etc.
and that’s why these computers are popularly known as tightly
coupled systems.
 Advantages of Network Operating System:
• Highly stable centralized servers
• New technologies and hardware up-gradation are easily
integrated to the system
• Server access are possible remotely from different locations
and types of systems

 Disadvantages of Network Operating System:


• Servers are costly
• User has to depend on central location for most operations
• Maintenance and updates are required regularly
Real-Time Operating System –
 These types of OSs serves the real-time systems. The time interval
required to process and respond to inputs is very small. This time
interval is called response time.
 Real-time systems are used when there are time requirements are
very strict like missile systems, air traffic control systems, robots
etc.
Interrupts
Basic Definition:

Interrupts are signals sent to the CPU by external devices, normally I/O
devices. They tell the CPU to stop its current activities and execute the
appropriate part of the operating system.

Types of Interrupts

•Hardware
•Software
•TRAP
Operating-System Operations
• Operating system is an Interrupt Driven System

• If there are no processes to execute, no I/O devices to


service, and no users to whom to respond, an operating
system will sit quietly, waiting for something to happen.

• A form of interrupt is a trap (or an exception), which is a


software-generated interrupt caused either by an error
(for example, division by zero or invalid memory access)
or by a specific request from a user program that an
operating-system service be performed by executing a
special operation called a system call

77
Multiprogramming and Multitasking
One of the most important aspects of operating systems is the ability
to run multiple programs, as a single program cannot, in general,
keep either the CPU or the I/O devices busy at all times.

Multiprogramming increases CPU utilization, as well as keeping


users satisfied, by organizing programs so that the CPU always has
one to execute. In a multiprogrammed system, a program in
execution is termed a process.

Multitasking is a logical extension of multiprogramming. In


multitasking systems, the CPU executes multiple processes by
switching among them, but the switches occur frequently, providing
the user with a fast response time.

78
Memory Layout for Multiprogrammed System
Modes of Operation
 A mechanism that allows the OS to protect itself and other
system components
 Two modes:
• User mode
• Kernel mode
• A bit, called the mode bit, is added to the hardware of the computer to
indicate the current mode: kernel (0) or user (1).
 Mode bit (0 or 1) provided by hardware
• Provides ability to distinguish when system is running user code or
kernel code
• Some instructions designated as privileged, only executable in kernel
mode
• Systems call by a user asking the OS to perform some function
changes from user mode to kernel mode.
• Return from a system call resets the mode to user mode.
Transition from User to Kernel Mode

At system boot time, the hardware starts in kernel mode. The operating
system is then loaded and starts user applications in user mode.
Whenever a trap or interrupt occurs, the hardware switches from user
mode to kernel mode (that is, changes the state of the mode bit to 0).
Thus, whenever the operating system gains control of the computer, it is
in kernel mode. The system always switches to user mode (by setting the
mode bit to 1) before passing control to a user program
Timer
To prevent process to be in infinite loop (process hogging resources),
a timer is used, which is a hardware device.

 Timer is a counter that is decremented by the physical clock.


 Timer is set to interrupt the computer after some time period
 Operating system sets the counter (privileged instruction)
 When counter reaches the value zero, and interrupt is generated.
 The OS sets up the value of the counter before scheduling a
process to regain control or terminate program that exceeds allotted
time
Functions of Operating System

83
Process Management Activities
The operating system is responsible for the following activities in
connection with process management:

 Creating and deleting both user and system


processes
 Suspending and resuming processes
 Providing mechanisms for process synchronization
 Providing mechanisms for process communication
 Providing mechanisms for deadlock handling
Memory Management
 To execute a program all (or part) of the instructions
must be in memory
 All (or part) of the data that is needed by the program must be in
memory.
 Memory management determines what is in memory and
when
• Optimizing CPU utilization and computer response to users
 Memory management activities
• Keeping track of which parts of memory are currently being used
and by whom
• Deciding which processes (or parts thereof) and data to move into
and out of memory
• Allocating and deallocating memory space as needed
Storage Management
 OS provides uniform, logical view of information storage
 Abstracts physical properties to logical storage unit - file
 Files are stored in a number of different storage medium.
• Disk
• Flash Memory
• Tape
 Each medium is controlled by device drivers (i.e., disk
drive, tape drive)
• Varying properties include access speed, capacity, data-transfer rate,
access method (sequential or random)
File System Management
 Files usually organized into directories
 Access control on most systems to determine who can
access what
 OS activities include
• Creating and deleting files and directories
• Primitives to manipulate files and directories
• Mapping files onto secondary storage
• Backup files onto stable (non-volatile) storage media
Caching
 Important principle, performed at many levels in a computer (in
hardware, operating system, software)
 Information in use copied from slower to faster storage temporarily
 Faster storage (cache) checked first to determine if
information is there
• If it is, information used directly from the cache (fast)
• If not, data copied to cache and used there
 Cache are smaller (size-wise) than storage being cached
• Cache management important design problem
• Cache size and replacement policy
I/O Subsystem
 One purpose of an operating system is to hide
peculiarities of hardware devices from the user
 I/O subsystem responsible for
• Memory management of I/O including buffering (storing
data temporarily while it is being transferred), caching
(storing parts of data in faster storage for performance),
spooling (the overlapping of output of one job with input
of other jobs)
• General device-driver interface
Protection and Security
 Protection – A mechanism for controlling access of processes (or users)
to resources defined by the OS
 Security – A defense of the system against internal and
external attacks
• Huge range, including denial-of-service, worms, viruses, identity theft, theft of
service
 Systems generally first distinguish among users, to determine
who can do what
• User identities (user IDs, security IDs) include name and associated
number, one per user
• User ID is associated with all files and processes of that user to
determine access control
• Group identifier (group ID) allows set of users to be defined and
controls managed, then also associated with each process, file
• Privilege escalation allows user to change to effective ID with more
rights
Operating System Design
and Implementation
Operating System Structure

 Various ways to structure an operating


system:
• Monolithic structure
• Layered – An abstraction
• Microkernel – Mach
• Modules- Solaris
Monolithic Structures
 A Monolithic kernel is an OS architecture where the entire
operating system (which includes the device drivers, file
system, and the application IPC) is working in kernel space.

 Examples of operating systems that use a monolithic kernel


are - Linux, BSDs (FreeBSD, OpenBSD, NetBSD), Solaris,
DOS, Microsoft Windows (95,98,Me) etc.
Advantages of Monolithic Kernel –

One of the major advantage of having monolithic kernel is that it provides CPU
scheduling, memory management, file management and other operating system
functions through system calls.
The other one is that it is a single large process running entirely in a single address
space.
It is a single static binary file. Example of some Monolithic Kernel based OSs are:
Unix, Linux.

Disadvantages of Monolithic Kernel –

One of the major disadvantage of monolithic kernel is that, if anyone service fails
it leads to entire system failure.
If user has to add any new service. User needs to modify entire operating system.
Modularity Approach
 The monolithic approach results in a situation where changes to one part of
the system can have wide-ranging effects to other parts.

 Alternatively, we could design system where the operating system is divided


into separate, smaller components that have specific and limited
functionality. The sum of all these components comprises the kernel.

 The advantage of this modular approach is that changes in one component


only affect that component, and no others, allowing system implementers
more freedom when changing the inner workings of the system and in
creating modular operating systems.
Layered Approach
 A system can be made modular in many ways. One method is the
layered approach, in which the operating system is broken into a
number of layers (levels). The bottom layer (layer 0) is the hardware; the
highest (layer N) is the user interface.

Layered Structure is a type of system structure


in which the different services of the operating
system are split into various layers, where each
layer has a specific well-defined task to perform.
It was created to improve the pre-existing
structures like the Monolithic structure ( UNIX )
and the Simple structure ( MS-DOS ).

Example – The Windows NT operating system


uses this layered approach as a part of it.
Layered Approach (Cont.)
 A typical operating-system layer -- say, layer M -- consists of data structures
and a set of routines that can be invoked by higher-level layers. Layer M in
turn, can invoke operations on lower-level layers.
 Each layer is implemented only with operations provided by lower-level
layers. A layer does not need to know how these operations are implemented;
it needs to know only what these operations do.
 The main advantage of the layered approach is simplicity of construction and
debugging. The layers are selected so that each uses functions (operations)
and services of only lower-level layers.
 This approach simplifies debugging and system verification. The first layer
can be debugged without any concern for the rest of the system. Once the
first layer is debugged, its correct functioning can be assumed while the
second layer is debugged, and so on. If an error is found during the
debugging of a particular layer, the error must be on that layer.
Microkernel System Structure
 Moves as much from the kernel into user space
 Mach example of microkernel
• Mac OS X kernel (Darwin) partly based on Mach
 Communication takes place between user modules using
message passing
 Benefits:
• Easier to extend a microkernel
• Easier to port the operating system to new architectures
• More reliable (less code is running in kernel mode)
• More secure
 Detriments:
• Performance overhead of user space to kernel space
communication
Microkernel System Structure

Application File Device user


Program System Driver mode

messages messages

Interprocess memory CPU kernel


Communication managment scheduling mode

microkernel

hardware
Modules
 Most modern operating systems implement kernel modules
• Uses object-oriented approach
• Each core component is separate
• Each talks to the others over known interfaces
• Each is loadable as needed within the kernel

 Overall, similar to layers but with more flexible


Solaris Modular Approach
Virtual Machines
 A virtual machine takes the layered approach to its logical
conclusion. It treats hardware and the operating system
kernel as though they were all hardware.

 A virtual machine provides an interface identical to the


underlying bare hardware.

 The operating system host creates the illusion that a process


has its own processor and (virtual memory).

 Each guest provided with a (virtual) copy of underlying


computer.
Virtual Machines History and Benefits

 First appeared commercially in IBM mainframes in 1972


 Fundamentally, multiple execution environments (different
operating systems) can share the same hardware
 Protect from each other
 Some sharing of file can be permitted, controlled
 Commutate with each other, other physical systems via
networking
 Useful for development, testing
 Consolidation of many low-resource use systems onto
fewer busier systems
Virtual Machines (Cont.)

(a) Nonvirtual machine (b) virtual machine


The BIOS/MBR Boot Process
 Booting may be defined as process of loading the operating system into
memory. The booting process starts from the moment when we power on
computer and continues till moment, computer is ready for use.
 Regardless of the computer or operating system, desktop PCs and laptops all
power on and start up using one of two ways:
 The traditional BIOS-MBR method
 The newer UEFI-GPT (Unified Extensive Firmware Interface with Guide
partition table) method, used by the latest versions of Windows, Linux,
and Mac OS X on newer PCs, laptops, and tablets.

106
Components of the Boot Process
 The BIOS
 The BIOS is the lowest level of software that interfaces with the hardware as a
whole.
 The BIOS code is baked into the motherboard of your PC, usually stored on
what is called an EEPROM and is considerably hardware-specific.
 Once your PC has been powered on, the BIOS begins its work as part of the
POST (Power-On Self Test) process.
 During the POST procedure, quick tests are conducted where possible, and
errors caused by incompatible hardware, disconnected devices, or failing
components are often caught.
 BIOS Boot Handoff: Provide option to go in boot setting.
 In last, BIOS will load a very small program from the first hard disk to the
memory and tell the CPU to execute its contents. This hard drive is known as
“the boot device,” “startup disk,” or “drive 0” and can usually be picked or set in
the BIOS setup.
 The BIOS loads the first 512 bytes from the hard drive of the selected boot
device – these 512 bytes are what is commonly known as the MBR, or the
107
Master Boot Record.
Components of the Boot Process
 The Master Boot Record (MBR)
 The Master Boot Record (MBR) is the information in the first sector of any
hard disk or diskette that identifies how and where an operating system is located
so that it can be boot (loaded) into the computer's main storage or random access
memory
 Size of MBR is 512 bytes
 The MBR also contains a very important bit of code known as the “bootstrap
code” which is called first stage boot loader. It look up the active partition from
the partition table, and load that code into the memory for execution by the CPU
as the next link in the boot chain.
 It loads the first sector of the active partition, meaning another 512 bytes

108
Components of the Boot Process
 The Partition Boot Sector
 The bootstrap code in the MBR will usually load a sequence of bytes from the start of the
active partition. The exact layout of a partition depends what filesystem the partition has
been created or formatted with, but generally looks something like in the figure
 The MBR loads the first 512 bytes of the active partition into the memory and instructs
the CPU to execute them.
 The very first (three) bytes of the partition bootsector contain a single JMP instruction,
telling the CPU to skip xx bytes ahead and execute the next stage of the bootloader from
there.
 The CPU follows the JMP instruction and seeks to the beginning of the bootstrap code
contained within the partition bootsector, and starts to execute. This bootstrap code is
called second stage bootloader.
 The second-stage bootloader looks up a file stored on the partition itself (as a regular file),
and tells the CPU to execute its contents to begin the final part of the boot process.

109
Components of the Boot Process
 The Bootloader
 The PC has been handed-off from the BIOS to the bootstrap code in the MBR
and from the MBR to the bootstrap code in the partition bootsector, and from
there to the executable boot files on the active partition.
 The actual logic involved in determining which operating system to load, where
to load it from, which parameters/options to pass on to it, and completing any
interactions with the user that might be available, the actual process of starting
the operating system begins.

110
Booting Process

111
112
UEFI-GPT Booting Process
 UEFI stands for Unified Extensible Firmware Interface.
 It does the same job as a BIOS, but with one basic difference: it
stores all data about initialization and startup in an .efi file, instead
of storing it on the firmware.
 This .efi file is stored on a special partition called EFI System
Partition (ESP) on the hard disk. This ESP partition also contains
the bootloader.
 UEFI was designed to overcome many limitations of the old BIOS,
including:

113
UEFI-GPT Booting Process
 UEFI supports drive sizes upto 9 zettabytes, whereas BIOS only
supports 2.2 terabytes.
 UEFI provides faster boot time.
 UEFI offers security like "Secure Boot", which prevents the
computer from booting from unauthorized/unsigned applications.
 UEFI runs in 32bit or 64bit mode, whereas BIOS runs in 16bit
mode. So UEFI is able to provide a GUI (navigation with mouse)
as opposed to BIOS which allows navigation only using the
keyboard.
 BIOS uses the Master Boot Record (MBR) to save information
about the hard drive data while UEFI uses the GUID partition table
(GPT). The major difference between the two is that MBR uses 32-
bit entries in its table which limits the total physical partitions to
only 4. 114
 UEFI supports larger HDDs and SDDs. UEFI’s theoretical size
limit for bootable drives is more than nine zettabytes, while BIOS
can only boot from drives 2.2 terabytes or smaller.
 With GPT, you can create theoretically unlimited partitions on the
hard disk, even though it is generally restricted to 128 partitions by
most Oses
 Unlike MBR that limits each partition to only 2TB in size, each
partition in GPT can hold up to 2^64 blocks in length (as it is using
64-bit), which is equivalent to 9.44ZB for a 512-byte block (1 ZB
is 1 billion terabytes). In Microsoft Windows, that size is limited to
256TB.

115
Why Choose UEFI?
 One reason to choose this over the more familiar BIOS is that Intel
no longer intends to support the “traditional” BIOS in 2020.
 UEFI provides the following functionality and advantages:
 Languages: BIOS is written in assembler, while UEFI is written in simpler C-
language.
 Drives: UEFI supports larger HDDs and SDDs. UEFI’s theoretical size limit for
bootable drives is more than nine zettabytes, while BIOS can only boot from
drives 2.2 terabytes or smaller.
 Boot time: In most cases, UEFI provides a faster booting time for the operating
system.
 Security: UEFI offers improved security features. “Secure Boot” prevents the
computer from booting from unsigned or unauthorized applications.
 Data processors: UEFI runs in 32-bit or 64-bit mode. BIOS runs only in 16-bit
mode and may utilize only 1 MB of executable memory.
 GUI: UEFI provides a more intuitive graphical user interface that you may
navigate with a mouse and keyboard, unlike BIOS. 116
Computer System Architecture
 Based on General Purpose Processors
• Single processor
• Multiprocessor
• Clustering
Single Processor System
 System have only one General Purpose CPU.

 But it can have number of *Special Purpose Processors*


• Eg. Microprocessor in keyboard
Device Controller
How a Modern Computer Works
A von Neumann architecture and a depiction of the interplay of all
components of a computer system.
Multiprocessor Processor System
 Such Systems have more than one GP CPU.
 Advantages:
• Throughput
 ?? N processors = N time Speedup ??
• Economy of scale
• Reliability (Eg. 10 systems)
• Graceful Degradation
• Fault Tolerant
 Graceful Degradation: The ability to continue providing services
proportional to the level of surviving hardware.

 Fault Tolerant: It is a mechanism to allow the failure to be


detected, diagnosed and if possible corrected.
• Eg. HP NonStop
 Multiprocessor systems
• Asymmetric
• Symmetric

 Memory access models


• UMA
• NUMA

 Multicore processors
Symmetric Multiprocessing Architecture
Multicore Systems
 Most CPU design now includes multiple computing cores on a
single chip. Such multiprocessor systems are termed multicore.
 Multicore systems can be more efficient than multiple chips
with single cores because:
• On-chip communication is faster than between-chip communication.
• One chip with multiple cores uses significantly less power than multiple
single-core chips, an important issue for laptops as well as mobile
devices.

 Note -- while multicore systems are multiprocessor systems, not


all multiprocessor systems are multicore.
A dual-core with two cores placed on the same chip
??

 Which one is better multiprocessor or multicore ?


Clustered Systems
Like multiprocessor systems, but multiple systems working together

 Usually sharing storage via a storage-area network (SAN)


 Provides a high-availability service which survives failures
• Asymmetric clustering has one machine in hot-standby mode
• Symmetric clustering has multiple nodes running applications,
monitoring each other
 Some clusters are for high-performance computing (HPC)
• Applications must be written to use parallelization
 Some have distributed lock manager (DLM) to avoid
conflicting operations
Clustered Systems

You might also like