You are on page 1of 109

Maharaja Surajmal Institute (Affiliated to GGSIPU, Delhi)

Course: Bachelor of Computer Application Subject Module

On

Operating System (BCA-301) Semester V (Credit: 4)

Module Contributor(s):

Mr. Suraj Pal Chauhan


Dr. Pooja Singh
Ms. Vinita Tomar
Ms. Kanika Kundu

1
Operating System (BCA 301)

Module Objectives: This module consists of four units related to assessment of Learning. Every unit is
divided into lessons according to the content of each unit. The module focuses on the core concepts of
operating systems. It primarily includes the concepts of operating systems including memory management,
process management, information management and device management.

Unit-I: In this unit you will be acquainted with different concepts associated with the various available
Operating Systems. It discusses about the different types of operating systems including , Simple Batch
Systems, Multiprogrammed Batches systems, Time-Sharing Systems, Personal-computer systems, Parallel
systems, Distributed Systems, Real-Time Systems Memory Management: The unit also discusses the concept
of memory management schemes of operating systems that includes swapping, Contiguous allocation, Paging,
Segmentation. It also reflects the importance of Virtual Memory on the basis of Demand Paging, Page
Replacement, Page-replacement Algorithms, Performance of Demand Paging, Allocation of Frames and
Thrashing.

Unit-II: This unit will make you enable to acquire the features of Processes in operating systems. It discusses
the Process Concept, Process Scheduling and various Operation on Processes. The unit also discusses the
concept of CPU Scheduling, different Scheduling Criteria and various Scheduling Algorithms. The
mechanism of Process Synchronization is also taken up in this unit which is further achieved by understanding
The Critical-Section Problem implemented with the help of Synchronization Hardware, Semaphores and also
the Classical Problems of Synchronizations are discussed as an integral part of the unit.

Unit-III: This unit will empower you to understand the need and process of Deadlocks and Device
Management. The unit includes the very important topic of Deadlocks in operating system. It includes the
concept of System Model, Deadlock Characterization and Methods for Handling Deadlocks. Apart from this
it includes the various Deadlock Prevention, Deadlock Avoidance and Deadlock Detection techniques. Apart
from this the recovery mechanism from deadlocks is also discussed in this unit. The Device Management:
Techniques are discussed in this unit. Starting with the various types for Devices including Dedicated Devices,
Shared Devices, Virtual Devices; Input or Output Devices and Storage Devices. The device management also
talks about the techniques of Buffering and Secondary Storage Structure. The detailed study of Disk Structure
is also performed in this unit. Also the techniques of Disk Scheduling, Disk Management, Swap-Space
Management and Disk Reliability is also taken up as a part of this unit.

Unit-IV: You will be recognizing the concept of Information Management. It discusses the different file
systems approaches including A Simple File System and General Model of a File System. The Types of File
Systems and File-System Interface are also taken up in this unit. Apart from the concept of Files the various
Access Methods are also discussed. Along with file system the Directory Structure is also included. The
Protection Goals of file system is discussed. The various Access rights are also studied to ensure the protection
and security to the file systems. It is performed through the concept of Consistency Semantics Security along
with Authentication mechanism. It avoids various kinds of Program threats and System threats. The
Encryption techniques are discussed to enable the security parameter to the files in operating system.

2
CONTENTS
Unit No. Unit Name Page Number
I Operating System and concepts

Lesson 1: Introduction to Operating System 4-38


Lesson 2: Memory Management
Lesson 3: Virtual Memory

II Processes

Lesson1: Process 39-74


Lesson2: CPU Scheduling
Lesson3: Process Synchronization

III Deadlocks
75-84
Lesson 1: Deadlock Detection and Protection
Lesson 2: Device management
IV Information Management
85-108
Lesson 1: File System Interface
Lesson 2: Protection and File Security
Glossary/Key Words 109
References and Further Readings 109

3
UNIT-I OPERATING SYSTEM AND CONCEPTS

A) Learning Outcomes of the unit:

After completing this unit/lesson, the students will be able to:

 Explain the concepts of operating system.


 Understand the different types of operating system
 Identify different functions of operating system.
 Understand the concepts of memory management.
 Relate the difference between paging and segmentation
 Understand the process of swapping
 Understand the concepts of demand paging and its performance.
 Different types of page replacement algorithms.
 Understand the concepts of thrashing.

B) Introduction
An Operating System (OS) is an interface between a computer user and computer hardware. An operating
system is a software which performs all the basic tasks like file management, memory management, process
management, handling input and output, and controlling peripheral devices such as disk drives and printers.
Memory management is the functionality of an operating system which handles or manages primary memory
and moves processes back and forth between main memory and disk during execution. Memory management
keeps track of each and every memory location, regardless of either it is allocated to some process or it is free.
It checks how much memory is to be allocated to processes. It decides which process will get memory at what
time. It tracks whenever some memory gets freed or unallocated and correspondingly it updates the status.
Virtual Memory is a storage allocation scheme in which secondary memory can be addressed as though it
were part of main memory. The addresses a program may use to reference memory are distinguished from the
addresses the memory system uses to identify physical storage sites, and program generated addresses are
translated automatically to the corresponding machine addresses. The size of virtual storage is limited by the
addressing scheme of the computer system and amount of secondary memory is available not by the actual
number of the main storage locations.
It is a technique that is implemented using both hardware and software. It maps memory addresses used by a
program, called virtual addresses, into physical addresses in computer memory.

C) Body of Content

The topics covered in this unit include:


Lesson 1: Operating system and its concepts, Different types of operating system, Functions of operating
system.

Lesson 2: Memory management and its concepts. Logical versus Physical


4
Address space, swapping, Contiguous allocation, difference between Paging and Segmentation

Lesson 3: Virtual memory and its concepts. Demand Paging, Different Page-replacement Algorithms,
Performance of Demand Paging, Allocation of Frames, Thrashing, Other Considerations.

Lesson 1: Introduction to Operating System

Operating System -Introduction


An Operating System (OS) is an interface between a computer user and computer hardware. An operating
system is a software which performs all the basic tasks like file management, memory management, process
management, handling input and output, and controlling peripheral devices such as disk drives and printers.
Some popular Operating Systems include Linux, Windows, OS X, VMS, OS/400, AIX, z/OS, etc.

An operating system is a program that acts as an interface between the user and the computer hardware and
controls the execution of all kinds of programs.

Operating system major goals:


 Execute user programs and make solving user problems easier.
 Optimize the use of computer resources so as to maximize its throughput.
 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).

Abstract View of System Components:

5
Functions of Operating System

 Memory Management
 Processor Management
 Device Management
 File Management
 Security
 Control over system performance
 Job accounting
 Error detecting aids
 Coordination between other software and users

Memory Management
Memory management refers to management of Primary Memory or Main Memory. Main memory is a large
array of words or bytes where each word or byte has its own address.
Main memory provides a fast storage that can be accessed directly by the CPU. For a program to be executed,
it must be in the main memory. An Operating System does the following activities for memory management

• Keeps tracks of primary memory, i.e., what part of it are in use by whom, what part are not in use.
• In multiprogramming, the OS decides which process will get memory when and how much.
• Allocates the memory when a process requests it to do so.
• De-allocates the memory when a process no longer needs it or has been terminated.

6
Processor Management
In multiprogramming environment, the OS decides which process gets the processor when and for how much
time. This function is called process scheduling. An Operating System does the following activities for
processor management −
• Keeps tracks of processor and status of process. The program responsible for this task is known as
traffic controller.
• Allocates the processor (CPU) to a process.
• De-allocates processor when a process is no longer required.

Device Management
An Operating System manages device communication via their respective drivers. It does the following
activities for device management −
• Keeps tracks of all devices. Program responsible for this task is known as the I/O controller.
• Decides which process gets the device when and for how much time.
• Allocates the device in the efficient way.
• De-allocates devices.

File Management
A file system is normally organized into directories for easy navigation and usage. These directories may
contain files and other directions.
An Operating System does the following activities for file management −
• Keeps track of information, location, uses, status etc. The collective facilities are often known as file
system.
• Decides who gets the resources.

• Allocates the resources.


• De-allocates the resources.

Other Important Activities


Following is some of the important activities that an Operating System performs −
Security − By means of password and similar other techniques, it prevents unauthorized access to programs
and data.
Control over system performance − Recording delays between request for a service and response from the
system.
Job accounting − Keeping track of time and resources used by various jobs and users.
Error detecting aids − Production of dumps, traces, error messages, and other debugging and error detecting

7
aids.
Coordination between other softwares and users − Coordination and assignment of compilers, interpreters,
assemblers and other software to the various users of the computer systems.

Types of Operating System


Operating systems are there from the very first computer generation and they keep evolving with time. In this
chapter, we will discuss some of the important types of operating systems which are most commonly used.

Batch operating system


The users of a batch operating system do not interact with the computer directly. Each user prepares his job
on an off-line device like punch cards and submits it to the computer operator. To speed up processing, jobs
with similar needs are batched together and run as a group. The programmers leave their programs with the
operator and the operator then sorts the programs with similar requirements into batches.

The problems with Batch Systems are as follows −


• Lack of interaction between the user and the job.
• CPU is often idle, because the speed of the mechanical I/O devices is slower than the CPU.
• Difficult to provide the desired priority.

Time-sharing operating systems


Time-sharing is a technique which enables many people, located at various terminals, to use a particular
computer system at the same time. Time- sharing or multitasking is a logical extension of multiprogramming.
Processor's time which is shared among multiple users simultaneously is termed as time-sharing.
The main difference between Multiprogrammed Batch Systems and Time- Sharing Systems is that in case of
Multiprogrammed batch systems, the objective is to maximize processor use, whereas in Time-Sharing
Systems, the objective is to minimize response time.
Multiple jobs are executed by the CPU by switching between them, but the switches occur so frequently.
Thus, the user can receive an immediate response. For example, in a transaction processing, the processor
executes each user program in a short burst or quantum of computation. That is, if n users are present, then
each user can get a time quantum. When the user submits the command, the response time is in few seconds
at most.
The operating system uses CPU scheduling and multiprogramming to provide each user with a small portion
of a time. Computer systems that were designed primarily as batch systems have been modified to time-
sharing systems.

Advantages of Timesharing operating systems are as follows −


• Provides the advantage of quick response.
• Avoids duplication of software.
8
• Reduces CPU idle time.

Disadvantages of Time-sharing operating systems are as follows −


• Problem of reliability.
• Question of security and integrity of user programs and data.
• Problem of data communication.

Distributed operating System


Distributed systems use multiple central processors to serve multiple real- time applications and multiple
users. Data processing jobs are distributed among the processors accordingly.
The processors communicate with one another through various communication lines (such as high-speed
buses or telephone lines). These are referred as loosely coupled systems or distributed systems. Processors in
a distributed system may vary in size and function. These processors are referred as sites, nodes, computers,
and so on.

The advantages of distributed systems are as follows −


• With resource sharing facility, a user at one site may be able to use the resources available at another.
• Speedup the exchange of data with one another via electronic mail.
• If one site fails in a distributed system, the remaining sites can potentially continue operating.
• Better service to the customers.
• Reduction of the load on the host computer.
• Reduction of delays in data processing.

Network operating System


A Network Operating System runs on a server and provides the server the capability to manage data, users,
groups, security, applications, and other networking functions. The primary purpose of the network operating
system is to allow shared file and printer access among multiple computers in a network, typically a local area
network (LAN), a private network or to other networks.
Examples of network operating systems include Microsoft Windows Server 2003, Microsoft Windows Server
2008, UNIX, Linux, Mac OS X, Novell NetWare, and BSD.

The advantages of network operating systems are as follows −


• Centralized servers are highly stable.
• Security is server managed.
• Upgrades to new technologies and hardware can be easily integrated into the system.
• Remote access to servers is possible from different locations and types of systems.

9
The disadvantages of network operating systems are as follows −
• High cost of buying and running a server.
• Dependency on a central location for most operations.
• Regular maintenance and updates are required.

Real Time operating System


A real-time system is defined as a data processing system in which the time interval required to process and
respond to inputs is so small that it controls the environment. The time taken by the system to respond to an
input and display of required updated information is termed as the response time. So, in this method, the
response time is very less as compared to online processing.
Real-time systems are used when there are rigid time requirements on the operation of a processor or the flow
of data and real-time systems can be used as a control device in a dedicated application. A real-time operating
system must have well-defined, fixed time constraints, otherwise the system will fail. For example, Scientific
experiments, medical imaging systems, industrial control systems, weapon systems, robots, air traffic control
systems, etc.

There are two types of real-time operating systems.


Hard real-time systems
Hard real-time systems guarantee that critical tasks complete on time. In hard real-time systems, secondary
storage is limited or missing and the data is stored in ROM. In these systems, virtual memory is almost never
found.

Soft real-time systems


Soft real-time systems are less restrictive. A critical real-time task gets priority over other tasks and retains
the priority until it completes. Soft real-time systems have limited utility than hard real-time systems. For
example, multimedia, virtual reality, Advanced Scientific Projects like undersea exploration and planetary
rovers, etc.

Operating System – Properties


Batch processing
Batch processing is a technique in which an Operating System collects the programs and data together in a
batch before processing starts. An operating system does the following activities related to batch processing

• The OS defines a job which has predefined sequence of commands, programs and data as a single unit.
• The OS keeps a number a job in memory and executes them without any manual information.
• Jobs are processed in the order of submission, i.e., first come first served fashion.

10
• When a job completes its execution, its memory is released and the output for the job gets copied into an
output spool for later printing or processing.

Advantages
 Batch processing takes much of the work of the operator to the computer.
 Increased performance as a new job gets started as soon as the previous job is finished,
without any manual intervention.

Disadvantages

 Difficult to debug program.


 A job could enter an infinite loop.
 Due to lack of protection scheme, one batch job can affect pending jobs.

Multitasking
Multitasking is when multiple jobs are executed by the CPU simultaneously by switching between them.
Switches occur so frequently that the users may interact with each program while it is running. An OS does
the following activities related to multitasking −
 The user gives instructions to the operating system or to a program directly, and receives an immediate
response.
 The OS handles multitasking in the way that it can handle multiple operations / executes multiple programs
at a time.
 Multitasking Operating Systems are also known as Time-sharing systems.
 These Operating Systems were developed to provide interactive use of a computer system at a reasonable
cost.
 A time-shared operating system uses the concept of CPU scheduling and multiprogramming to provide
each user with a small portion of a time-shared CPU.
 Each user has at least one separate program in memory
11
 A program that is loaded into memory and is executing is commonly referred to as a process.
 When a process executes, it typically executes for only a very short time before it either finishes or needs
to perform I/O.
 Since interactive I/O typically runs at slower speeds, it may take a long time to complete. During this time,
a CPU can be utilized by another process.
 The operating system allows the users to share the computer simultaneously. Since each action or command
in a time-shared system tends to be short, only a little CPU time is needed for each user.
 As the system switches CPU rapidly from one user/program to the next, each user is given the impression
that he/she has his/her own CPU, whereas actually one CPU is being shared among many users.

Multiprogramming
Sharing the processor, when two or more programs reside in memory at the same time, is referred as
multiprogramming. Multiprogramming assumes a single shared processor. Multiprogramming increases CPU
utilization by organizing jobs so that the CPU always has one to execute.
The following figure shows the memory layout for a multiprogramming system.

An OS does the following activities related to multiprogramming.


• The operating system keeps several jobs in memory at a time.
• This set of jobs is a subset of the jobs kept in the job pool.
• The operating system picks and begins to execute one of the jobs in the memory.
Multiprogramming operating systems monitor the state of all active programs and system resources using
memory management programs to ensures that the CPU is never idle, unless there are no jobs to process.
Advantages
 High and efficient CPU utilization.

 User feels that many programs are allotted CPU almost simultaneously.
Disadvantages
 CPU scheduling is required.

12
 To accommodate many jobs in memory, memory management is required.

Interactivity
Interactivity refers to the ability of users to interact with a computer system. An Operating system does the
following activities related to interactivity −

 Provides the user an interface to interact with the system.


 Manages input devices to take inputs from the user. For example, keyboard.
 Manages output devices to show outputs to the user. For example, Monitor.
The response time of the OS needs to be short, since the user submits and waits for the result.

Real Time System


Real-time systems are usually dedicated, embedded systems. An operating system does the following
activities related to real-time system activity.
 In such systems, Operating Systems typically read from and react to sensor data.
 The Operating system must guarantee response to events within fixed periods of time to
ensure correct performance.

Distributed Environment
A distributed environment refers to multiple independent CPUs or processors in computer system. An
operating system does the following activities related to distributed environment −
 The OS distributes computation logics among several physical processors.
 The processors do not share memory or a clock. Instead, each processor has its own local
memory.
 The OS manages the communications between the processors. They communicate with each
other through various communication lines.

13
Lesson 2: Memory Management

Memory Management
Memory management is the functionality of an operating system which handles or manages primary memory
and moves processes back and forth between main memory and disk during execution. Memory management
keeps track of each and every memory location, regardless of either it is allocated to some process or it is free.
It checks how much memory is to be allocated to processes. It decides which process will get memory at what
time. It tracks whenever some memory gets freed or unallocated and correspondingly it updates the status.
 Single process monitor
 Mono programming or uni programming system.
 Program must be brought (from disk) into memory and placed within a process for it to be executed.
 Input queue – collection of processes on the disk that are waiting to be brought into memory for execution.
 User programs go through several steps before being executed
 Binding of Instructions and Data to Memory
 Address binding of instructions and data to memory addresses can happen at three different stages
 Compile time: If memory location known a priori, absolute code can be generated; must recompile code
if starting location changes.
 Load time: Must generate relocatable code if memory location is not known at compile time
 Execution time: Binding delayed until run time if the process can be moved during its execution from one
memory segment to another. Need hardware support for address maps (e.g., base and limit registers)
Multistep Processing of a User Program

Logical vs. Physical Address Space


14
 The concept of a logical address space that is bound to a separate physical address space is central to proper
memory management.
 Logical address – is the address of an instruction or data as used by a program; generated by the CPU; also
referred to as virtual address.
 Physical address – is the effective memory address of an instruction or data that is obtained after the address
binding has been done i.e. after logical addresses are mapped to their physical addresses. address seen by the
memory unit.
 Logical and physical addresses are the same in compile-time and load-time address-binding schemes; and
logical (virtual) and physical addresses differ in execution-time address-binding scheme.

Dynamic Loading
 If the routines are loaded in the main memory at the time of execution or running then it is called dynamic
loading.
 Routine is not loaded until it is called.
 Advantages - Better memory-space utilization; unused routine is never loaded.
 Useful when large amounts of code are needed to handle infrequently occurring cases.
 No special support from the operating system is required implemented through program design.

Dynamic Linking
 Linking at the time of execution is known as dynamic linking.
 Linking is postponed until execution time.
 Small piece of code, stub (is used to locate the appropriate memory-resident library routine.) is included in
the image for each library routine reference.
 When the stub is executed, it checks to see whether the needed routine is already in memory if not it loads
it into the memory.
 Stub replaces itself with the address of the routine, and executes the routine.
 Unlike dynamic loading, dynamic linking generally requires help from the operating system.

Swapping
 A Process needs to be in memory to be executed.
 A process can be swapped temporarily out of memory to a backing store, and then brought back into
memory for continued execution. (fig.)

15
 Backing store – fast disk large enough to accommodate copies of all memory images for all users; must
provide direct access to these memory images.
 Roll out, roll in variant – swapping variant used for priority-based scheduling algorithms; lower-priority
process is swapped out so higher-priority process can be loaded and executed.
 Major part of swap time is transfer time; total transfer time is directly proportional to the amount of memory
swapped.
 Modified versions of swapping are found on many systems, i.e., LINUX and Microsoft Windows.

System maintains a ready queue of ready-to-run processes which have memory images on disk

Contiguous Allocation

 Main memory usually divided into two partitions:


o Resident operating system, usually held in low memory with interrupt vector.
o User processes, usually held in high memory.

16
Partitioning

Single-partition allocation
In this type, relocation register scheme are used to protect user processes from each other, and from changing
operating-system code and data.
Relocation register contains value of smallest physical address and limit register contains range of logical
addresses. Each logical address must be less than the limit register.

Multiple-partition allocation
In this type, main memory is divided into a number of fixed sized partitions where each partition should
contain only one process. When a partition is free a process is selected from the input queue and is loaded
into free partition. When the process terminates the partition becomes available for another process. It supports
multiprogramming

o MFT (Multiprogramming with fixed no. of tasks)


o Fixed partition memory – In this the main memory is divided into a number of fixed partitions at the system
generation time. It is also known as static partitioning.
o No. of partitions are fixed and but can be of different sizes.
o 1 Partition = 1 Process
o 2) MVT (Multiprogramming with variable no of tasks)
o Variable partitioning – In this the number of partitions and their sizes are variable. They are not defined at
the time of system generation. It is also known as dynamic partitioning as partition are created dynamically.

Hole – block of available memory; holes of various size are scattered throughout memory.

o When a process arrives, it is allocated memory from a hole large enough to accommodate it.
o Operating system maintains information about:
a) allocated partitions b) free partitions (hole)
When process 8 terminates then it creates a hole. Then process 9 starts and so on

Dynamic Storage-Allocation Problem


How to satisfy a request of size n from a list of free holes?

17
There are many solutions to this problem. The set of holes is searched to determine which hole is best to
allocate. There are some common strategies (algorithms) used to select a free hole from the set of available
holes -

o First-fit: Allocates the process in first hole or first free large enough partition.
o Best-fit: Allocates the process in the smallest possible hole or memory partition.
o Worst-fit: Allocates the process in the largest hole or largest possible memory partition; must also search
entire list
o Next-fit: It is same as first fit but doesn’t start from first partition. It starts from the place where the last
partition ended. It is fast in searching.

o External fragmentation – it exists when enough total memory space exists to satisfy a request, but it is not
contiguous. Storage is fragmented into a large number of small holes.
o
o Internal fragmentation – allocated memory may be slightly larger than requested memory; this size
difference is memory internal to a partition, but not being used.
o One solution to the problem of external fragmentation is compaction.

 The goal is to shuffle memory contents to place all free memory together in one large block.
 Compaction is possible only if relocation is dynamic, and is done at execution time.
 Eg. The three holes of sizes 100k, 300k and 260k can be compacted into one hole of size 660k
 As Compaction is not always possible in every case so we have other solutions also.

Address Translation Scheme

 Every Address generated by CPU is divided into 2 parts:


o Page number (p) – used as an index into a page table which contains base address of each page in physical
memory
o Page offset (d) – combined with base address to define the physical memory address that is sent to the
memory unit
o It’s given that logical address space 2m and page size 2n

Paging
A computer can address more memory than the amount physically installed on the system. This extra memory
is actually called virtual memory and it is a section of a hard that's set up to emulate the computer's RAM.
Paging technique plays an important role in implementing virtual memory.
Paging is a memory management technique in which process address space is broken into blocks of the same
size called pages (size is power of 2, between 512 bytes and 8192 bytes). The size of the process is measured
in the number of pages.
Similarly, main memory is divided into small fixed-sized blocks of (physical) memory called frames and the
size of a frame is kept the same as that of a page to have optimum utilization of the main memory and to
avoid external fragmentation.

18
Paging Hardware Architecture

Memory Protection

o For eg- Suppose one Page or Frame is only in read mode.


o So, modification is not allowed for this Page or Frame.
o So, this is one kind of protection with the help of protection bits
o Memory protection is done with the help of protection bits with each frame.
o Bits are maintained in Page Table so during Frame no. in Page table Protection bits are also checked.
o Protection bits are Valid-invalid bit attached to each entry in the page table:
 “valid” indicates that the associated page is in the process’ logical address space, and is thus a legal page.
 “invalid” indicates that the page is not in the process’ logical address space.

Hierarchical Page Tables

o We use it when we want to store lakhs of entries in page table.


o Break up the logical address space into multiple page tables
o A simple technique is a two-level page table

Segmentation
Segmentation is a memory management technique in which each job is
divided into several segments of different sizes, one for each module that
contains pieces that perform related functions. Each segment is actually a different logical address space of
the program.

o Memory-management scheme that supports user view of memory


o Here, we have unequal sized partitions of logical memory.
o A program is a collection of segments
19
A segment is a logical unit such as:
main program
procedure
function
method
object
local variables, global variables
common block
stack
symbol table
arrays

Segmentation Hardware support

20
Lesson 3: Virtual Memory

Virtual Memory

Virtual memory – is a separation of user logical memory from physical memory. In this method, we keep only
a part of the user’s point of view process in the memory and other part on the disk (Secondary storage). Here,
we need to maintain entire image of the process in disk storage.

Advantages-
 Only part of the program needs to be in memory for execution
 Logical address space can therefore be much larger than physical address space
 Need to allow pages to be swapped in and swapped out.

Virtual memory can be implemented via:


 Demand paging
 Demand segmentation

Demand Paging
o Complete process should be in the disk in the form of pages.
o In this technique, a page is brought into the memory for its execution only when it is demanded.

The process of loading the page into memory on demand (whenever page fault occurs) is known as demand
paging.

21
The process includes the following steps:

Advantages
 Less I/O needed
 Reduces memory requirement.
 Swap time is also reduced.
 Faster response
 More users

Disadvantages – the occurrence of page fault.


o Page is needed  reference to it
 invalid reference  abort
 not-in-memory  bring to memory

22
Also known as Lazy Swapper method as it swaps the pages only when it is needed.

Page Fault

o When the page (data) requested by the program is not available in the memory, it is called page fault. This
usually results in the application being shut down.
o It is used in the performance measurement.
o If there is a reference to a page, first reference to that page will trap to operating system:

1. Operating system looks at another table to decide:


 Invalid reference  abort
 Just not in memory
2. Get empty frame
3. Swap page into frame
4. Reset tables
5. Set validation bit = v
6. Restart the instruction that caused the page fault

23
Page Replacement Algorithm

o It is the technique used by operating system to decide which memory pages to swap out.
o It is also decided that in memory, how many frames to allocate to each process.
o When page replacement is required, we must select the frames that are to be replaced.
o Prevent over-allocation of memory by modifying page-fault service routine to include page replacement
o Use modify (dirty) bit to reduce overhead of page transfers – only modified pages are written to disk
o Page replacement completes separation between logical memory and physical memory – large virtual
memory can be provided on a smaller physical memory

Basic Page Replacement

1. Find the location of the desired page on disk


2. Find a free frame:
- If there is a free frame, use it
- If there is no free frame, use a page replacement algorithm to select a victim frame
3. Bring the desired page into the (newly) free frame; update the page and frame tables
4. Restart the process

24
o Want lowest page-fault rate
o Evaluate algorithm by running it on a particular string of memory references (reference string) and
computing the number of page faults on that string

Reference String – string of memory references is called as reference string.


o In all our examples, the reference string is
1, 2, 3, 4, 1, 2, 5, 1, 2, 3, 4, 5
Page fault – it is a type of interrupt raised when a running process accesses a memory page that is mapped
into virtual memory but not loaded in main memory.

Graph of Page Faults Versus the Number of Frames

25
26
Counting Algorithms
o Keep a counter of the number of references that have been made to each page
o LFU Algorithm (Least Frequently used): replaces page with smallest count or frequency
o MFU Algorithm (Most Frequently Used) : replaces a page that has the maximum frequency count of all
pages.

Allocation of Frames
OS allocates Frames to Main Memory using these allocation schemes -

o Equal allocation –Frames will be allocated equally among processes.


Suppose, Process P1 -> requires 10 frames for its execution
P2 -> requires 30 frames for its execution
Let Main memory = 40 frames
So, 20 frames will be given to P1 and P2 (memory wasted)
o Proportional allocation – Allocate according to the size of process.
P1 -> Frames required by P1/ (P1+P2) * Memory Frames
P1 -> 10/ (10+30) * 40 => 10/40 * 40 = 10 frames
P2 -> Frames required by P2/ (P1+P2) * Memory Frames
P2 -> 30 /(10+30) * 40 =>30/40 * 40 = 30 frames
27
Priority Allocation –Process having high priority will be allocated more frames and process having low
priority will allocated less frames.
o Use a proportional allocation scheme using priorities rather than size
o If process Pi generates a page fault,
 select for replacement one of its frames
 select for replacement a frame from a process with lower priority number

Global replacement – process selects a replacement frame from the set of all frames; one process can take a
frame from another process.
o That is, Process P1 can take frame from Process P2 and vice versa.
o Here we can store page of Process P1 in Frame of Process P2 also and vice versa.
o Disadvantage is that the paging behavior of one process depends upon paging behavior of another process.

Local replacement – each process selects from only its own set of allocated frames

Thrashing
Thrashing is a situation if the system spends more time in paging instead of their execution.
OR
Thrashing is a process where processes are busy swapping pages in and out.
If a process does not have “enough” pages for execution then the page-fault rate will be very high. This leads
to:
o low CPU utilization
o operating system thinks that it needs to increase the degree of multiprogramming
o another process added to the system

Methods to handle Thrashing


o Working Set Model
 The set of pages that a process is currently using is called its working set.
• Working set model works based on the principle of locality of reference. It specifies that over a short period
of time certain number of instructions are executed repeatedly and repeatedly.
• WS principle states that –
• A program should not run, unless its working set is fully loaded in the memory.
• No page of a process should be replaced, if it forms part of its working set.
• WS is represented with  and lets =10

28
   working-set window i.e a fixed number of page references
Example: 10,000 instruction
 Three cases arise-
l if  too small will not encompass entire locality
l if  too large will encompass several localities
l if  =   will encompass entire program
 if D (demand for the process) > m (main memory frames)  Thrashing will occur
 Policy if D > m, then suspend one of the processes

2) Page-Fault Frequency Scheme

 Establish “acceptable” page-fault rate


l If actual rate too low, process loses frame
l If actual rate too high, process gains frame

Demand Paging
Operating system also uses demand segmentation, which is similar to
demand paging. Operating system to uses demand segmentation where
there is insufficient hardware available to implement ‘Demand Paging’.

29
o The segment table has a valid bit to specify if the segment is already in physical memory or not. If a
segment is not in physical memory, then segment fault results, which traps to the operating system and brings
the needed segment into physical memory, much like a page fault.
o Demand segmentation allows for pages that are often referenced with each other to be brought into memory
together, this decreases the number of page faults.
o If there is no space in the memory then LRU segment is chosen for replacement and is written to disk.
But it suffers from the problem of external fragmentation.

D) Conclusion
This unit is an attempt to provide a detailed understanding of operating system, its functions and its types.
Concepts of memory management, swapping, difference between paging and segmentation and detailed
description of virtual memory with all replacement algorithms. Concepts of thrashing and other
considerations.

E) Progress Check: MCQ’s and question answer

a) Multiple Choice Questions

Q1. Which of the following page replacement algorithms suffers from Belady’s anomaly?
A) FIFO
B) LRU
C) Optimal Page Replacement
D) Both LRU and FIFO

Q2. What is the swap space in the disk used for?


A) Saving temporary html pages
B) Saving process data
C) Storing the super-block
D) Storing device drivers

Q3. Increasing the RAM of a computer typically improves performance because:


A) Virtual memory increases
B) Larger RAMs are faster
C) Fewer page faults occur
D) Fewer segmentation faults occur

Q4. When the memory allocated to a process is slightly larger than the process, then:
A) internal fragmentation occurs
B) external fragmentation occurs
C) both a and b
D) neither a nor

Q5. Virtual memory is

30
A) Large secondary memory
B) Large main memory
C) Illusion of large main memory
D) None of the above

Q6. Page fault occurs when


A) When a requested page is in memory
B) When a requested page is not in memory
C) When a page is currupted
D) When an exception is thrown

Q7. Thrashing occurs when


A) When a page fault occurs
B) Processes on system frequently access pages not memory
C) Processes on system are in running state
D) Processes on system are in waiting state

Q8. Consider data given in the above question. What is the minimum number of pages colours needed to
guarantee that no two synonyms map to different sets in the processor cache of this computer?
A) 2
B) 4
C) 8
D) 16

Q9. Sometimes the overhead of keeping track of a hole might be :


A) larger than the memory
B) larger than the hole itself
C) very small
D) All of these

Q10. The essential content(s) in each entry of a page table is / are


A) Virtual page numbers
B) Page frame number
C) Both virtual page number and page frame number
D) Access right information

Q11. A multilevel page table is preferred in comparison to a single level page table for translating virtual
address to physical address because
A) It reduces the memory access time to read or write a memory location.
B) It helps to reduce the size of page table needed to implement the virtual address space of a process.
C) It is required by the translation lookaside buffer.
D) It helps to reduce the number of page faults in page replacement algorithms.

Q12. External fragmentation will not occur when:


A) first fit is used
B) best fit is used
C) worst fit is used
31
D) no matter which algorithm is used, it will always occur
Q13. CPU fetches the instruction from memory according to the value of:
A) program counter
B) status register
C) instruction registers
D) program status word

Q14. A memory buffer used to accommodate a speed differential is called:


A) stack pointer
B) cache
C) accumulator
D) disk buffer

Q15. Which one of the following is the address generated by CPU?


A) physical address
B) absolute address
C) logical address
D) none of the mentioned

Q16. Run time mapping from virtual to physical address is done by:
A) memory management unit
B) CPU
C) PCI
D) none of the mentioned

Q17. Memory management technique in which system stores and retrieves data from secondary storage for
use in main memory is called:
A) fragmentation
B) paging
C) mapping
D) none of the mentioned

Q18. The address of a page table in memory is pointed by:


A) stack pointer
B) page table base register
C) page register
D) program counter

Q19. Program always deals with:


A) logical address
B) absolute address
C) physical address
D) relative address

Q20. The page table contains:


A) base address of each page in physical memory
B) page offset
32
C) page size
D) none of the mentioned

Q21. What is compaction?


A) a technique for overcoming internal fragmentation
B) a paging technique
C) a technique for overcoming external fragmentation
D) a technique for overcoming fatal error

Q22 Operating System maintains the page table for:


A) each process
B) each thread
C) each instruction
D) each address

Q23. A solution to the problem of external fragmentation is :


A) compaction
B) larger memory space
C) smaller memory space
D) None of these

Q24. Another solution to the problem of external fragmentation problem is to :


A) permit the logical address space of a process to be noncontiguous
B) permit smaller processes to be allocated memory at last
C) permit larger processes to be allocated memory at last
D) All of these

Q25. If relocation is static and is done at assembly or load time, compaction _________.
A) cannot be done
B) must be done
C) must not be done
D) can be done

Q26. The disadvantage of moving all process to one end of memory and all holes to the other direction,
producing one large hole of available memory is :
A) the cost incurred
B) the memory used
C) the CPU used
D) All of these

Q27. -------- is generally faster than -------- and ---------.


A) first fit, best fit, worst fit
B) best fit, first fit, worst fit
C) worst fit, best fit, first fit
D) None of these

Q28. External fragmentation exists when:


33
A) enough total memory exists to satisfy a request but it is not contiguous
B) the total memory is insufficient to satisfy a request
C) a request cannot be satisfied even when the total memory is free
D) None of these

Q29. In ………………. technique, each process is divided into a number of segments and process loaded by
loading all of its segments into dynamic partitions that need not be contiguous.
A) Fixed partitioning
B) Simple Paging
C) Virtual memory paging
D) Simple segmentation

Q30. Which of the following is/are the strengths of virtual memory segmentation techniques used in
memory management.
i) No internal fragmentation
ii) A higher degree of multiprogramming
iii) More efficient to use of main memory
iv) Large virtual address space
v) Protection and sharing support?

A) i, ii, iii and iv only


B) i, ii, iii and v only
C) i, ii, iv and v only
D) ii, iii, iv and v only

b) Long Question Answer

Q1. What is an Operating System?

Ans. An Operating System is the interface between the computer hardware and the end-user. Processing of
data, running applications, file management and handling the memory is all managed by the computer OS.
Windows, Mac, Android etc. Are examples of Operating systems which are generally used nowadays. All
modern computing devices including Laptops, Tablet, mobile phones, etc. comprise an Operating System
which helps in the smooth working of the device.
Q2. What are different types of Operating system. Explain in brief.

Ans. Different types of Operating System along with brief information about each of them:
1. Batch Operating System

 There is no direct communication between the computer and the OS


 There is an intermediate, the Operator, which needs to distribute the work into batches and sort similar jobs
 Multiple users can use it
 Can easily manager a large amount of work

34
2. Real-Time Operating System

 It has a data processing system


 The processing time is very small between the user’s command and the output
 Used in fields where the response needs to be quick and rapid
3. Time-Sharing Operating System

 Multiple people at various terminals can use a program at the same time
 The main motive is to minimize the response time
4. Distributed Operating System

 When two or more systems are connected to each other and one can open files which are not present in
their system but in other devices connected in the network
 Its usage has now increased over the years
 They use multiple central processors to serve real-time applications
 Failure of one system does not affect the other systems connected in the network
5. Embedded Operating System

 These special Operating systems are built into larger systems


 They generally are limited to single specific functions like an ATM
6. Network Operating System

 They have one main server which is connected to other client servers
 All the management of files, processing of data, access to sharing files, etc. are performed over this small
network
 It is also a secure operating system for working with multiple users

Q3. Explain the functions of Operating system.

Ans. Function of operating system are as follows:

• It helps with memory management. It keeps a track of the files being saved in the Main memory and the
primary memory of the computer device

 Whenever a computer is turned on, the Operating system automatically starts to work. Thus, the booting
and rebooting process of a computer device is also an important function of the OS
 It provides a user interface
 Managing of basic peripheral devices is done by the operating system
 Using the password protection option of an operating system, the data in the device can be kept secure
 It coordinates with the software and the user
 Easy navigation and organisation of files and programs are managed by the OS

35
 Any kind of program which needs to be run through the system is done by the operating system
 If any kind of error or bug is found during the program is detected using the operating system

Q4. What is memory management in operating system?

Ans. In operating systems, memory management is the function responsible for managing the computer's
primary memory. The memory management function keeps track of the status of each memory location, either
allocated or free. It tracks when memory is freed or unallocated and updates the status.

Q5. What are the types of memory management techniques?

Ans. Six famous memory management techniques are: Fixed Partitioning, Dynamic Partitioning, Simple
Paging, Simple Segmentation, Virtual-Memory Paging and Virtual- Memory Segmentation.

Q6. What are the differences between?


a) Logical and physical address?
b) Page table and segment table?
c) First-fit placement and best-fit placement?
d) Contiguous and non – contiguous storage allocation
e) Multiple contiguous fixed partitions (MFT) and multiple contiguous variable partitions (MVT).

Ans.

36
Q7. Explain demand paging?

Ans. Demand paging is a method that loads pages into memory on demand. This method is mostly used in
virtual memory. In this, a page is only brought into memory when a location on that particular page is
referenced during execution. The following steps are generally followed:

 Attempt to access the page.


 If the page is valid (in memory) then continue processing instructions as normal.
 If a page is invalid then a page-fault trap occurs.
 Check if the memory reference is a valid reference to a location on secondary memory. If not, the
process is terminated (illegal memory access). Otherwise, we have to page in the required page.
 Schedule disk operation to read the desired page into main memory.
 Restart the instruction that was interrupted by the operating system trap.

Q8. What is virtual memory?

Ans. It is a memory management technique feature of OS that creates the illusion to users of a very large
(main) memory. It is simply space where a greater number of programs can be stored by themselves in the
form of pages. It enables us to increase the use of physical memory by using a disk and also allows us to have
memory protection. It can be managed in two common ways by OS i.e., paging and segmentation. It acts as
temporary storage that can be used along with RAM for computer processes.

37
Q9. What is thrashing in OS?

Ans. It is generally a situation where the CPU performs less productive work and more swapping or paging
work. It spends more time swapping or paging activities rather than its execution. By evaluating the level of
CPU utilization, a system can detect thrashing. It occurs when the process does not have enough pages due to
which the page-fault rate is increased. It inhibits much application-level processing that causes computer
performance to degrade or collapse.

Q10. What is the difference between multitasking and multiprocessing OS?

Ans. Multitasking: It is a system that allows more efficient use of computer hardware. This system works on
more than one task at one time by rapidly switching between various tasks. These systems are also known as
time-sharing systems.

Multiprocessing: It is a system that allows multiple or various processors in a computer to process two or
more different portions of the same program simultaneously. It is used to complete more work in a shorter
period of time.

c) Suggested Skill Development Projects/ Assignments

Teams of 2 or 3 students will select an operating system topic that is not covered in class or important topics
from the syllabus and present it to their peers in the final week of this semester by making a relevant
presentation on it.

38
UNIT-2: PROCESSES

A) Learning Outcomes of the unit:


After completing this unit/lesson, the students will be able to:
 Explain what a process is and what is it’s life cycle.
 Identify the similarities and differences between the various CPU Scheduling algorithms.
 Relate how the different processes can be scheduled.
 Use the various resources judiciously.
 Make use of synchronization process.
 Employ the outcomes of semaphores in solving various classical problems.

B) Introduction:
This unit basically deals with the processes and their life cycle. It tells how different processes can
be scheduled together to complete particular tasks on time. A brief insight has been also given on the
concept of process synchronization which includes critical section problem, semaphores and classical
problems of synchronization.

Lesson 1: Process

PROCESSES:

(A) Process Concept:


 A process is a program in execution. The execution of a process must progress in a sequential fashion.
 A process is defined as an entity which represents the basic unit of work to be implemented in the
system.
 we write our computer programs in a text file and when we execute this program, it becomes a process
which performs all the tasks mentioned in the program.
 When a program is loaded into the memory and it becomes a process, it can be divided into four
sections ─ stack, heap, text and data. The following image shows a simplified layout of a process
inside main memory −

39
Process Life Cycle
 When a process executes, it passes through different states. These stages may differ in different
operating systems, and the names of these states are also not standardized.
 In general, a process can have one of the following five states at a time.

40
Process Control Block (PCB)
 A Process Control Block is a data structure maintained by the Operating System for every process.
The PCB is identified by an integer process ID (PID). A PCB keeps all the information needed to
keep track of a process as listed below in the table −

 The architecture of a PCB is completely dependent on Operating System and may contain different
information in different operating systems. Here is a simplified diagram of a PCB –

41
 The PCB is maintained for a process throughout its lifetime, and is deleted once the process
terminates.

Process Scheduling:

Definition
 Process scheduling is the activity of the process manager that handles the removal of the running
process from the CPU and the selection of another process on the basis of a particular strategy.
 Process scheduling is an essential part of a Multiprogramming operating systems. Such operating
systems allow more than one process to be loaded into the executable memory at a time and the
loaded process shares the CPU using time multiplexing.

Process Scheduling Queues


 The OS maintains all PCBs in Process Scheduling Queues. The OS maintains a separate queue for
each of the process states and PCBs of all processes in the same execution state are placed in the
same queue. When the state of a process is changed, its PCB is unlinked from its current queue and
moved to its new state queue.
The Operating System maintains the following important process scheduling queues −
 Job queue − This queue keeps all the processes in the system.
 Ready queue − This queue keeps a set of all processes residing in main memory, ready and waiting
to execute. A new process is always put in this queue.
 Device queues − The processes which are blocked due to unavailability of an I/O device constitute
this queue.

42
 The OS can use different policies to manage each queue (FIFO, Round Robin, Priority, etc.). The OS
scheduler determines how to move processes between the ready and run queues which can only have
one entry per processor core on the system; in the above diagram, it has been merged with the CPU.

Schedulers

 Schedulers are special system software which handles process scheduling in various ways. Their main
task is to select the jobs to be submitted into the system and to decide which process to run. Schedulers
are of three types −

 Long-Term Scheduler
 Short-Term Scheduler
 Medium-Term Scheduler

Long Term Scheduler

 It is also called a job scheduler. A long-term scheduler determines which programs are admitted to
the system for processing. It selects processes from the queue and loads them into memory for
execution. Process loads into the memory for CPU scheduling.
 The primary objective of the job scheduler is to provide a balanced mix of jobs, such as I/O bound
and processor bound. It also controls the degree of multiprogramming. If the degree of
multiprogramming is stable, then the average rate of process creation must be equal to the average
departure rate of processes leaving the system.
 On some systems, the long-term scheduler may not be available or minimal. Time-sharing operating
systems have no long term scheduler. When a process changes the state from new to ready, then there
is use of long-term scheduler.

Short Term Scheduler

 It is also called as CPU scheduler. Its main objective is to increase system performance in accordance
with the chosen set of criteria. It is the change of ready state to running state of the process. CPU
scheduler selects a process among the processes that are ready to execute and allocates CPU to one
of them.

43
 Short-term schedulers, also known as dispatchers, make the decision of which process to execute
next. Short-term schedulers are faster than long-term schedulers.

Medium Term Scheduler

 Medium-term scheduling is a part of swapping. It removes the processes from the memory. It reduces
the degree of multiprogramming. The medium-term scheduler is in-charge of handling the swapped
out-processes.
 A running process may become suspended if it makes an I/O request. A suspended process cannot
make any progress towards completion. In this condition, to remove the process from memory and
make space for other processes, the suspended process is moved to the secondary storage. This
process is called swapping, and the process is said to be swapped out or rolled out. Swapping may
be necessary to improve the process mix.
Comparison among Scheduler

Context Switch
 A context switch is the mechanism to store and restore the state or context of a CPU in Process
Control block so that a process execution can be resumed from the same point at a later time. Using
this technique, a context switcher enables multiple processes to share a single CPU. Context
switching is an essential part of a multitasking operating system features.
 When the scheduler switches the CPU from executing one process to execute another, the state from
the current running process is stored into the process control block. After this, the state for the process
to run next is loaded from its own PCB and used to set the PC, registers, etc. At that point, the second
process can start executing.

44
 Context switches are computationally intensive since register and memory state must be saved and
restored. To avoid the amount of context switching time, some hardware systems employ two or more
sets of processor registers. When the process is switched, the following information is stored for later
use:

 Program Counter
 Scheduling information
 Base and limit register value
 Currently used register
 Changed State
 I/O State information
 Accounting information

(B) Operation on Processes:


 There are many operations that can be performed on processes. Some of these are process creation,
process preemption, process blocking, and process termination. These are given in detail as follows –
1. Process Creation
 Processes need to be created in the system for different operations. This can be done by the following
events −

 User request for process creation


 System initialization
 Execution of a process creation system call by a running process
 Batch job initialization

45
 A process may be created by another process using fork(). The creating process is called the parent
process and the created process is the child process. A child process can have only one parent but a
parent process may have many children. Both the parent and child processes have the same memory
image, open files, and environment strings. However, they have distinct address spaces.
 A diagram that demonstrates process creation using fork() is as follows −

2. Process Preemption
 An interrupt mechanism is used in preemption that suspends the process executing currently and the
next process to execute is determined by the short-term scheduler. Preemption makes sure that all
processes get some CPU time for execution.
 A diagram that demonstrates process preemption is as follows −

3. Process Blocking
 The process is blocked if it is waiting for some event to occur. This event may be I/O as the I/O events
are executed in the main memory and don't require the processor. After the event is complete, the
process again goes to the ready state.
46
 A diagram that demonstrates process blocking is as follows −

4. Process Termination
 After the process has completed the execution of its last instruction, it is terminated. The resources
held by a process are released after it is terminated.
 A child process can be terminated by its parent process if its task is no longer relevant. The child
process sends its status information to the parent process before it terminates. Also, when a parent
process is terminated, its child processes are terminated as well as the child processes cannot run if the
parent processes are terminated.

47
Lesson 2: CPU Scheduling

CPU SCHEDULING:

(A) Basic Concepts:


 CPU scheduling is a process that allows one process to use the CPU while the execution of another
process is on hold (in waiting state) due to unavailability of any resource like I/O etc, thereby making
full use of CPU. The aim of CPU scheduling is to make the system efficient, fast, and fair.

 Whenever the CPU becomes idle, the operating system must select one of the processes in the ready
queue to be executed. The selection process is carried out by the short-term scheduler (or CPU
scheduler). The scheduler selects from among the processes in memory that are ready to execute and
allocates the CPU to one of them.

CPU Scheduling: Dispatcher

 Another component involved in the CPU scheduling function is the Dispatcher. The dispatcher is the
module that gives control of the CPU to the process selected by the short-term scheduler. This
function involves:

 Switching context
 Switching to user mode
 Jumping to the proper location in the user program to restart that program from where it left last time.
 The dispatcher should be as fast as possible, given that it is invoked during every process switch. The
time taken by the dispatcher to stop one process and start another process is known as the Dispatch
Latency. Dispatch Latency can be explained using the below figure:

Types of CPU Scheduling

 CPU scheduling decisions may take place under the following four circumstances:

1. When a process switches from the running state to the waiting state(for I/O request or invocation of
wait for the termination of one of the child processes).
2. When a process switches from the running state to the ready state (for example, when an interrupt
occurs).
48
3. When a process switches from the waiting state to the ready state(for example, completion of I/O).
4. When a process terminates.

 In circumstances 1 and 4, there is no choice in terms of scheduling. A new process(if one exists in the
ready queue) must be selected for execution. There is a choice, however in circumstances 2 and 3.
 When Scheduling takes place only under circumstances 1 and 4, we say the scheduling scheme is non-
preemptive; otherwise, the scheduling scheme is preemptive.

Non-Preemptive Scheduling
 Under non-preemptive scheduling, once the CPU has been allocated to a process, the process keeps
the CPU until it releases the CPU either by terminating or by switching to the waiting state.
 This scheduling method is used by the Microsoft Windows 3.1 and by the Apple Macintosh operating
systems.
 It is the only method that can be used on certain hardware platforms because It does not require the
special hardware (for example a timer) needed for preemptive scheduling.
 In non-preemptive scheduling, it does not interrupt a process running CPU in the middle of the
execution. Instead, it waits till the process completes its CPU burst time, and then after that it can
allocate the CPU to any other process.
 Some Algorithms based on non-preemptive scheduling are: Shortest Job First (SJF basically non-
preemptive) Scheduling and Priority (non- preemptive version) Scheduling, etc.

Preemptive Scheduling

 In this type of Scheduling, the tasks are usually assigned with priorities. At times it is necessary to run
a certain task that has a higher priority before another task although it is running. Therefore, the
running task is interrupted for some time and resumed later when the priority task has finished its
execution.
 Thus this type of scheduling is used mainly when a process switches either from running state to ready
state or from waiting state to ready state. The resources (that is CPU cycles) are mainly allocated to
the process for a limited amount of time and then are taken away, and after that, the process is again
placed back in the ready queue in the case if that process still has a CPU burst time remaining. That
process stays in the ready queue until it gets the next chance to execute.
49
 Some Algorithms that are based on preemptive scheduling are Round Robin Scheduling (RR), Shortest
Remaining Time First (SRTF), Priority (preemptive version) Scheduling, etc.

(B) Scheduling Criteria

There are many different criteria to check when considering the "best" scheduling algorithm, they are:

1. CPU Utilization
 To make out the best use of the CPU and not to waste any CPU cycle, the CPU would be working
most of the time(Ideally 100% of the time). Considering a real system, CPU usage should range from
40% (lightly loaded) to 90% (heavily loaded.)

2. Throughput
 It is the total number of processes completed per unit of time or rather says the total amount of work
done in a unit of time. This may range from 10/second to 1/hour depending on the specific processes.
3. Turnaround Time
 It is the amount of time taken to execute a particular process, i.e. The interval from the time of
submission of the process to the time of completion of the process(Wall clock time).
4. Waiting Time
 The sum of the periods spent waiting in the ready queue amount of time a process has been waiting in
the ready queue to acquire get control on the CPU.

SCHEDULING ALGORITHMS:

1. First Come First Serve (FCFS)


50
 Jobs are executed on first come, first serve basis.
 It is a non-preemptive, pre-emptive scheduling algorithm.
 Easy to understand and implement.
 Its implementation is based on FIFO queue.
 Poor in performance as average wait time is high.

Wait time of each process is as follows −

Average Wait Time: (0+4+6+13) / 4 = 5.75

2. Shortest Job Next (SJN)

 This is also known as shortest job first, or SJF


 This is a non-preemptive, pre-emptive scheduling algorithm.
 Best approach to minimize waiting time.
 Easy to implement in Batch systems where required CPU time is known in advance.
 Impossible to implement in interactive systems where required CPU time is not known.
 The processer should know in advance how much time process will take.

51
Given: Table of processes, and their Arrival time, Execution time

3. Priority Based Scheduling

 Priority scheduling is a non-preemptive algorithm and one of the most common scheduling algorithms
in batch systems.
 Each process is assigned a priority. Process with highest priority is to be executed first and so on.
 Processes with same priority are executed on first come first served basis.
 Priority can be decided based on memory requirements, time requirements or any other resource
requirement.
Given: Table of processes, and their Arrival time, Execution time, and priority. Here we are considering 1 is
the lowest priority.

52
4. Shortest Remaining Time

 Shortest remaining time (SRT) is the preemptive version of the SJN algorithm.
 The processor is allocated to the job closest to completion but it can be preempted by a newer ready
job with shorter time to completion.
 Impossible to implement in interactive systems where required CPU time is not known.
 It is often used in batch environments where short jobs need to give preference.

5. Round Robin Scheduling

 Round Robin is the preemptive process scheduling algorithm.


 Each process is provided a fix time to execute, it is called a quantum.
 Once a process is executed for a given time period, it is preempted and other process executes for a
given time period.
 Context switching is used to save states of preempted processes.

Wait time of each process is as follows –

6. Multiple-Level Queues Scheduling


 Multiple-level queues are not an independent scheduling algorithm. They make use of other existing
algorithms to group and schedule jobs with common characteristics.

 Multiple queues are maintained for processes with common characteristics.


 Each queue can have its own scheduling algorithms.
 Priorities are assigned to each queue.

53
 For example, CPU-bound jobs can be scheduled in one queue and all I/O-bound jobs in another queue.
The Process Scheduler then alternately selects jobs from each queue and assigns them to the CPU
based on the algorithm assigned to the queue

MULTIPROCESSOR SCHEDULING:

 In multiple-processor scheduling multiple CPU’s are available and hence Load Sharing becomes
possible. However it is more complex as compared to single processor scheduling. In this there are
cases when the processors are identical i.e. HOMOGENEOUS, in terms of their functionality; we can
use any processor available to run any process in the queue.

Approaches to Multiple-Processor Scheduling –


1. when all the scheduling decisions and I/O processing are handled by a single processor which is called
the Master Server and the other processors executes only the user code. This is simple and reduces
the need of data sharing. This entire scenario is called Asymmetric Multiprocessing.
2. A second approach uses Symmetric Multiprocessing where each processor is self scheduling. All
processes may be in a common ready queue or each processor may have its own private queue for
ready processes. The scheduling proceeds further by having the scheduler for each processor examine
the ready queue and select a process to execute.

Lesson 3: Process Synchronization


(A) Introduction:

Process Synchronization is a technique which is used to coordinate the process that use shared Data. There
are two types of Processes in an Operating Systems:-
1. Independent Process –
The process that does not affect or is affected by the other process while its execution then the process is
called Independent Process. Example The process that does not share any shared variable, database, files,
etc.
2. Cooperating Process –
The process that affect or is affected by the other process while execution, is called a Cooperating Process.
Example The process that share file, variable, database, etc are the Cooperating Process.
Process Synchronization is mainly used for Cooperating Process that shares the resources. Let us consider
an example of

54
//racing condition image

It is the condition where several processes tries to access the resources and modify the shared data
concurrently and outcome of the process depends on the particular order of execution that leads to data
inconsistency, this condition is called Race Condition. This condition can be avoided using the technique
called Synchronization or Process Synchronization, in which we allow only one process to enter and
manipulates the shared data in critical section.

(B) CRITICAL SECTION


 The critical section is a code segment where the shared variables can be accessed. An atomic action
is required in a critical section i.e. only one process can execute in its critical section at a time. All the
other processes have to wait to execute in their critical sections.
 A diagram that demonstrates the critical section is as follows −

55
This setup can be defined in various regions like:
 Entry Section –
It is part of the process which decide the entry of a particular process in the Critical Section, out of many
other processes.
 Critical Section –
It is the part in which only one process is allowed to enter and modify the shared variable. This part of the
process ensures that only no other process can access the resource of shared data.
 Exit Section –
This process allows the other process that are waiting in the Entry Section, to enter into the Critical
Sections. It checks that a process that after a process has finished execution in Critical Section can be
removed through this Exit Section.
 Remainder Section –
The other parts of the Code other than Entry Section, Critical Section and Exit Section are known as
Remainder Section.

 Critical Section problems must satisfy these three requirements:


 Mutual Exclusion
Mutual exclusion implies that only one process can be inside the critical section at any time. If any
other processes require the critical section, they must wait until it is free.

 Progress
Progress means that if a process is not using the critical section, then it should not stop any other
process from accessing it. In other words, any process can enter a critical section if it is free.

 Bounded Waiting
Bounded waiting means that each process must have a limited waiting time. Itt should not wait
endlessly to access the critical section.
There are two approaches that are commonly used in operating system to handle critical section.
 Preemptive Kernel − A preemptive kernel allows a process to be preempted while it is running in
kernel mode.
 Non-Preemptive Kernels − A non-preemptive kernel doesn’t allow a process running in kernel mode
to be preempted.
Peterson’s Solution
Peterson’s solution is a classic based software solution to the critical-section problem. It is restricted to two
processes that alternate execution between their critical sections and remainder sections. Peterson’ section
requires two data items to be shared between the two processes i.e.

 Int turn;
 Boolean flag[2];

56
Here, variable turn indicates whose turn is to enter its critical section and flag array indicated whether the
process is ready to enter its critical section.
If turn == i, it means process Pi is allowed to enter in its critical section.
If flag[j] is TRUE, it means process j is ready to enter in its critical section
Given below is the structure of process P in Peterson’s solution

(C)Synchronization Hardware
It is implemented using two types of instructions −

 Test and Set()


 swap()
 Test and Set () is a hardware solution to solve the problem of synchronization. In this, there is a shared
variable which is shared by multiple processes known as Lock which can have one value from 0 and
1 where 1 represents Lock gained and 0 represents Lock released.
 Whenever the process is trying to enter their critical sections they need to enquire about the value of
lock. If the value of lock is 1 then they have to wait until the value of lock won't get changed to 0.
Given below is the mutual-exclusion implementation with TestAndSet()

(D)Semaphores
Semaphore is a synchronization tool that is used to overcome the problems generated by TestAndSet() and
Swap() instructions. A semaphore S is an integer variable that can be accessed through two standard atomic
operations that are wait() and signal()

57
Function for wait():

wait(S) {
While S <= 0
; // no operation
S--;
}

Function for Signal():

signal(S) {
S++;
}

When one process is modifying the value of semaphore then no other process can simultaneously manipulate
the same semaphore value.
Given below is the mutual-exclusion implementation with semaphore

Operating system use two types of semsphores that are −


Counting Semaphore − the value of this type of semaphore can over an unrestricted domain
Binary Semaphore − the value of this type of semaphore can over between 0 and 1. They are also known as
Mutex Locks. Operating system makes use of this for resolving the issues with critical section in multiple
processes
Advantages of Semaphores
Some of the advantages of semaphores are as follows −

 Semaphores allow only one process into the critical section. They follow the mutual exclusion
principle strictly and are much more efficient than some other methods of synchronization.
 There is no resource wastage because of busy waiting in semaphores as processor time is not wasted
unnecessarily to check if a condition is fulfilled to allow a process to access the critical section.
 Semaphores are implemented in the machine independent code of the microkernel. So they are
machine independent.
Disadvantages of Semaphores
Some of the disadvantages of semaphores are as follows −

58
 Semaphores are complicated so the wait and signal operations must be implemented in the correct
order to prevent deadlocks.
 Semaphores are impractical for last scale use as their use leads to loss of modularity. This happens
because the wait and signal operations prevent the creation of a structured layout for the system.
 Semaphores may lead to a priority inversion where low priority processes may access the critical
section first and high priority processes later.

(E)Classical problems of Synchronization:


Following are the problems:
1. Bounded Buffer Problem

 Problem Statement
There is a buffer of n slots and each slot is capable of storing one unit of data. There are two processes running,
namely, producer and consumer, which are operating on the buffer.

A producer tries to insert data into an empty slot of the buffer. A consumer tries to remove data from a filled
slot in the buffer. As you might have guessed by now, those two processes won't produce the expected output
if they are being executed concurrently.
There needs to be a way to make the producer and consumer work in an independent manner.

Solution:
One solution of this problem is to use semaphores. The semaphores which will be used here are:
 m, a binary semaphore which is used to acquire and release the lock.
 empty, a counting semaphore whose initial value is the number of slots in the buffer, since, initially
all slots are empty.
 full, a counting semaphore whose initial value is 0.
At any instant, the current value of empty represents the number of empty slots in the buffer and full represents
the number of occupied slots in the buffer.

59
The Producer Operation

The pseudocode of the producer function looks like this:

do

{// wait until empty > 0 and then decrement 'empty'


wait(empty);
// acquire lock
wait(mutex);
/* perform the insert operation in a slot */
// release lock
signal(mutex);
// increment 'full'
signal(full);
}while(TRUE)
Copy

 Looking at the above code for a producer, we can see that a producer first waits until there is atleast
one empty slot.

 Then it decrements the empty semaphore because, there will now be one less empty slot, since the
producer is going to insert data in one of those slots.

 Then, it acquires lock on the buffer, so that the consumer cannot access the buffer until producer
completes its operation.

 After performing the insert operation, the lock is released and the value of full is incremented because
the producer has just filled a slot in the buffer.

The Consumer Operation

The pseudocode for the consumer function looks like this:

do
{ // wait until full > 0 and then decrement 'full'
wait(full);
// acquire the lock
wait(mutex);
60
/* perform the remove operation in a slot */
// release the lock
signal(mutex);
// increment 'empty'
signal(empty);
} while(TRUE);

 The consumer waits until there is atleast one full slot in the buffer.

 Then it decrements the full semaphore because the number of occupied slots will be decreased by one,
after the consumer completes its operation.

 After that, the consumer acquires lock on the buffer.

 Following that, the consumer completes the removal operation so that the data from one of the full
slots is removed.

 Then, the consumer releases the lock.

 Finally, the empty semaphore is incremented by 1, because the consumer has just removed data from
an occupied slot, thus making it empty.

2. Dining Philosophers Problem


 Used to evaluate situations where there is a need of allocating multiple resources to multiple processes.

Problem Statement
Consider there are five philosophers sitting around a circular dining table. The dining table has five chopsticks
and a bowl of rice in the middle as shown in the below figure.

Dining Philosophers Problem

61
At any instant, a philosopher is either eating or thinking. When a philosopher wants to eat, he uses two
chopsticks - one from their left and one from their right. When a philosopher wants to think, he keeps down
both chopsticks at their original place.

Solution:

From the problem statement, it is clear that a philosopher can think for an indefinite amount of time. But when
a philosopher starts eating, he has to stop at some point of time. The philosopher is in an endless cycle of
thinking and eating.

An array of five semaphores, stick[5], for each of the five chopsticks.

The code for each philosopher looks like:

while(TRUE)
{ wait(stick[i]);
/* mod is used because if i=5, next
chopstick is 1 (dining table is circular)
*/wait(stick[(i+1) % 5]);
/* eat */
signal(stick[i]);
signal(stick[(i+1) % 5]);
/* think */
}
When a philosopher wants to eat the rice, he will wait for the chopstick at his left and picks up that chopstick.
Then he waits for the right chopstick to be available, and then picks it too. After eating, he puts both the
chopsticks down.

But if all five philosophers are hungry simultaneously, and each of them pickup one chopstick, then a deadlock
situation occurs because they will be waiting for another chopstick forever. The possible solutions for this
are:

 A philosopher must be allowed to pick up the chopsticks only if both the left and right chopsticks are
available.

 Allow only four philosophers to sit at the table. That way, if all the four philosophers pick up four
chopsticks, there will be one chopstick left on the table. So, one philosopher can start eating and
eventually, two chopsticks will be available. In this way, deadlocks can be avoided.

62
3. Readers Writer Problem

Problem Statement
 There is a shared resource which should be accessed by multiple processes. There are two types of
processes in this context. They are reader and writer. Any number of readers can read from the shared
resource simultaneously, but only one writer can write to the shared resource. When a writer is writing
data to the resource, no other process can access the resource. A writer cannot write to the resource if
there are non zero number of readers accessing the resource at that time.

Solution
 From the above problem statement, it is evident that readers have higher priority than writer. If a writer
wants to write to the resource, it must wait until there are no readers currently accessing that resource.
 Here, we use one mutex m and a semaphore w. An integer variable read_count is used to maintain
the number of readers currently accessing the resource. The variable read_count is initialized to 0. A
value of 1 is given initially to m and w.
 Instead of having the process to acquire lock on the shared resource, we use the mutex m to make the
process to acquire and release lock whenever it is updating the read_count variable.
 The code for the writer process looks like this:
while(TRUE)
{ wait(w);
/* perform the write operation */
signal(w);
}
And, the code for the reader process looks like this:

while(TRUE)
{
//acquire lock
wait(m);
read_count++;
if(read_count == 1)
wait(w);
//release lock
signal(m);
/* perform the reading operation */
// acquire lock
wait(m);
read_count--;
if(read_count == 0)
signal(w);
// release lock
signal(m);
}

63
Here is the Code explained

 As seen above in the code for the writer, the writer just waits on the w semaphore until it gets a chance
to write to the resource.

 After performing the write operation, it increments w so that the next writer can access the resource.

 On the other hand, in the code for the reader, the lock is acquired whenever the read_count is updated
by a process.

 When a reader wants to access the resource, first it increments the read_count value, then accesses
the resource and then decrements the read_count value.

 The semaphore w is used by the first reader which enters the critical section and the last reader which
exits the critical section.

 The reason for this is, when the first readers enters the critical section, the writer is blocked from the
resource. Only new readers can access the resource now.

 Similarly, when the last reader exits the critical section, it signals the writer using the w semaphore
because there are zero readers now and a writer can have the chance to access the resource.

C) Conclusion
A process is a program in execution. The execution of a process must progress in a sequential fashion.
A process is defined as an entity which represents the basic unit of work to be implemented in the system.
CPU scheduling is a process that allows one process to use the CPU while the execution of another
process is on hold (in waiting state) due to unavailability of any resource like I/O etc, thereby making
full use of CPU. The aim of CPU scheduling is to make the system efficient, fast, and fair Process
Synchronization is a technique which is used to coordinate the process that use shared Data.

D) Progress Check: MCQ’s and long answer question

MCQ’S

1. The systems which allow only one process execution at a time, are called __________
a) uniprogramming systems
b) uniprocessing systems
c) unitasking systems
d) none of the mentioned

2. In operating system, each process has its own __________


a) address space and global variables
b) open files
64
c) pending alarms, signals and signal handlers
d) all of the mentioned

3. In Unix, Which system call creates the new process?


a) fork
b) create
c) new
d) none of the mentioned

4.A process can be terminated due to __________


a) normal exit
b) fatal error
c) killed by another process
d) all of the mentioned

5.What is the ready state of a process?


a) when process is scheduled to run after some execution
b) when process is unable to run until some task has been completed
c) when process is using the CPU
d) none of the mentioned

6.What is interprocess communication?


a) communication within the process
b) communication between two process
c) communication between two threads of same process
d) none of the mentioned

7.A process stack does not contain __________


a) Function parameters
b) Local variables
c) Return addresses
d) PID of child process

8.Which system call can be used by a parent process to determine the termination of child process?
a) wait
b) exit
c) fork
d) get

9.The address of the next instruction to be executed by the current process is provided by the ____
a) CPU registers
b) Program counter
c) Process stack
d) Pipe

10. Cascading termination refers to termination of all child processes if the parent process terminates
__
65
a) Normally
b) Abnormally
c) Normally or abnormally
d) None of the mentioned

11.Which module gives control of the CPU to the process selected by the short-term scheduler?
a) dispatcher
b) interrupt
c) scheduler
d) none of the mentioned

12. The processes that are residing in main memory and are ready and waiting to execute are kept on a
list called _____________
a) job queue
b) ready queue
c) execution queue
d) process queue

13. The interval from the time of submission of a process to the time of completion is termed as ______
a) waiting time
b) turnaround time
c) response time
d) throughput

14. Which scheduling algorithm allocates the CPU first to the process that requests the CPU first?
a) first-come, first-served scheduling
b) shortest job scheduling
c) priority scheduling
d) none of the mentioned

15. In priority scheduling algorithm ____________


a) CPU is allocated to the process with highest priority
b) CPU is allocated to the process with lowest priority
c) Equal priority processes can not be scheduled
d) None of the mentioned

16. In priority scheduling algorithm, when a process arrives at the ready queue, its priority is compared
with the priority of ____________
a) all process
b) currently running process
c) parent process
d) init process

17. Which algorithm is defined in Time quantum?


a) shortest job scheduling algorithm
b) round robin scheduling algorithm
c) priority scheduling algorithm
66
d) multilevel queue scheduling algorithm

18. Process are classified into different groups in ____________


a) shortest job scheduling algorithm
b) round robin scheduling algorithm
c) priority scheduling algorithm
d) multilevel queue scheduling algorithm

19. In multilevel feedback scheduling algorithm ____________


a) a process can move to a different classified ready queue
b) classification of ready queue is permanent
c) processes are not classified into groups
d) none of the mentioned

20. Which one of the following can not be scheduled by the kernel?
a) kernel level thread
b) user level thread
c) process
d) none of the mentioned

21. Which process can be affected by other processes executing in the system?
a) cooperating process
b) child process
c) parent process
d) init process

22. When several processes access the same data concurrently and the outcome of the execution
depends on the particular order in which the access takes place is called ________
a) dynamic condition
b) race condition
c) essential condition
d) critical condition

23. If a process is executing in its critical section, then no other processes can be executing in their
critical section. What is this condition called?
a) mutual exclusion
b) critical exclusion
c) synchronous exclusion
d) asynchronous exclusion

24. Which one of the following is a synchronization tool?


a) thread
b) pipe
c) semaphore
d) socket

25. A semaphore is a shared integer variable __________


67
a) that can not drop below zero
b) that can not be more than zero
c) that can not drop below one
d) that can not be more than one

Long Answer Questions

Question: What is a process scheduler? State the characteristics of a good process scheduler?
OR
What is scheduling? What criteria affect the scheduler's performance?
Answer: Scheduling can be defined as a set of policies and mechanisms which controls the order in which
the work to be done is completed. The scheduling program which is a system software concerned with
scheduling is called the scheduler and the algorithm it uses is called the scheduling algorithm.
Various criteria or characteristics that help in designing a good scheduling algorithm are:
 CPU Utilization − A scheduling algorithm should be designed so that CPU remains busy as possible.
It should make efficient use of CPU.
 Throughput − Throughput is the amount of work completed in a unit of time. In other words
throughput is the processes executed to number of jobs completed in a unit of time. The scheduling
algorithm must look to maximize the number of jobs processed per time unit.
 Response time − Response time is the time taken to start responding to the request. A scheduler must
aim to minimize response time for interactive users.
 Turnaround time − Turnaround time refers to the time between the moment of submission of a job/
process and the time of its completion. Thus how long it takes to execute a process is also an important
factor.
 Waiting time − It is the time a job waits for resource allocation when several jobs are competing in
multiprogramming system. The aim is to minimize the waiting time.
 Fairness − A good scheduler should make sure that each process gets its fair share of the CPU.

Question: Explain time slicing? How its duration affects the overall working of the system?
Answer: Time slicing is a scheduling mechanism/way used in time sharing systems. It is also termed as
Round Robin scheduling. The aim of Round Robin scheduling or time slicing scheduling is to give all
processes an equal opportunity to use CPU. In this type of scheduling, CPU time is divided into slices that
are to be allocated to ready processes. Short processes may be executed within a single time quantum. Long
processes may require several quanta.
The Duration of time slice or Quantum
The performance of time slicing policy is heavily dependent on the size/duration of the time quantum. When
the time quantum is very large, the Round Robin policy becomes a FCFS policy. Too short quantum causes
too many process/context switches and reduces CPU efficiency. So the choice of time quanta is a very

68
important design decision. Switching from one process to another requires a certain amount of time to save
and load registers, update various tables and lists etc.
Consider, as an example, process switch or context switch takes 5 m sec and time slice duration be 20 m sec.
Thus CPU has to spend 5 m sec on process switching again and again wasting 20% of CPU time. Let the
time slice size be set to say 500 m sec and 10 processes are in the ready queue. If P1 starts executing for first
time slice then P2 will have to wait for 1/2 sec; and waiting time for other processes will increase. The
unlucky last (P10) will have to wait for 5 sec, assuming that all others use their full time slices. To conclude
setting the time slice.
 Too short will cause too many process switches and will lower CPU efficiency.
 Setting too long will cause poor response to short interactive processes.
 A quantum around 100 m sec is usually reasonable.

Question: What is Shortest Remaining Time, SRT scheduling?


Answer: Shortest Remaining Time, SRT is a preemptive scheduling. In SRT, the process with smallest
runtime to complete (i.e remaining time) is scheduled to run next, including new arrivals. In SRT, a running
process may be preempted by new process with shorter estimated run time. It keeps track of the elapsed
service time of the running process and handles occasional preemption.
Consider the following 4 jobs
Job Arrival Time Burst Time

1 0 7

2 1 4

3 3 9

4 4 5

The schedule of the SRT is as follows:

Job1 is started at time 0, being only job in queue. Job 2 arrives at time 1. The remaining time for job 1 is
larger (6 time units) than the time required by job2 (4 time units), so job 1 is preempted and job2 is scheduled.
The average turnaround time for the above is:

Job Turn Around Time

1 16-0 = 16

69
2 5-1 = 4

3 25-3 = 22

4 10-4 = 6

48

The average turnaround time is 48/4=12 time unit.


Advantage : Average turnaround time is less.
Disadvantage : Sometime a running process which is being almost completed is preempted because a
new job with very small runtime arrives. It is not really worth doing.

Question: What are the different principles which must be considered while selection of a scheduling
algorithm?
Answer: The objective/principle which should be kept in view while selecting a scheduling policy are the
following −
1. Fairness − All processes should be treated the same. No process should suffer indefinite
postponement.
2. Maximize throughput − Attain maximum throughput. The largest possible number of processes per
unit time should be serviced.
3. Predictability − A given job should run in about the same predictable amount of time and at about
the same cost irrespective of the load on the system.
4. Maximum resource usage − The system resources should be kept busy. Indefinite postponement
should be avoided by enforcing priorities.
5. Controlled Time − There should be control over the different times −
o Response time
o Turnaround time
o Waiting time
The objective should be to minimize above mentioned times.

70
Question: Shown below is the workload for 5 jobs arriving at time zero in the order given below −

Job Burst Time

1 10

2 29

3 3

4 7

4 12

Now find out which algorithm among FCFS, SJF And Round Robin with quantum 10, would give the
minimum average time.
Answer: For FCFS, the jobs will be executed as:

Job Waiting Time

1 0

2 10

3 39

4 42

5 49

71
140

The average waiting time is 140/5=28.


For SJF (non-preemptive), the jobs will be executed as:

Job Waiting Time

1 10

2 32

3 0

4 3

5 20

65

The average waiting time is 65/5=13.


For Round Robin, the jobs will be executed as:

Job Waiting Time

1 0

72
2 32

3 20

4 23

5 40

115

The average waiting time is 115/5=23.


Thus SJF gives the minimum average waiting time.

Question: Explain semaphores and write a short note on it.


Answer: Dijkestra proposed a significant technique for managing concurrent processes for complex mutual
exclusion problems. He introduced a new synchronization tool called Semaphore.
Semaphores are of two types −
1. Binary semaphore
2. Counting semaphore
Binary semaphore can take the value 0 & 1 only. Counting semaphore can take nonnegative integer values.
Two standard operations, wait and signal are defined on the semaphore. Entry to the critical section is
controlled by the wait operation and exit from a critical region is taken care by signal operation. The wait,
signal operations are also called P and V operations. The manipulation of semaphore (S) takes place as
following:
1. The wait command P(S) decrements the semaphore value by 1. If the resulting value becomes negative
then P command is delayed until the condition is satisfied.
2. The V(S) i.e. signals operation increments the semaphore value by 1.
Mutual exclusion on the semaphore is enforced within P(S) and V(S). If a number of processes attempt P(S)
simultaneously, only one process will be allowed to proceed & the other processes will be waiting.These
operations are defined as under −
P(S) or wait(S):
If S > 0 then
Set S to S-1
Else

73
Block the calling process (i.e. Wait on S)

V(S) or signal(S):
If any processes are waiting on S
Start one of these processes
Else
Set S to S+1
The semaphore operation are implemented as operating system services and so wait and signal are atomic in
nature i.e. once started, execution of these operations cannot be interrupted.
Thus semaphore is a simple yet powerful mechanism to ensure mutual exclusion among concurrent
processes.

E) Suggested Skill Development Projects/Assignments


Learners are expected to engage with any two of the following or such similar activities:
Assignment 1:
1. What is process and its types?
2. What are roles of process in operating system?
3. What is process explain process state diagram?
4. What are the 5 basic states of a process?

Assignment 2:
1. What is CPU scheduling in OS?
2. Is CPU scheduling performed by OS?
3. Which are the criteria used for CPU scheduling?
4. Why do we need scheduling in operating system?
5. What are the 5 scheduling types?
6. Which scheduling algorithm is best?

Assignment 3:

1. What is synchronization OS?


2. What are the types of process synchronization?
3. What is the purpose of synchronizing processes?
4. What is synchronization hardware OS?

74
UNIT-3: DEADLOCKS AND DEVICE MANAGEMENT

A) Learning Outcomes of the unit:


After completing this unit/lesson, the students will be able to understand the concept of deadlocks
and how to break or avoid the existence of deadlocks in operating system as it is definitely going t
reduce the efficiency of the system. It also gives an insight of the several mechanisms used to
overcome the problem of deadlocks such as banker’s algorithm. The unit also gives a detailed
understanding of the concept of device management. The students will learn various algorithms to
access the drives both for the purpose of reading and writing.

B) Introduction
In a multiprogramming environment several processes may compete for a finite number of resources.
A process request resources; if the resource is available at that time a process enters the wait state.
Waiting process may never change its state because the resources requested are held by other waiting
process. This situation is known as deadlock. Apart from the deadlocks the unit also gives a detailed
understanding of the disk structures the types of disks and storage devices. The various disk scheduling
algorithms need to be studied as the main content of this chapter since the prime responsibility of the
operating system is to maintain efficiency in every functionality of the operating system.

C) Body of Content
The topics covered in this unit are mentioned below:
Lesson 1: Deadlocks, this discusses about the concept of deadlocks and the various mechanism for
detection, avoidance and prevention of deadlocks.
Lesson 2: This focuses on the concepts of disk structure, types of storage devices and various disk
scheduling algorithms.

Lesson 1: Deadlock Detection and Protection

In a multiprogramming environment several processes may compete for a finite number of resources. A
process request resources; if the resource is available at that time a process enters the wait state. Waiting
process may never change its state because the resources requested are held by other waiting process. This
situation is known as deadlock.
Example
 System has 2 disk drives.
 P1 and P2 each hold one disk drive and each needs another one.
 2 train approaches each other at crossing, both will come to full stop and neither shall start
until other has gone.
 Traffic only in one direction.
 Each section of a bridge can be viewed as a resource.
 If a deadlock occurs, it can be resolved if one car backs up (preempt resources and rollback).
 Several cars may have to be backed up if a deadlock occurs. • Starvation is possible

System Model:
A system consists of a finite number of resources to be distributed among a number of competing processes.
The resources are partitioned into several types each of which consists of a number of identical instances. A
75
process may utilized a resources in the following sequence
 Request: In this state one can request a resource.
 Use: In this state the process operates on the resource.
 Release: In this state the process releases the resources.

Deadlock Characteristics:
In a deadlock process never finish executing and system resources are tied up. A deadlock situation can arise
if the following four conditions hold simultaneously in a system.
 Mutual Exclusion: At a time only one process can use the resources. If another process
requests that resource, requesting process must wait until the resource has been released.
 Hold and wait: A process must be holding at least one resource and waiting to additional
resource that is currently held by other processes.
 No Preemption: Resources allocated to a process can’t be forcibly taken out from it unless it
releases that resource after completing the task.
 Circular Wait: A set {P0 , P1 , …….Pn} of waiting state/ process must exists such that P0 is
waiting for a resource that is held by P1 , P1 is waiting for the resource that is held by P2 …..
P(n – 1) is waiting for the resource that is held by Pn and Pn is waiting for the resources that
is held by P4 .

RESOURCE ALLOCATION GRAPH


Deadlocks can be described more precisely in terms of a directed graph called a system resource allocation
graph. This graph consists of a set of vertices V and a set of edges E. the set of vertices V is partitioned into
2 different types of nodes:
P = {P1, P2….Pn}, the set consisting of all the active processes in the system. R= {R1, R2….Rm}, the set
consisting of all resource types in the system.
A directed edge from process Pi to resource type Rj is denoted by Pi ->Rj. It signifies that process.
Pi has requested an instance of resource type Rj and is currently waiting for that resource.

A directed edge from resource type Rj to process Pi is denoted by Rj ->Pi, it signifies that an instance of
resource type Rj has been allocated to process Pi.
A directed edge Pi ->Rj is called a requested edge.
A directed edge Rj->Pi is called an assignment edge.
We represent each process Pi as a circle, each resource type Rj as a rectangle. Since resource type Rj may
have more than one instance. We represent each such instance as a dot within the rectangle. A request edge
points to only the rectangle Rj. An assignment edge must also designate one of the dots in the rectangle.
When process Pi requests an instance of resource type Rj, a request edge is inserted in the resource allocation
graph. When this request can be fulfilled, the request edge is instantaneously transformed to an assignment
edge. When the process no longer needs access to the resource, it releases the resource, as a result, the
assignment edge is deleted.

76
The sets P, R, E:
P= {P1, P2, P3}
R= {R1, R2, R3, R4}
E= {P1 ->R1, P2 ->R3, R1 ->P2, R2 ->P2, R2 ->P1, R3 ->P3}

One instance of resource type R1


Two instances of resource type R2
One instance of resource type R3
Three instances of resource type R4

PROCESS STATES:
Process P1 is holding an instance of resource type R2 and is waiting for an instance of resource type R1.
Process P2 is holding an instance of R1 and an instance of R2 and is waiting for instance of R3. Process P3 is
holding an instance of R3.

77
If the graph contains no cycles, then no process in the system is deadlocked. If the graph does contain a cycle,
then a deadlock may exist. Suppose that process P3 requests an instance of resource type R2. Since no resource
instance is currently available, a request edge P3 ->R2 is added to the graph.

2 cycles:
P1 ->R1 ->P2 ->R3 ->P3 ->R2 ->P1
P2 ->R3 ->P3 ->R2 ->P2

Graph With A Cycle But No Deadlock

Processes P1, P2, P3 are deadlocked. Process P2 is waiting for the resource R3, which is held by process
P3.process P3 is waiting for either process P1 (or) P2 to release resource R2. In addition, process P1 is waiting
for process P2 to release resource R1.
We also have a cycle: P1 ->R1 ->P3 ->R2 ->P1
However there is no deadlock. Process P4 may release its instance of resource type R2. That resource can
then be allocated to P3, breaking the cycle.

Methods for Handling Deadlocks


The problem of deadlock can deal with the following 3 ways.
 We can use a protocol to prevent or avoid deadlock ensuring that the system will never enter
to a deadlock state.
 We can allow the system to enter a deadlock state, detect it and recover.
 We can ignore the problem all together.

DEADLOCK PREVENTION
For a deadlock to occur, each of the 4 necessary conditions must held. By ensuring that at least one of these
conditions cannot hold, we can prevent the occurrence of a deadlock.

Mutual Exclusion – The mutual exclusion condition holds for non sharable. The example is a printer cannot
be simultaneously shared by several processes. Sharable resources do not require mutual exclusive access and
thus cannot be involved in a dead lock. The example is read only files which are in sharing condition. If
several processes attempt to open the read only file at the same time they can be guaranteed simultaneous
access.
78
Hold and Wait – must guarantee that whenever a process requests a resource, it does not hold any other
resources. Hold and wait: To ensure that the hold and wait condition never occurs in the system, we must
guaranty that whenever a process requests a resource it does not hold any other resources. There are two
protocols to handle these problems such as one protocol that can be used requires each process to request and
be allocated all its resources before it begins execution. The other protocol allows a process to request
resources only when the process has no resource. These protocols have two main disadvantages. First,
resource utilization may be low, since many of the resources may be allocated but unused for a long period.
Second, starvation is possible. A process that needs several popular resources may have to wait indefinitely,
because at least one of the resources that it needs is always allocated to some other process.

No Preemption –
 If a process that is holding some resources requests another resource that cannot be immediately
allocated to it, then all resources currently being held are released
 Preempted resources are added to the list of resources for which the process is waiting
 Process will be restarted only when it can regain its old resources, as well as the new ones that it is
requesting.

Circular Wait – Impose a total ordering of all resource types, and require that each process requests resources
in an increasing order of enumeration. We can ensure that this condition never holds by ordering of all resource
type and to require that each process requests resource in an increasing order of enumeration. Let R 45 | P a g
e = {R1 , R2 , …….Rn}be the set of resource types. We assign to each resource type a unique integer number,
which allows us to compare two resources and to determine whether one precedes another in our ordering

Deadlock Avoidance
Requires that the system has some additional a priori information available
 Simplest and most useful model requires that each process declare the maximum number of
resources of each type that it may need
 The deadlock-avoidance algorithm dynamically examines the resource allocation state to ensure that
there can never be a circular-wait condition
 Resource-allocation state is defined by the number of available and allocated resources, and the
maximum demands of the processes

Safe State
When a process requests an available resource, system must decide if immediate allocation leaves the system
in a safe state System is in safe state if there exists a sequence <P1, P2, P3,….Pn) of ALL the processes in the
systems such that for each Pi, the resources that Pi can still request can be satisfied by currently available
resources + resources held by all the Pj, with j < I

That is:
 If Pi resource needs are not immediately available, then Pi can wait until all Pj have finished
 When Pj is finished, Pi can obtain needed resources, execute, return allocated resources, and
terminate
 When Pi terminates, Pi +1 can obtain its needed resources, and so on If a system is in safe state
no deadlocks

79
 If a system is in unsafe state possibility of deadlock Avoidance ensure that a system will never
enter an unsafe state

Avoidance algorithms Single instance of a resource type


 Use a resource-allocation graph Multiple instances of a resource type
 Use the banker’s algorithm

Resource-Allocation Graph Scheme

In this graph a new type of edge has been introduced is known as claim edge. Claim edge Pi→Rj indicates
that process Pj may request resource Rj ; represented by a dashed line. Claim edge converts to request edge
when a process requests a resource. Request edge converted to an assignment edge when the resource is
allocated to the process. When a resource is released by a process, assignment edge reconverts to a claim
edge. Resources must be claimed a priori in the system.

 P2 requesting R1, but R1 is already allocated to P1.


 Both processes have a claim on resource R2
 What happens if P2 now requests resource R2?

Cannot allocate resource R2 to process P2


Why? Because resulting state is unsafe
 P1 could request R2, thereby creating deadlock!

Use only when there is a single instance of each resource type


80
 Suppose that process Pi requests a resource Rj
 The request can be granted only if converting the request edge to an assignment edge does not result
in the formation of a cycle in the resource allocation graph.
 Here we check for safety by using cycle-detection algorithm.

Banker’s Algorithm
This algorithm can be used in banking system to ensure that the bank never allocates all its available cash
such that it can no longer satisfy the needs of all its customer. This algorithm is applicable to a system with
multiple instances of each resource type. When a new process enter in to the system it must declare the
maximum number of instances of each resource type that it may need. This number may not exceed the total
number of resources in the system. Several data structure must be maintained to implement the banker’s
algorithm.

Let,
 n = number of processes
 m = number of resources types

 Available: Vector of length m. If Available[j] = k, there are k instances of resource type Rj


available.

 Max: n x m matrix. If Max [i,j] = k, then process Pimay request at most k instances of resource
type Rj .

 Allocation: n x m matrix. If Allocation [i,j] = k then Pi is currently allocated k instances of Rj.

 Need: n x m matrix. If Need [i,j] = k, then Pi may need k more instances of Rj to complete its
task. Need [i,j] = Max[i,j] – Allocation [i,j].

Safety Algorithm
1. Let Workand Finish be vectors of length m and n, respectively.
Initialize: Work = Available Finish [i] = false for i = 0, 1, …,n- 1.
2. Find and i such that both:
a. Finish [i] = false
b. Need ≤ Work
If no such i exists, go to step 4.
3. Work = Work + Allocation
a. Finish[i] = true; go to step 2.
4. If Finish [i] == true for all i, then the system is in a safe state.

Resource Allocation Algorithm


Request = request vector for process Pi. If Requesti [j] = k then process Pi wants k instances of resource type
Rj.
1. If Requesti ≤ Needi go to step 2. Otherwise, raise error condition, since process has exceeded its
maximum claim.
2. If Requesti ≤ Available, go to step 3. Otherwise Pi must wait, since resources are not available.

81
3. Pretend to allocate requested resources to Pi by modifying the state as follows:
a. Available = Available – Request;
b. Allocationi= Allocationi + Requesti ;
c. Needi=Needi – Requesti ;
If safe ⇒ the resources are allocated to Pi.
If unsafe ⇒ Pi must wait, and the old resource-allocation state is restored

Example
 5 processes P0 through P4 ;
 3 resource types:
o A (10 instances), B (5instances), and C (7 instances).
 Snapshot at time T0 :

The content of the matrix Need is defined to be Max – Allocation.

 The system is in a safe state since the sequence satisfies safety criteria. P1 requests (1, 0, 2)
 Check that Request ≤ Available (that is, (1,0,2) ≤ (3,3,2) ⇒ true.

82
 Executing safety algorithm shows that sequence satisfies safety requirement.
 Can request for (3,3,0) by P4 be granted? –NO
 Can request for (0,2,0) by P0 be granted? –NO (Results Unsafe)

Deadlock Detection
If a system doesn’t employ either a deadlock prevention or deadlock avoidance, then deadlock situation may
occur. In this environment the system must provide
 An algorithm to recover from the deadlock.
 An algorithm to remove the deadlock is applied either to a system which pertains single in instance
each resource type or a system which pertains several instances of a resource type.

Single Instance of each Resource type


If all resources only a single instance then we can define a deadlock detection algorithm which uses a new
form of resource allocation graph called “Wait for graph”. We obtain this graph from the resource allocation
graph by removing the nodes of type resource and collapsing the appropriate edges. The below figure
describes the resource allocation graph and corresponding wait for graph.

 For single instance


 Pi ->Pj (Pi is waiting for Pj to release a resource that Pi needs)
 Pi ->Pj exist if and only if RAG contains 2 edges Pi ->Rq and Rq ->Pj for some resource Rq

Several Instances of a Resource type


The wait for graph scheme is not applicable to a resource allocation system with multiple instances of reach
resource type. For this case the algorithm employs several data structures which are similar to those used in
the banker’s algorithm like available, allocation and request.
 Available: A vector of length m indicates the number of available resources of each type.
 Allocation: An n x m matrix defines the number of resources of each type currently allocated to each
process.
 Request: An n x m matrix indicates the current request of each process. If Request [ij ] = k, then
process Pi is requesting k more instances of resource type. Rj.

1. Let Work and Finish be vectors of length m and n, respectively Initialize:

83
a. Work = Available
b. For i = 1,2, …, n, if Allocationi≠ 0, then Finish[i] = false;otherwise, Finish[i] = true.
2. Find an index i such that both:
a. Finish[i] == false
b. Requesti≤Work
If no such i exists, go to step 4.
3. Work = Work + Allocation
Finish [i] = true
Go to step 2
4. If Finish [i] = false, for some i, 1≤ i≤ n, then the system is in a deadlock state. Moreover, if Finish [i]
= false, then process Pi is deadlocked.

Recovery from Deadlock


When a detection algorithm determines that a deadlock exists, several alternatives exist. One possibility is to
inform the operator that a deadlock has occurred, and to let the operator deal with the deadlock manually. The
other possibility is to let the system recover from the deadlock automatically. There are two options for
breaking a deadlock. One solution is simply to abort one or more processes to break the circular wait. The
second option is to preempt some resources from one or more of the deadlocked processes.

Process Termination: To eliminate deadlocks by aborting a process, we use one of two methods. In both
methods, the system reclaims all resources allocated to the terminated processes.
 Abort all deadlocked processes: This method clearly will break the deadlock cycle, but at a great
expense; these processes may have computed for a long time, and the results of these partial
computations must be discarded and probably recomputed later.
 Abort one process at a time until the deadlock cycle is eliminated: This method incurs considerable
overhead, since after each process is aborted, a deadlock detection algorithm must be invoked to
determine whether any processes are still deadlocked.

Resource Preemption:
To eliminate deadlocks using resource preemption, we successively preempt some resources from processes
and give these resources to other processes until the deadlock cycle is broken. If preemption is required to
deal with deadlocks, then three issues need to be addressed.

 Selecting a victim: Which resources and which processes are to be preempted? As in process
termination, we must determine the order of preemption to minimize cost. Cost factors may include
such parameters as the numbers of resources a deadlock process is holding, and the amount of time a
deadlocked process has thus far consumed during its execution.
 Rollback: If we preempt a resource from a process, what should be done with that process? Clearly,
it cannot continue with its normal execution; it is missing some needed resource. We must rollback
the process to some safe state, and restart it from that state.
 Starvation: In a system where victim selection is based primarily on cost factors, it may happen that
the same process is always picked as a victim. As a result, this process never completes its designated
task, a starvation situation that needs to be dealt with in any practical system. Clearly, we must ensure
that a process can be picked as a victim only a small finite number of times. The most common solution
is to include the number of rollbacks in the cost factor.

84
UNIT-4: INFORMATION MANAGEMENT

A) Learning Outcomes of the unit:


After completing this unit/lesson, the students will be able to:
 Explain the concept of the basic file system and various categories of file system. It will also discuss the
different file interfaces that includes the concept of file system and the different access methods of files as
well.
 Identify the similarities and differences between the file system and the directory structure. It focuses on
the concept of directory and sub directory. It also talks about the differences of files from directories. The
file protection methods are also discussed during this unit. This includes the various access rights of the
files.

B) Introduction
A file is collection of specific information stored in the memory of computer system. File management is
defined as the process of manipulating files in computer system, it management includes the process of
creating, modifying and deleting the files. File structure basically comprise of four terms: Field, Record, File,
Database. The unit describes the concepts of Files and file system interfaces, the allocation methods and the
various protection and security features and techniques used in file system.

C)Body of Content

The topics included in this unit are described as follows:


Lesson 1: File System Interface, File structure, File Operations, File and directories, File Allocation
Methods, Disk space management
Lesson 2: File security, Access Control mechanisms,

Lesson 1: File System Interface

A file is a named collection of related information that is recorded on secondary storage such as
magnetic disks, magnetic tapes and optical disks. In general, a file is a sequence of bits, bytes, lines
or records whose meaning is defined by the files creator and user.

File Structure A File Structure should be according to a required format that the operating system
can understand.
 A file has a certain defined structure according to its type.
 A text file is a sequence of characters organized into lines.
 A source file is a sequence of procedures and functions.
 An object file is a sequence of bytes organized into blocks that are understandable by the machine.
 When operating system defines different file structures, it also contains the code to support these
file structure. Unix, MS-DOS support minimum number of file structure

85
File management is one of the basic and important features of operating system. Operating system is used to
manage files of computer system. All the files with different extensions are managed by operating system.

A file is collection of specific information stored in the memory of computer system. File management is
defined as the process of manipulating files in computer system, it management includes the process of
creating, modifying and deleting the files. File structure basically comprise of four terms: Field, Record, File,
Database.

Field:
 basic element of data
 contains a single value
 fixed or variable length

Record:
 collection of related fields that can be treated as a unit by some application program
 fixed or variable length

File:
 collection of similar records
 treated as a single entity
 may be referenced by name
 access control restrictions usually apply at the file level

Database:
 collection of related data
 relationships among elements of data are explicit
 designed for use by a number of different applications
 consists of one or more types of files

The following are some of the tasks performed by file management of operating system of any computer
system:
1. It helps to create new files in computer system and placing them at the specific locations.
2. It helps in easily and quickly locating these files in computer system.
3. It makes the process of sharing of the files among different users very easy and user friendly.
4. It helps to stores the files in separate folders known as directories. These directories help users to search file
quickly or to manage the files according to their types or uses.
5. It helps the user to modify the data of files or to modify the name of the file in the directories.
6. Provide a standardized set of I/O interface routines to user processes
7. Provide I/O support for multiple users in the case of multiple user systems

86
In the above figure shows the general hierarchy of the storage in an operating system. In this figure the root
directory is present at the highest level in the hierarchical structure. It includes all the subdirectories in which
the files are stored. Subdirectory is a directory present inside another directory in the file storage system. The
directory base storage system ensures better organization of files in the memory of the computer system.

File Operations include following:


 Create
 Delete
 Open
 Close
 Read
 Write

File management of function in operating system (OS) is based on the following concepts:

1. File Attributes
It specifies the characteristics of the files such as type, date of last modification, size, location on disk etc. file
attributes help the user to understand the value and location of files. File attributes is one most important
feature. It is uses to describe all the information regarding particular file.

87
2. File Operations
It specifies the task that can be performed on a file such as opening and closing of file.

3. File Access permission


It specifies the access permissions related to a file such as read and write.

4. File Systems
It specifies the logical method of file storage in a computer system. Some of the commonly used files
systems include FAT and NTFS.

The system may contain features like:


 Assigning queued document numbers for processing
 Owner and process mapping to track various stages of processing
 Report generation
 Notes
 Status
 Create, modify, copy, delete and other file operations

File systems and operating systems

1. Unix and Unix-like operating systems

Unix like operating systems create a virtual file system, which makes all the files on all the devices appear to
exist in a single hierarchy. This means, in those systems, there is one root directory, and every file existing on
the system is located under it somewhere. Unix-like systems can use a RAM disk or network shared resource
as its root directory.

2. Linux

Linux supports numerous file systems, but common choices for the system disk on a block device include XFS,
JFS, and btrsfs. For raw flash without a flash translation layer(FTL) or Memory Technology Device(MTD),
there are UBIFS, JFFS 2 and YAFFS, among others. SquashFS is a common compressed read-only file system.

3. macOS

macOS uses the Apple File System(APFS), which recently replaced a file system inherited from classic mac
old called HFS Plus(HFS+). Apple also uses the term “Mac OS Extended” for HFS+.HFS Plus is a metadata-
rich and case-preserving but (usually) case-insensitive file system. Due to the Unix roots of macOS, Unix
permissions were added to HFS Plus. Later versions of HFS Plus added journaling to prevent corruption of the
file system structure and introduced a number of optimizations to the allocation algorithms in an attempt to
defragment files automatically without requiring an external defragmenter.

88
4. Microsoft Windows

Windows makes use of the FAT, NTFS, exFAT , Live File System and ReFS file systems (the last of these is
only supported and usable in Windows Servers, Windows 8,8.1,10.

File systems Interface

1. Space management

File System Fragmentation occurs when unused space or single files are not contiguous. As a file system is
used, files are created, modified and deleted. When a file is created the file system allocates space for the data.
Some file systems permit or require specifying an initial space allocation and subsequent incremental
allocations as the file grows. As files are deleted the space they were allocated eventually is considered
available for use by other files. This creates alternating used and unused areas of various sizes. This is free
space fragmentation. When a file is created and there is not an area of contiguous space available for its initial
allocation the space must be assigned in fragments. When a file is modified such that it becomes larger it may
exceed the space initially allocated to it, another allocation must be assigned elsewhere and the file becomes
fragmented.

2. Filenames

A filename (or file name) is used to identify a storage location in the file system. Most file systems have
restrictions on the length of filenames. In some file systems, filenames are not case sensitive(i.e., the names
MYFILE and myfile refer to the same file in a directory); in others, filenames are case sensitive (i.e., the names
MYFILE, MyFile , and myfile refer to three separate files that are in the same directory).

3. Directories

File systems typically have directories (also called folders) which allow the user to group files into separate
collections. This may be implemented by associating the file name with an index in a table of contents or an
inode in a Unix-like file system. Directory structures may be flat (i.e. linear), or allow hierarchies where
directories may contain subdirectories. The first file system to support arbitrary hierarchies of directories was
used in the Multics operating system.

FILE DIRECTORIES:
Collection of files is a file directory. The directory contains information about the files, including
attributes, location and ownership. Much of this information especially that is concerned with storage is
managed by the operating system. The directory is itself a file, accessible by various file management
routines.

Information contained in a device directory are:


 Name

89
 Type
 Address
 Current length
 Maximum length
 Date last accessed
 Date last updated
 Owner id
 Protection information

Operation performed on directory are:

 Search for a file


 Create a file
 Delete a file
 List a directory
 Rename a file
 Traverse the file system

Advantages of maintaining directories are:

 Efficiency: A file can be located more quickly.


 Naming: It becomes convenient for users as two users can have same name for different files or may
have different name for same file.
 Grouping: Logical grouping of files can be done by properties e.g. all java programs, all games etc.

SINGLE-LEVEL DIRECTORY
In this a single directory is maintained for all the users.

 Naming problem: Users cannot have same name for two files.
 Grouping problem: Users cannot group files according to their need.

90
TWO-LEVEL DIRECTORY
In this separate directories for each user is maintained.

 Path name: Due to two levels there is a path name for every file to locate that file.
 Now, we can have same file name for different user.
 Searching is efficient in this method.

TREE-STRUCTURED DIRECTORY:
Directory is maintained in the form of a tree. Searching is efficient and also there is grouping capability.
We have absolute or relative path name for a file. Tree-Structured Directories
 This generalization to a directory tree structure of arbitrary height allows users to create their own
subdirectories and organize their files accordingly.
 Directory
o Becomes simply another file.
o Contains a set of files or subdirectories.
o All directories have the same internal format.
o One bit in directory entry defines entry as file or directory.
o Special commands are used to create and delete directories.

91
Advantages
 Efficient searching
 Grouping Capability
 Each user has a current directory (working directory)
 cd /spell/mail/prog
 type list

Acyclic-Graph Directories
An acyclic graph is a graph with no cycle and allows us to share subdirectories and files. The same file or
subdirectories may be in two different directories. It is a natural generalization of the tree-structured directory.
... It is the point to note that the shared file is not the same as the copy file
 A tree structure prohibits the sharing of files or directories.
 Acyclic graphs allow directories to have shared subdirectories and files.

When the same files need to be accessed in more than one place in the directory structure ( e.g. because they
are being shared by more than one user / process ), it can be useful to provide an acyclic-graph structure.
(Note the directed arcs from parent to child.)

 UNIX provides two types of links for implementing the acyclic-graph structure.

92
o A hard link (usually just called a link) involves multiple directory entries that both refer to the same file.
Hard links are only valid for ordinary files in the same file system.
o A symbolic link that involves a special file, containing information about where to find the linked file.
Symbolic links may be used to link directories and/or files in other file systems, as well as ordinary files in
the current file system.
 Windows only supports symbolic links, termed shortcuts.
 Hard links require a reference count, or link count for each file, keeping track of how many directory
entries are currently referring to this file. Whenever one of the references is removed the link count is reduced,
and when it reaches zero, the disk space can be reclaimed.
 For symbolic links there is some question as to what to do with the symbolic links when the original file is
moved or deleted:
o One option is to find all the symbolic links and adjust them also.
o Another is to leave the symbolic links dangling, and discover that they are no longer valid the next time
they are used.
o What if the original file is removed, and replaced with another file having the same name before the
symbolic link is next used?

FILE ALLOCATION METHODS

1. Continuous Allocation: A single continuous set of blocks is allocated to a file at the time of file creation.
Thus, this is a pre-allocation strategy, using variable size portions. The file allocation table needs just a
single entry for each file, showing the starting block and the length of the file. This method is best from the
point of view of the individual sequential file. Multiple blocks can be read in at a time to improve I/O
performance for sequential processing. It is also easy to retrieve a single block. For example, i f a file starts

93
at block b, and the ith block of the file is wanted, its location on secondary storage is simply b+i -1.

Disadvantage

 External fragmentation will occur, making it difficult to find contiguous blocks of space of sufficient
length. Compaction algorithm will be necessary to free up additional space on disk.
 Also, with pre-allocation, it is necessary to declare the size of the file at the time of creation.

94
2. Linked Allocation (Non-contiguous allocation) : Allocation is on an individual block basis. Each block
contains a pointer to the next block in the chain. Again the file table needs just a single entry for each file,
showing the starting block and the length of the file. Although pre-allocation is possible, it is more common
simply to allocate blocks as needed. Any free block can be added to the chain. The blocks need not be
continuous. Increase in file size is always possible if free disk block is available. There is no external
fragmentation because only one block at a time is needed but there can be internal fragmentation but it
exists only in the last disk block of file.

Disadvantage:

 Internal fragmentation exists in last disk block of file.


 There is an overhead of maintaining the pointer in every disk block.
 If the pointer of any disk block is lost, the file will be truncated.
 It supports only the sequential access of files.

3. Indexed Allocation:

It addresses many of the problems of contiguous and chained allocation. In this case, the file allocation
table contains a separate one-level index for each file: The index has one entry for each block allocated to
the file. Allocation may be on the basis of fixed-size blocks or variable-sized blocks. Allocation by blocks
eliminates external fragmentation, whereas allocation by variable-size blocks improves locality. This
allocation technique supports both sequential and direct access to the file and thus is the most popular
form of file allocation.

95
Disk Free Space Management
Just as the space that is allocated to files must be managed, so the space that is not currently allocated to
any file must be managed. To perform any of the file allocation techniques, it is necessary to know what
blocks on the disk are available. Thus we need a disk allocation table in addition to a file allocation table.
The following are the approaches used for free space management.

1. Bit Tables: This method uses a vector containing one bit for each block on the disk. Each entry for a 0
corresponds to a free block and each 1 corresponds to a block in use.
For example: 00011010111100110001
In this vector every bit correspond to a particular block and 0 implies that, that particular block is free and
1 implies that the block is already occupied. A bit table has the advantage that it is relatively easy to find
one or a contiguous group of free blocks. Thus, a bit table works well with any of the file allocation methods.
Another advantage is that it is as small as possible.
2. Free Block List: In this method, each block is assigned a number sequentially and the list of the numbers
of all free blocks is maintained in a reserved block of the disk.

File system as an abstract user interface


In some cases, a file system may not make use of a storage device but can be used to organize and represent
access to any data, whether it is stored or dynamically generated (e.g. procfs).

96
Limitations

1. Converting the type of a file system

It may be advantageous or necessary to have files in a different file system than they currently exist. Reasons
include the need for an increase in the space requirements beyond the limits of the current file system. The
depth of path may need to be increased beyond the restrictions of the file system. There may be performance
or reliability considerations. Providing access to another operating system which does not support the existing
file system is another reason.

2. Long file paths and long file names


In hierarchical file systems, files are accessed by means of a path that is a branching list of directories containing
the file. Different file systems have different limits on the depth of the path. File systems also have a limit on
the length of an individual filename.
Copying files with long names or located in paths of significant depth from one file system to another may
cause undesirable results. This depends on how the utility doing the copying handles the discrepancy.

File system structure:


Disk provides the bulk of secondary storage on which a file system is maintained. They have 2 characteristics
that make them a convenient medium for storing multiple files.
1. A disk can be rewritten in place. It is possible to read a block from the disk, modify the block, and write it
back into same place.
2. A disk can access directly any block of information it contains.

97
I/O Control: consists of device drivers and interrupt handlers to transfer information between the main
memory and the disk system. The device driver writes specific bit patterns to special locations in the I/O
controller’s memory to tell the controller which device location to act on and what actions to take. The Basic
File System needs only to issue commands to the appropriate device driver to read and write physical blocks
on the disk. Each physical block is identified by its numeric disk address (Eg. Drive 1, cylinder 73, track2,
sector 10).
The File Organization Module knows about files and their logical blocks and physical blocks. By knowing
the type of file allocation used and the location of the file, file organization module can translate logical block
address to physical addresses for the basic file system to transfer. Each file’s logical blocks are numbered
from 0 to n. so, physical blocks containing the data usually do not match the logical numbers. A translation is
needed to locate each block.

The Logical File System manages all file system structure except the actual data (contents of file). It maintains
file structure via file control blocks. A file control block (inode in Unix file systems) contains information
about the file, ownership, permissions, location of the file contents

98
Lesson 2: Protection and File Security

Protection

 Files must be kept safe for reliability ( against accidental damage ), and protection ( against deliberate
malicious access. ) The former is usually managed with backup copies. This section discusses the latter.
 One simple protection scheme is to remove all access to a file. However this makes the file unusable, so
some sort of controlled access must be arranged.

Types of Access

The files which have direct access of the any user have the need of protection. The files which are not
accessible to other users doesn’t require any kind of protection. The mechanism of the protection provide
the facility of the controlled access by just limiting the types of access to the file. Access can be given or
not given to any user depends on several factors, one of which is the type of access required. Several
different types of operations can be controlled:

 The following low-level operations are often controlled:


o Read - View the contents of the file
o Write - Change the contents of the file.
o Execute - Load the file onto the CPU and follow the instructions contained therein.
o Append - Add to the end of an existing file.
o Delete - Remove a file from the system.
o List -View the name and other attributes of files on the system.
 Higher-level operations, such as copy, can generally be performed through combinations of the above.

Access Control

There are different methods used by different users to access any file. The general way of protection is to
associate identity-dependent access with all the files and directories an list called access-control list
(ACL) which specify the names of the users and the types of access associate with each of the user. The
main problem with the access list is their length. If we want to allow everyone to read a file, we must list
all the users with the read access. This technique has two undesirable consequences:
Constructing such a list may be tedious and unrewarding task, especially if we do not know in advance the
list of the users in the system.
Previously, the entry of the any directory is of the fixed size but now it changes to the variable size which
results in the complicates space management. These problems can be resolved by use of a condensed version
of the access list. To condense the length of the access-control list, many systems recognize three
classification of users in connection with each file:
 Owner – Owner is the user who has created the file.
 Group – A group is a set of members who has similar needs and they are sharing the same file.
 Universe – In the system, all other users are under the category called universe.

99
 One approach is to have complicated Access Control Lists, ACL, which specify exactly what access is
allowed or denied for specific users or groups.
o The AFS uses this system for distributed access.
o Control is very finely adjustable, but may be complicated, particularly when the specific users involved
are unknown. (AFS allows some wild cards, so for example all users on a certain remote system may be
trusted, or a given username may be trusted when accessing from any remote system.)
 UNIX uses a set of 9 access control bits, in three groups of three. These correspond to R, W, and X
permissions for each of the Owner, Group, and Others. (See "man chmod" for full details.) The RWX bits
control the following privileges for ordinary files and directories:

bit Files Directories


Read (view)
R Read directory contents. Required to get a listing of the directory.
file contents.
Write
W (change) file Change directory contents. Required to create or delete files.
contents.
Access detailed directory information. Required to get a long listing,
Execute file or to access any specific file in the directory. Note that if a user has X
X contents as a but not R permissions on a directory, they can still access specific files,
program. but only if they already know the name of the file they are trying to
access.

In addition there are some special bits that can also be applied:

 The set user ID (SUID) bit and/or the set group ID (SGID) bits applied to executable files temporarily
change the identity of whoever runs the program to match that of the owner / group of the executable program.
This allows users running specific programs to have access to files (while running that program) to which
they would normally be unable to access. Setting of these two bits is usually restricted to root, and must be
done with caution, as it introduces a potential security leak.
 The sticky bit on a directory modifies write permission, allowing users to only delete files for which they
are the owner. This allows everyone to create files in /tmp, for example, but to only delete files which they
have created, and not anyone else's.
 The SUID, SGID, and sticky bits are indicated with an S, S, and T in the positions for execute permission
for the user, group, and others, respectively. If the letter is lower case, (s, s, t), then the corresponding execute
permission is not also given. If it is upper case, (S, S, T), then the corresponding execute permission IS given.
 The numeric form of chmod is needed to set these advanced bits.

100
Progress Check: MCQ’s and long answer question

1. Reliability of files can be enhanced by :


a)by keeping duplicate copies of the file
b) making a different partition for the files
c) by keeping them in external storage
d) keeping the files safely in the memory
Answer A
2. Security is only provided at the _____ level.
a) none of the mentioned
b) high
c) central
d) lower
Answer D
3. The major issue with access control lists is :
a) their maintenance
b) all of the mentioned
c) their permissions
d) their length
Answer D
4. Many systems recognize three classifications of users in connection with each file (to condense the access
control list) :
a) Universe
b) Group
c) owner
d) All of the mentioned
Answer D
5. In a group, All users get _______ access to a file.
a) different
b)same
c) similar
d) none of the mentioned
Answer C
6. The universe consists of:
a) all users in the system
b) all users that are not owners
c)all users that aren’t included in the group or owners
d) none of the mentioned
Answer A
7. By a password If each access to a file is controlled, then the disadvantage is that :
a) it is not reliable
b) all of the mentioned
c) it is not efficient

101
d) ) user will need to remember a lot of passwords
Answer D
8. In a different level directory structure :
a) the subdirectories do not need protection once the directory is protected
b) the same previous techniques will be used as in the other structure
c) a mechanism for directory protection will have to apply
d) none of the mentioned
Answer C
9. The directory protection is handled in Unix _________ to the file protection.
a) none of the mentioned
b) it is not handled at all
c) similar
d) different
Answer C
10. Each containing a file system or ______, Disks are segmented into one or more partitions.
a) left ‘ripe’
b) made into swap space
c) made into backup space
d) left ‘raw’
Answer D

11. Such as access by fraudulent people , Destruction of files for malicious reasons is classified as being
a)unauthorized access
b)accessed
c)destroyed
d)modified
Answer D

12. If the order of operation on two or more files are similar in files, then the operation will be
a)sequential
b)combinational
c)complex
d)simple
Answer D

13. In which records are accessed from and inserted into file Access is classified as;
a)random access
b)duplicate access
c)direct access
d)sequential access
Answer D

102
14. A ……………….. is the basic element of data where individual field contains a single value, such as an
employees last name, a data or the value of the sensor reading.
A) field
B) record
C) file
D) database
Answer A

15. A ……………………. is collection of related fields that can be treated as a unit by some application program.
A)field
B)record
C)file
D) database

Answer B

16. …………………….. communicate directly with peripheral devices or their controllers or channels.
A)Device drivers
B) Physical I/O
C) Basic I/O supervisor
D) Logical I/O

Answer A

17. The ……………………. is responsible for all file I/O initiation and termination.
A) Device drivers
B) Physical I/O
C) Basic I/O supervisor
D) Logical I/O

Answer C

18. ……………………….. provides a general purpose record I/O capability and maintains basic data about files.
A) Device drivers
B) Physical I/O
C) Basic I/O supervisor
D) Logical I/O
Answer D

19. In the ……………………… file organization, data are collected in the order in which they arrive where each
record consists of one burst of data.
A) pile
B) sequential
103
C) indexed sequential
D) indexed
Answer A
20. In …………………….. file organization, a fixed format is used for records where all records are of the same
length, consisting of the same number of fixed length fields in a particular order.
A) pile
B) sequential
C) indexed sequential
D) indexed
Answer B
21. The ……………………… maintains the key characteristic of the sequential file: Records are organized in
sequence based on a key field.
A) pile
B) sequential file
C) indexed sequential file
D) indexed file
Answer C

22. The ……………………… retains one limitation of the sequential file: effective processing is limited to that
which is based on a single field of the file.
A) pile
B) sequential file
C) indexed sequential file
D) indexed file
Answer C
23. …………………… are used mostly in applications where data are rarely processed exhaustively.
A) pile
B) sequential file
C) indexed sequential file
D) indexed file
Answer D

24. Airline reservation systems and inventory control system are the examples of …………………….. system.
A) pile
B) sequential file
C) indexed sequential file
D) indexed file
Answer B
25. The …………………. greatly reduced the time required to access a single record, without sacrificing the
sequential nature of the file.
A) pile
B) sequential file
C) indexed sequential file
D) indexed file

104
Answer C

26. In free space management, …………. method has negligible space overhead because there is no need for a disk
allocation table, merely for a pointer to the beginning of the chain and the length of the first portion.
A) Bit tables
B) Chained Free Portions
C) Indexing
D) Free Block List
Answer B

27. In …………………….. method on free space management, each block is assigned in a reserved portion of the
disk.
A) Bit tables
B) Chained Free Portions
C) Indexing
D) Free Block List
Answer D

28. A ………………… on free space management has the advantages that it relatively easy to find one or a
contiguous group of free blocks.
A) Bit table
B) Chained Free Portion
C) Indexing
D) Free Block List
Answer A

29. In ………………………….. method, the file allocation table contains a separate one level index for each file,
the index has one entry for each portion allocated to the file.
A) Chained allocation
B) Indexed allocation
C) Contiguous allocation
D) Variable allocation
Answer B

30. ………………….. is a preallocation strategy, using variable size portions where the file allocation table needs
just a single entry for each file, showing the starting block and the length of the file.
A) Chained allocation
B) Indexed allocation
C) Contiguous allocation
D) Variable allocation
Answer C

105
Long Answer Questions

1. What is a File?

A file is a named collection of related information that is recorded on secondary storage. A file contains either
programs or data. A file has certain “structure” based on its type.
File attributes: Name, identifier, type, size, location, protection, time, date
File operations: creation, reading, writing, repositioning, deleting, truncating, appending, renaming
File types: executable, object, library, source code etc.

2. List the various File Attributes.


A file has certain other attributes, which vary from one operating system to another, but typically consist of
these: Name, identifier, type, location, size, protection, time, date and user identification.

3. What are the various File Operations?

The basic file operations are,


Creating a file
Writing a file
Reading a file
Repositioning within a file
Deleting a file
Truncating a file

4. What is the information associated with an Open File?

Several pieces of information are associated with an open file which may be: File pointer
File open count, Disk location of the file Access rights

5. What are the different Accessing Methods of a File?

The different types of accessing a file are:

Sequential access: Information in the file is accessed sequentially


Direct access: Information in the file can be accessed without any particular order.
Other access methods: Creating index for the file, indexed sequential access method (ISAM) etc.

6. What is Directory?
The device directory or simply known as directory records information- such as name, location, size, and
type for all files on that particular partition. The directory can be viewed as a symbol table that translates file
names into their directory entries.

106
7. What are the operations that can be performed on a Directory?

The operations that can be performed on a directory are,


Search for a file
Create a file
Delete a file
Rename a file
List directory
Traverse the file system

8. What are the most common schemes for defining the Logical Structure of aDirectory?

The most common schemes for defining the logical structure of a


Single directory Level Directory
Two level Directory
Tree Structured Directories
Acyclic-Graph Directories
Central Graph Directory

9. What is Access Control List (ACL)?

The most general scheme to implement identity-dependent access is to associate with each file and directory
an access control unit.

10. What are the Structures used in File-System Implementation?


Several on-disk and in-memory structures are used to implement a file system
On disk structure include
Boot control block
Partition block
Directory structure used to organize the file control block(FCB)
In memory structure include
In memory partition table
In memory directory structure
System wide open file table
Per process open table

11. Define Seek Time and Latency Time.

107
The time taken by the head to move to the appropriate cylinder or track is called seek time. Once the head is
at right track, it must wait until the desired block rotates under the read-write head. This delay is latency time.

12. What are the Allocation Methods of a Disk Space?

Three major methods of allocating disk space which are widely in use are
Contiguous allocation
Linked allocation
Indexed allocation

13. What are the advantages of Contiguous Allocation?


The advantages are,
Supports direct access
Supports sequential access
Number of disk seeks is minimal

14. What are the drawbacks of Contiguous Allocation of Disk Space?


The disadvantages are,
Suffers from extrernal fragmentation
Suffers from internal fragmentation
Difficulty in finding space for a new file
File connot be extended
Size of the file is to be declared in advance

15. Define Rotational Latency and Disk Bandwidth.


Rotational latency is the additional time waiting for the disk to rotate the desired sector to the disk head. The
disk bandwidth is the total number of bytes transferred, divided by the time between the first request for
service and the completion of the last transfer.

d) Suggested Skill Development Projects/ Assignments

Teams of 2 or 3 students will be allotted a topic from the subject of operating system covered in class or
important topics from the syllabus and will be required to give a presentation to their peers during the end of
semester by making a relevant presentation particularly including the higher level concepts and using the
practical approach for the same.

108
Keywords:

Operating System, swapping, paging, segmentation, virtual memory, page replacement, demand paging,
thrashing, Semaphores, Process, scheduling algorithms, Arrival Time (AT), Completion Time (CT), Burst
Time (BT), Turn Around Time (TAT), Turn Around Time (TAT), Response Time, Gant Chart, Deadlocks,
resource Allocation Graph, mutual exclusion, hold and wait, circular wait, no preemption, bankers algorithm,
file allocation, access control mechanism, file operations, allocation methods, directory structure, seek time,
latency time, free space management, security, file protection.

Essential and Additional Readings

• Silbersachatz and Galvin, “Operating System Concepts”, John Wiley & Sons, 7th Ed. 2005
• Haldar/Aravind, “Operating System”, Pearson Edu.
• “Thomas Anderson and Michael Dahlin, “Operating Systems: Principles & Practices, Volume III Memory
Management”, Second Edition
• Andrew S.Tanenbaum and Herbert Bos, “Modern Operating System”, Pearson.

References

• Madnick E., Donovan J., “Operating Systems”, Tata McGraw Hill, 2001
• Tannenbaum, “Operating Systems”, PHI, 4th Edition, 2000
• An Introduction to Operating Systems: Concepts & Practice, Bhatt, PHI
• William Stallings, “Operating Systems: Internals and Design Principles”, Seventh Edition, Prentice Hall

109

You might also like