You are on page 1of 157

CS0206-OPERATING SYSTEMS

SRM UNIVERSITY,RAMAPURAM

CS0206 OPERATING SYSTEMS


Prerequisite CS0201, CS0203

L
3

T
0

P
0

PURPOSE
Every computer professional should have a basic understanding of how an operating
system controls the computing resources and provide services to the users. This course
provides an introduction to the operating system functions, design and implementation. It
serves as strong foundation for other courses like networks, compiler design, data base
systems.
INSTRUCTIONAL OBJECTIVES
The students learn about:
1. Structure and functions of OS
2.
Process scheduling, Deadlocks
3.
Device management
4.
Memory management
5.
File systems
UNIT 1 INTRODUCTION
9
Computer system overview-basic elements, Instruction execution, Interrupts, memory
hierarchy, I/O communication techniques, operating system overview-objectives and
functions, Evolution of OS Microsoft windows overview.
UNIT 2 PROCESSES
9
Process description and control - process states, process description, process control;
Processes and Threads, Symmetric Multiprocessing and microkernels. Windows Thread
and SMP management. Case studies-UNIX, SOLARIS thread management
UNIT 3 CONCURRENCY AND SCHEDULING
9
Principles of concurrency - mutual exclusion, semaphores, monitors, Readers/Writers
problem; Deadlocks prevention- avoidance detection .Scheduling : Types of
scheduling scheduling algorithms. Case studies- UNIX scheduling.
UNIT 4 MEMORY
9
Memory management requirements, partitioning, paging, and segmentation; Virtual
memory - Hardware and control structures, operating system software, Linux memory
management, case studies- WINDOWS memory management, UNIX and SOLARIS
Memory management
UNIT 5 INPUT/OUTPUT AND FILE SYSTEMS
9
I/O management and disk scheduling I/O devices, organization of I/O functions; OS
design issues, I/O buffering, disk scheduling, Disk cache, File management
organization, directories, file sharing, record blocking, secondary storage management;
case studies-LINUX I/O, UNIX File management.
TOTAL 45

DEPARTMENT OF COMPUTER SCIENCE AND ENGINEERING

Page 1 of 157

C
3

CS0206-OPERATING SYSTEMS

SRM UNIVERSITY,RAMAPURAM

Notes for all five units Prepared by Mrs. Antony Vigil A.P[O.G]/SRMU/RMP

UNIT 1
INTRODUCTION
Computer system overview-Basic elements, Instruction execution, Interrupts, Memory
hierarchy, I/O communication techniques, Operating system overview-Objectives and
functions, Evolution of OS Microsoft windows overview.

COMPUTER SYSTEM OVERVIEW


A program that acts as an intermediary between a user of a computer and the
computer hardware is Operating System.

Operating system goals:

Execute user programs and make solving user problems easier.

Make the computer system convenient to use.

Use the computer hardware in an efficient manner

Computer System Components:

1. Hardware provides basic computing resources (CPU, memory, I/O devices).


2. Operating system controls and coordinates the use of the hardware among the
various application programs for the various users.
3. Applications programs define the ways in which the system resources are used
to solve the computing problems of the users (compilers, database systems, video
games, business programs).
4. Users (people, machines, other computers).

DEPARTMENT OF COMPUTER SCIENCE AND ENGINEERING

Page 2 of 157

CS0206-OPERATING SYSTEMS

SRM UNIVERSITY,RAMAPURAM

Abstract View of System Components:

Operating System Definitions:


Resource allocator manages and allocates resources.

Control program controls the execution of user programs and operations of


I/O devices.

Kernel the one program running at all times (all else being application
programs).

Simple Batch Systems:

Hire an operator

User operator

Add a card reader

Reduce setup time by batching similar jobs

Automatic job sequencing

Automatically transfers control from one job to another.

Resident monitor

Initial control in monitor

When job completes control transfers back to monitor

DEPARTMENT OF COMPUTER SCIENCE AND ENGINEERING

Page 3 of 157

CS0206-OPERATING SYSTEMS

SRM UNIVERSITY,RAMAPURAM

Memory Layout for a Simple Batch System:

Basic Elements
Computer =Processor + Memory + I/O modules

Processor: Controls the operation of the computer and performs its data
processing functions. When there is only on processor, it is often
referred to as CPU.

Main Memory: Stores data and programs.

I/O modules: Move data between the computer and its external environments
(e.g., disk drive, network, terminals).

System interconnection: Some structures and mechanisms that provide for


communication among processors, main memory, and I/O modules

DEPARTMENT OF COMPUTER SCIENCE AND ENGINEERING

Page 4 of 157

CS0206-OPERATING SYSTEMS

SRM UNIVERSITY,RAMAPURAM

DEPARTMENT OF COMPUTER SCIENCE AND ENGINEERING

Page 5 of 157

CS0206-OPERATING SYSTEMS

SRM UNIVERSITY,RAMAPURAM

Instruction Execution

Execute cycle involves data transfer between processor and memory (or an I/O
module), data processing or changing control flow. Most processors use pipeline
instruction execution and allow Direct Memory Access

The processor fetches the instruction from memory


o Program counter (PC) holds address of the instruction to be fetched next
o Fetched instruction is placed in the instruction register (IR)
o Program counter is incremented after each fetch

Processor then executes instruction in the IR

Categories of instructions:

Processor-memory
o Transfer data between processor and memory

Processor-I/O
o Data transferred to or from a peripheral device

Data processing
o Arithmetic or logic operation on data

Control
o Alter sequence of execution

DEPARTMENT OF COMPUTER SCIENCE AND ENGINEERING

Page 6 of 157

CS0206-OPERATING SYSTEMS

SRM UNIVERSITY,RAMAPURAM

Interrupts
Interrupt (or exception):

Signal sent to processor


e.g. attempt to divide by zero
e.g. illegal attempt to access address
e.g. execution of trap instruction (to make system call)
e.g. I/O transfer has completed

Source and priority of interrupt are recorded

All computers provide a mechanism by which other modules may interrupt the
normal processing of the processor.

The Classes of Interrupts:

Program: Generated by some condition that occurs as a result of an instruction.

Timer: Generated by a timer within the processor.

I/O: Generated by an I/O controller.

Hardware failure: Generated by a failure such as power failure.

Interrupt is an interruption of the normal sequence of execution. After interrupt is


completed, the normal program execution is resumed. Interrupts are provided primarily to
improve processing efficiency.
E.g., Avoid CPU waiting for slow I/O devices.CPU can continue to execute other
instructions

DEPARTMENT OF COMPUTER SCIENCE AND ENGINEERING

Page 7 of 157

CS0206-OPERATING SYSTEMS

SRM UNIVERSITY,RAMAPURAM

DEPARTMENT OF COMPUTER SCIENCE AND ENGINEERING

Page 8 of 157

CS0206-OPERATING SYSTEMS

SRM UNIVERSITY,RAMAPURAM

Interrupt Cycle:
During the interrupt cycle, the processor checks to see if any interrupts have
occurred, indicated by the presence of an interrupt signal. If interrupt is pending, the
processor suspends execution of the current program and executes an Interrupt Handling
Routine. IHR determine the nature of interrupt and performs whatever actions are needed.

DEPARTMENT OF COMPUTER SCIENCE AND ENGINEERING

Page 9 of 157

CS0206-OPERATING SYSTEMS

Program Timing: Short I/O Wait

SRM UNIVERSITY,RAMAPURAM

Program Timing: Long I/O Wait

DEPARTMENT OF COMPUTER SCIENCE AND ENGINEERING

Page 10 of 157

CS0206-OPERATING SYSTEMS

SRM UNIVERSITY,RAMAPURAM

Interrupt Processing:

Multiple Interrupts:

Multiple interrupts can occur close to one another.

To disable interrupt when executing the interrupt handler routine.


o New interrupts will have to remain pending until the completion of
interrupt
o Handler routine.
o Does not consider priority of interrupts and time-critical needs.

To define priorities for interrupts.


o High-priority interrupts can interrupt the execution of IHR of low-priority
interrupts.

DEPARTMENT OF COMPUTER SCIENCE AND ENGINEERING

Page 11 of 157

CS0206-OPERATING SYSTEMS

SRM UNIVERSITY,RAMAPURAM

DEPARTMENT OF COMPUTER SCIENCE AND ENGINEERING

Page 12 of 157

CS0206-OPERATING SYSTEMS

SRM UNIVERSITY,RAMAPURAM

Interrupt handling:

Contents of PC and PSW are stored automatically.

Interrupt service routine (ISR) is executed in supervisor mode.

ISR may store contents of other registers.

ISR may call other operating system routines.

Eventually contents of registers may be restored and execution continued in user


mode from point of interruption

Multiprogramming:

More than one process is active on a single processor

There is a ready queue of processes waiting for the processor

A process must wait after making an I/O request or after a timer interrupt

DEPARTMENT OF COMPUTER SCIENCE AND ENGINEERING

Page 13 of 157

CS0206-OPERATING SYSTEMS

SRM UNIVERSITY,RAMAPURAM

The Memory Hierarchy


Advantage:

Faster access time


Greater capacity

Going down the hierarchy


o Decreasing cost per bit
o Increasing capacity
o Increasing access time
o Decreasing frequency of access of the memory by the processor

Storage devices can be put in order of increasing capacity, namely,


o registers, cache memory, main memory, hard disk, tape
o access time also increases
DEPARTMENT OF COMPUTER SCIENCE AND ENGINEERING

Page 14 of 157

CS0206-OPERATING SYSTEMS

SRM UNIVERSITY,RAMAPURAM

o cost per bit decreases


o first three are volatile caching
o copying information into faster device
Secondary memory:

Managed by the operating system


Nonvolatile
Auxiliary memory
Used to store program and data files

Cache Memory:

Processors can execute instructions faster than instructions (and data) can be
fetched from main memory

Cache memory provides a solution which relies on locality of reference and is


invisible to OS

DEPARTMENT OF COMPUTER SCIENCE AND ENGINEERING

Page 15 of 157

CS0206-OPERATING SYSTEMS

SRM UNIVERSITY,RAMAPURAM

I/O module

Is an interface between the bus and a device

Hides the complexity of the device from the processor

The processor issues commands to an I/O module

The I/O module controls the device to perform the requested action

It also buffers data and maintains a status register that the processor can read

Techniques for performing I/O:

(i) Programmed I/O


(ii) Interrupt-driven I/O
(iii)Direct memory access (DMA)

(i) Programmed I/O

I/O module performs the requested action

No interrupts occur

Module sets appropriate bits in the I/O status register

Processor checks status until operation is complete

Processor does a busy-wait for each character

DEPARTMENT OF COMPUTER SCIENCE AND ENGINEERING

Page 16 of 157

CS0206-OPERATING SYSTEMS

SRM UNIVERSITY,RAMAPURAM

DEPARTMENT OF COMPUTER SCIENCE AND ENGINEERING

Page 17 of 157

CS0206-OPERATING SYSTEMS

SRM UNIVERSITY,RAMAPURAM

(ii)Interrupt-driven I/O

Processor is interrupted when I/O module ready to exchange data

Processor saves context of program executing and begins executing interrupt


handler

No needless waiting

Involves much processor overhead because . . .


o Every character read or written passes through the processor
o One interrupt for each character

DEPARTMENT OF COMPUTER SCIENCE AND ENGINEERING

Page 18 of 157

CS0206-OPERATING SYSTEMS

SRM UNIVERSITY,RAMAPURAM

(iii)Direct memory access (DMA)

Transfers an entire block of data directly to or from memory

An interrupt is sent when the transfer of the entire block is complete

One interrupt per block of data

A DMA controller transfers data directly between a device (typically a disk) and
memory.
o The data does not pass through the processor

The I/O module has authority to read from or write to memory


o This relieves the processor responsibility for the exchange

The DMA competes with the processor for memory access


o This is known as cycle stealing
o Although cycle stealing halts the processor, this is not an interrupt

DEPARTMENT OF COMPUTER SCIENCE AND ENGINEERING

Page 19 of 157

CS0206-OPERATING SYSTEMS

SRM UNIVERSITY,RAMAPURAM

Operating System Overview: Objectives & Functions


An operating system (OS) 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.

Convenience
o Makes the computer more convenient to use.

Efficiency
o Allows computer system resources to be used in an efficient manner

Ability to evolve
o Permit effective development, testing, and introduction of new system
functions without interfering with service

DEPARTMENT OF COMPUTER SCIENCE AND ENGINEERING

Page 20 of 157

CS0206-OPERATING SYSTEMS

SRM UNIVERSITY,RAMAPURAM

Convenience:
Services provided by the operating system:

Program development
o Editors and debuggers

Program execution

Access to I/O devices

Controlled access to files

System access
o Login and passwords

Error detection and response


o Internal and external hardware errors
o Memory error
o Device failure
o Software errors
o Arithmetic overflow
o Access forbidden memory locations
o Operating system cannot grant a request made by an application

Accounting
o Collect usage statistics
o Monitor performance
o This information can be used . . .
o To anticipate future enhancements
o For billing purposes

Efficiency:

The OS promotes efficiency by managing resources


o Processor(s)
o Memory
o Devices
o Files

DEPARTMENT OF COMPUTER SCIENCE AND ENGINEERING

Page 21 of 157

CS0206-OPERATING SYSTEMS

SRM UNIVERSITY,RAMAPURAM

The OS functions same way as ordinary computer software


o It is program that is executed
o The OS frequently relinquishes control of the processor and relies on the
processor to regain control

Ability to evolve:
A major OS should be able to evolve over time in response to . . .

Hardware upgrades and new types of hardware such as


o Paging hardware for virtual memory
o Multiple processors

New services such as


o Overlapping windows
o Client / server computing

Errors in the OS

The Operating System as Resource manager:

A computer is a set of resources for the movement, storage, and processing of


data and for the control of these functions. The operating system is responsible for
managing these resources. The operating system is nothing more than a computer
program.
The operating system functions in the same way as ordinary computer software;
that is, it is a program executed by the processor.
The operating system frequently relinquishes control and must depend on the
processor to allow it to regain control

Key difference between OS and other programs:

OS can direct processor in the use of other system resources and in the timing of
its execution of other program.
In order to do this, processor must cease executing OS and execute other
programs, i.e., OS relinquishes control of processor.

DEPARTMENT OF COMPUTER SCIENCE AND ENGINEERING

Page 22 of 157

CS0206-OPERATING SYSTEMS

SRM UNIVERSITY,RAMAPURAM

DEPARTMENT OF COMPUTER SCIENCE AND ENGINEERING

Page 23 of 157

CS0206-OPERATING SYSTEMS

SRM UNIVERSITY,RAMAPURAM

Evolution of operating systems


(i)Serial processing
(ii)Simple batch systems
(iii)Multiprogrammed batch systems
(iv)Time-sharing systems

(i) Serial processing:


Use card reader. No operating system.
2 main problems:
1. Manual scheduling of use
2. Setup Time

Loading the compiler

Loading the source program

Saving compiled program

Loading and linking object files

(ii)Simple batch systems:


Use a piece of software known as the monitor. So users no longer have direct
access to the machine
2 points of view:
Monitor point of view:

Monitor software controlled a sequence of jobs

Monitor must always in main memory and available for execution. That
portion is referred as Resident Memory.

The rest of the monitor consists of utilities and common functions.

DEPARTMENT OF COMPUTER SCIENCE AND ENGINEERING

Page 24 of 157

CS0206-OPERATING SYSTEMS

SRM UNIVERSITY,RAMAPURAM

Interrupt Processing

Monitor

Device Drivers
Job Sequencing
Control

Boundary

Language

Interpreter
User Program Area

Fig: Memory Layout for a Resident Memory


Processor point of view:

Processor is executing instructions from the portion of main memory


containing the monitor

Only one user job could run at a time

Processor idle waiting for I/O

But idle time between jobs and within jobs eliminated

Job Control Language (JCL):

A special type of programming language

Provides commands to the monitor


o Identifies new jobs
o Specifies what compiler to use
o Specifies which object files to load and link
o Specifies what data to use

Example of JCL cards (with / /) in a deck

DEPARTMENT OF COMPUTER SCIENCE AND ENGINEERING

Page 25 of 157

CS0206-OPERATING SYSTEMS

SRM UNIVERSITY,RAMAPURAM

/ / JOB
/ / FORT

< source program


cards >

/ / LOAD
/ / RUN

< data cards >

/ / END

Protection
After an //END card
or an error, the
monitor flushes
cards until the next
//JOB card

Hardware support for simple batch:

Memory protection
o Does not allow the memory area containing the monitor to be altered
by a job

Timer
o Prevents a job from monopolizing the system

Privileged instructions
o Certain machine level instructions can only be executed by the monitor
l E.g. - I/O instructions (a program should not read cards of next
job)
l Note:

a program requests that the monitor perform the

instruction

Interrupts
o Allow processor to do something else while waiting for I/O
o Early computer models did not have this capability

The need for memory protection and privileged instructions led to the concept
of processor modes

A bit in the PSW register toggles the processor between user mode and kernel
mode

DEPARTMENT OF COMPUTER SCIENCE AND ENGINEERING

Page 26 of 157

CS0206-OPERATING SYSTEMS

SRM UNIVERSITY,RAMAPURAM

Each user program executes in user mode


o Certain privileged instructions may not be executed
o Only the program area may be referenced

The monitor executes in kernel mode


o Privileged instructions may be executed
o Protected areas of memory may be accessed

(iii) Multiprogrammed batch systems:

Several jobs resident in memory simultaneously

Gives processor something to do while one job is waiting for I/O

DEPARTMENT OF COMPUTER SCIENCE AND ENGINEERING

Page 27 of 157

CS0206-OPERATING SYSTEMS

SRM UNIVERSITY,RAMAPURAM

DEPARTMENT OF COMPUTER SCIENCE AND ENGINEERING

Page 28 of 157

CS0206-OPERATING SYSTEMS

SRM UNIVERSITY,RAMAPURAM

(iv)Time-sharing systems

Adds an interactive computing capability to a multiprogrammed batch system


o Processors time is shared among multiple interactive users
o Multiple users simultaneously access the system through terminals

Essential for transaction processing systems

Example: Compatible Time-Sharing System (CTSS)


o First time-sharing system
o Developed at MIT in 1961 for the IBM 709

Problems:

Multiprogramming and time sharing led to the identification of new problems


o Memory protection
o File security
o Contention for resources
E.g. printers

DEPARTMENT OF COMPUTER SCIENCE AND ENGINEERING

Page 29 of 157

CS0206-OPERATING SYSTEMS

SRM UNIVERSITY,RAMAPURAM

Microsoft Windows Overview

Single-user multitasking OS that evolved out of MS-DOS

Modular structure for flexibility


o Any module can be removed, upgraded, or replaced without rewriting the
entire system

Executes on a variety of hardware platforms


o Pentium, Itanium, PowerPC, Alpha, etc.
o Provided by the Hardware Abstraction Layer (HAL)
o This isolates the operating system from platform-specific hardware
differences

Supports applications written for other operating systems


o This is provided by various environment subsystems

DEPARTMENT OF COMPUTER SCIENCE AND ENGINEERING

Page 30 of 157

CS0206-OPERATING SYSTEMS

SRM UNIVERSITY,RAMAPURAM

Windows organization

Kernel

Consists of the most used components


o Thread scheduling
o Process switching
o Interrupt handling
o SMP

Does not run in threads and is not preemptible

Executive

Contains base operating system services


o Memory management
o Process and thread management
o Security
o I/O
o Interprocess communication

Hardware abstraction layer(HAL)


Map between hardware commands and responses

Device Drivers

Windowing and Graphics system

User Mode Processes:

Special system support processes

Service Processes

Environment subsystems

User Applications

Windows client-server model:

Simplifies the Executive


o Possible to construct a variety of APIs

Improves reliability
o Each service runs on a separate process with its own partition of memory
o Clients cannot not directly access hardware

DEPARTMENT OF COMPUTER SCIENCE AND ENGINEERING

Page 31 of 157

CS0206-OPERATING SYSTEMS

SRM UNIVERSITY,RAMAPURAM

Provides a uniform means for applications to communicate via Local Procedure


Call (LPC)

Provides base for distributed computing

Windows threads and SMP:

Operating system routines can run on any available processor or simultaneously


on different processors

Multiple threads of execution within a single process may execute on different


processors simultaneously

Server processes may use multiple threads to process requests from multiple
processes simultaneously

Mechanisms provided to share data and resources between processes

Windows Objects:

Encapsulation

Object Class and instance

Inheritance

Polymorphism

DEPARTMENT OF COMPUTER SCIENCE AND ENGINEERING

Page 32 of 157

CS0206-OPERATING SYSTEMS

SRM UNIVERSITY,RAMAPURAM

UNIT 2
PROCESSES

Process description and control - Process states, Process description, Process control,
Processes and Threads, Symmetric Multiprocessing and Microkernels, Windows
Thread and SMP Management. Case studies-UNIX, SOLARIS thread management

Process description and control


Processes:

A program in execution

An instance of a program running on a computer

An entity that can be assigned to and executed on the computer

A process is comprised of:


o Program code (possibly shared)
o A set of data
o A number of attributes describing the state of the process

Process Control Block (PCB)

While the process is running it has a number of elements including


o Identifier
o State
o Priority
o Program counter
o Memory pointers
o Context data
o I/O status information
o Accounting information

DEPARTMENT OF COMPUTER SCIENCE AND ENGINEERING

Page 33 of 157

CS0206-OPERATING SYSTEMS

SRM UNIVERSITY,RAMAPURAM

Process States

The behavior of an individual process is shown by listing the sequence of


instructions that are executed

This list is called a Trace

Dispatcher is a small program which switches the processor from one process to
another

Each process runs to completion


Program Counter
8000

DEPARTMENT OF COMPUTER SCIENCE AND ENGINEERING

Page 34 of 157

CS0206-OPERATING SYSTEMS

SRM UNIVERSITY,RAMAPURAM

Two-State Process Model:

Process may be in one of two states


o Running
o Not-running

DEPARTMENT OF COMPUTER SCIENCE AND ENGINEERING

Page 35 of 157

CS0206-OPERATING SYSTEMS

SRM UNIVERSITY,RAMAPURAM

The creation and Termination of Processes:


Creation

Termination

New batch job

Normal Completion

Interactive Login

Memory unavailable

Created by OS to provide a service

Protection error

Spawned by existing process

Operator or OS Intervention

Process Creation

The OS builds a data structure to manage the process

Traditionally, the OS created all processes


o But it can be useful to let a running process create another

This action is called process spawning


o Parent Process is the original, creating, process
o Child Process is the new process

Reasons for Process Creation:

New Batch job

Interactive Logon

Created by OS to provide a service

Spawned by existing process

Process Termination

There must be some way that a process can indicate completion.

This indication may be:


o A HALT instruction generating an interrupt alert to the OS.
o A user action (e.g. log off, quitting an application)
o A fault or error
o Parent process Terminating

DEPARTMENT OF COMPUTER SCIENCE AND ENGINEERING

Page 36 of 157

CS0206-OPERATING SYSTEMS

SRM UNIVERSITY,RAMAPURAM

DEPARTMENT OF COMPUTER SCIENCE AND ENGINEERING

Page 37 of 157

CS0206-OPERATING SYSTEMS

SRM UNIVERSITY,RAMAPURAM

Five-State Process Model

DEPARTMENT OF COMPUTER SCIENCE AND ENGINEERING

Page 38 of 157

CS0206-OPERATING SYSTEMS

SRM UNIVERSITY,RAMAPURAM

Suspended Processes:

Processor is faster than I/O so all processes could be waiting for I/O
o Swap these processes to disk to free up more memory and use processor
on more processes

Blocked state becomes suspend state when swapped to disk

Two new states


o Blocked/Suspend
o Ready/Suspend

DEPARTMENT OF COMPUTER SCIENCE AND ENGINEERING

Page 39 of 157

CS0206-OPERATING SYSTEMS

SRM UNIVERSITY,RAMAPURAM

DEPARTMENT OF COMPUTER SCIENCE AND ENGINEERING

Page 40 of 157

CS0206-OPERATING SYSTEMS

SRM UNIVERSITY,RAMAPURAM

Reason for Process Suspension


Reason

Comment

Swapping

The OS needs to release sufficient main memory to bring in a


process that is ready to execute.

Other OS Reason

OS suspects process of causing a problem.

Interactive User Request

e.g. debugging or in connection with the use of a resource.

Timing

A process may be executed periodically (e.g., an accounting or


system monitoring process) and may be suspended while waiting
for the next time.

Parent Process Request

A parent process may wish to suspend execution of a descendent


to examine or modify the suspended process, or to coordinate the
activity of various descendants.

Process Description
Processes and Resources:

DEPARTMENT OF COMPUTER SCIENCE AND ENGINEERING

Page 41 of 157

CS0206-OPERATING SYSTEMS

SRM UNIVERSITY,RAMAPURAM

Operating System Control Structures:

For the OS is to manage processes and resources, it must have information about
the current status of each process and resource.

Tables are constructed for each entity the operating system manages

OS Control Tables

Memory Tables

Memory tables are used to keep track of both main and secondary memory.

Must include this information:


o Allocation of main memory to processes
o Allocation of secondary memory to processes
o Protection attributes for access to shared memory regions
o Information needed to manage virtual memory
DEPARTMENT OF COMPUTER SCIENCE AND ENGINEERING

Page 42 of 157

CS0206-OPERATING SYSTEMS

SRM UNIVERSITY,RAMAPURAM

I/O Tables

Used by the OS to manage the I/O devices and channels of the computer.

The OS needs to know


o Whether the I/O device is available or assigned
o The status of I/O operation
o The location in main memory being used as the source or destination of
the I/O transfer

File Tables

These tables provide information about:


o Existence of files
o Location on secondary memory
o Current Status
o Other attributes.

Sometimes this information is maintained by a file management system

Process Tables

To manage processes the OS needs to know details of the processes


o Current state
o Process ID
o Location in memory

Process control block


o Process image is the collection of program. Data, stack, and attributes

Process Attributes

We can group the process control block information into three general categories:
o Process identification
o Processor state information
o Process control information

DEPARTMENT OF COMPUTER SCIENCE AND ENGINEERING

Page 43 of 157

CS0206-OPERATING SYSTEMS

SRM UNIVERSITY,RAMAPURAM

Process Identification

Each process is assigned a unique numeric identifier.

Many of the other tables controlled by the OS may use process identifiers to
cross-reference process tables

Processor State Information

This consists of the contents of processor registers.


o User-visible registers
o Control and status registers
o Stack pointers

Program status word (PSW)


o contains status information
o Example: the EFLAGS register on Pentium processors

Pentium II EFLAGS Register:

DEPARTMENT OF COMPUTER SCIENCE AND ENGINEERING

Page 44 of 157

CS0206-OPERATING SYSTEMS

SRM UNIVERSITY,RAMAPURAM

Structure of Process Images in Virtual Memory:

DEPARTMENT OF COMPUTER SCIENCE AND ENGINEERING

Page 45 of 157

CS0206-OPERATING SYSTEMS

SRM UNIVERSITY,RAMAPURAM

Process Control
Modes of Execution:
Most processors support at least two modes of execution

User mode
o Less-privileged mode
o User programs typically execute in this mode

System mode
o More-privileged mode
o Kernel of the operating system

Process Creation:

Once the OS decides to create a new process it:


o Assigns a unique process identifier
o Allocates space for the process
o Initializes process control block
o Sets up appropriate linkages
o Creates or expand other data structures

Process switch is switch the process state between the status like read, blocked,
suspend. Mode switch is the switch the process privilege between the mode like use
mode, kernel mode. Generally a mode switch is considered less expensive compared to a
process switch.

Process Switching:

Several design issues are raised regarding process switching


o What events trigger a process switch?
o We must distinguish between mode switching and process switching.
o What must the OS do to the various data structures under its control to
achieve a process switch?

DEPARTMENT OF COMPUTER SCIENCE AND ENGINEERING

Page 46 of 157

CS0206-OPERATING SYSTEMS

SRM UNIVERSITY,RAMAPURAM

When to switch processes?


A process switch may occur any time that the OS has gained control from the
currently running process. Possible events giving OS control are:
Mechanism

Cause

Use

Interrupt

External to the execution of

Reaction to an asynchronous

the current instruction

external event

Trap

Associated with the execution Handling of an error or an

Supervisor call

of the current instruction

exception condition

Explicit request

Call to an operating system


function

The steps in a process switch are:


1. Save context of processor including program counter and other registers
2. Update the process control block of the process that is currently in the
Running state
3. Move process control block to appropriate queue ready; blocked;
ready/suspend
4. Select another process for execution
5. Update the process control block of the process selected
6. Update memory-management data structure
7. Restore context of the selected process

DEPARTMENT OF COMPUTER SCIENCE AND ENGINEERING

Page 47 of 157

CS0206-OPERATING SYSTEMS

SRM UNIVERSITY,RAMAPURAM

Execution of the Operating System

DEPARTMENT OF COMPUTER SCIENCE AND ENGINEERING

Page 48 of 157

CS0206-OPERATING SYSTEMS

SRM UNIVERSITY,RAMAPURAM

Non-process Kernel (a)

Execute kernel outside of any process

The concept of process is considered to apply only to user programs


o Operating system code is executed as a separate entity that operates in
privileged mode

Execution within User Processes (b)


o Operating system software within context of a user process
o No need for Process Switch to run OS routine
Process-based Operating System(c)

Process-based operating system


o Implement the OS as a collection of system process

DEPARTMENT OF COMPUTER SCIENCE AND ENGINEERING

Page 49 of 157

CS0206-OPERATING SYSTEMS

SRM UNIVERSITY,RAMAPURAM

Processes and Threads

Resource ownership - process includes a virtual address space to hold the process
image

Scheduling/execution- follows an execution path that may be interleaved with


other processes

These two characteristics are treated independently by the operating system


o Dispatching is referred to as a thread or lightweight process

Multithreading:

Operating system supports multiple threads of execution within a single process

MS-DOS supports a single thread

UNIX supports multiple user processes but only supports one thread per process

Windows, Solaris, Linux, Mach, and OS/2 support multiple threads

Process:

Have a virtual address space which holds the process image

Protected access to processors, other processes, files, and I/O resources

Thread:

An execution state (running, ready, etc.)

Saved thread context when not running

Has an execution stack

Some per-thread static storage for local variables

Access to the memory and resources of its process


o All threads of a process share this

DEPARTMENT OF COMPUTER SCIENCE AND ENGINEERING

Page 50 of 157

CS0206-OPERATING SYSTEMS

SRM UNIVERSITY,RAMAPURAM

DEPARTMENT OF COMPUTER SCIENCE AND ENGINEERING

Page 51 of 157

CS0206-OPERATING SYSTEMS

SRM UNIVERSITY,RAMAPURAM

Benefits of Threads

Takes less time to create a new thread than a process

Less time to terminate a thread than a process

Less time to switch between two threads within the same process

Since threads within the same process share memory and files, they can
communicate with each other without invoking the kernel

Uses of Threads in a Single-User Multiprocessing System

Foreground to background work

Asynchronous processing

Speed of execution

Modular program structure

Threads

Suspending a process involves suspending all threads of the process since all
threads share the same address space

Termination of a process, terminates all threads within the process

Thread States:

States associated with a change in thread state


o Spawn

Spawn another thread

o Block
o Unblock
o Finish

Deallocate register context and stacks

DEPARTMENT OF COMPUTER SCIENCE AND ENGINEERING

Page 52 of 157

CS0206-OPERATING SYSTEMS

SRM UNIVERSITY,RAMAPURAM

DEPARTMENT OF COMPUTER SCIENCE AND ENGINEERING

Page 53 of 157

CS0206-OPERATING SYSTEMS

SRM UNIVERSITY,RAMAPURAM

Adobe PageMaker

User-Level Threads

All thread management is done by the application

The kernel is not aware of the existence of threads

DEPARTMENT OF COMPUTER SCIENCE AND ENGINEERING

Page 54 of 157

CS0206-OPERATING SYSTEMS

SRM UNIVERSITY,RAMAPURAM

Kernel-Level Threads:

Windows is an example of this approach

Kernel maintains context information for the process and the threads

Scheduling is done on a thread basis

Combined Approaches

Example is Solaris

Thread creation done in the user space

Bulk of scheduling and synchronization of threads within application

DEPARTMENT OF COMPUTER SCIENCE AND ENGINEERING

Page 55 of 157

CS0206-OPERATING SYSTEMS

SRM UNIVERSITY,RAMAPURAM

DEPARTMENT OF COMPUTER SCIENCE AND ENGINEERING

Page 56 of 157

CS0206-OPERATING SYSTEMS

SRM UNIVERSITY,RAMAPURAM

Symmetric Multiprocessing and Microkernels

Kernel can execute on any processor

Typically each processor does self-scheduling form the pool of available process
or threads

SMP Architecture:
Categories of Computer Systems:

Single Instruction Single Data (SISD) stream


o Single processor executes a single instruction stream to operate on data
stored in a single memory

Single Instruction Multiple Data (SIMD) stream


o Each instruction is executed on a different set of data by the different
processors

Multiple Instruction Single Data (MISD) stream


o A sequence of data is transmitted to a set of processors, each of which
executes a different instruction sequence. Never implemented

Multiple Instruction Multiple Data (MIMD)


o A set of processors simultaneously execute different instruction sequences
on different data sets

DEPARTMENT OF COMPUTER SCIENCE AND ENGINEERING

Page 57 of 157

CS0206-OPERATING SYSTEMS

SRM UNIVERSITY,RAMAPURAM

Multiprocessor Operating System Design Considerations:

Simultaneous concurrent processes or threads

Scheduling

Synchronization

Memory management

Reliability and fault tolerance

Microkernels:

Small operating system core

Contains only essential core operating systems functions

Many services traditionally included in the operating system are now external
subsystems
o Device drivers
o File systems
o Virtual memory manager
o Windowing system
o Security services

DEPARTMENT OF COMPUTER SCIENCE AND ENGINEERING

Page 58 of 157

CS0206-OPERATING SYSTEMS

SRM UNIVERSITY,RAMAPURAM

Benefits of a Microkernel Organization:

Uniform interface on request made by a process


o Dont distinguish between kernel-level and user-level services
o All services are provided by means of message passing

Extensibility
o Allows the addition of new services

Flexibility
o New features added
o Existing features can be subtracted

Portability
o Changes needed to port the system to a new processor is changed in the
microkernel - not in the other services

Reliability
o Modular design
o Small microkernel can be rigorously tested

DEPARTMENT OF COMPUTER SCIENCE AND ENGINEERING

Page 59 of 157

CS0206-OPERATING SYSTEMS

SRM UNIVERSITY,RAMAPURAM

Distributed system support


o Message are sent without knowing what the target machine is

Object-oriented operating system


o Components are objects with clearly defined interfaces that can be
interconnected to form software

Microkernel Design:

Low-level memory management


o Mapping each virtual page to a physical page frame

Interprocess Communication

I/O and Interrupt Management

DEPARTMENT OF COMPUTER SCIENCE AND ENGINEERING

Page 60 of 157

CS0206-OPERATING SYSTEMS

SRM UNIVERSITY,RAMAPURAM

Windows Thread and SMP Management

Implemented as objects

An executable process may contain one or more threads

Both processes and thread objects have built-in synchronization capabilities

DEPARTMENT OF COMPUTER SCIENCE AND ENGINEERING

Page 61 of 157

CS0206-OPERATING SYSTEMS

Windows Process Object

SRM UNIVERSITY,RAMAPURAM

Windows Thread Object

Windows 2000 Thread States

Ready
Standby
Running
Waiting
Transition
Terminated

DEPARTMENT OF COMPUTER SCIENCE AND ENGINEERING

Page 62 of 157

CS0206-OPERATING SYSTEMS

SRM UNIVERSITY,RAMAPURAM

CASE STUDIES: SOLARIS, UNIX, LINUX

Process includes the users address space, stack, and process control block

User-level threads

Lightweight processes (LWP)

Kernel threads

DEPARTMENT OF COMPUTER SCIENCE AND ENGINEERING

Page 63 of 157

CS0206-OPERATING SYSTEMS

SRM UNIVERSITY,RAMAPURAM

Solaris Lightweight Data Structure:

Identifier

Priority

Signal mask

Saved values of user-level registers

Kernel stack

Resource usage and profiling data

Pointer to the corresponding kernel thread

Pointer to the process structure

DEPARTMENT OF COMPUTER SCIENCE AND ENGINEERING

Page 64 of 157

CS0206-OPERATING SYSTEMS

SRM UNIVERSITY,RAMAPURAM

DEPARTMENT OF COMPUTER SCIENCE AND ENGINEERING

Page 65 of 157

CS0206-OPERATING SYSTEMS

SRM UNIVERSITY,RAMAPURAM

Linux Task Data Structure:

State

Scheduling information

Identifiers

Interprocess communication

Links

Times and timers

File system

Address space

Processor-specific context

Linux States of a Process:

Running

Interruptable

Uninterruptable

Stopped

Zombie

DEPARTMENT OF COMPUTER SCIENCE AND ENGINEERING

Page 66 of 157

CS0206-OPERATING SYSTEMS

SRM UNIVERSITY,RAMAPURAM

DEPARTMENT OF COMPUTER SCIENCE AND ENGINEERING

Page 67 of 157

CS0206-OPERATING SYSTEMS

SRM UNIVERSITY,RAMAPURAM

UNIT 3
CONCURRENCY AND SCHEDULING

Principles of concurrency - Mutual exclusion, Semaphores, Monitors, Readers/Writers


Problem; Deadlocks Prevention- Avoidance Detection; Scheduling: Types of
scheduling Scheduling Algorithms, Case studies-UNIX scheduling.

Concurrency
3 different contexts:

Multiple applications: Allow processing time to be shared

Structured applications: set of concurrent processes

Operating system structure: implemented as set of processes or threads.

DEPARTMENT OF COMPUTER SCIENCE AND ENGINEERING

Page 68 of 157

CS0206-OPERATING SYSTEMS

SRM UNIVERSITY,RAMAPURAM

Principles of Concurrency
In single processor multiprogramming system, process is interleaved in time to
yield the appearance of simultaneous execution.

Difficulties of Concurrency:

Sharing of global resources

Operating system managing the allocation of resources optimally

Difficult to locate programming errors

Process Interaction:

DEPARTMENT OF COMPUTER SCIENCE AND ENGINEERING

Page 69 of 157

CS0206-OPERATING SYSTEMS

SRM UNIVERSITY,RAMAPURAM

Competition among Processes:

Mutual Exclusion
o Critical sections

Only one program at a time is allowed in its critical section

Example only one process at a time is allowed to send


command to the printer

Deadlock

Starvation

Mutual Exclusion
Hardware Support:

Interrupt Disabling
o A process runs until it invokes an operating system service or until it
is interrupted
o Disabling interrupts guarantees mutual exclusion
o Processor is limited in its ability to interleave programs
o Multiprocessing

disabling interrupts on one processor will not guarantee


mutual exclusion

Test and Set Instruction


boolean testset (int i) {
if (i == 0) {
i = 1;
return true;
}
else {
return false;
}
}

DEPARTMENT OF COMPUTER SCIENCE AND ENGINEERING

Page 70 of 157

CS0206-OPERATING SYSTEMS

SRM UNIVERSITY,RAMAPURAM

Exchange Instruction
void exchange(int register,int memory)
{
int temp;
temp = memory;
memory = register;
register = temp;
}

DEPARTMENT OF COMPUTER SCIENCE AND ENGINEERING

Page 71 of 157

CS0206-OPERATING SYSTEMS

SRM UNIVERSITY,RAMAPURAM

Semaphores

Special variable called a semaphore is used for signaling

If a process is waiting for a signal, it is suspended until that signal is sent

Semaphore is a variable that has an integer value


o May be initialized to a nonnegative number
o Wait operation decrements the semaphore value
o Signal operation increments semaphore value

Semaphore Primitives:

DEPARTMENT OF COMPUTER SCIENCE AND ENGINEERING

Page 72 of 157

CS0206-OPERATING SYSTEMS

SRM UNIVERSITY,RAMAPURAM

Binary Semaphore Primitives:

DEPARTMENT OF COMPUTER SCIENCE AND ENGINEERING

Page 73 of 157

CS0206-OPERATING SYSTEMS

SRM UNIVERSITY,RAMAPURAM

Mutual Exclusion Using Semaphores:

DEPARTMENT OF COMPUTER SCIENCE AND ENGINEERING

Page 74 of 157

CS0206-OPERATING SYSTEMS

SRM UNIVERSITY,RAMAPURAM

DEPARTMENT OF COMPUTER SCIENCE AND ENGINEERING

Page 75 of 157

CS0206-OPERATING SYSTEMS

SRM UNIVERSITY,RAMAPURAM

DEPARTMENT OF COMPUTER SCIENCE AND ENGINEERING

Page 76 of 157

CS0206-OPERATING SYSTEMS

SRM UNIVERSITY,RAMAPURAM

Producer/Consumer Problem:

One or more producers are generating data and placing these in a buffer

A single consumer is taking items out of the buffer one at time

Only one producer or consumer may access the buffer at any one time

DEPARTMENT OF COMPUTER SCIENCE AND ENGINEERING

Page 77 of 157

CS0206-OPERATING SYSTEMS

SRM UNIVERSITY,RAMAPURAM

Monitors

Monitor is a software module

Chief characteristics
o Local data variables are accessible only by the monitor
o Process enters monitor by invoking one of its procedures
o Only one process may be executing in the monitor at a time

DEPARTMENT OF COMPUTER SCIENCE AND ENGINEERING

Page 78 of 157

CS0206-OPERATING SYSTEMS

SRM UNIVERSITY,RAMAPURAM

Synchronization:

Sender and receiver may or may not be blocking (waiting for message)

Blocking send, blocking receive


o Both sender and receiver are blocked until message is delivered
o Called a rendezvous

Nonblocking send, blocking receive


o Sender continues on
o Receiver is blocked until the requested message arrives

Nonblocking send, nonblocking receive


o Neither party is required to wait

Addressing:

Direct addressing
o Send primitive includes a specific identifier of the destination process
o Receive primitive could know ahead of time which process a message is
expected
o Receive primitive could use source parameter to return a value when the
receive operation has been performed

Indirect addressing
o Messages are sent to a shared data structure consisting of queues
o Queues are called mailboxes
o One process sends a message to the mailbox and the other process picks
up the message from the mailbox

DEPARTMENT OF COMPUTER SCIENCE AND ENGINEERING

Page 79 of 157

CS0206-OPERATING SYSTEMS

SRM UNIVERSITY,RAMAPURAM

Message Format:

DEPARTMENT OF COMPUTER SCIENCE AND ENGINEERING

Page 80 of 157

CS0206-OPERATING SYSTEMS

SRM UNIVERSITY,RAMAPURAM

Readers/Writers Problem

Any number of readers may simultaneously read the file

Only one writer at a time may write to the file

If a writer is writing to the file, no reader may read it

Deadlock

Permanent blocking of a set of processes that either compete for system resources
or communicate with each other

No efficient solution

Involve conflicting needs for resources by two or more processes

DEPARTMENT OF COMPUTER SCIENCE AND ENGINEERING

Page 81 of 157

CS0206-OPERATING SYSTEMS

SRM UNIVERSITY,RAMAPURAM

Reusable Resources:

Used by only one process at a time and not depleted by that use

Processes obtain resources that they later release for reuse by other processes

Processors, I/O channels, main and secondary memory, devices, and data
structures such as files, databases, and semaphores

Deadlock occurs if each process holds one resource and requests the other

Example of Deadlock:

Resource Allocation Graphs:

Directed graph that depicts a state of the system of resources and processes

DEPARTMENT OF COMPUTER SCIENCE AND ENGINEERING

Page 82 of 157

CS0206-OPERATING SYSTEMS

SRM UNIVERSITY,RAMAPURAM

Conditions for Deadlock:

Mutual exclusion
o Only one process may use a resource at a time

Hold-and-wait
o A process may hold allocated resources while awaiting assignment of
others

No preemption
o No resource can be forcibly removed from a process holding it

Circular wait

A closed chain of processes exists, such that each process holds at least one resource
needed by the next process in the chain

Deadlock Prevention

Mutual Exclusion
o Must be supported by the operating system

Hold and Wait


o Require a process request all of its required resources at one time

No Preemption
o Process must release resource and request again
o Operating system may preempt a process to require it releases its
resources

Circular Wait
o Define a linear ordering of resource types
DEPARTMENT OF COMPUTER SCIENCE AND ENGINEERING

Page 83 of 157

CS0206-OPERATING SYSTEMS

SRM UNIVERSITY,RAMAPURAM

Deadlock Avoidance

A decision is made dynamically whether the current resource allocation request


will, if granted, potentially lead to a deadlock

Requires knowledge of future process request

Two Approaches to Deadlock Avoidance:

Do not start a process if its demands might lead to deadlock

Do not grant an incremental resource request to a process if this allocation might


lead to deadlock

Deadlock Avoidance:

Maximum resource requirement must be stated in advance

Processes under consideration must be independent; no synchronization


requirements

There must be a fixed number of resources to allocate

No process may exit while holding resources

Deadlock Detection

DEPARTMENT OF COMPUTER SCIENCE AND ENGINEERING

Page 84 of 157

CS0206-OPERATING SYSTEMS

SRM UNIVERSITY,RAMAPURAM

Strategies once Deadlock Detected:

Abort all deadlocked processes

Back up each deadlocked process to some previously defined checkpoint, and


restart all process
o Original deadlock may occur

Successively abort deadlocked processes until deadlock no longer exists

Successively preempt resources until deadlock no longer exists

Selection Criteria Deadlocked Processes:

Least amount of processor time consumed so far

Least number of lines of output produced so far

Most estimated time remaining

Least total resources allocated so far

Lowest priority

Strengths and Weaknesses of the Strategies:

DEPARTMENT OF COMPUTER SCIENCE AND ENGINEERING

Page 85 of 157

CS0206-OPERATING SYSTEMS

SRM UNIVERSITY,RAMAPURAM

Scheduling
Aim of Scheduling:

Assign processes to be executed by the processor(s)

Response time

Throughput

Processor efficiency

Long-Term Scheduling

Determines which programs are admitted to the system for processing

Controls the degree of multiprogramming

More processes, smaller percentage of time each process is executed

Medium-Term Scheduling

Part of the swapping function

Based on the need to manage the degree of multiprogramming


DEPARTMENT OF COMPUTER SCIENCE AND ENGINEERING

Page 86 of 157

CS0206-OPERATING SYSTEMS

SRM UNIVERSITY,RAMAPURAM

Short-Term Scheduling

Known as the dispatcher

Executes most frequently

Invoked when an event occurs


o Clock interrupts
o I/O interrupts
o Operating system calls
o Signals

DEPARTMENT OF COMPUTER SCIENCE AND ENGINEERING

Page 87 of 157

CS0206-OPERATING SYSTEMS

SRM UNIVERSITY,RAMAPURAM

Scheduling Algorithms

First-Come-First-Serve (FCFS)

Round-Robin

Shortest Process Next

Shortest Remaining Time

Highest Response Ratio Next (HRRN)

DEPARTMENT OF COMPUTER SCIENCE AND ENGINEERING

Page 88 of 157

CS0206-OPERATING SYSTEMS

SRM UNIVERSITY,RAMAPURAM

First-Come-First-Serve (FCFS):

Each process joins the Ready queue

When the current process ceases to execute, the oldest process in the Ready
queue is selected

A short process may have to wait a very long time before it can execute

Favors CPU-bound processes

I/O processes have to wait until CPU-bound process completes

Round-Robin (RR):

Uses preemption based on a clock

An amount of time is determined that allows each process to use the


processor for that length of time

Clock interrupt is generated at periodic intervals

When an interrupt occurs, the currently running process is placed in the


read queue
o Next ready job is selected

Known as time slicing


DEPARTMENT OF COMPUTER SCIENCE AND ENGINEERING

Page 89 of 157

CS0206-OPERATING SYSTEMS

SRM UNIVERSITY,RAMAPURAM

Shortest Process Next (SPN):

Nonpreemptive policy

Process with shortest expected processing time is selected next

Short process jumps ahead of longer processes

Predictability of longer processes is reduced

If estimated time for process not correct, the operating system may abort it

Possibility of starvation for longer processes

Shortest Remaining Time (SRT):

Preemptive version of shortest process next policy

Must estimate processing time

DEPARTMENT OF COMPUTER SCIENCE AND ENGINEERING

Page 90 of 157

CS0206-OPERATING SYSTEMS

SRM UNIVERSITY,RAMAPURAM

Highest Response Ratio Next (HRRN):

Choose next process with the greatest ratio

CASE STUDY-UNIX Scheduling

Multilevel feedback using round robin within each of the priority queues

If a running process does not block or complete within 1 second, it is


preempted

Priorities are recomputed once per second

Base priority divides all processes into fixed bands of priority levels

Bands

Decreasing order of priority


o Swapper
o Block I/O device control
o File manipulation
o Character I/O device control
o User processes

DEPARTMENT OF COMPUTER SCIENCE AND ENGINEERING

Page 91 of 157

CS0206-OPERATING SYSTEMS

SRM UNIVERSITY,RAMAPURAM

DEPARTMENT OF COMPUTER SCIENCE AND ENGINEERING

Page 92 of 157

CS0206-OPERATING SYSTEMS

SRM UNIVERSITY,RAMAPURAM

UNIT 4
MEMORY
Memory management requirements, Partitioning, Paging, and Segmentation; Virtual
memory - Hardware and control structures, operating system software- Linux memory
management, Case studies- WINDOWS memory management, UNIX and SOLARIS
Memory management

Memory Management:

Subdividing memory to accommodate multiple processes

Memory needs to be allocated to ensure a reasonable supply of ready processes to


consume available processor time

Memory Management Requirements

Relocation
o Programmer does not know where the program will be placed in memory
when it is executed
o While the program is executing, it may be swapped to disk and returned to
main memory at a different location (relocated)
o Memory references must be translated in the code to actual physical
memory address

Protection
o Processes should not be able to reference memory locations in another
process without permission
o Impossible to check absolute addresses at compile time
o Must be checked at rum time
o Memory protection requirement must be satisfied by the processor
(hardware) rather than the operating system (software)
o Operating system cannot anticipate all of the memory references a
program will make

DEPARTMENT OF COMPUTER SCIENCE AND ENGINEERING

Page 93 of 157

CS0206-OPERATING SYSTEMS

SRM UNIVERSITY,RAMAPURAM

Sharing
o Allow several processes to access the same portion of memory
o Better to allow each process access to the same copy of the program rather
than have their own separate copy

Logical Organization
o Programs are written in modules
o Modules can be written and compiled independently
o Different degrees of protection given to modules (read-only, execute-only)
o Share modules among processes

Physical Organization
o Memory available for a program plus its data may be insufficient

Overlaying allows various modules to be assigned the same region


of memory

o Programmer does not know how much space will be available

DEPARTMENT OF COMPUTER SCIENCE AND ENGINEERING

Page 94 of 157

CS0206-OPERATING SYSTEMS

SRM UNIVERSITY,RAMAPURAM

Partitioning
Fixed Partitioning:

Equal-size partitions
o Any process whose size is less than or equal to the partition size can be
loaded into an available partition
o If all partitions are full, the operating system can swap a process out of a
partition
o A program may not fit in a partition. The programmer must design the
program with overlays
o Main memory use is inefficient. Any program, no matter how small,
occupies an entire partition. This is called internal fragmentation.

DEPARTMENT OF COMPUTER SCIENCE AND ENGINEERING

Page 95 of 157

CS0206-OPERATING SYSTEMS

SRM UNIVERSITY,RAMAPURAM

Placement Algorithm with Partitions:

Equal-size partitions
o Because all partitions are of equal size, it does not matter which partition
is used

Unequal-size partitions
o Can assign each process to the smallest partition within which it will fit
o Queue for each partition
o Processes are assigned in such a way as to minimize wasted memory
within a partition

DEPARTMENT OF COMPUTER SCIENCE AND ENGINEERING

Page 96 of 157

CS0206-OPERATING SYSTEMS

SRM UNIVERSITY,RAMAPURAM

Dynamic Partitioning

Partitions are of variable length and number

Process is allocated exactly as much memory as required

Eventually get holes in the memory. This is called external fragmentation

Must use compaction to shift processes so they are contiguous and all free
memory is in one block

DEPARTMENT OF COMPUTER SCIENCE AND ENGINEERING

Page 97 of 157

CS0206-OPERATING SYSTEMS

SRM UNIVERSITY,RAMAPURAM

Dynamic Partitioning Placement Algorithm:

Operating system must decide which free block to allocate to a process

Best-fit algorithm
o Chooses the block that is closest in size to the request
o Worst performer overall
o Since smallest block is found for process, the smallest amount of
fragmentation is left
o Memory compaction must be done more often

First-fit algorithm
o Scans memory form the beginning and chooses the first available block
that is large enough
o Fastest
o May have many process loaded in the front end of memory that must be
searched over when trying to find a free block

Next-fit
o Scans memory from the location of the last placement
o More often allocate a block of memory at the end of memory where the
largest block is found
o The largest block of memory is broken up into smaller blocks
o Compaction is required to obtain a large block at the end of memory

DEPARTMENT OF COMPUTER SCIENCE AND ENGINEERING

Page 98 of 157

CS0206-OPERATING SYSTEMS

SRM UNIVERSITY,RAMAPURAM

Buddy System:

Entire space available is treated as a single block of 2U

If a request of size s such that 2U-1 < s <= 2U, entire block is allocated
o Otherwise block is split into two equal buddies
o Process continues until smallest block greater than or equal to s is
generated

DEPARTMENT OF COMPUTER SCIENCE AND ENGINEERING

Page 99 of 157

CS0206-OPERATING SYSTEMS

SRM UNIVERSITY,RAMAPURAM

DEPARTMENT OF COMPUTER SCIENCE AND ENGINEERING

Page 100 of 157

CS0206-OPERATING SYSTEMS

SRM UNIVERSITY,RAMAPURAM

Relocation

When program loaded into memory the actual (absolute) memory locations are
determined

A process may occupy different partitions which means different absolute


memory locations during execution (from swapping)

Compaction will also cause a program to occupy a different partition which


means different absolute memory locations

Addresses

Logical
o Reference to a memory location independent of the current assignment of
data to memory
o Translation must be made to the physical address

Relative
o Address expressed as a location relative to some known point

Physical
o The absolute address or actual location in main memory

DEPARTMENT OF COMPUTER SCIENCE AND ENGINEERING

Page 101 of 157

CS0206-OPERATING SYSTEMS

SRM UNIVERSITY,RAMAPURAM

Paging

Partition memory into small equal fixed-size chunks and divide each process into
the same size chunks

The chunks of a process are called pages and chunks of memory are called frames

Operating system maintains a page table for each process


o Contains the frame location for each page in the process
o Memory address consist of a page number and offset within the page

Assignment of Process Pages to Free Frames:

DEPARTMENT OF COMPUTER SCIENCE AND ENGINEERING

Page 102 of 157

CS0206-OPERATING SYSTEMS

SRM UNIVERSITY,RAMAPURAM

Segmentation

All segments of all programs do not have to be of the same length

There is a maximum segment length

Addressing consist of two parts - a segment number and an offset

Since segments are not equal, segmentation is similar to dynamic partitioning

DEPARTMENT OF COMPUTER SCIENCE AND ENGINEERING

Page 103 of 157

CS0206-OPERATING SYSTEMS

SRM UNIVERSITY,RAMAPURAM

DEPARTMENT OF COMPUTER SCIENCE AND ENGINEERING

Page 104 of 157

CS0206-OPERATING SYSTEMS

SRM UNIVERSITY,RAMAPURAM

Virtual Memory
Hardware and Control Structures:

Memory references are dynamically translated into physical addresses at run time
o A process may be swapped in and out of main memory such that it
occupies different regions

A process may be broken up into pieces that do not need to located contiguously
in main memory

All pieces of a process do not need to be loaded in main memory during execution

Execution of a Program:

Operating system brings into main memory a few pieces of the program

Resident set - portion of process that is in main memory

An interrupt is generated when an address is needed that is not in main memory

Operating system places the process in a blocking state

Piece of process that contains the logical address is brought into main memory
o Operating system issues a disk I/O Read request
o Another process is dispatched to run while the disk I/O takes place
o An interrupt is issued when disk I/O complete which causes the operating
system to place the affected process in the Ready state

Types of Memory:

Real memory
o Main memory

Virtual memory
o Memory on disk
o Allows for effective multiprogramming and relieves the user of tight
constraints of main memory

Thrashing

Swapping out a piece of a process just before that piece is needed

The processor spends most of its time swapping pieces rather than executing user
instructions

DEPARTMENT OF COMPUTER SCIENCE AND ENGINEERING

Page 105 of 157

CS0206-OPERATING SYSTEMS

SRM UNIVERSITY,RAMAPURAM

Principle of Locality

Program and data references within a process tend to cluster

Only a few pieces of a process will be needed over a short period of time

Possible to make intelligent guesses about which pieces will be needed in the
future

This suggests that virtual memory may work efficiently

Paging

Each process has its own page table

Each page table entry contains the frame number of the corresponding page in
main memory

A bit is needed to indicate whether the page is in main memory or not

Modify Bit in Page Table:

Modify bit is needed to indicate if the page has been altered since it was last
loaded into main memory

If no change has been made, the page does not have to be written to the disk when
it needs to be swapped out

DEPARTMENT OF COMPUTER SCIENCE AND ENGINEERING

Page 106 of 157

CS0206-OPERATING SYSTEMS

SRM UNIVERSITY,RAMAPURAM

Inverted Page Table:

Page number

Process identifier

Control bits

Chain pointer

DEPARTMENT OF COMPUTER SCIENCE AND ENGINEERING

Page 107 of 157

CS0206-OPERATING SYSTEMS

SRM UNIVERSITY,RAMAPURAM

Translation Lookaside Buffer:

Each virtual memory reference can cause two physical memory accesses
o One to fetch the page table
o One to fetch the data

To overcome this problem a high-speed cache is set up for page table entries
o Called a Translation Lookaside Buffer (TLB)

Given a virtual address, processor examines the TLB

If page table entry is present (TLB hit), the frame number is retrieved and the real
address is formed

If page table entry is not found in the TLB (TLB miss), the page number is used
to index the process page table

First checks if page is already in main memory


o If not in main memory a page fault is issued

The TLB is updated to include the new page entry

DEPARTMENT OF COMPUTER SCIENCE AND ENGINEERING

Page 108 of 157

CS0206-OPERATING SYSTEMS

SRM UNIVERSITY,RAMAPURAM

DEPARTMENT OF COMPUTER SCIENCE AND ENGINEERING

Page 109 of 157

CS0206-OPERATING SYSTEMS

SRM UNIVERSITY,RAMAPURAM

Page Size:

Smaller page size, less amount of internal fragmentation

Smaller page size, more pages required per process

More pages per process means larger page tables

Larger page tables means large portion of page tables in virtual memory

Secondary memory is designed to efficiently transfer large blocks of data so a


large page size is better

Small page size, large number of pages will be found in main memory

As time goes on during execution, the pages in memory will all contain portions
of the process near recent references. Page faults low.

Increased page size causes pages to contain locations further from any recent
reference. Page faults rise.

Segmentation:

May be unequal, dynamic size

Simplifies handling of growing data structures

Allows programs to be altered and recompiled independently

Lends itself to sharing data among processes

Lends itself to protection

Segment Tables:

Corresponding segment in main memory

Each entry contains the length of the segment

A bit is needed to determine if segment is already in main memory

Another bit is needed to determine if the segment has been modified since it was
loaded in main memory

DEPARTMENT OF COMPUTER SCIENCE AND ENGINEERING

Page 110 of 157

CS0206-OPERATING SYSTEMS

SRM UNIVERSITY,RAMAPURAM

Combined Paging and Segmentation:

Paging is transparent to the programmer

Segmentation is visible to the programmer

Each segment is broken into fixed-size pages

DEPARTMENT OF COMPUTER SCIENCE AND ENGINEERING

Page 111 of 157

CS0206-OPERATING SYSTEMS

SRM UNIVERSITY,RAMAPURAM

Basic Replacement Algorithms:

Optimal policy
o Selects for replacement that page for which the time to the next reference
is the longest
o Impossible to have perfect knowledge of future events

Least Recently Used (LRU)


o Replaces the page that has not been referenced for the longest time
o By the principle of locality, this should be the page least likely to be
referenced in the near future
o Each page could be tagged with the time of last reference. This would
require a great deal of overhead
DEPARTMENT OF COMPUTER SCIENCE AND ENGINEERING

Page 112 of 157

CS0206-OPERATING SYSTEMS

SRM UNIVERSITY,RAMAPURAM

First-in, first-out (FIFO)


o Treats page frames allocated to a process as a circular buffer
o Pages are removed in round-robin style
o Simplest replacement policy to implement
o Page that has been in memory the longest is replaced
o These pages may be needed again very soon

Clock Policy
o Additional bit called a use bit
o When a page is first loaded in memory, the use bit is set to 1
o When the page is referenced, the use bit is set to 1
o When it is time to replace a page, the first frame encountered with the use
bit set to 0 is replaced.
o During the search for replacement,each use bit set to 1 is changed to 0

DEPARTMENT OF COMPUTER SCIENCE AND ENGINEERING

Page 113 of 157

CS0206-OPERATING SYSTEMS

SRM UNIVERSITY,RAMAPURAM

Cleaning Policy:

Demand cleaning
o A page is written out only when it has been selected for replacement

Precleaning
o Pages are written out in batches

Load Control:

Determines the number of processes that will be resident in main memory

Too few processes, many occasions when all processes will be blocked and much
time will be spent in swapping

Too many processes will lead to thrashing

DEPARTMENT OF COMPUTER SCIENCE AND ENGINEERING

Page 114 of 157

CS0206-OPERATING SYSTEMS

SRM UNIVERSITY,RAMAPURAM

Operating System Softwares


Linux Memory Management
2 components:
1. First, Physical memory management system deals with allocating and freeing
pages, groups of pages and small blocks of memory.
2. The second component handles virtual memory, which is memory mapped into
the address space of running processes.

Management of Physical Memory:


Page allocator is responsible for allocating and freeing all physical pages and is
capable of allocating ranges of physically contiguous pages on request.
Buddy-Heap Algorithm:

Keep track of available physical pages

Pairs adjacent units of allocatable together.


8 KB
8 KB

16KB
8 KB

4 KB
4 KB

Fig: Splitting of Memory in a Buddy Heap

Address Translation:

Page directory

Page middle directory

Page table

DEPARTMENT OF COMPUTER SCIENCE AND ENGINEERING

Page 115 of 157

CS0206-OPERATING SYSTEMS

SRM UNIVERSITY,RAMAPURAM

Linux processes:

Each process gets 3GB virtual memory

Remaining 1GB for kernel and page tables

Virtual address space composed of areas with same protection, paging properties
(pageable or not, direction of growth)

Each process has a linked list of areas, sorted by virtual address (text, data,
memory-mapped-files,)

Linux main memory management:

Kernel never swapped

The rest: user pages, file system buffers, variable-size device drivers

The buddy algorithm is used. In addition:


o Linked lists of same-size free blocks are maintained
o To reduce internal fragmentation, a second memory allocation scheme
manages smaller units inside buddy-blocks

Demand paging (no pre-paging)

Dynamic backing store management.

DEPARTMENT OF COMPUTER SCIENCE AND ENGINEERING

Page 116 of 157

CS0206-OPERATING SYSTEMS

SRM UNIVERSITY,RAMAPURAM

Windows Memory Management


Virtual Address Space:

Virtual address space layout for 3 user processes

White areas are private per process

Shaded areas are shared among all processes

DEPARTMENT OF COMPUTER SCIENCE AND ENGINEERING

Page 117 of 157

CS0206-OPERATING SYSTEMS

SRM UNIVERSITY,RAMAPURAM

Memory Management Concepts:

Each virtual page can be in one of following states:


o Free/invalid Currently not in use, a reference causes access violation
o Committed code/data was mapped to virtual page
o Reserved allocated to thread, not mapped yet. When a new thread starts,
1MB of process space is reserved to its stack
o Readable/writable/executable

Dynamic (just-in-time) backing store management


o Improves performance of writing modified data in chunks
o Up to 16 pagefiles

Supports memory-mapped files

Can use 4K or 4M pages

Implementation of Memory Management:

Fig: A page table entry for a mapped page on the Pentium

DEPARTMENT OF COMPUTER SCIENCE AND ENGINEERING

Page 118 of 157

CS0206-OPERATING SYSTEMS

SRM UNIVERSITY,RAMAPURAM

Physical Memory Management:

Fig: Various page lists and transitions between them

DEPARTMENT OF COMPUTER SCIENCE AND ENGINEERING

Page 119 of 157

CS0206-OPERATING SYSTEMS

SRM UNIVERSITY,RAMAPURAM

CASE STUDIES
UNIX and SOLARIS Memory Management
Paging system:

Page replacement:
o Page frame data table used for page replacement
o Lists are created within table using pointers (e.g., list of free
frames available; when number of frames on this table drops below
a threshold, the kernel will steal a number of pages to compensate)
o Page replacement algorithm in SVR4 is a modified clock policy
algorithm, the two-handed clock algorithm

It uses the reference bit in the page table entry for each
page in memory that is eligible (not locked) to be swapped
out

Bit is set to 0 when page is first brought in and set to 1


when page is referenced for read or write

The front-hand of the algorithm sweeps through the eligible


pages and sets reference bits to 1

The backhand, later, sweeps same list and checks the


referenced bit: if 0, page is placed on the list to be paged
out

Parameters: Scan rate and Handspead

Kernel memory allocator:

Requirement
o Kernel generates and destroys frequently small tables and buffers
(e.g, file descriptor blocks) which require dynamic memory
allocation
o These tables and buffers are much smaller than typical machine
page size: paging mechanism would be inefficient

DEPARTMENT OF COMPUTER SCIENCE AND ENGINEERING

Page 120 of 157

CS0206-OPERATING SYSTEMS

SRM UNIVERSITY,RAMAPURAM

Solution: SVR4 uses the lazy buddy system


o Observation: demand for blocks of particular size varies slowly in
time
o Solution: defer coalescing blocks until it seems likely that it is
needed and then coalesce as many blocks as possible
o Strategy: try to maintain a pool of locally free blocks and only
invoke coalescing if the number of free blocks exceeds a threshold
o Criterion for coalescing: the number of locally free blocks of a
given size should not exceed the number of allocated blocks of that
size

DEPARTMENT OF COMPUTER SCIENCE AND ENGINEERING

Page 121 of 157

CS0206-OPERATING SYSTEMS

SRM UNIVERSITY,RAMAPURAM

UNIX Page Daemon:

It is assumed useful to keep a pool of free pages

freeing of page frames is done by a pagedaemon - a process that sleeps most of


the time

awakened periodically to inspect the state of memory - if less than 'th of page
frames are free, then it frees page frames

this strategy performs better than evicting pages when needed (and writing the
modified to disk in a hurry)

The net result is the use of all of available memory as page-pool

Uses a global clock algorithm two-handed clock

DEPARTMENT OF COMPUTER SCIENCE AND ENGINEERING

Page 122 of 157

CS0206-OPERATING SYSTEMS

SRM UNIVERSITY,RAMAPURAM

UNIT 5
INPUT/OUTPUT AND FILE SYSTEMS
I/O management and disk scheduling I/O devices, organization of I/O functions; OS
design issues, I/O Buffering, disk scheduling, Disk cache, File management
Organization, Directories, File sharing, Record blocking, Secondary storage
management; Case studies-LINUX I/O, UNIX File management.

I/O Management and Disk Scheduling


I/O Devices
Categories of I/O Devices:

Human readable
o Used to communicate with the user
o Printers
o Video display terminals
o Display
o Keyboard
o Mouse

Machine readable
o Used to communicate with electronic equipment
o Disk and tape drives
o Sensors
o Controllers
o Actuators

Communication
o Used to communicate with remote devices
o Digital line drivers
o Modems

Differences in I/O Devices

Data rate
o May be differences of several orders of magnitude between the data
transfer rates

DEPARTMENT OF COMPUTER SCIENCE AND ENGINEERING

Page 123 of 157

CS0206-OPERATING SYSTEMS

SRM UNIVERSITY,RAMAPURAM

Application
o Disk used to store files requires file management software
o Disk used to store virtual memory pages needs special hardware and
software to support it
o Terminal used by system administrator may have a higher priority

Complexity of control

Unit of transfer
o Data may be transferred as a stream of bytes for a terminal or in larger
blocks for a disk

Data representation
o Encoding schemes

Error conditions
o Devices respond to errors differently

Organization of the I/O Function

Programmed I/O
o Process is busy-waiting for the operation to complete

Interrupt-driven I/O
o I/O command is issued
o Processor continues executing instructions
o I/O module sends an interrupt when done

Direct Memory Access (DMA)


o DMA module controls exchange of data between main memory and the
I/O device
o Processor interrupted only after entire block has been transferred

DEPARTMENT OF COMPUTER SCIENCE AND ENGINEERING

Page 124 of 157

CS0206-OPERATING SYSTEMS

SRM UNIVERSITY,RAMAPURAM

Evolution of the I/O Function:

Controller or I/O module with interrupts


o Processor does not spend time waiting for an I/O operation to be
performed

Direct Memory Access


o Blocks of data are moved into memory without involving the processor
o Processor involved at beginning and end only

I/O module is a separate processor

I/O processor
o I/O module has its own local memory
o Its a computer in its own right

Direct Memory Access:

Processor delegates I/O operation to the DMA module

DMA module transfers data directly to or form memory

When complete DMA module sends an interrupt signal to the processor

DEPARTMENT OF COMPUTER SCIENCE AND ENGINEERING

Page 125 of 157

CS0206-OPERATING SYSTEMS

SRM UNIVERSITY,RAMAPURAM

Operating System Design Issues

Efficiency
o Most I/O devices extremely slow compared to main memory
o Use of multiprogramming allows for some processes to be waiting on I/O
while another process executes
o I/O cannot keep up with processor speed
o Swapping is used to bring in additional Ready processes which is an I/O
operation

Generality
o Desirable to handle all I/O devices in a uniform manner
o Hide most of the details of device I/O in lower-level routines so that
processes and upper levels see devices in general terms such as read,
write, open, close, lock, unlock

DEPARTMENT OF COMPUTER SCIENCE AND ENGINEERING

Page 126 of 157

CS0206-OPERATING SYSTEMS

SRM UNIVERSITY,RAMAPURAM

I/O Buffering

Reasons for buffering:


o Processes must wait for I/O to complete before proceeding
o Certain pages must remain in main memory during I/O

Block-oriented
o Information is stored in fixed sized blocks
o Transfers are made a block at a time
o Used for disks and tapes

Stream-oriented
o Transfer information as a stream of bytes
o Used for terminals, printers, communication ports, mouse and other
pointing devices, and most other devices that are not secondary storage

Single Buffer

Block-oriented
o User process can process one block of data while next block is read in
o Swapping can occur since input is taking place in system memory, not
user memory
o Operating system keeps track of assignment of system buffers to user
processes

Stream-oriented
o Used a line at time
o User input from a terminal is one line at a time with carriage return
signaling the end of the line
o Output to the terminal is one line at a time

DEPARTMENT OF COMPUTER SCIENCE AND ENGINEERING

Page 127 of 157

CS0206-OPERATING SYSTEMS

SRM UNIVERSITY,RAMAPURAM

Double Buffer

Use two system buffers instead of one

A process can transfer data to or from one buffer while the operating system
empties or fills the other buffer

Circular Buffer

More than two buffers are used

Each individual buffer is one unit in a circular buffer

Used when I/O operation must keep up with process

DEPARTMENT OF COMPUTER SCIENCE AND ENGINEERING

Page 128 of 157

CS0206-OPERATING SYSTEMS

SRM UNIVERSITY,RAMAPURAM

Disk Scheduling
Disk Performance Parameters
o To read or write, the disk head must be positioned at the desired track and
at the beginning of the desired sector
o Seek time
o Time it takes to position the head at the desired track
o Rotational delay or rotational latency
o Time it takes for the beginning of the sector to reach the head

Timing of a Disk I/O Transfer

DEPARTMENT OF COMPUTER SCIENCE AND ENGINEERING

Page 129 of 157

CS0206-OPERATING SYSTEMS

SRM UNIVERSITY,RAMAPURAM

Disk Performance Parameters:

Access time
o Sum of seek time and rotational delay
o The time it takes to get in position to read or write

Data transfer occurs as the sector moves under the head

Disk Scheduling Policies:

Seek time is the reason for differences in performance

For a single disk there will be a number of I/O requests

If requests are selected randomly, we will poor performance

Disk Scheduling Algorithms:

First-in, first-out (FIFO)


o Process request sequentially
o Fair to all processes
o Approaches random scheduling in performance if there are many
processes

Priority
o Goal is not to optimize disk use but to meet other objectives
o Short batch jobs may have higher priority
o Provide good interactive response time

Last-in, first-out
o Good for transaction processing systems

The device is given to the most recent user so there should be little
arm movement

o Possibility of starvation since a job may never regain the head of the line

DEPARTMENT OF COMPUTER SCIENCE AND ENGINEERING

Page 130 of 157

CS0206-OPERATING SYSTEMS

SRM UNIVERSITY,RAMAPURAM

Shortest Service Time First


o Select the disk I/O request that requires the least movement of the disk
arm from its current position
o Always choose the minimum Seek time

SCAN
o Arm moves in one direction only, satisfying all outstanding requests until
it reaches the last track in that direction
o Direction is reversed

C-SCAN
o Restricts scanning to one direction only
o When the last track has been visited in one direction, the arm is returned to
the opposite end of the disk and the scan begins again

DEPARTMENT OF COMPUTER SCIENCE AND ENGINEERING

Page 131 of 157

CS0206-OPERATING SYSTEMS

SRM UNIVERSITY,RAMAPURAM

N-step-SCAN
o Segments the disk request queue into subqueues of length N
o Subqueues are processed one at a time, using SCAN
o New requests added to other queue when queue is processed

FSCAN
o Two queues
o One queue is empty for new requests

Disk Cache

Buffer in main memory for disk sectors

Contains a copy of some of the sectors on the disk

Least Recently Used

Least Recently Used:

The block that has been in the cache the longest with no reference to it is replaced

The cache consists of a stack of blocks

Most recently referenced block is on the top of the stack

When a block is referenced or brought into the cache, it is placed on the top of the
stack

DEPARTMENT OF COMPUTER SCIENCE AND ENGINEERING

Page 132 of 157

CS0206-OPERATING SYSTEMS

SRM UNIVERSITY,RAMAPURAM

DEPARTMENT OF COMPUTER SCIENCE AND ENGINEERING

Page 133 of 157

CS0206-OPERATING SYSTEMS

SRM UNIVERSITY,RAMAPURAM

File Management

File management system consists of system utility programs that run as privileged
applications

Input to applications is by means of a file

Output is saved in a file for long-term storage

File System Properties:

Long-term existence

Sharable between processes

Structure

File Operations:

Create

Delete

Open

Close

Read

Write

DEPARTMENT OF COMPUTER SCIENCE AND ENGINEERING

Page 134 of 157

CS0206-OPERATING SYSTEMS

SRM UNIVERSITY,RAMAPURAM

Terms Used with Files:

Field
o Basic element of data
o Contains a single value
o Characterized by its length and data type

Record
o Collection of related fields
o Treated as a unit
Example: employee record

File
o Collection of similar records
o Treated as a single entity
o Have file names
o May restrict access

Database
o Collection of related data
o Relationships exist among elements

Typical Operations:

Retrieve All

Retrieve One

Retrieve Next

Retrieve Previous

Insert One

Delete One

Update One

Retrieve Few

DEPARTMENT OF COMPUTER SCIENCE AND ENGINEERING

Page 135 of 157

CS0206-OPERATING SYSTEMS

SRM UNIVERSITY,RAMAPURAM

File Management Systems

The way a user of application may access files

Programmer does not need to develop file management software

Meet the data management needs and requirements of the user

Guarantee that the data in the file are valid

Optimize performance

Provide I/O support for a variety of storage device types

Minimize or eliminate the potential for lost or destroyed data

Provide a standardized set of I/O interface routines

Provide I/O support for multiple users

DEPARTMENT OF COMPUTER SCIENCE AND ENGINEERING

Page 136 of 157

CS0206-OPERATING SYSTEMS

SRM UNIVERSITY,RAMAPURAM

File Management Functions:

Identify and locate a selected file

Use a directory to describe the location of all files plus their attributes

On a shared system describe user access control

Blocking for access to files

Allocate files to free blocks

Manage free storage for available blocks

DEPARTMENT OF COMPUTER SCIENCE AND ENGINEERING

Page 137 of 157

CS0206-OPERATING SYSTEMS

SRM UNIVERSITY,RAMAPURAM

File Organization
Criteria for File Organization:

Short access time


o Needed when accessing a single record
o Not needed for batch mode

Ease of update
o File on CD-ROM will not be updated, so this is not a concern

Economy of storage
o Should be minimum redundancy in the data
o Redundancy can be used to speed access such as an index

Simple maintenance

Reliability

File Organization:

The Pile
o Data are collected in the order they arrive
o Purpose is to accumulate a mass of data and save it
o Records may have different fields
o No structure
o Record access is by exhaustive search

DEPARTMENT OF COMPUTER SCIENCE AND ENGINEERING

Page 138 of 157

CS0206-OPERATING SYSTEMS

SRM UNIVERSITY,RAMAPURAM

The Sequential File


o Fixed format used for records
o Records are the same length
o All fields the same (order and length)
o Field names and lengths are attributes of the file
o One field is the key filed

Uniquely identifies the record

Records are stored in key sequence

o New records are placed in a log file or transaction file


o Batch update is performed to merge the log file with the master file

Indexed Sequential File

Index provides a lookup capability to quickly reach the vicinity of the


desired record
o Contains key field and a pointer to the main file
o Indexed is searched to find highest key value that is equal to or
precedes the desired key value
o Search continues in the main file at the location indicated by the
pointer

DEPARTMENT OF COMPUTER SCIENCE AND ENGINEERING

Page 139 of 157

CS0206-OPERATING SYSTEMS

SRM UNIVERSITY,RAMAPURAM

New records are added to an overflow file

Record in main file that precedes it is updated to contain a pointer to the


new record

The overflow is merged with the main file during a batch update

Multiple indexes for the same key field can be set up to increase efficiency

Indexed File

Uses multiple indexes for different key fields

May contain an exhaustive index that contains one entry for every record
in the main file

May contain a partial index

DEPARTMENT OF COMPUTER SCIENCE AND ENGINEERING

Page 140 of 157

CS0206-OPERATING SYSTEMS

SRM UNIVERSITY,RAMAPURAM

The Direct or Hashed File


o Directly access a block at a known address
o Key field required for each record

File Directories

Contains information about files


o Attributes
o Location
o Ownership

Directory itself is a file owned by the operating system

Provides mapping between file names and the files themselves

Simple Structure for a Directory:

List of entries, one for each file

Sequential file with the name of the file serving as the key

Provides no help in organizing the files

Forces user to be careful not to use the same name for two different files

DEPARTMENT OF COMPUTER SCIENCE AND ENGINEERING

Page 141 of 157

CS0206-OPERATING SYSTEMS

SRM UNIVERSITY,RAMAPURAM

DEPARTMENT OF COMPUTER SCIENCE AND ENGINEERING

Page 142 of 157

CS0206-OPERATING SYSTEMS

SRM UNIVERSITY,RAMAPURAM

Two-level Scheme for a Directory:

One directory for each user and a master directory

Master directory contains entry for each user


o Provides address and access control information

Each user directory is a simple list of files for that user

Still provides no help in structuring collections of files

Hierarchical or Tree-Structured Directory:

Master directory with user directories underneath it

Each user directory may have subdirectories and files as entries

Files can be located by following a path from the root, or master, directory down
various branches

This is the pathname for the file

Can have several files with the same file name as long as they have unique path
names

Current directory is the working directory

Files are referenced relative to the working directory

DEPARTMENT OF COMPUTER SCIENCE AND ENGINEERING

Page 143 of 157

CS0206-OPERATING SYSTEMS

SRM UNIVERSITY,RAMAPURAM

DEPARTMENT OF COMPUTER SCIENCE AND ENGINEERING

Page 144 of 157

CS0206-OPERATING SYSTEMS

SRM UNIVERSITY,RAMAPURAM

File Sharing

In multiuser system, allow files to be shared among users

Two issues
o Access rights
o Management of simultaneous access

Access Rights:

None
o User may not know of the existence of the file
o User is not allowed to read the user directory that includes the file

Knowledge
o User can only determine that the file exists and who its owner is

Execution
o The user can load and execute a program but cannot copy it

Reading
o The user can read the file for any purpose, including copying and
execution

Appending
o The user can add data to the file but cannot modify or delete any of the
files contents

Updating
o The user can modify, deleted, and add to the files data. This includes
creating the file, rewriting it, and removing all or part of the data

Changing protection
o User can change access rights granted to other users

Deletion
o User can delete the file

Owners
o Has all rights previously listed
o May grant rights to others using the following classes of users

Specific user

User groups

DEPARTMENT OF COMPUTER SCIENCE AND ENGINEERING

Page 145 of 157

CS0206-OPERATING SYSTEMS

SRM UNIVERSITY,RAMAPURAM

All for public files

Management of simultaneous Access:

User may lock entire file when it is to be updated

User may lock the individual records during the update

Mutual exclusion and deadlock are issues for shared access

Record Blocking
Fixed Blocking:

DEPARTMENT OF COMPUTER SCIENCE AND ENGINEERING

Page 146 of 157

CS0206-OPERATING SYSTEMS

SRM UNIVERSITY,RAMAPURAM

Variable Blocking-Spanned:

Variable Blocking-Unspanned:

DEPARTMENT OF COMPUTER SCIENCE AND ENGINEERING

Page 147 of 157

CS0206-OPERATING SYSTEMS

SRM UNIVERSITY,RAMAPURAM

Secondary Storage Management

Space must be allocated to files

Must keep track of the space available for allocation

Preallocation:

Need the maximum size for the file at the time of creation

Difficult to reliably estimate the maximum potential size of the file

Tend to overestimated file size so as not to run out of space

Methods of File Allocation:

Contiguous allocation
o Single set of blocks is allocated to a file at the time of creation
o Only a single entry in the file allocation table

Starting block and length of the file

o External fragmentation will occur


o Need to perform compaction

DEPARTMENT OF COMPUTER SCIENCE AND ENGINEERING

Page 148 of 157

CS0206-OPERATING SYSTEMS

SRM UNIVERSITY,RAMAPURAM

Chained allocation

Allocation on basis of individual block

Each block contains a pointer to the next block in the chain

Only single entry in the file allocation table


o Starting block and length of file

No external fragmentation

Best for sequential files

No accommodation of the principle of locality

DEPARTMENT OF COMPUTER SCIENCE AND ENGINEERING

Page 149 of 157

CS0206-OPERATING SYSTEMS

SRM UNIVERSITY,RAMAPURAM

DEPARTMENT OF COMPUTER SCIENCE AND ENGINEERING

Page 150 of 157

CS0206-OPERATING SYSTEMS

SRM UNIVERSITY,RAMAPURAM

Indexed allocation
o File allocation table contains a separate one-level index for each file
o The index has one entry for each portion allocated to the file
o The file allocation table contains block number for the index

DEPARTMENT OF COMPUTER SCIENCE AND ENGINEERING

Page 151 of 157

CS0206-OPERATING SYSTEMS

SRM UNIVERSITY,RAMAPURAM

DEPARTMENT OF COMPUTER SCIENCE AND ENGINEERING

Page 152 of 157

CS0206-OPERATING SYSTEMS

SRM UNIVERSITY,RAMAPURAM

CASE STUDIES
LINUX FILE MANAGEMENT
Linux Virtual File System:

Uniform file system interface to user processes

Represents any conceivable file systems general feature and behavior

Assumes files are objects that share basic properties regardless of the target file
system

DEPARTMENT OF COMPUTER SCIENCE AND ENGINEERING

Page 153 of 157

CS0206-OPERATING SYSTEMS

SRM UNIVERSITY,RAMAPURAM

Primary Objects in VFS:

Superblock object
o Represents a specific mounted file system

Inode object
o Represents a specific file

Dentry object
o Represents a specific directory entry

File object
o Represents an open file associated with a process

DEPARTMENT OF COMPUTER SCIENCE AND ENGINEERING

Page 154 of 157

CS0206-OPERATING SYSTEMS

SRM UNIVERSITY,RAMAPURAM

UNIX File Management


Types of files:
o Regular, or ordinary
o Directory
o Special
o Named pipes
o Links
o Symbolic links
Inodes:

Index node

Control structure that contains key information for a particular file

DEPARTMENT OF COMPUTER SCIENCE AND ENGINEERING

Page 155 of 157

CS0206-OPERATING SYSTEMS

SRM UNIVERSITY,RAMAPURAM

DEPARTMENT OF COMPUTER SCIENCE AND ENGINEERING

Page 156 of 157

CS0206-OPERATING SYSTEMS

SRM UNIVERSITY,RAMAPURAM

DEPARTMENT OF COMPUTER SCIENCE AND ENGINEERING

Page 157 of 157

You might also like