Sei sulla pagina 1di 203

SEMESTER II

Core Paper-VI
Operating Systems
Madras University
M.Sc. (IT) C-19 Batch
SEMESTER – II
Core Paper - VI :Operating Systems
Objective of the course :This course introduces the fundamental concepts of operating
Systems with case studied on Unix and Windows.
Unit 1: Introduction – Multiprogramming - Time sharing - Distributed system - Real-Time
systems - I/O structure - Dual-mode operation - Hardware protection _ General system
architecture - Operating system services - System calls - System programs - System design
and implementation. Process Management: Process concept - Concurrent process -
Scheduling concepts - CPU scheduling -Scheduling algorithms, Multiple processor
Scheduling
Unit 2: Process Management: Process Synchronization - Critical section - Synchronization
hardware - Semaphores, classical problem of synchronization, Interprocess communication.
Deadlocks: Characterization, Prevention, Avoidance, and Detection.
Unit 3: Storage management - Swapping, single and multiple partition allocation - paging -
segmentation - paged segmentation, virtual memory - demand paging - page replacement and
algorithms, thrashing. Secondary storage management - disk structure - free space
management - allocation methods – disk scheduling - performance and reliability
improvements - storage hierarchy.
Unit 4: Files and protection - file system organization - file operations - access methods -
consistency semantics - directory structure organization - file protection - implementation
issues - security – encryption
Unit 5: Case Studies: UNIX and Windows operating systems.

1. Recommended Texts :
i) A. Silberschatz P.B. Galvin, Gange, 2002, Operating System Concepts, 6th Edn.,
Addison-Wesley Publishing Co., Boston.
2. Reference Books:
i) H.M. Deitel, 1990, An Introduction to Operating Systems, Addison Wesley Publishing Co.,
Boston
ii) D.M. Dhamdhare , 2002, Operating System, Tata McGraw-Hill, New Delhi.
iii) A.S. Tanenbaum , Operating Systems: Design and Implementation, Prentice-Hall of India,
New Delhi.
iv) Nutt, 2005, Operating Systems, 3 rd Edition, Pearson Education, Delhi.
INTRODUCTION

An operating system (OS) is a set of programs that control the execution of application
programs and act as an intermediary between a user of a computer and the computer
hardware. OS is software that manages the computer hardware as well as providing an
environment for application programs to run.
Examples of OS are: Windows, Windows/NT, OS/2 and MacOS

The objectives of OS are:


(1) To make the computer system convenient and easy to use for the user.
(2) To use the computer hardware in an efficient way.
(3) To execute user programs and make solving user problems easier

OS performs many functions such as:


1. Implementing user interface.
2. Sharing HW among users.
3. Allowing users to share data among themselves.
4. Preventing users from interfering with one another.
5. Scheduling resource among users.
6. Facilitating I/O operations.
7. Recovering from errors.
8. Accounting for resource storage.
9. Facilitating parallel operations.
10. Organizing data for secure and rapid access.
11. Handling network communications.

Operating system Categories


The main categories of modern OS may be classified into three groups which are
distinguished by the nature of interaction that takes place between the computer and the
user:
1. Batch system

In this type of OS, users submit jobs on regular schedule (e.g. daily, weekly, monthly) to a
central place where the user of such system did not interact directly with computer system.
To speed up the processing, jobs with similar needs were batched together and were run
through the computer as a group. Thus, the programmer would leave the programs with the
operator. The output from each job would send to the appropriate programmer. The major
task of this type was to transfer control automatically from one job to the next.
Disadvantages of Batch System
1. Turnaround time can be large from user standpoint.
2. Difficult to debug program.

2. Time-Sharing System

This type of OS provides on-line communication between the user and the system, the user
gives his instructions directly and receives intermediate response, and therefore it called
interactive system.
The time-sharing system allows many users simultaneously share the computer system. The
CPU is multiplexed rapidly among several programs, which are kept in memory and on disk.
A program swapped in and out of memory to the disk.
Time sharing system reduces the CPU ideal time. The disadvantage is more complex.

3. Real time operating system

Real Time System is characterized by supplying immediate response. It guarantees that


critical tasks complete on time. This type must have a pre-known maximum time limit for
each of the functions to be performed on the computer. 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.
The airline reservation system is an example of this type.
Performance development of OS:

1. On-line and off-line operation


A special subroutine was written for each I/O device called a device controller. Some I/O
devices has been equipped for either on-line operation (they are connected to the
processor), or off-line operations (they are run by control unit).

2. Buffering
A buffer is an area of primary storage for holding data during I/O transfer. On input, the data
are placed in the buffer by an I/O channel, when the transfer is complete the data may be
accessed the processor. The buffing may be single or double.

3. Spooling (Simultaneously Peripheral Operation On-Line)

Spooling uses the disk as a very large buffer. Spooling is useful because device access data
that different rates. The buffer provides a waiting station where data can rest while the
slower device catches up.
Spooling allows overlapping between the computation of one job and I/O of another job.

4. Multiprogramming

In multiprogramming several programs are kept in main memory at the same time, and the
CPU is switching between them, thus the CPU always has a program to be execute. The OS
begins to execute one program from memory, if this program need wait such as an I/O
operation, the OS switches to another program. Multiprogramming increases CPU utilization.
Multiprogramming system provide an environment in which the various system resources
are utilized effectively, but they do not provide for user interaction with the computer
system.
Advantages
a) High CPU utilization.
b) It appears that many programs are allotted CPU almost simultaneously.

Disadvantages
a) CPU scheduling is requiring.
b) To accommodate many jobs in memory, memory management is required.

5. Parallel system

There are more than on processor in the system. These processors share the computer bus,
clock, memory and I/O devices.
The advantage is to increase throughput (the number of programs completed in time unit).

6. Distributed system

Distribute the computation among several physical processors. It involves connecting 2 or


more independent computer systems via communication link. So, each processor has its own
O.S. and local memory; processors communicate with one another through various
communications lines, such as high-speed buses or telephone lines.

Advantages of distributed systems:

a) Resources Sharing – You can share files and printers.


b) Computation speed up – A job can be partitioned so that each processor can do a portion
concurrently (load sharing).
c) Reliability – If one processor failed the rest still can function with no problem.
d) Communications – Such as electronic mail, ftp
7. Personal computer

Personal computers – computer system dedicated to a single user. PC operating systems


were neither multi-user nor multi-tasking. The goal of PC operating systems were to
maximize user convenience and responsiveness instead of maximizing CPU and I/O
utilization.
•Examples: Microsoft Windows and Apple Macintosh

OPERATING SYSTEM SERVICE

An operating system provides services to programs and to the users of those programs. The
common services provided by the operating system are:
1. Program execution: Operating system loads a program into memory and executes the
program. The program must be able to end its execution, either normally or abnormally.
2. I/O Operation: I/O means any file or any specific I/O device. Program may require any I/O
device while running. So operating system must provide the required I/O.
3. File system manipulation: Program needs to read a file or write a file. The operating system
gives the permission to the program for operation on file.
4. Communication: Data transfer between two processes is required for some time. The both
processes are on the one computer or on different computer but connected through
computer network. Communication may be implemented by two methods:

a. Shared memory
b. Message passing.

5. Error detection: error may occur in CPU, in I/O devices or in the memory hardware. The
operating system constantly needs to be aware of possible errors. It should take the
appropriate action to ensure correct and consistent computing.
OPERATING SYSTEM OPERATIONS

Modern operating systems are interrupt driven. If there are no processes to execute, no I/O
devices to service, and no users to whom to respond, an operating system will sit quietly,
waiting for something to happen. Events are almost always signaled by the occurrence of an
interrupt or a trap. A trap is a software-generated interrupt caused either by an error (for
example, division by zero or invalid memory access) or by a specific request from a user
program that an operating-system service be performed. For each type of interrupt, separate
segments of code in the operating system determine what action should be taken. An
interrupt service routine is provided that is responsible for dealing with the interrupt.
Since the operating system and the users share the hardware and software resources of the
computer system, we need to make sure that an error in a user program could cause
problems only for the one program that was running. With sharing, many processes could
be adversely affected by a bug in one program. A properly designed operating system must
ensure that an incorrect (or malicious) program cannot cause other programs to
execute incorrectly.

A) Dual-Mode Operation

We must be able to distinguish between the execution of operating-system code and user
defined code. The approach is to separate the two modes of operation: user mode and
kernel mode (also called supervisor mode, system mode, or privileged mode). A bit, called
the mode bit is added to the hardware of the computer to indicate the current mode: kernel
(0) or user (1). The dual mode of operation provides us with the means for protecting the
operating system from errant users-and errant users from one another.
System calls provide the means for a user program to ask the operating system to perform
tasks reserved for the operating system on the user program's behalf.
B) Protection CPU

To ensure that the operating system maintains must control over the CPU. We must prevent
a user program from getting stuck in an infinite loop or not calling system services and
never returning control to the operating system. To accomplish this goal, we can use a timer.
A timer can be set to interrupt the computer after a specified fixed or variable period.

OPERATING SYSTEM COMPONENTS

1. Process Management

In multiprogramming environment, OS decides which process gets the processor when and
how much time. The operating system is responsible for the following activities in regard to
process management:
1) Creating and deleting both user and system processes
2) Suspending and resuming processes
3) Providing mechanisms for process synchronization
4) Providing mechanisms for process communication
5) Providing mechanisms for deadlock handling

2. Memory Management

Main memory is a large array of words or bytes where each word or byte has its own address.
The operating system is responsible for the following activities in regard to memory
management:
1) Keeping track of which parts of memory are currently being used and by whom
2) Deciding which processes (or parts thereof) and data to move into and out of memory
3) Allocating and deallocating memory space as needed.
3. File system Management

The operating system is responsible for the following activities in regard to file management:
1) Creating and deleting files
2) Creating and deleting directories to organize files
3) Supporting primitives for manipulating files and directories
4) Mapping files onto secondary storage
5) Backing up files on stable (nonvolatile) storage media

4. Secondary storage Management

OS provides the following activities in connection with disk management:


1. Free-space management.
2. Storage allocation
3. Disk scheduling.

SYSTEM CALL AND SYSTEM PROGRAM

System calls provide an interface between the running program and the operating system.
User cannot execute privileged instructions; the user must ask OS to execute them- system
calls. System calls are implemented using traps.
OS gains control through trap, switches to kernel mode, performs service, switches back to
user mode, and gives control back to user.
Example about how system calls are used from the OS to read data from one file and copy
them to another file, shown in figure. the programmer never see this level of details

Acquire input filename


Write prompt to screen
Accept input
Acquire output filename
Write prompt to screen
Accept input
Open the input file
If file does not exist, abort
Create output file
If file exist, abort
Loop
Read from input file
Write to output file
Until read fail
Close output file
Write completion message on screen
Terminate normally

System program provide basic function to users, so that they don’t need to write their own
environment for program development (editors, compilers) and program execution (shells).

PROTECTION AND SECURITY

Protection refers to mechanism that control the access of programs or users to the both
system and resources. The protection mechanism must:
1. Distinguish between unauthorized and authorized user.
2. Specify the control to be imposed.
3. Provide a means of enforcement.

Security measures are responsible for defending a computer system from external or
internal attack.
MULTIPROGRAMMING

overcome the problem of underutilization of CPU and main memory, the multiprogramming
was introduced. The multiprogramming is interleaved execution of multiple jobs by the same
computer.
In multiprogramming system, when one program is waiting for I/O transfer; there is another
program ready to utilize the CPU. So it is possible for several jobs to share the time of the
CPU. But it is important to note that multiprogramming is not defined to be the execution of
jobs at the same instance of time. Rather it does mean that there are a number of jobs
available to the CPU (placed in main memory) and a portion of one is executed then a
segment of another and so on. A simple process of multiprogramming is shown in figure
As shown in fig, at the particular situation, job' A' is not utilizing the CPU time because it is
busy in I/ 0 operations. Hence the CPU becomes busy to execute the job 'B'. Another job C is
waiting for the CPU for getting its execution time. So in this state the CPU will never be idle
and utilizes maximum of its time.

A program in execution is called a "Process", "Job" or a "Task". The concurrent execution of


programs improves the utilization of system resources and enhances the system throughput
as compared to batch and serial processing. In this system, when a process requests some
I/O to allocate; meanwhile the CPU time is assigned to another ready process. So, here when
a process is switched to an I/O operation, the CPU is not set idle.

Multiprogramming is a common approach to resource management. The essential


components of a single-user operating system include a command processor, an input/
output control system, a file system, and a transient area. A multiprogramming operating
system builds on this base, subdividing the transient area to hold several independent
programs and adding resource management routines to the operating system's basic
functions.
Time Sharing Operating System

A time sharing system allows many users to share the computer resources simultaneously.
In other words, time sharing refers to the allocation of computer resources in time slots to
several programs simultaneously. For example a mainframe computer that has many users
logged on to it. Each user uses the resources of the mainframe -i.e. memory, CPU etc. The
users feel that they are exclusive user of the CPU, even though this is not possible with one
CPU i.e. shared among different users.

The time sharing systems were developed to provide an interactive use of the computer
system. A time shared system uses CPU scheduling and multiprogramming to provide each
user with a small portion of a time-shared computer. It allows many users to share the
computer resources simultaneously. As the system switches rapidly from one user to the
other, a short time slot is given to each user for their executions.

The time sharing system provides the direct access to a large number of users where CPU
time is divided among all the users on scheduled basis. The OS allocates a set of time to each
user. When this time is expired, it passes control to the next user on the system. The time
allowed is extremely small and the users are given the impression that they each have their
own CPU and they are the sole owner of the CPU. This short period of time during that a user
gets attention of the CPU; is known as a time slice or a quantum. The concept of time sharing
system is shown in figure.
In above figure the user 5 is active but user 1, user 2, user 3, and user 4 are in waiting state
whereas user 6 is in ready status.

As soon as the time slice of user 5 is completed, the control moves on to the next ready user
i.e. user 6. In this state user 2, user 3, user 4, and user 5 are in waiting state and user 1 is in
ready state. The process continues in the same way and so on.

The time-shared systems are more complex than the multi-programming systems. In time-
shared systems multiple processes are managed simultaneously which requires an adequate
management of main memory so that the processes can be swapped in or swapped out
within a short time.

Note: The term 'Time Sharing' is no longer commonly used, it has been replaced by
'Multitasking System'.

Distributed system:

Manages a collection of independent computers and makes them appear to the users of the
system as if it were a single computer.
Distributed Operating System is a model where distributed applications are running on
multiple computers linked by communications. A distributed operating system is an
extension of the network operating system that supports higher levels of communication
and integration of the machines on the network.

This system looks to its users like an ordinary centralized operating system but runs on
multiple, independent central processing units (CPUs).

These systems are referred as loosely coupled systems where each processor has its own local
memory and processors communicate with one another through various communication
lines, such as high speed buses or telephone lines. By loosely coupled systems, we mean that
such computers possess no hardware connections at the CPU - memory bus level, but are
connected by external interfaces that run under the control of software.

The Distributed Os involves a collection of autonomous computer systems, capable of


communicating and cooperating with each other through a LAN / WAN. A Distributed Os
provides a virtual machine abstraction to its users and wide sharing of resources like as
computational capacity, I/O and files etc.

The structure shown in fig contains a set of individual computer systems and workstations
connected via communication systems, but by this structure we can not say it is a distributed
system because it is the software, not the hardware, that determines whether a system is
distributed or not.

The users of a true distributed system should not know, on which machine their programs
are running and where their files are stored. LOCUS and MICROS are the best examples of
distributed operating systems.

Using LOCUS operating system it was possible to access local and distant files in uniform
manner. This feature enabled a user to log on any node of the network and to utilize the
resources in a network without the reference of his/her location. MICROS provided sharing
of resources in an automatic manner. The jobs were assigned to different nodes of the whole
system to balance the load on different nodes.

Below given are some of the examples of distributed operating systems:

l. IRIX operating system; is the implementation of UNIX System V, Release 3 for Silicon
Graphics multiprocessor workstations.

2. DYNIX operating system running on Sequent Symmetry multiprocessor computers.

3. AIX operating system for IBM RS/6000 computers.

4. Solaris operating system for SUN multiprocessor workstations.

5. Mach/OS is a multithreading and multitasking UNIX compatible operating system;

6. OSF/1 operating system developed by Open Foundation Software: UNIX compatible.

Distributed systems provide the following advantages:


1 Sharing of resources.

2 Reliability.

3 Communication.

4 Computation speed up

Distributed systems are potentially more reliable than a central system because if a system
has only one instance of some critical component, such as a CPU, disk, or network interface,
and that component fails, the system will go down. When there are multiple instances, the
system may be able to continue in spite of occasional failures. In addition to hardware
failures, one can also consider software failures. Distributed systems allow both hardware
and software errors to be dealt with.

A distributed system is a set of computers that communicate and collaborate each other
using software and hardware interconnecting components. Multiprocessors (MIMD
computers using shared memory architecture), multicomputer connected through static or
dynamic interconnection networks (MIMD computers using message passing architecture)
and workstations connected through local area network are examples of such distributed
systems.

A distributed system is managed by a distributed operating system. A distributed operating


system manages the system shared resources used by multiple processes, the process
scheduling activity (how processes are allocating on available processors), the
communication and synchronization between running processes and so on. The software for
parallel computers could be also tightly coupled or loosely coupled. The loosely coupled
software allows computers and users of a distributed system to be independent each other
but having a limited possibility to cooperate. An example of such a system is a group of
computers connected through a local network. Every computer has its own memory, hard
disk. There are some shared resources such files and printers. If the interconnection network
broke down, individual computers could be used but without some features like printing to
a non-local printer.
REAL TIME SYSTEM:

An RTOS is a class of operating systems that are intended for real time-applications • What
is a real time application? • A real time application is an application that guarantees both
correctness of result and the added constraint of meeting a deadline
• An operating system which follows the Real Time criteria. • ² Efficiency, Predictability and
Timeliness – important • – All components of an RTOS must have these properties. • Some
tasks which may delay things: • – Interrupt Processing, Context Switching, Inter-task
communication,
• IO • To cut back on (variable) overhead for these tasks: • – Multiprogramming, Memory
Management, File (and other) IO, IPC, • etc.
• An RTOS will provide facilities to guarantee deadlines will be met • An RTOS will provide
scheduling algorithms in order to enable deterministic behavior in the system • An RTOS is
valued more for predictability than throughput
The real-time operating system used for a real-time application means for those
applications where data processing should be done in the fixed and small quantum of time.
It is different from general purpose computer where time concept is not considered as much
crucial as in Real-Time Operating System. RTOS is a time-sharing system based on clock
interrupts. Interrupt Service Routine (ISR) serve the interrupt, raised by the system. RTOS
used Priority to execute the process. When a high priority process enters in system low
priority process preempted to serve higher priority process. Real-time operating system
synchronized the process. So that they can communicate with each other. Resources can be
used efficiently without wastage of time.

RTOS are controlling traffic signal; Nuclear reactors Control scientific experiments, medical
imaging systems, industrial system, fuel injection system, home appliance are some
application of Real Time operating system

Real time Operating Systems are very fast and quick respondent systems. These systems
are used in an environment where a large number of events (generally external) must be
accepted and processed in a short time. Real time processing requires quick transaction and
characterized by supplying immediate response. For example, a measurement from a
petroleum refinery indicating that temperature is getting too high and might demand for
immediate attention to avoid an explosion.

In real time operating system there is a little swapping of programs between primary and
secondary memory. Most of the time, processes remain in primary memory in order to
provide quick response, therefore, memory management in real time system is less
demanding compared to other systems.

Time Sharing Operating System is based on Event-driven and time-sharing the design.

The event Driven : In event-driven switching, higher priority task requires CPU service first
than a lower priority task, known as priority scheduling.

Time Sharing : Switching takes place after fixed time quantum known as Round Robin
Scheduling.

In these design, we mainly deal with three states of the process cycle

1) Running : when CPU is executing a process, then it is in running state.

2) Ready : When a process has all the resources require performing a process, but still it is
not in running state because of the absence of CPU is known as the Ready state.

3) Blocked : when a process has not all required resources for execution, then it is blocked
state.

Interrupt Latency : Interrupt latency is time between an interrupt is generated by a device


and till it serviced. In RTOS, Interrupt maintained in a fixed amount of time, i.e., latency time
bounded.

Memory Allocation: RTOS support static as well as dynamic memory allocation.Both


allocations used for different purpose.Like Static Memory, the allocation is used for compile
and design time using stack data structure. Dynamic memory allocation used for runtime
used heap data structure.

The primary functions of the real time operating system are to:

1. Manage the processor and other system resources to meet the requirements of an
application.
2. Synchronize with and respond to the system events.

3. Move the data efficiently among processes and to perform coordination among these
processes.

The Real Time systems are used in the environments where a large number of events
(generally external to the computer system) is required to be accepted and is to be processed
in the form of quick response. Such systems have to be the multitasking. So the primary
function of the real time operating system is to manage certain system resources, such as the
CPU, memory, and time. Each resource must be shared among the competing processes to
accomplish the overall function of the system Apart from these primary functions of the real
time operating system there are certain secondary functions that are not mandatory but are
included to enhance the performance:

1. To provide an efficient management of RAM.

2. To provide an exclusive access to the computer resources.

The term real time refers to the technique of updating files with the transaction data
immediately just after the event that it relates with.

Few more examples of real time processing are:

1. Airlines reservation system.

2. Air traffic control system.

3. Systems that provide immediate updating.

4. Systems that provide up to the minute information on stock prices.

5. Defense application systems like as RADAR.

Real time operating systems mostly use the preemptive priority scheduling. These support
more than one scheduling policy and often allow the user to set parameters associated with
such policies, such as the time-slice in Round Robin scheduling where each task in the task
queue is scheduled up to a maximum time, set by the time-slice parameter, in a round robin
manner. Hundred of the priority levels are commonly available for scheduling. Some specific
tasks can also be indicated to be non-preemptive.
COMPONENTS OF REAL TIME OPERATING SYSTEM

A real time operating system contains the following components:


• The Scheduler: this element in RTOS tells that in which order the tasks can be executed
which is generally based on the priority. It is the main element of RTOS.
• Symmetric Multiprocessing (SMP): a number of different multiple tasks can be handled
by the RTOS as this is its ability so that parallel processing can be done. This is also known
as multitasking.
• Function Library: It is the element of RTOS which acts as a interface so that it can connect
kernel and application code. This application code sends the requests to the kernel via
function library so that the application can give the desired result.
• Memory Management: this element is required so that the system can allocate the
memory to every program. It is an important element of the RTOS.
• Fast dispatch latency or context switch time: The term dispatch latency means the time
interval between the termination of the task which is identified by the operating system and
the time taken by the thread, which is in the ready queue, that has started processing.
As its name implies, context switch time is the time which the system takes to switch from
one running thread to another. This time saves the context of the current task and also
replaces it with the context of new thread and this time needs to be minimal and optimal for
an RTOS.
• User-defined data objects and classes: RTOS makes use of the programming languages
like C/C++ which are organized according to the type of their operation. RTOS will use them
so that it can control the specified application.
TYPES OF REAL-TIME OPERATING SYSTEM
1) Soft Real-Time Operating System: A process might not be executed in given deadline. It
can be crossed it then executed next, without harming the system. Example are a digital
camera, mobile phones, etc.

2) Hard Real-Time Operating System: A process should be executed in given deadline. The
deadline should not be crossed. Preemption time for Hard Real-Time Operating System is
almost less than few microseconds.

Examples are Airbag control in cars, anti-lock brake, engine control system, etc.

APPLICATIONS OF REAL TIME OPERATING SYSTEM

• Control systems: RTOS are designed in such a way so that they can control actuators and
sensors. They can execute control system commands. Controlled systems are those which
can be monitored by using sensors and their tasks can be altered with the help of actuators.
Now the task of the RTOS is to read the data from the sensors and move the actuators by
performing some calculations so that flight’s movement can be handled.

• Image processing (IP): real time image processing is done so that we can make some
adjustments for the moving objects. In this, we need our computers, cameras or other
gadgets should work in real time as utmost precision is required in the industrial automation
tasks. For ex, something happens with the conveyor belt when the item is moving
downwards or some other defect occurs, we can control these problems in the real time if
our system works in real time.

• Voice Over IP (VoIP): we can transmit voice over IPs or internet protocol in real time. This
is known as VoIP. In this our voice is digitalized, compressed into small form and converted
into IP packets in real time before transmitting it to the other network on router through IP.
Difference between in GPOS and RTOS
General-Purpose Operating
Real-Time Operating System (RTOS)
System (GPOS)

1) It used for desktop pc, laptop. 1) It applied for the embedded application.

2) Process-based Scheduling used. 2) Time-based scheduling used like round robin.

3) Interrupt latency is not


3) Interrupt lag is minimal, measured in few
considered as much crucial as in
microseconds.
RTOS.

4) No priority inversion 4) Priority inversion mechanism is current.Once


mechanism is present in the priority set by the programmer, it can’t be changed by
system. the system itself.

5) Kernel operations may or may


5) Kernel operation can be preempted.
not be preempted.

Considerations for choosing a RTOS


• Performance: it is the most important factor needed to be considered while choosing for
a RTOS. As we know RTOS is a system which gives us guarantee about the data within a given
frame of time. So it does not take care of our PC whatever is happening with it. So we must
have to keep a check on its performance.

• Unique features: A good RTS must be scalable and it has some extra features like efficient
protection of the memory of the system, how it operates to execute commands etc.. So, we
have to evaluate these features so that our system can run effectively and efficiently.
• Your IT team: A good RTOS is that which works in the favor of our IT team. Here, favor
means reducing the labor intensity of the team. If this intensity of the team get reduced, then
the team can concentrate on other factors. So we have to decide those RTOS, through which
our team can easily familiar with.

• Middleware: if there will be no middleware support in RTOS, then the problem of time-
consuming integration of processes will take place. Here, middleware support means those
components which can be integrated with our RTOS.
Advantages of real time operating system

• Error free: Real time operating systems are error-free. So there is no chance of getting an
error when we are performing the task.

• Maximum Consumption: we can achieve maximum consumption by using RTOS. It keeps


all the devices active and produces more output by making use of all the resources.

• Embedded system usage: As the programs of the RTOS are of small size. So, we make use
of RTOS in embedded systems.

• Task shifting: In RTOS, shifting time of the tasks is very small.


• 24/7 performance: RTOS is best used for those applications which run constantly or you
can say that which runs for 24 hours and 7 days in a week. This is possible because of fewer
shifting of tasks so that it can give maximum output.

• Application focus: in RTOS, a very few tasks are managed so that exact results can be given
by the system. It gives very less importance to those applications which are in waiting state.
DISADVANTAGES OF REAL TIME OPERATING SYSTEM
• Limited tasks: RTOS can run very limited tasks simultaneously and it concentrates on only
those applications which contain error so that it can avoid them. Due to this, some tasks have
to wait for unlimited time.

• Low multi-tasking: As RTOS is the system which concentrates on few tasks. So sometimes
it is difficult for these systems to do multi-tasking.

• Device driver and interrupt signals: Some specific drivers are needed for the RTOS so
that it can give fast response to the interrupt signals to maintain its speed.

• Expensive: As stated earlier, a lot of resources are used by RTOS which makes it expensive.

• Low priority of tasks: The tasks which has the low priority has to wait for a long time as
the RTOS maintain the accuracy of the current programs which are under execution.

• Thread priority: as we know that there is very less switching of tasks is done in RTOS. So
that’s why its thread priority is not good.

• Precision of code: as we know, in RTOS, event handling of tasks is a strict process as it is


error-free. So more precision in code is required which is a difficult task for the programmer
as getting exact precision is not so easy.

• Complex algorithms: it makes use of some complex algorithms so that the system can give
the desired output. These complex algorithms are difficult to understand.

• Use heavy system resources: real time operating system makes use of a lot of resources.
Sometimes it is not good for the system. It seems to be expensive also.
• Not easy to program: Sometime the programmer has to write the complex programs for
the efficient working of the RTOS which is not an easy task.

• Other factors: some other factors like error handling, memory management and CPU are
also needed to be considered.

Input/Output

The layer below the File System is the Input/output (I/O) system which controls all physical
communication with external devices, such as disk drives, keyboard, printers and displays.
If a higher layer requires access to a device, Ex: if the file system wants to read a block from
disk, a request is sent to the I/O layer.

Processes in the I/O layer are usually activated by means of interrupts. An interrupt occurs
when an external device, such as a disk drive or communications port signals that it is ready
to send or receive data. When the Kernel receives an interrupt, it determined which device
it came from and then calls upon the correct I/O process to handle the transfer.

There are two main categories of Input/Output: Character I/O, where the data is in the form
of single numbers or characters (eg: keys pressed on a keyboard) and Block I/O, where
larger blocks of data are to be transferred at the same time (eg: reading or writing a block
from disk).

With Character I/O, only small amounts of data, normally a single byte, are transferred on
each interrupt. The total amount of data to be transferred may be large, but it is broken into
small quantities. The commonest character-mode devices are the keyboard, mouse,
joysticks, communications ports and printers.
Block I/O is used when large amounts of data (eg: a 2,048-byte disk block) need to be read
into or written back out from memory. Block-mode I/O could be carried out in the same way
as character I/O, by reading or writing the data one byte at a time, but this is a waste of
processor time.

Instead, the interrupting device carries out the transfer of the data into memory by itself.
This is known as Direct Memory Access (DMA) and is used wherever large amounts of data
need to be transferred between a device and contiguous memory locations as a block. All
that the I/O process needs to do is tell the device where in memory to start writing the data
and when to start. Another interrupt is sent when the device has finished transferring the
data, and the I/O process informs the higher-level requesting process that data has been
written or read.
IO SYSTEMS

There are two modes of operation in the operating system to make sure it works correctly.
These are user mode and kernel mode.

A diagram that illustrates the transition from user mode to kernel mode and back again is as
follows:

The following are the modes:


• User Mode:
The system is in user mode when the operating system is running a user application
such as handling a text editor. The transition from user mode to kernel mode occurs
when the application requests the help of operating system or an interrupt or a
system call occurs.
The mode bit is set to 1 in the user mode. It is changed from 1 to 0 when switching
from user mode to kernel mode.
• Kernel Mode
The system starts in kernel mode when it boots and after the operating system is
loaded, it executes applications in user mode. There are some privileged instructions
that can only be executed in kernel mode. These are interrupt instructions, input
output management etc. If the privileged instructions are executed in user mode, it is
illegal and a trap is generated.
The mode bit is set to 0 in the kernel mode. It is changed from 0 to 1 when switching
from kernel mode to user mode.

The concept of modes of operation in operating system can be extended beyond the dual
mode. This is known as the multimode system. In those cases the more than 1 bit is used by
the CPU to set and handle the mode.

An example of the multimode system can be described by the systems that support
virtualisation. These CPU’s have a separate mode that specifies when the virtual machine
manager (VMM) and the virtualisation management software is in control of the system.
For these systems, the virtual mode has more privileges than user mode but less than kernel
mode.

HARDWARE PROTECTION

A computer contains various hardware like processor, RAM, monitor etc. So, OS must ensure
that these devices remain intact (not directly accessible by the user).

It is divided into three categories:


1) CPU Protection

It means that a process should not hogg (hold) CPU forever otherwise other processes will
not get the process. For that purpose, a timer is introduced to prevent such a situation. A
process is given a certain time for execution after which a signal is sent to the process which
makes the process to leave CPU. Hence process will not hogg the CPU.

2) Memory Protection

There may be multiple processes in the memory so it is possible that one process may try to
access other process memory.

To prevent such situation, we use two register:

1. Base Register
2. Limit Register

Base register store the starting address of the program and Limit Register store the size of
the process. So whenever a process wants to access address in memory then it is checked
that it can access the memory or not.

3) I/O protection

To ensure CPU protection OS ensure that below case should not occur

• View I/O of other process


• Terminate I/O of another process
• Give priority to a particular process I/O

If an application process wants to access any I/O device then it will be done through system
call so that OS will monitor the task.
Like In C language write() and read() is a system call to read and write on file. There are two
modes in instruction execute:

1. User mode
The system performs a task on behalf of user application this instruction. In this
mode, the user cannot directly access hardware and reference memory.
2. Kernel mode
Whenever a direct access to hardware is required a system call is used by the
application program.

General System Architecture.


STORE PROGRAM CONTROL CONCEPT

The term Stored Program Control Concept refers to the storage of instructions in
computer memory to enable it to perform a variety of tasks in sequence or intermittently.

The idea was introduced in the late 1040s by John von Neumann who proposed that a
program be electronically stored in the binary-number format in a memory device so that
instructions could be modified by the computer as determined by intermediate
computational results.

ENIAC (Electronic Numerical Integrator and Computer) was the first computing system
designed in the early 1940s. It was based on Stored Program Concept in which machine use
memory for processing data.
Stored Program Concept can be further classified in three basic ways:

1. Von-Neumann Model

2. General Purpose System

3. Parallel Processing

Flynn's
Classification of Computers

M.J. Flynn proposed a classification for the organization of a computer system by the number
of instructions and data items that are manipulated simultaneously.

The sequence of instructions read from memory constitutes an instruction stream.


The operations performed on the data in the processor constitute a data stream.

Parallel processing may occur in the instruction stream, in the data stream, or both.
Flynn's classification divides computers into four major groups that are:

1. Single instruction stream, single data stream (SISD)

2. Single instruction stream, multiple data stream (SIMD)

3. Multiple instruction stream, single data stream (MISD)

4. Multiple instruction stream, multiple data stream (MIMD)


OPERATING SYSTEM SERVICES

User interface. Almost all operating systems have a user interface (UI). This interface can
take several forms. One is a command-line interface (CLI), which uses text commands and
a method for entering them (say, a keyboard for typing in commands in a specific format
with specific options). Another is a batch interface, in which commands and directives to
control those commands are entered into files, and those files are executed. Most commonly,
a graphical user interface (GUI) is used. Here, the interface is a window system with a
pointing device to direct I/O, choose from menus, and make selections and a keyboard to
enter text. Some systems provide two or all three of these variations.

• Program execution. The system must be able to load a program into memory and to run
that program. The program must be able to end its execution, either normally or abnormally
(indicating error).

• I/O operations. A running program may require I/O, which may involve a file or an I/O
device. For specific devices, special functions may be desired (such as recording to a CD or
DVD drive or blanking a display screen). For efficiency and protection, users usually cannot
control I/O devices directly. Therefore, the operating system must provide a means to do
I/O.

• File-system manipulation. The file system is of particular interest. Obviously, programs


need to read and write files and directories. They also need to create and delete them by
name, search for a given file, and list file information. Finally, some operating systems
include permissions
management to allow or deny access to files or directories based on file ownership. Many
operating systems provide a variety of file systems, sometimes to allow personal choice and
sometimes to provide specific features or performance characteristics.

Communications. There are many circumstances in which one process needs to exchange
information with another process. Such communication may occur between processes that
are executing on the same computer or between processes that are executing on different
computer systems tied together by a computer network. Communications may be
implemented via shared memory, in which two or more processes read and write to a
shared section of memory, or message passing, in which packets of information in
predefined formats are moved between processes by the
operating system.

• Error detection. The operating system needs to be detecting and correcting errors
constantly. Errorsmay occur in the CPU and memory hardware (such as a memory error or
a power failure), in I/O devices (such as a parity error on disk, a connection failure on a
network, or lack of paper in the printer), and in the user program (such as an arithmetic
overflow, an attempt to access an illegal memory location, or a too-great use of CPU time).
For each type of error, the operating system should take the appropriate action to ensure
correct and consistent computing. Sometimes, it has no choice
but to halt the system. At other times, it might terminate an error-causing process or return
an error code to a process for the process to detect and possibly correct. Another set of
operating system functions exists not for helping the user but rather for ensuring the
efficient operation of the system itself. Systems with multiple users can gain efficiency by
sharing the computer resources among the users.

• Resource allocation. When there are multiple users or multiple jobs running at the same
time, resources must be allocated to each of them. The operating system manages many
different types of resources. Some (such as CPU cycles, main memory, and file storage) may
have special allocation
code, whereas others (such as I/O devices) may have much more general request and release
code. For instance, in determining how best to use the CPU, operating systems have CPU-
scheduling routines that take into account the speed of the CPU, the jobs that must be
executed, the number of registers available, and other factors. There may also be routines to
allocate printers, USB storage drives, and other peripheral devices.

• Accounting. We want to keep track of which users use how much and what kinds of
computer resources. This record keeping may be used for accounting (so that users can be
billed) or simply for accumulating usage statistics. Usage statistics may be a valuable tool for
researchers who wish to reconfigure the system to improve computing services.

• Protection and security. The owners of information stored in a multiuser or networked


computer system may want to control use of that information. When several separate
processes execute concurrently, it should not be possible for one process to interfere with
the others or with the operating system itself. Protection involves ensuring that all access to
system resources is controlled. Security of the system from outsiders is also important. Such
security starts with requiring each user to authenticate himself or herself to the system,
usually by means of a password, to gain access to system resources. It extends to defending
external I/O devices, including network adapters, from invalid access attempts and to
recording all such connections for detection of break-ins. If a system is to be protected and
secure, precautions must be instituted throughout it. A chain is only as strong as its weakest
link.
SYSTEM CALLS

The system call provides an interface to the operating system services.


Application developers often do not have direct access to the system calls, but can access
them through an application programming interface (API). The functions that are included
in the API invoke the actual system calls. By using the API, certain benefits can be gained:

• Portability: as long a system supports an API, any program using that API can compile
and run.
• Ease of Use: using the API can be significantly easier then using the actual system call.

System Call Parameters

Three general methods exist for passing parameters to the OS:

1. Parameters can be passed in registers.


2. When there are more parameters than registers, parameters can be stored in a block
and the block address can be passed as a parameter to a register.
3. Parameters can also be pushed on or popped off the stack by the operating system.
TYPES OF SYSTEM CALLS

There are 5 different categories of system calls:


process control, file manipulation, device manipulation, information maintenance
and communication.

Process Control

A running program needs to be able to stop execution either normally or abnormally.


When execution is stopped abnormally, often a dump of memory is taken and can be
examined with a debugger.

File Management

Some common system calls are create, delete, read, write, reposition, or close. Also, there
is a need to determine the file attributes – get and set file attribute. Many times the OS
provides an API to make these system calls.

Device Management

Process usually require several resources to execute, if these resources are available,
they will be granted and control returned to the user process. These resources are also
thought of as devices. Some are physical, such as a video card, and others are abstract,
such as a file.

User programs request the device, and when finished they release the device. Similar to
files, we can read, write, and reposition the device.

Information Management

Some system calls exist purely for transferring information between the user program
and the operating system. An example of this is time, or date.
The OS also keeps information about all its processes and provides system calls to report
this information.

Communication

There are two models of interprocess communication, the message-passing model and
the shared memory model.

• Message-passing uses a common mailbox to pass messages between processes.


• Shared memory use certain system calls to create and gain access to create and
gain access to regions of memory owned by other processes. The two processes
exchange information by reading and writing in the shared data.

SYSTEM PROGRAMS

System programs provide basic functioning to users so that they do not need to write their
own environment for program development (editors, compilers) and program execution
(shells). In some sense, they are bundles of useful system calls.

These are the programs that are used and required to run the system - machine, input output
devices and other connected peripherals. They are also known as System softwares.

Application programming interface is a bunch of objects and classes or namespaces defined


in the operating system which provide an interface to the developer for developing
application which use the resources of the device. Kernel of the OS defines the API and a
control over the gateway to the device controllers that are connected.
OPERATING-SYSTEM DESIGN AND IMPLEMENTATION

Design Goals
• At the highest level, system design is dominated by the choice of hardware and system
type.
• Beyond this level, the requirements can be divided into two groups: user goals, and
system goals.
o User goals include convenience, reliability, security, and speed.
o System goals include ease of design, implementation, maintenance,
flexibility, and efficiency.

Implementation

• At first, operating systems were written in assembly, but now C/C++ is the language
commonly used
• Small blocks of assembly code are still needed, especially related to some low level
I/O functions in device drivers, turning interrupts on and off and the Test and Set
Instruction for Synchronization Facilities.
• Using higher level languages allows the code to be written faster. It also makes the OS
much easier to port to different hardware platforms.

OPERATING SYSTEM DESIGN GOALS

It is quite complicated to define all the goals and specifications of the operating system while
designing it.The design changes depending on the type of the operating system i.e if it is batch
system, time shared system, single user system, multi user system, distributed system etc.
There are basically two types of goals while designing an operating system. These are:
User Goals

The operating system should be convenient, easy to use, reliable, safe and fast according to
the users. However, these specifications are not very useful as there is no set method to
achieve these goals.

System Goals

The operating system should be easy to design, implement and maintain. These are
specifications required by those who create, maintain and operate the operating system. But
there is not specific method to achieve these goals as well.

OPERATING SYSTEM MECHANISMS AND POLICIES

There is no specific way to design an operating system as it is a highly creative task. However,
there are general software principles that are applicable to all operating systems.
A subtle difference between mechanism and policy is that mechanism shows how to do
something and policy shows what to do. Policies may change over time and this would lead
to changes in mechanism. So, it is better to have a general mechanism that would require few
changes even when a policy change occurs.
For example - If the mechanism and policy are independent, then few changes are required
in mechanism if policy changes. If a policy favours I/O intensive processes over CPU
intensive processes, then a policy change to preference of CPU intensive processes will not
change the mechanism.
OPERATING SYSTEM IMPLEMENTATION

The operating system needs to be implemented after it is designed. Earlier they were written
in assembly language but now higher level languages are used. The first system not written
in assembly language was the Master Control Program (MCP) for Burroughs Computers.

Advantages of Higher Level Language


There are multiple advantages to implementing an operating system using a higher level
language such as: the code is written more fast, it is compact and also easier to debug and
understand. Also, the operating system can be easily moved from one hardware to another
if it is written in a high level language.

Disadvantages of Higher Level Language


Using high level language for implementing an operating system leads to a loss in speed and
increase in storage requirements. However in modern systems only a small amount of code
is needed for high performance, such as the CPU scheduler and memory manager. Also, the
bottleneck routines in the system can be replaced by assembly language equivalents if
required.

PROCESS MANAGEMENT:

PROCESS
A process is basically 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.
To put it in simple terms, 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 simpliOied layout of
a process inside main memory −

S.N. Component & Description

1 Stack
The process Stack contains the temporary data such as method/function
parameters, return address and local variables.

2 Heap
This is dynamically allocated memory to a process during its run time.

3 Text
This includes the current activity represented by the value of Program Counter
and the contents of the processor's registers.

4 Data
This section contains the global and static variables.

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.

S.N. State & Description

1 Start
This is the initial state when a process is first started/created.

2 Ready
The process is waiting to be assigned to a processor. Ready processes are waiting
to have the processor allocated to them by the operating system so that they can
run. Process may come into this state after Start state or while running it by but
interrupted by the scheduler to assign CPU to some other process.

3 Running
Once the process has been assigned to a processor by the OS scheduler, the process
state is set to running and the processor executes its instructions.

4 Waiting
Process moves into the waiting state if it needs to wait for a resource, such as
waiting for user input, or waiting for a file to become available.

5 Terminated or Exit
Once the process finishes its execution, or it is terminated by the operating system,
it is moved to the terminated state where it waits to be removed from main
memory.
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 −

S.N. Information & Description

1 Process State
The current state of the process i.e., whether it is ready, running, waiting, or
whatever.

2 Process privileges
This is required to allow/disallow access to system resources.

3 Process ID
Unique identification for each of the process in the operating system.

4 Pointer
A pointer to parent process.

5 Program Counter
Program Counter is a pointer to the address of the next instruction to be executed
for this process.
6 CPU registers
Various CPU registers where process need to be stored for execution for running
state.

7 CPU Scheduling Information


Process priority and other scheduling information which is required to schedule
the process.

8 Memory management information


This includes the information of page table, memory limits, Segment table
depending on memory used by the operating system.

9 Accounting information
This includes the amount of CPU used for process execution, time limits, execution
ID etc.

10 IO status information
This includes a list of I/O devices allocated to the process.

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

The PCB is maintained for a process throughout its lifetime, and is deleted once the process
terminates.
PROCESS STATE DIAGRAM

PROCESS MANAGEMENT INTRODUCTION

A Program does nothing unless its instructions are executed by a CPU. A program in
execution is called a process. In order to accomplish its task, process needs the computer
resources.

There may exist more than one process in the system which may require the same resource
at the same time. Therefore, the operating system has to manage all the processes and the
resources in a convenient and efficient way.
Some resources may need to be executed by one process at one time to maintain the
consistency otherwise the system can become inconsistent and deadlock may occur.

The operating system is responsible for the following activities in connection with Process
Management

1. Scheduling processes and threads on the CPUs.

2. Creating and deleting both user and system processes.

3. Suspending and resuming processes.

4. Providing mechanisms for process synchronization.

5. Providing mechanisms for process communication.

Attributes of a process

The Attributes of the process are used by the Operating System to create the process control
block (PCB) for each of them. This is also called context of the process. Attributes which are
stored in the PCB are described below.

1. Process ID

When a process is created, a unique id is assigned to the process which is used for unique
identification of the process in the system.

2. Program counter

A program counter stores the address of the last instruction of the process on which the
process was suspended. The CPU uses this address when the execution of this process is
resumed.
3. Process State

The Process, from its creation to the completion, goes through various states which are new,
ready, running and waiting. We will discuss about them later in detail.

4. Priority

Every process has its own priority. The process with the highest priority among the
processes gets the CPU first. This is also stored on the process control block.

5. General Purpose Registers

Every process has its own set of registers which are used to hold the data which is generated
during the execution of the process.

6. List of open files

During the Execution, Every process uses some files which need to be present in the main
memory. OS also maintains a list of open files in the PCB.

7. List of open devices

OS also maintain the list of all open devices which are used during the execution of the
process.
The process, from its creation to completion, passes through various states. The minimum
number of states is five.

The names of the states are not standardized although the process may be in one of the
following states during execution.

1. New

A program which is going to be picked up by the OS into the main memory is called a new
process.
2. Ready

Whenever a process is created, it directly enters in the ready state, in which, it waits for the
CPU to be assigned. The OS picks the new processes from the secondary memory and put all
of them in the main memory.

The processes which are ready for the execution and reside in the main memory are called
ready state processes. There can be many processes present in the ready state.

3. Running

One of the processes from the ready state will be chosen by the OS depending upon the
scheduling algorithm. Hence, if we have only one CPU in our system, the number of running
processes for a particular time will always be one. If we have n processors in the system then
we can have n processes running simultaneously.

4. Block or wait

From the Running state, a process can make the transition to the block or wait state
depending upon the scheduling algorithm or the intrinsic behavior of the process.

When a process waits for a certain resource to be assigned or for the input from the user
then the OS move this process to the block or wait state and assigns the CPU to the other
processes.

5. Completion or termination

When a process finishes its execution, it comes in the termination state. All the context of the
process (Process Control Block) will also be deleted the process will be terminated by the
Operating system.
6. Suspend ready

A process in the ready state, which is moved to secondary memory from the main memory
due to lack of the resources (mainly primary memory) is called in the suspend ready state.

If the main memory is full and a higher priority process comes for the execution then the OS
have to make the room for the process in the main memory by throwing the lower priority
process out into the secondary memory. The suspend ready processes remain in the
secondary memory until the main memory gets available.

7. Suspend wait

Instead of removing the process from the ready queue, it's better to remove the blocked
process which is waiting for some resources in the main memory. Since it is already waiting
for some resource to get available hence it is better if it waits in the secondary memory and
make room for the higher priority process. These processes complete their execution once
the main memory gets available and their wait is finished.

OPERATIONS ON THE PROCESS


1. Creation

Once the process is created, it will be ready and come into the ready queue (main memory)
and will be ready for the execution.

2. Scheduling

Out of the many processes present in the ready queue, the Operating system chooses one
process and start executing it. Selecting the process which is to be executed next, is known
as scheduling.
3. Execution

Once the process is scheduled for the execution, the processor starts executing it. Process
may come to the blocked or wait state during the execution then in that case the processor
starts executing the other processes.

4. Deletion/killing

Once the purpose of the process gets over then the OS will kill the process. The Context of
the process (PCB) will be deleted and the process gets terminated by the Operating system.

PROCESS SCHEDULERS

Operating system uses various schedulers for the process scheduling described below.

1. Long term scheduler

Long term scheduler is also known as job scheduler. It chooses the processes from the pool
(secondary memory) and keeps them in the ready queue maintained in the primary memory.

Long Term scheduler mainly controls the degree of Multiprogramming. The purpose of long
term scheduler is to choose a perfect mix of IO bound and CPU bound processes among the
jobs present in the pool.

If the job scheduler chooses more IO bound processes then all of the jobs may reside in the
blocked state all the time and the CPU will remain idle most of the time. This will reduce the
degree of Multiprogramming. Therefore, the Job of long term scheduler is very critical and
may affect the system for a very long time.
2. Short term scheduler

Short term scheduler is also known as CPU scheduler. It selects one of the Jobs from the ready
queue and dispatch to the CPU for the execution.

A scheduling algorithm is used to select which job is going to be dispatched for the execution.
The Job of the short term scheduler can be very critical in the sense that if it selects job whose
CPU burst time is very high then all the jobs after that, will have to wait in the ready queue
for a very long time.

This problem is called starvation which may arise if the short term scheduler makes some
mistakes while selecting the job.

3. Medium term scheduler

Medium term scheduler takes care of the swapped out processes.If the running state
processes needs some IO time for the completion then there is a need to change its state from
running to waiting.

Medium term scheduler is used for this purpose. It removes the process from the running
state to make room for the other processes. Such processes are the swapped out processes
and this procedure is called swapping. The medium term scheduler is responsible for
suspending and resuming the processes.

It reduces the degree of multiprogramming. The swapping is necessary to have a perfect mix
of processes in the ready queue.
PROCESS QUEUES

The Operating system manages various types of queues for each of the process states. The
PCB related to the process is also stored in the queue of the same state. If the Process is
moved from one state to another state then its PCB is also unlinked from the corresponding
queue and added to the other state queue in which the transition is made.

There are the following queues maintained by the Operating system.


1. Job Queue

In starting, all the processes get stored in the job queue. It is maintained in the secondary
memory. The long term scheduler (Job scheduler) picks some of the jobs and put them in the
primary memory.

2. Ready Queue

Ready queue is maintained in primary memory. The short term scheduler picks the job from
the ready queue and dispatch to the CPU for the execution.
3. Waiting Queue

When the process needs some IO operation in order to complete its execution, OS changes
the state of the process from running to waiting. The context (PCB) associated with the
process gets stored on the waiting queue which will be used by the Processor when the
process finishes the IO.

VARIOUS TIMES RELATED TO THE PROCESS

1. Arrival Time

The time at which the process enters into the ready queue is called the arrival time.
2. Burst Time

The total amount of time required by the CPU to execute the whole process is called the Burst
Time. This does not include the waiting time. It is confusing to calculate the execution time
for a process even before executing it hence the scheduling problems based on the burst time
cannot be implemented in reality.
3. Completion Time

The Time at which the process enters into the completion state or the time at which the
process completes its execution, is called completion time.
4. Turnaround time

The total amount of time spent by the process from its arrival to its completion, is called
Turnaround time.
5. Waiting Time

The Total amount of time for which the process waits for the CPU to be assigned is called
waiting time.
6. Response Time

The difference between the arrival time and the time at which the process first gets the CPU
is called Response Time.

CPU SCHEDULING

In the uniprogrammming systems like MS DOS, when a process waits for any I/O
operation to be done, the CPU remains idol. This is an overhead since it wastes the time and
causes the problem of starvation. However, In Multiprogramming systems, the CPU doesn't
remain idle during the waiting time of the Process and it starts executing other processes.
Operating System has to define which process the CPU will be given.

In Multiprogramming systems, the Operating system schedules the processes on the CPU
to have the maximum utilization of it and this procedure is called CPU scheduling. The
Operating System uses various scheduling algorithm to schedule the processes.

This is a task of the short term scheduler to schedule the CPU for the number of processes
present in the Job Pool. Whenever the running process requests some IO operation then the
short term scheduler saves the current context of the process (also called PCB) and changes
its state from running to waiting. During the time, process is in waiting state; the Short term
scheduler picks another process from the ready queue and assigns the CPU to this process.
This procedure is called context switching.

What is saved in the Process Control Block?

The Operating system maintains a process control block during the lifetime of the process.
The Process control block is deleted when the process is terminated or killed. There is the
following information which is saved in the process control block and is changing with the
state of the process.
Why do we need Scheduling?

In Multiprogramming, if the long term scheduler picks more I/O bound processes then most
of the time, the CPU remains idol. The task of Operating system is to optimize the utilization
of resources.

If most of the running processes change their state from running to waiting then there may
always be a possibility of deadlock in the system. Hence to reduce this overhead, the OS
needs to schedule the jobs to get the optimal utilization of CPU and to avoid the possibility
to deadlock.
SCHEDULING ALGORITHMS

There are various algorithms which are used by the Operating System to schedule the
processes on the processor in an efficient way.

The Purpose of a Scheduling algorithm

1. Maximum CPU utilization

2. Fare allocation of CPU

3. Maximum throughput

4. Minimum turnaround time

5. Minimum waiting time

6. Minimum response time

There are the following algorithms which can be used to schedule the jobs.

1. First Come First Serve

It is the simplest algorithm to implement. The process with the minimal arrival time will get
the CPU first. The lesser the arrival time, the sooner will the process gets the CPU. It is the
non-preemptive type of scheduling.

2. Round Robin

In the Round Robin scheduling algorithm, the OS defines a time quantum (slice). All the
processes will get executed in the cyclic way. Each of the process will get the CPU for a small
amount of time (called time quantum) and then get back to the ready queue to wait for its
next turn. It is a preemptive type of scheduling.
3. Shortest Job First

The job with the shortest burst time will get the CPU first. The lesser the burst time, the
sooner will the process get the CPU. It is the non-preemptive type of scheduling.

4. Shortest remaining time first

It is the preemptive form of SJF. In this algorithm, the OS schedules the Job according to the
remaining time of the execution.

5. Priority based scheduling

In this algorithm, the priority will be assigned to each of the processes. The higher the
priority, the sooner will the process get the CPU. If the priority of the two processes is same
then they will be scheduled according to their arrival time.

6. Highest Response Ratio Next

In this scheduling Algorithm, the process with highest response ratio will be scheduled next.
This reduces the starvation in the system.

FCFS SCHEDULING

First come first serve (FCFS) scheduling algorithm simply schedules the jobs according to
their arrival time. The job which comes first in the ready queue will get the CPU first. The
lesser the arrival time of the job, the sooner will the job get the CPU. FCFS scheduling may
cause the problem of starvation if the burst time of the first process is the longest among all
the jobs.
Advantages of FCFS

o Simple

o Easy

o First come, First serv

Disadvantages of FCFS

1. The scheduling method is non preemptive, the process will run to the completion.

2. Due to the non-preemptive nature of the algorithm, the problem of starvation may
occur.

3. Although it is easy to implement, but it is poor in performance since the average


waiting time is higher as compare to other scheduling algorithms.

Example

Let's take an example of The FCFS scheduling algorithm. In the Following schedule, there are
5 processes with process ID P0, P1, P2, P3 and P4. P0 arrives at time 0, P1 at time 1, P2 at
time 2, P3 arrives at time 3 and Process P4 arrives at time 4 in the ready queue. The processes
and their respective Arrival and Burst time are given in the following table.

The Turnaround time and the waiting time are calculated by using the following formula.
1. Turn Around Time = Completion Time - Arrival Time
2. Waiting Time = Turnaround time - Burst Time

The average waiting Time is determined by summing the respective waiting time of all the
processes and divided the sum by the total number of processes.
Process Arrival Burst Completion Turn Around Waiting
ID Time Time Time Time Time

0 0 2 2 2 0

1 1 6 8 7 1

2 2 4 12 8 4

3 3 9 21 18 9

4 4 12 33 29 17

Avg Waiting Time=31/5

(Gantt chart)

Convoy Effect in FCFS

FCFS may suffer from the convoy effect if the burst time of the first job is the highest among
all. As in the real life, if a convoy is passing through the road then the other persons may get
blocked until it passes completely. This can be simulated in the Operating System also.

If the CPU gets the processes of the higher burst time at the front end of the ready queue then
the processes of lower burst time may get blocked which means they may never get the CPU
if the job in the execution has a very high burst time. This is called convoy effect or
starvation.
Example

In the Example, We have 3 processes named as P1, P2 and P3. The Burt Time of process P1
is highest.

The Turnaround time and the waiting time in the following table, are calculated by the
formula,
1. Turn Around Time = Completion Time - Arrival Time
2. Waiting Time = Turn Around Time - Burst Time

In the First scenario, The Process P1 arrives at the first in the queue although; the burst time
of the process is the highest among all. Since, the Scheduling algorithm, we are following is
FCFS hence the CPU will execute the Process P1 first.

In this schedule, the average waiting time of the system will be very high. That is because of
the convoy effect. The other processes P2, P3 have to wait for their turn for 40 units of time
although their burst time is very low. This schedule suffers from starvation.

Process Arrival Burst Completion Turn Around Waiting


ID Time Time Time Time Time

1 0 40 40 40 0

2 1 3 43 42 39

3 1 1 44 43 42
Avg waiting Time = 81/3

In the Second scenario, If Process P1 would have arrived at the last of the queue and the
other processes P2 and P3 at earlier then the problem of starvation would not be there.

Following example shows the deviation in the waiting times of both the scenarios. Although
the length of the schedule is same that is 44 units but the waiting time will be lesser in this
schedule.

Process Arrival Burst Completion Turn Around Waiting


ID Time Time Time Time Time

1 1 40 44 43 3

2 0 3 3 3 0

3 0 1 4 4 3

Avg Waiting Time=6/3

FCFS with Overhead

In the above Examples, we are assuming that all the processes are the CPU bound processes
only. We were also neglecting the context switching time.
However if the time taken by the scheduler in context switching is considered then the
average waiting time of the system will be increased which also affects the efficiency of the
system.

Context Switching is always an overhead. The Following Example describeshow the


efficiency will be affected if the context switching time is considered in the system.
Example

In the following Example, we are considering five processes P1, P2, P3, P4, P5 and P6. Their
arrival time and Burst time are given below.

Process ID Arrival Time Burst Time

1 0 3

2 1 2

3 2 1

4 3 4

5 4 5

6 5 2

If the context switching time of the system is 1 unit then the Gantt chart of the system will be
prepared as follows.

Given δ=1 unit;


The system will take extra 1 unit of time (overhead) after the execution of every process to
schedule the next process.

1. Inefficiency= (6/23) X 100 %


2.
3. Efficiencyͷ = (1-6/23) X 100 %
4.

SHORTEST JOB FIRST (SJF) SCHEDULING

Till now, we were scheduling the processes according to their arrival time (in FCFS
scheduling). However, SJF scheduling algorithm, schedules the processes according to their
burst time.

In SJF scheduling, the process with the lowest burst time, among the list of available
processes in the ready queue, is going to be scheduled next.

However, it is very difficult to predict the burst time needed for a process hence this
algorithm is very difficult to implement in the system.

Advantages of SJF
1. Maximum throughput

2. Minimum average waiting and turnaround time

Disadvantages of SJF

1. May suffer with the problem of starvation

2. It is not implementable because the exact Burst time for a process can't be known in
advance.

There are different techniques available by which, the CPU burst time of the process can be
determined. We will discuss them later in detail.
Example

In the following example, there are five jobs named as P1, P2, P3, P4 and P5. Their arrival
time and burst time are given in the table below.

PID Arrival Burst Completion Turn Around Waiting


Time Time Time Time Time

1 1 7 8 7 0

2 3 3 13 10 7

3 6 2 10 4 2

4 7 10 31 24 14

5 9 8 21 12 4

Since, No Process arrives at time 0 hence; there will be an empty slot in the Gantt chart from
time 0 to 1 (the time at which the first process arrives).

According to the algorithm, the OS schedules the process which is having the lowest burst
time among the available processes in the ready queue.

Till now, we have only one process in the ready queue hence the scheduler will schedule this
to the processor no matter what is its burst time.

This will be executed till 8 units of time. Till then we have three more processes arrived in
the ready queue hence the scheduler will choose the process with the lowest burst time.

Among the processes given in the table, P3 will be executed next since it is having the lowest
burst time among all the available processes.

So that's how the procedure will go on in shortest job first (SJF) scheduling algorithm.
Avg Waiting Time = 27/5

SHORTEST REMAINING TIME FIRST (SRTF) SCHEDULING ALGORITHM

This Algorithm is the preemptive version of SJF scheduling. In SRTF, the execution of the
process can be stopped after certain amount of time. At the arrival of every process, the short
term scheduler schedules the process with the least remaining burst time among the list of
available processes and the running process.

Once all the processes are available in the ready queue, No preemption will be done and the
algorithm will work as SJF scheduling. The context of the process is saved in the Process
Control Block when the process is removed from the execution and the next process is
scheduled. This PCB is accessed on the next execution of this process.
Example

In this Example, there are five jobs P1, P2, P3, P4, P5 and P6. Their arrival time and burst
time are given below in the table.

Process Arrival Burst Completion Turn Waiting Response


ID Time Time Time Around Time Time
Time

1 0 8 20 20 12 0

2 1 4 10 9 5 1

3 2 2 4 2 0 2

4 3 1 5 2 1 4

5 4 3 13 9 6 10

6 5 2 7 2 0 5

Avg Waiting Time = 24/6

The Gantt chart is prepared according to the arrival and burst time given in the table.

1. Since, at time 0, the only available process is P1 with CPU burst time 8. This is the only
available process in the list therefore it is scheduled.
2. The next process arrives at time unit 1. Since the algorithm we are using is SRTF
which is a preemptive one, the current execution is stopped and the scheduler checks
for the process with the least burst time.
Till now, there are two processes available in the ready queue. The OS has executed
P1 for one unit of time till now; the remaining burst time of P1 is 7 units. The burst
time of Process P2 is 4 units. Hence Process P2 is scheduled on the CPU according to
the algorithm.

3. The next process P3 arrives at time unit 2. At this time, the execution of process P3 is
stopped and the process with the least remaining burst time is searched. Since the
process P3 has 2 unit of burst time hence it will be given priority over others.

4. The Next Process P4 arrives at time unit 3. At this arrival, the scheduler will stop the
execution of P4 and check which process is having least burst time among the
available processes (P1, P2, P3 and P4). P1 and P2 are having the remaining burst
time 7 units and 3 units respectively.
P3 and P4 are having the remaining burst time 1 unit each. Since, both are equal hence
the scheduling will be done according to their arrival time. P3 arrives earlier than P4
and therefore it will be scheduled again.

5. The Next Process P5 arrives at time unit 4. Till this time, the Process P3 has completed
its execution and it is no more in the list. The scheduler will compare the remaining
burst time of all the available processes. Since the burst time of process P4 is 1 which
is least among all hence this will be scheduled.

6. The Next Process P6 arrives at time unit 5, till this time, the Process P4 has completed
its execution. We have 4 available processes till now, that are P1 (7), P2 (3), P5 (3)
and P6 (2). The Burst time of P6 is the least among all hence P6 is scheduled. Since,
now, all the processes are available hence the algorithm will now work same as SJF.
P6 will be executed till its completion and then the process with the least remaining
time will be scheduled.

Once all the processes arrive, No preemption is done and the algorithm will work as SJF.
ROUND ROBIN SCHEDULING ALGORITHM

Round Robin scheduling algorithm is one of the most popular scheduling algorithm which
can actually be implemented in most of the operating systems. This is the preemptive
version of first come first serve scheduling. The Algorithm focuses on Time Sharing. In this
algorithm, every process gets executed in a cyclic way. A certain time slice is defined in the
system which is called time quantum. Each process present in the ready queue is assigned
the CPU for that time quantum, if the execution of the process is completed during that time
then the process will terminate else the process will go back to the ready queue and waits
for the next turn to complete the execution.

Advantages

1. It can be actually implementable in the system because it is not depending on the


burst time.
2. It doesn't suffer from the problem of starvation or convoy effect.

3. All the jobs get a fare allocation of CPU.

Disadvantages

1. The higher the time quantum, the higher the response time in the system.

2. The lower the time quantum, the higher the context switching overhead in the system.

3. Deciding a perfect time quantum is really a very difficult task in the system.

HIGHEST RESPONSE RATIO NEXT (HRRN) SCHEDULING

Highest Response Ratio Next (HRNN) is one of the most optimal scheduling algorithms. This
is a non-preemptive algorithm in which, the scheduling is done on the basis of an extra
parameter called Response Ratio. A Response Ratio is calculated for each of the available
jobs and the Job with the highest response ratio is given priority over the others.

Response Ratio is calculated by the given formula.


1. Response Ratio = (W+S)/S

Where,
1. W → Waiting Time
2. S → Service Time or Burst Time

If we look at the formula, we will notice that the job with the shorter burst time will be given
priority but it is also including an extra factor called waiting time. Since,
1. HRNN α W
2. HRNN α (1/S)

Hence,

1. This algorithm not only favors shorter job but it also concern the waiting time of the
longer jobs.

2. Its mode is non preemptive hence context switching is minimal in this algorithm
PRIORITY SCHEDULING

In Priority scheduling, there is a priority number assigned to each process. In some systems,
the lower the number, the higher the priority. While, in the others, the higher the number,
the higher will be the priority. The Process with the higher priority among the available
processes is given the CPU. There are two types of priority scheduling algorithm exists. One
is Preemptive priority scheduling while the other is Non Preemptive Priority scheduling.

The priority number assigned to each of the process may or may not vary. If the priority
number doesn't change itself throughout the process, it is called static priority, while if it
keeps changing itself at the regular intervals, it is called dynamic priority.

Multiprocessor Operating system

A multiprocessor system consists of several processors which share memory. In the


multiprocessor, there is more than one processor in the system. The reason we use
multiprocessor is that sometimes load on the processor is very high but input output on
other function is not required. This type of operating system is more reliable as even if on
processor goes down the other can still continues to work. This system is relatively cheap
because we are only having the copies of processor but other devices like input-output and
Memory are shared. In the multiprocessor system all the processor operate under the single
operating system. Multiplicity of the processor and how the processors work together are
transparent to the other.

In this, the user does not know in which processor their process work. A process is divided
into several small processes and they work independently on the different processor. A
system can be both multi-programmed by having multiple programs running at the same
time and multiprocessing by having more than one physical and the processor.

In this diagram, there are more than 1 CPU and they shared a common memory
MULTIPROCESSING SCHEDULING

In the multiprocessor scheduling, there are multiple CPU’s which share the load so that
various process run simultaneously. In general, the multiprocessor scheduling is complex as
compared to single processor scheduling. In the multiprocessor scheduling, there are many
processors and they are identical and we can run any process at any time.

The multiple CPU’s in the system are in the close communication which shares a common
bus, memory and other peripheral devices. So we can say that the system is a tightly coupled
system. These systems are used when we want to process a bulk amount of data. These
systems are mainly used in satellite, weather forecasting etc.

Multiprocessing system work on the concept of symmetric multiprocessing model. In this


system, each processor work on the identical copy of the operating system and these copies
communicate with each other. We the help of this system we can save money because of
other devices like peripherals. Power supplies and other devices are shared. The most
important thing is that we can do more work in a short period of time. If one system fails in
the multiprocessor system the whole system will not halt only the speed of the processor
will be slow down. The whole performance of the multiprocessing system is managed by the
operating system . operating system assigns different task to the different processor in the
system. In the multiprocessing system, the process is broken into the thread which they can
be run independently. These type of system allow the threads to run on more than one
processor simultaneously. In these systems the various process in the parallel so this is
called parallel processor. Parallel processing is the ability of the CPU to run various process
simultaneously. In the multiprocessing system, there is dynamically sharing of resources
among the various processors.

Multiprocessor operating system is a kind of regular OS which handles many systems calls
at the same time, do memory management, provide file management also the input-output
devices.
There are some extra features which multiprocessor perform:

• Process synchronization
• Resource management
• Scheduling

There are various organizations of multiprocessor operating system:

1. Each CPU has its own OS

In this types of the organization then there are much Central processing units in the system
and each CPU has its own private operating system and memory is shared among all the
processors and input-output system are also shared. All the system is connected by the single
bus.

2. Master slave multiprocessor

In this type of multiprocessor model, there is a single data structure which keeps track of the
ready processes. In this model, one central processing unit works as master and other central
processing unit work as a slave. In this, all the processors are handled by the single processor
which is called master server. The master server runs the operating system process and the
slave server run the user processes. The memory and input-output devices are shared among
all the processors and all the processor are connected to a common bus. This system is simple
and reduces the data sharing so this system is called Asymmetric multiprocessing.
3. Symmetric multiprocessor

Symmetric Multiprocessors (SMP) is the third model. In this model, there is one copy of the
OS in memory, but any central processing unit can run it. Now, when a system call is made,
then the central processing unit on which the system call was made traps to the kernel and
then processes that system call. This model balances processes and memory dynamical. This
approach uses Symmetric Multiprocessing where each processor is self-scheduling. The
scheduling proceeds further by having the scheduler for each processor examine the ready
queue and select a process to execute. In this system, this is possible that all the process may
be in common ready queue or each processor may have its own private queue for the ready
process.
There are mainly three sources of contention that can be found in a multiprocessor
operating system.

• Locking system
As we know that the resources are shared in the multiprocessor system so there is a
need to protect these resources for safe access among the multiple processors. The
main purpose of locking scheme is to serialize access of the resources by the multiple
processors.
• Shared data
When the multiple processor access the same data at the same time then there may
be a chance of inconsistency of data so to protect this we have to use some protocols
or locking scheme.
• Cache coherence
It is the shared resource data which is stored in the multiple local caches. Suppose
there are two clients have a cached copy of memory and one client change the
memory block and the other client could be left with invalid cache without
notification of the change so this kind of conflict can be resolved by maintaining a
coherence view of the data.
PROCESS SYNCHRONIZATION
On the basis of synchronization, processes are categorized as one of the following two types:
• Independent Process : Execution of one process does not affects the execution of other
processes.
• Cooperative Process : Execution of one process affects the execution of other
processes.
Process synchronization problem arises in the case of Cooperative process also because
resources are shared in Cooperative processes.

Critical Section Problem


Critical section is a code segment that can be accessed by only one process at a time. Critical
section contains shared variables which need to be synchronized to maintain consistency of
data variables.

In the entry section, the process requests for entry in the Critical Section.
Any solution to the critical section problem must satisfy three requirements:
• Mutual Exclusion : If a process is executing in its critical section, then no other process
is allowed to execute in the critical section.
• Progress : If no process is in the critical section, then no other process from outside
can block it from entering the critical section.
• Bounded Waiting : A bound must exist on the number of times that other processes
are allowed to enter their critical sections after a process has made a request to enter
its critical section and before that request is granted.

PETERSON’S SOLUTION
Peterson’s Solution is a classical software based solution to the critical section problem.
In Peterson’s solution, we have two shared variables:
• boolean flag[i] :Initialized to FALSE, initially no one is interested in entering the critical
section
• int turn : The process whose turn is to enter the critical section.
Peterson’s Solution preserves all three conditions :
• Mutual Exclusion is assured as only one process can access the critical section at any
time.
• Progress is also assured, as a process outside the critical section does not blocks other
processes from entering the critical section.
• Bounded Waiting is preserved as every process gets a fair chance.

DISADVANTAGES OF PETERSON’S SOLUTION


• It involves Busy waiting
• It is limited to 2 processes.
TESTANDSET

TestAndSet is a hardware solution to the synchronization problem. In TestAndSet, we have


a shared lock variable which can take either of the two values, 0 or 1.
0 Unlock
1 Lock
Before entering into the critical section, a process inquires about the lock. If it is locked, it
keeps on waiting till it become free and if it is not locked, it takes the lock and executes the
critical section.
In TestAndSet, Mutual exclusion and progress are preserved but bounded waiting cannot be
preserved.

Question : The enter_CS() and leave_CS() functions to implement critical section of a process
are realized using test-and-set instruction as follows:
int TestAndSet(int &lock) {
int initial = lock;
lock = 1;
return initial;
}

void enter_CS(X)
{
while test-and-set(X) ;
}

void leave_CS(X)
{
X = 0;
}
In the above solution, X is a memory location associated with the CS and is initialized to 0.
Now, consider the following statements:

I. The above solution to CS problem is deadlock-free


II. The solution is starvation free.
III. The processes enter CS in FIFO order.
IV. More than one process can enter CS at the same time.

Which of the above statements is TRUE?


(A) I
(B) II and III
(C) II and IV
(D) IV

SEMAPHORES

A Semaphore is an integer variable, which can be accessed only through two operations
wait() and signal().

There are two types of semaphores : Binary Semaphores and Counting Semaphores

• BINARY SEMAPHORES : They can only be either 0 or 1. They are also known as
mutex locks, as the locks can provide mutual exclusion. All the processes can share the
same mutex semaphore that is initialized to 1. Then, a process has to wait until the lock
becomes 0. Then, the process can make the mutex semaphore 1 and start its critical
section. When it completes its critical section, it can reset the value of mutex semaphore
to 0 and some other process can enter its critical section.
• COUNTING SEMAPHORES: They can have any value and are not restricted over a
certain domain. They can be used to control access a resource that has a limitation on
the number of simultaneous accesses. The semaphore can be initialized to the number
of instances of the resource. Whenever a process wants to use that resource, it checks
if the number of remaining instances is more than zero, i.e., the process has an instance
available. Then, the process can enter its critical section thereby decreasing the value
of the counting semaphore by 1. After the process is over with the use of the instance
of the resource, it can leave the critical section thereby adding 1 to the number of
available instances of the resource.

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
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.
//diagram of the view of CS

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:


1. Mutual Exclusion –

It states that no other process is allowed to execute in the critical section if a process is
executing in critical section.

2. Progress –

When no process is in the critical section, then any process from outside that request
for execution can enter in the critical section without any delay. Only those process can
enter that have requested and have finite time to enter the process.

3. Bounded Waiting –

An upper bound must exist on the number of times a process enters so that other
processes are allowed to enter their critical sections after a process has made a request
to enter its critical section and before that request is granted.
Process Synchronization are handled by two approaches:

1. Software Approach –

In Software Approach, Some specific Algorithm approach is used to maintain


synchronization of the data. Like in Approach One or Approach Two, for a number of
two process, a temporary variable like (turn) or boolean variable (flag) value is used to
store the data. When the condition is True then the process in waiting State, known as
Busy Waiting State. This does not satisfy all the Critical Section requirements.
Another Software approach known as Peterson’s Solution is best for Synchronization.
It uses two variables in the Entry Section so as to maintain consistency, like Flag
(boolean variable) and Turn variable(storing the process states). It satisfy all the three
Critical Section requirements.
//Image of Peterson’s Algorithm
2. Hardware Approach –

The Hardware Approach of synchronization can be done through Lock & Unlock
technique.Locking part is done in the Entry Section, so that only one process is allowed
to enter into the Critical Section, after it complete its execution, the process is moved to
the Exit Section, where Unlock Operation is done so that another process in the Lock
Section can repeat this process of Execution.This process is designed in such a way that
all the three conditions of the Critical Sections are satisfied.
//Image of Lock

Using Interrupts –

These are easy to implement.When Interrupt are disabled then no other process is allowed
to perform Context Switch operation that would allow only one process to enter into the
Critical State.
//Image of Interrupts
Test_and_Set Operation –

This allows boolean value (True/False) as a hardware Synchronization, which is atomic in


nature i.e no other interrupt is allowed to access.This is mainly used in Mutual Exclusion
Application. Similar type operation can be achieved through Compare and Swap function. In
this process, a variable is allowed to accessed in Critical Section while its lock operation is
ON.Till then, the other process is in Busy Waiting State. Hence Critical Section Requirements
are achieved.
CRITICAL SECTION

In simple terms a critical section is group of instructions/statements or region of code that


need to be executed atomically (read this post for atomicity), such as accessing a resource
(file, input or output port, global data, etc.).
In concurrent programming, if one thread tries to change the value of shared data at the
same time as another thread tries to read the value (i.e. data race across threads), the result
is unpredictable.
The access to such shared variable (shared memory, shared files, shared port, etc…) to be
synchronized. Few programming languages have built in support for synchronization.
It is critical to understand the importance of race condition while writing kernel mode
programming (a device driver, kernel thread, etc.). since the programmer can directly access
and modifying kernel data structures.
A simple solution to critical section can be thought as shown below,
acquireLock();
Process Critical Section
releaseLock();
A thread must acquire a lock prior to executing critical section. The lock can be acquired by
only one thread. There are various ways to implement locks in the above pseudo code. Let
us discuss them in future articles.
SYNCHRONIZATION HARDWARE

Many systems provide hardware support for critical section code. The critical section
problem could be solved easily in a single-processor environment if we could disallow
interrupts to occur while a shared variable or resource is being modified.
In this manner, we could be sure that the current sequence of instructions would be allowed
to execute in order without pre-emption. Unfortunately, this solution is not feasible in a
multiprocessor environment.
Disabling interrupt on a multiprocessor environment can be time consuming as the message
is passed to all the processors.
This message transmission lag, delays entry of threads into critical section and the system
efficiency decreases.

SEMAPHORE
Semaphore is a simply a variable. This variable is used to solve critical section problem and
to achieve process synchronization in the multi processing environment.
The two most common kinds of semaphores are counting semaphores and binary
semaphores. Counting semaphore can take non-negative integer values and Binary
semaphore can take the value 0 & 1. only.
Now let us see how it do so.
First look at two operations which can be used to access and change the value of semaphore
variable.
Some point regarding P and V operation
1. P operation is also called wait, sleep or down operation and V operation is also called
signal, wake-up or up operation.
2. Both operations are atomic and semaphore(s) is always initialized to one.
3. A critical section is surrounded by both operations to implement process
synchronization.See below image.critical section of Process P is in between P and V
operation.

Now, let us see how it implements mutual exclusion. Let there be two processes P1 and P2
and a semaphore s is initialized as 1. Now if suppose P1 enters in its critical section then the
value of semaphore s becomes 0. Now if P2 wants to enter its critical section then it will wait
until s > 0, this can only happen when P1 finishes its critical section and calls V operation on
semaphore s. This way mutual exclusion is achieved. Look at the below image for details.
The description above is for binary semaphore which can take only two values 0 and 1. There
is one other type of semaphore called counting semaphore which can take values greater
than one.
Now suppose there is a resource whose number of instance is 4. Now we initialize S = 4 and
rest is same as for binary semaphore. Whenever process wants that resource it calls P or
wait function and when it is done it calls V or signal function. If value of S becomes zero than
a process has to wait until S becomes positive. For example, Suppose there are 4 process P1,
P2, P3, P4 and they all call wait operation on S(initialized with 4). If another process P5 wants
the resource then it should wait until one of the four process calls signal function and value
of semaphore becomes positive.
Problem in this implementation of semaphore
Whenever any process waits then it continuously checks for semaphore value (look at this
line while (s==0); in P operation) and waste CPU cycle. To avoid this another implementation
is provided below.

In this implementation whenever process waits it is added to a waiting queue of processes


associated with that semaphore. This is done through system call block() on that process.
When a process is completed it calls signal function and one process in the queue is resumed.
It uses wakeup() system call.

TYPES OF SEMAPHORES
There are two main types of semaphores i.e. counting semaphores and binary semaphores.
Details about these are given as follows:
1. Counting Semaphores
These are integer value semaphores and have an unrestricted value domain. These
semaphores are used to coordinate the resource access, where the semaphore count
is the number of available resources. If the resources are added, semaphore count
automatically incremented and if the resources are removed, the count is
decremented.

2. Binary Semaphores
The binary semaphores are like counting semaphores but their value is restricted to
0 and 1. The wait operation only works when the semaphore is 1 and the signal
operation succeeds when semaphore is 0. It is sometimes easier to implement binary
semaphores than counting semaphores.

Advantages of Semaphores

Some of the advantages of semaphores are as follows:


1. 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.
2. 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.
3. 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:


1. Semaphores are complicated so the wait and signal operations must be implemented
in the correct order to prevent deadlocks.
2. 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.
3. Semaphores may lead to a priority inversion where low priority processes may access
the critical section first and high priority processes later

CLASSICAL PROBLEMS OF SYNCHRONIZATION


Semaphore can be used in other synchronization problems besides Mutual Exclusion.
Below are some of the classical problem depicting flaws of process synchronaization in
systems where cooperating processes are present.
We will discuss the following three problems:

1. Bounded Buffer (Producer-Consumer) Problem


2. The Readers Writers Problem
3. Dining Philosophers Problem

BOUNDED BUFFER PROBLEM

• This problem is generalised in terms of the Producer Consumer problem, where a


finite buffer pool is used to exchange messages between producer and consumer
processes.
Because the buffer pool has a maximum size, this problem is often called the Bounded
buffer problem.
• Solution to this problem is, creating two counting semaphores "full" and "empty" to keep
track of the current number of full and empty buffers respectively.
• Here we have pool of n buffers.
• Mutex - Semaphore for mutual exclusion to access buffer pool, initialized to 1.
• Empty - Semaphore to count empty buffer N.
• Full - Semaphore to count fill buffer 0.

Producer Consumer

Wait (empty) Wait (empty)


Wait (mutex) Wait (mutex)
Critical section Critical section
Signal (mutex) Signal (mutex)
Signal (full) Signal (empty)

• We have used 3 semaphore e- empty cells, as well as underflow f- fixed cells as well
as overflow mutex, is used for the critical section.
• Example:

P C

while(1) while(1)
{ {
produce(); wait(p)
wait(e) wait(mutex)
wait(mutex) pick();
append() signal(mutex)
signal(mutex) signal(e)
signal(f) consume();
} }
THE READERS WRITERS PROBLEM

• There is a shared piece of text and 2 types of process in accessing this text reader and
writer.
• There is no clash between reader and reader therefore when a reader is inside critical
section then other readers may get an only entry but when a write is inside critical
section then neither the reader nor the writer gets an entry.
• Hence in the solution, we have used 3 resources a semaphore mutex for
synchronization between writer and reader-writer.
• While read count (RC) is a simple integer variable which is given security by reading
semaphore which works for synchronization between reader- reader.
• Writer
• while(1)
• {
• wait(mutex)
• write
• signal(mutex)
• }
• Reader
• while(1)
• {
• wait(Read)
• Rc = Rc + 1;

• if(Rc = = 1)
• {
• wait (mutex)
• }

• wait(Read)
• Rc = Rc-1

• if(Rc ==0)
• {
• signal(mutex)
• }

• signal(Read)
• }

DINING PHILOSOPHERS PROBLEM

• The dining philosopher's problem involves the allocation of limited resources to a group
of processes in a deadlock-free and starvation-free manner.
• There are five philosophers sitting around a table, in which there are five
chopsticks/forks kept beside them and a bowl of rice in the centre, 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.

In this problem, there is a circular table and number of philosopher a sitting on the table.
There is a chop-stick placed between every philosopher. Philosopher prior only 2 processes
either they think or eat (using 2 chop-stick).
Pi()
while(1)
{
think
P(s)
Wait (chop-stick[i])
Wait (chop-stick(i+1 % 5)
V(s)
Eat
Signal( chop-stick[i]
Signal (chop-stick(i+1 % 5)
Think
}

Solution using semaphore for philosopher synchronization

Solution suffers from dead-lock and the following modification can be done:

1. Allow n-1 philosopher to sit on the table.


2. Allow n+1 chop-stick to be put on the table.
3. n-1 philosopher picks the left chop-stick first(the right and then the last philosopher
pick the right first and left or vice-versa.
4. Division can be done between add an even number philosopher.
5. Take one more semaphore and consider 2 wait operation as a critical section.

Improved implementation

Here, there will be 2 improvements:

• The implementation will become effective as there is no wastage of CPU clock cycles.
• The bounded wait will also be ensured as it uses strictly follow First come first serve.

P(s) V(s)
{ {
s = s-1 s = s+1
if (s<0) if (s<1)
{ {
block(); dequeue();
Add it to the queue(); resume();
} }
} }

DEADLOCK
Deadlocks are a set of blocked processes each holding a resource and waiting to acquire a
resource held by another process.

HOW TO AVOID DEADLOCKS


Deadlocks can be avoided by avoiding at least one of the four conditions, because all this four
conditions are required simultaneously to cause deadlock.

1. Mutual Exclusion
Resources shared such as read-only files do not lead to deadlocks but resources, such as
printers and tape drives, requires exclusive access by a single process.
2. Hold and Wait
In this condition processes must be prevented from holding one or more resources while
simultaneously waiting for one or more others.

3. No Preemption
Preemption of process resource allocations can avoid the condition of deadlocks, where
ever possible.

4. Circular Wait
Circular wait can be avoided if we number all resources, and require that processes
request resources only in strictly increasing(or decreasing) order.

HANDLING DEADLOCK
The above points focus on preventing deadlocks. But what to do once a deadlock has occured.
Following three strategies can be used to remove deadlock after its occurrence.

1. Preemption
We can take a resource from one process and give it to other. This will resolve the
deadlock situation, but sometimes it does causes problems.

2. Rollback
In situations where deadlock is a real possibility, the system can periodically make a
record of the state of each process and when deadlock occurs, roll everything back to the
last checkpoint, and restart, but allocating resources differently so that deadlock does
not occur.

3. Kill one or more processes


This is the simplest way, but it works.
What is a Livelock?
There is a variant of deadlock called livelock. This is a situation in which two or more
processes continuously change their state in response to changes in the other process(es)
without doing any useful work. This is similar to deadlock in that no progress is made but
differs in that neither process is blocked or waiting for anything.
A human example of livelock would be two people who meet face-to-face in a corridor and
each moves aside to let the other pass, but they end up swaying from side to side without
making any progress because they always move the same way at the same time.

INTERPROCESS COMMUNICATION (IPC)

Interprocess communication (IPC) is a set of programming interfaces that allow a


programmer to coordinate activities among different program processes that can run
concurrently in an operating system. This allows a program to handle many user requests at
the same time. Since even a single user request may result in multiple processes running in
the operating system on the user's behalf, the processes need to communicate with each
other. The IPC interfaces make this possible. Each IPC method has its own advantages and
limitations so it is not unusual for a single program to use all of the IPC methods.
A process can be of two type:
• Independent process.
• Co-operating process.
An independent process is not affected by the execution of other processes while a co-
operating process can be affected by other executing processes. Though one can think that
those processes, which are running independently, will execute very efficiently but in
practical, there are many situations when co-operative nature can be utilised for increasing
computational speed, convenience and modularity. Inter process communication (IPC) is a
mechanism which allows processes to communicate each other and synchronize their
actions. The communication between these processes can be seen as a method of co-
operation between them. Processes can communicate with each other using these two ways:
1. Shared Memory
2. Message passing
An operating system can implement both method of communication.
Communication between processes using shared memory requires processes to share
some variable and it completely depends on how programmer will implement it. One way of
communication using shared memory can be imagined like this:

Suppose process1 and process2 are executing simultaneously and they share some
resources or use some information from other process, process1 generate information about
certain computations or resources being used and keeps it as a record in shared memory.
When process2 need to use the shared information, it will check in the record stored in
shared memory and take note of the information generated by process1 and act accordingly.
Processes can use shared memory for extracting information as a record from other process
as well as for delivering any specific information to other process.
For the communication between processes via message passing, processes communicate
with each other without using any kind of of shared memory. If two processes p1 and p2
want to communicate with each other, they proceed as follow:

Establish a communication link (if a link already exists, no need to establish it again.)
Start exchanging messages using basic primitives.
We need at least two primitives:
• send(message, destinaion) or send(message)
• receive(message, host) or receive(message)
The message size can be of fixed size or of variable size. if it is of fixed size, it is easy for OS
designer but complicated for programmer and if it is of variable size then it is easy for
programmer but complicated for the OS designer. A standard message can have two parts:
header and body.

The header part is used for storing Message type, destination id, source id, message length
and control information. The control information contains information like what to do if runs
out of buffer space, sequence number, priority. Generally, message is sent using FIFO style.
DEADLOCKS & PREVENTION

There are four conditions that are necessary to achieve deadlock:

1. Mutual Exclusion - At least one resource must be held in a non-sharable mode; If any
other process requests this resource, then that process must wait for the resource to
be released.
2. Hold and Wait - A process must be simultaneously holding at least one resource and
waiting for at least one resource that is currently being held by some other process.
3. No preemption - Once a process is holding a resource ( i.e. once its request has been
granted ), then that resource cannot be taken away from that process until the process
voluntarily releases it.
4. Circular Wait - A set of processes { P0, P1, P2, . . ., PN } must exist such that every P[
i ] is waiting for P[ ( i + 1 ) % ( N + 1 ) ]. ( Note that this condition implies the hold-
and-wait condition, but it is easier to deal with the conditions if the four are
considered separately. )

For a deadlock to occur, each of the four necessary conditions must hold. 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 must hold for non-sharable types of resources. For example,
a printer cannot be simultaneously shared by several processes. Read only files are a good
example of a sharable resource. If several processes attempt to open a read only file at the
same time, they can be granted simultaneous access to the file. In general, however, it is not
possible to prevent deadlocks by denying the mutual exclusion condition. Some resources
are intrinsically non-sharable.
Hold and Wait

In order to ensure that hold and wait condition never holds in the system, we must guarantee
that whenever a process requests a resource it does not hold any other resources. Two
protocols can be used for this:

First protocol: Each process requests and be allocated all of its resources before it begins
execution. This provision can be implemented by requiring that system calls requesting
resources for a particular process precede any other system calls.

Second protocol: A process requests resources only when it has none. A process may
request some resources and use them. Before it can request any additional resources,
however, it must release all the resources that it is currently allocated.

Disadvantages for the above two protocols include:

Low resource utilization Starvation is possible

No Preemption

In order to ensure that this condition does not hold, the following protocol may be used. If
the process that is holding some resources requests another resource that cannot be
immediately allocated to it (i.e. the process must wait), then all resources currently being
held are preempted i.e. these resources are implicitly released. The preempted resources are
added to the list of resources for which the process is waiting. The process will only be
restarted when it can regain its old resources, as well as the new ones that it is requesting.

Circular Wait

In order to ensure that the circular wait condition never holds, we may impose a total
ordering of all resource types i.e. we assign to each resource type a unique integer number
which allows us to compare two resources and determine whether one precedes another in
our ordering.
DEADLOCK AVOIDANCE

• The general idea behind deadlock avoidance is to prevent deadlocks from ever
happening, by preventing at least one of the aforementioned conditions.
• This requires more information about each process, AND tends to lead to low device
utilization. ( I.e. it is a conservative approach. )
• In some algorithms the scheduler only needs to know the maximum number of each
resource that a process might potentially use. In more complex algorithms the
scheduler can also take advantage of the schedule of exactly what resources may be
needed in what order.
• When a scheduler sees that starting a process or granting resource requests may lead
to future deadlocks, then that process is just not started or the request is not granted.
• A resource allocation state is defined by the number of available and allocated
resources, and the maximum requirements of all processes in the system.

BANKER'S ALGORITHM

• For resource categories that contain more than one instance the resource-allocation
graph method does not work, and more complex ( and less efficient ) methods must
be chosen.
• The Banker's Algorithm gets its name because it is a method that bankers could use
to assure that when they lend out resources they will still be able to satisfy all their
clients. ( A banker won't loan out a little money to start building a house unless they
are assured that they will later be able to loan out the rest of the money to finish the
house. )
• When a process starts up, it must state in advance the maximum allocation of
resources it may request, up to the amount available on the system.
• When a request is made, the scheduler determines whether granting the request
would leave the system in a safe state. If not, then the process must wait until the
request can be granted safely.
• The banker's algorithm relies on several key data structures: ( where n is the number
of processes and m is the number of resource categories. )
o Available[ m ] indicates how many resources are currently available of each
type.
o Max[ n ][ m ] indicates the maximum demand of each process of each resource.
o Allocation[ n ][ m ] indicates the number of each resource category allocated
to each process.
o Need[ n ][ m ] indicates the remaining resources needed of each type for each
process. ( Note that Need[ i ][ j ] = Max[ i ][ j ] - Allocation[ i ][ j ] for all i, j. )
• For simplification of discussions, we make the following notations / observations:
o One row of the Need vector, Need[ i ], can be treated as a vector corresponding
to the needs of process i, and similarly for Allocation and Max.
o A vector X is considered to be <= a vector Y if X[ i ] <= Y[ i ] for all i.

SAFETY ALGORITHM

• In order to apply the Banker's algorithm, we first need an algorithm for determining
whether or not a particular state is safe.
• This algorithm determines if the current state of a system is safe, according to the
following steps:
1. Let Work and Finish be vectors of length m and n respectively.
 Work is a working copy of the available resources, which will be
modified during the analysis.
 Finish is a vector of booleans indicating whether a particular process
can finish. ( or has finished so far in the analysis. )
 Initialize Work to Available, and Finish to false for all elements.
2. Find an i such that both (A) Finish[ i ] == false, and (B) Need[ i ] < Work. This
process has not finished, but could with the given available working set. If no
such i exists, go to step 4.
3. Set Work = Work + Allocation[ i ], and set Finish[ i ] to true. This corresponds
to process i finishing up and releasing its resources back into the work pool.
Then loop back to step 2.
4. If finish[ i ] == true for all i, then the state is a safe state, because a safe
sequence has been found.
• ( JTB's Modification:
1. In step 1. instead of making Finish an array of booleans initialized to false,
make it an array of ints initialized to 0. Also initialize an int s = 0 as a step
counter.
2. In step 2, look for Finish[ i ] == 0.
3. In step 3, set Finish[ i ] to ++s. S is counting the number of finished processes.
4. For step 4, the test can be either Finish[ i ] > 0 for all i, or s >= n. The benefit of
this method is that if a safe state exists, then Finish[ ] indicates one safe
sequence ( of possibly many. ) )

RESOURCE-REQUEST ALGORITHM ( THE BANKERS ALGORITHM )

• Now that we have a tool for determining if a particular state is safe or not, we are now
ready to look at the Banker's algorithm itself.
• This algorithm determines if a new request is safe, and grants it only if it is safe to do
so.
• When a request is made ( that does not exceed currently available resources ),
pretend it has been granted, and then see if the resulting state is a safe one. If so, grant
the request, and if not, deny the request, as follows:
1. Let Request[ n ][ m ] indicate the number of resources of each type currently
requested by processes. If Request[ i ] > Need[ i ] for any process i, raise an
error condition.
2. If Request[ i ] > Available for any process i, then that process must wait for
resources to become available. Otherwise the process can continue to step 3.
3. Check to see if the request can be granted safely, by pretending it has been
granted and then seeing if the resulting state is safe. If so, grant the request,
and if not, then the process must wait until its request can be granted
safely.The procedure for granting a request ( or pretending to for testing
purposes ) is:
 Available = Available - Request
 Allocation = Allocation + Request
 Need = Need - Request
UNIT III:

Since main memory is usually too small to accommodate all the data and programs
permanently, the computer system must provide secondary storage to back up main
memory. Modern computer systems use disks as the primary on-line storage medium for
information (both programs and data). The file system provides the mechanism for on-line
storage of and access to both data and programs residing on the disks. A file is a collection of
related information defined by its creator. The files are mapped by the operating system onto
physical devices. Files are normally f organized into directories for ease of use.

The devices that attach to a computer vary in many aspects. Some devices transfer a
character or a block of characters at a time. Some can be accessed only sequentially, others
randomly. Some transfer data synchronously, others asynchronously. Some are dedicated,
some shared. They can be read-only or read-write. They vary greatly in speed. In many ways,
they are also the slowest major component of the computer.

Because of all this device variation, the operating system needs to provide a wide range of
functionality to applications, to allow them to control all aspects of the devices. One key goal
of an operating system's I/O subsystem is to provide the simplest interface possible to the
rest of the system. Because devices are a performance bottleneck, another key is to optimize
I/O for maximum concurrency.

SWAPPING
Swapping is a mechanism in which a process can be swapped/moved temporarily out of main
memory to a backing store , and then brought back into memory for continued execution.
• For example, assume a multiprogramming environment with a round-robin CPU-
scheduling algorithm. When a quantum expires, the memory manager will start to swap out
(move) the process that just finished, and to swap in(bring back) another process to the
memory space that has been freed.

• A process must be loaded into memory in order to execute.


• If there is not enough memory available to keep all running processes in memory at the
same time, then some processes who are not currently using the CPU may have their memory
swapped out to a fast local disk called the backing store.
STANDARD SWAPPING
• If compile-time or load-time address binding is used, then processes must be swapped back
into the same memory location from which they were swapped out.

• If execution time binding is used, then the processes can be swapped back into any available
location.

• Swapping is a very slow process compared to other operations.


• To reduce swapping transfer overhead, it is desired to transfer as little information as
possible, which requires that the system know how much memory a process is using, as
opposed to how much it might use.

• Programmers can help with this by freeing up dynamic memory that they are no longer
using.
• It is important to swap processes out of memory only when they are idle, or more to the
point, only when there are no pending I/O operations.

• Swap only totally idle processes, or do I/O operations only into and out of OS buffers, which
are then transferred to or from process's main memory as a second step.

• Most modern OSes no longer use swapping, because it is too slow and there are faster
alternatives available. ( e.g. Paging. )

• Example of OS which use swapping. Some UNIX systems will still invoke swapping if the
system gets extremely full, and then discontinue swapping when the load reduces again.
Windows 3.1 would use a modified version of swapping that was somewhat controlled by
the user, swapping process's out if necessary and then only swapping them back in when the
user focused on that particular window.

SWAPPING ON MOBILE SYSTEMS


• Swapping is typically not supported on mobile platforms, for several reasons.

o Mobile devices typically use flash memory in place of more spacious hard drives for
persistent storage, so there is not as much space available.

o Flash memory can only be written to a limited number of times before it becomes
unreliable.

o The bandwidth to flash memory is also lower.


• Apple's IOS asks applications to voluntarily free up memory

o Read-only data, e.g. code, is simply removed, and reloaded later if needed.
o Modified data, e.g. the stack, is never removed, but Apps that fail to free up sufficient
memory can be removed by the OS

• Android follows a similar strategy.

o Prior to terminating a process, Android writes its application state to flash memory for
quick restarting.

SINGLE AND MULTIPLE PARTITION ALLOCATION


When the CPU scheduler selects a process for execution,the dispatcher loads the relocation
and limit registers with the correct values as part of the context switch.Because every
address generated by the CPU is checked against these registers,we can protect both the
operating system and the other users' programs and data from being modified by this
running process.

The relocation-register scheme provides an effective way to allow the operating-system size
to change dynamically.This flexibility is desirable in many situations.For example, the
operating system contains code and buffer space for device drivers.If a device driver[or other
operating-system service] is not commonly used,we do not want to keep the code and data
in memory,as we might be able to use that space for other purposes.Such code is sometimes
called transient operating-system code;it comes and goes as needed.Thus,using this code
changes the size of the operating system during program execution.
To protect the operating system code and data by the user processes as well as protect user
processes from one another using relocation register and limit register.

This is depicted in the figure below:

MEMORY ALLOCATION

Now we will discuss about the various memory allocation schemes.

Single Partition Allocation

In this scheme Operating system is residing in low memory and user processes are executing
in higher memory.
Advantages

• It is simple.
• It is easy to understand and use.

Disadvantages

• It leads to poor utilization of processor and memory.


• Users job is limited to the size of available memory.

Multiple-partition Allocation

One of the simplest methods for allocating memory is to divide memory into several fixed
sized partitions. There are two variations of this.
• Fixed Equal-size Partitions

It divides the main memory into equal number of fixed sized partitions, operating
system occupies some fixed portion and remaining portion of main memory is
available for user processes.

Advantages
o Any process whose size is less than or equal to the partition size can be loaded
into any available partition.
o It supports multiprogramming.

Disadvantages
o If a program is too big to fit into a partition use overlay technique.
o Memory use is inefficient, i.e., block of data loaded into memory may be
smaller than the partition. It is known as internal fragmentation.

• Fixed Variable Size Partitions

By using fixed variable size partitions we can overcome the disadvantages present in
fixed equal size partitioning. This is
shown in the figure below:
With unequal-size partitions, there are two ways to assign processes to partitions.
o Use multiple queues:- For each and every process one queue is present, as
shown in the figure below. Assign each process to the smallest partition within
which it will fit, by using the scheduling queues, i.e., when a new process is to
arrive it will put in the queue it is able to fit without wasting the memory space,
irrespective of other blocks queues.
Advantages
 Minimize wastage of memory.

Disadvantages
 This scheme is optimum from the system point of view. Because larger
partitions remains unused.
o Use single queue:- In this method only one ready queue is present for
scheduling the jobs for all the blocks irrespective of size. If any block is free
even though it is larger than the process, it will simply join instead of waiting
for the suitable block size.It is depicted in the figure below:

Advantages
 It is simple and minimum processing overhead.

Disadvantages
 The number of partitions specified at the time of system generation
limits the number of active processes.
 Small jobs do not use partition space efficiently.
DYNAMIC PARTITIONING

Even though when we overcome some of the difficulties in variable sized fixed
partitioning, dynamic partitioning require more sophisticated memory management
techniques. The partitions used are of variable length. That is when a process is
brought into main memory, it allocates exactly as much memory as it requires. Each
partition may contain exactly one process. Thus the degree of multiprogramming is
bound by the number of partitions. In this method when a partition is free a process
is selected from the input queue and is loaded into the free partition. When the
process terminates the partition becomes available for another process. This method
was used by IBM's mainframe operating system, OS/MVT (Multiprogramming with
variable number of tasks) and it is no longer in use now.

Let us consider the following scenario:


Code:
Process|Size (in kB)|Arrival time (in milli sec)|Service time (in milli sec)
P1 |350 |0 |40
P2 |400 |10 |45
P3 |300 |30 |35
P4 |200 |35 |25
Figure below is showing the allocation of blocks in different stages by using dynamic
partitioning method. That is the available main memory size is 1 MB. Initially the main
memory is empty except the operating system shown in Figure a. Then process 1 is
loaded as shown in Figure b, then process 2 is loaded as shown in Figure c without
the wastage of space and the remaining space in main memory is 146K it is free.
Process 1 is swaps out shown in Figure d for allocating the other higher priority
process. After allocating process 3, 50K whole is created it is called internal
fragmentation, shown in Figure e. Now process 2 swaps out shown in Figure f. Process
1 swaps in, into this block. But process 1 size is only
350K, this leads to create a whole of 50K shown in Figure
eg.

Like this, it creates a lot of small holes in memory. Ultimately memory becomes more
and more fragmented and it leads to decline memory usage. This is called ‘external
fragmentation’. To overcome external fragmentation by using a technique called
"compaction". As the part of the compaction process, from time to time, operating
system shifts the processes so that they are contiguous and this free memory is
together creates a block. In Figure h compaction results in a block of free memory of
length 246K.

Advantages
o Partitions are changed dynamically.
o It does not suffer from internal fragmentation.
Disadvantages
• It is a time consuming process (i.e., compaction).
• Wasteful of processor time, because from time to time to move a program from one
region to another in main memory without invalidating the memory references.

PLACEMENT ALGORITHM

If the free memory is present within a partition then it is called "internal fragmentation".
Similarly if the free blocks are present outside the partition, then it is called "external
fragmentation". Solution to the "external fragmentation" is compaction.

Solution to the "internal fragmentation" is the "placement" algorithm only.

Because memory compaction is time-consuming, when it is time to load or swap a process


into main memory and if there is more than one free block of memory of sufficient size, then
the operating system must decide which free block to allocate by using three different
placement algorithms.

This is Shown in the figure below:


• Best-fit:- It chooses the block, that is closest in size to the given request from the
beginning to the ending free blocks. We must search the entire list, unless it is ordered
by size. This strategy produces the smallest leftover hole.
• First-fit:- It begins to scan memory from the beginning and chooses the first available
block which is large enough. Searching can start either at the beginning of the set of
blocks or where the previous first-fit search ended. We can stop searching as soon as
we find a free block that is large enough.
• Worst-fit:- It allocates the largest block. We must search the entire the entire list,
unless it is sorted by size. This strategy produces the largest leftover hole, which may
be more useful than the smaller leftover hole from a best-fit approach.
• last-fit:- It begins to scan memory from the location of the last placement and chooses
the next available block. In the figure below the last allocated block is 18k, thus it
starts from this position and the next block itself can accommodate this 20K block in
place of 36K free block. It leads to the wastage of 16KB space.
First-fit algorithm is the simplest, best and fastest algorithm. Next-fit produce slightly worse
results than the first-fit and compaction may be required more frequently with next-fit
algorithm. Best-fit is the worst performer, even though it is to minimize the wastage space.
Because it consumes the lot of processor time for searching the block which is close to its
size.

FRAGMENTATION

Both the first-fit and best-fit strategies for memory allocation suffer from external
fragmentation. As the processes are loaded and removed from memory, the free
memory space is broken into little pieces. External fragmentation exists when there
is enough total memory space to satisfy a request, but the available spaces are not
contiguous. Storage is fragmented into a large number of small holes. This
fragmentation problem can be severe. In the worst case we could have a block of free
or wasted memory between every two processes. If all these small pieces of memory
were in one big free block instead, we might be able to run several more processes.

Whether we are using the first-fit or best-fit strategy can affect the amount of
fragmentation. First-fit is better for some systems and best-fit is better for others.
Another factor is which end of a free block is allocated. No matter which algorithm is
used external fragmentation will be a problem.

Depending on the total amount of memory storage and the average process size,
external fragmentation may be a minor or a major problem. Statistical analysis of first
fit for instance reveals that even with some optimization given N allocated blocks,
another 0.5N blocks will be lost to fragmentation. that is one-third of memory may be
unusable. This property is known as the 50-percent rule.
Memory fragmentation can be internal as well as external. Consider a multiple-
partition allo9cation scheme with a hole of 18,464 bytes. If we allocate exactly the
requested block we are left with a hole of 2 bytes. The overhead to keep track of this
hole will be substantially larger than the hole itself. The general approach to avoid
this problem is to break the physical memory into fixed-sized blocks and allocate
memory in units based on block size. With this approach , the memory allocated to a
process may be slightly larger than the requested memory. The difference between
these two numbers is internal fragmentation- memory that is internal to a partition
but is not being used.

One solution to the problem of external fragmentation is compaction.The goal is to


shuffle the contents so as to place all free memory together in one large block.
Compaction is not always possible , however if relocation is static and is done at
assembly or load time,compaction cannot be done. Compaction is possible only if
relocation is dynamic and is done at execution time. If addresses are relocated
dynamically, relocation requires only moving the program and data and then
changing the base register to reflect the new base address. When compaction is
possible we must determine its cost. The simplest compaction algorithm is to move
all processes toward one end of the memory. All holes move in the other direction
producing one large block of available memory. This scheme can be expensive.

Another possible solution to the external fragmentation is to permit the logical


address space of the processes to be non-contiguous, thus allowing a process to be
allocated physical memory wherever the latter is available.Two complementary
techniques to achieve this solution are paging and segmentation.
PAGING & SEGMENTATION
In paging, memory is divided in to equal size segments called pages whereas memory
segments could vary in size (this is why each segment is associated with a length attribute).
Sizes of the segments are determined according to the address space required by a process,
while address space of a process is divided in to pages of equal size in paging.
Segmentation provides security associated with the segments, whereas paging does not
provide such a mechanism.

Difference Between Paging and Segmentation in OS


The memory management in the operating system is an essential functionality, which
allows the allocation of memory to the processes for execution and deallocates the
memory when the process is no longer needed. In this article, we will discuss two
memory management schemes paging and segmentation. The basic difference
between paging and segmentation is that, “page” is a fixed-sized block whereas, a
“segment” is a variable-sized block
We will discuss some more differences between Paging and Segmentation with the help of
comparison chart shown below.
Content: Paging Vs Segmentation

1. Comparison Chart
2. Definition
3. Key Differences
4. Conclusion
Comparison Chart

BASIS FOR
PAGING SEGMENTATION
COMPARISON

Basic A page is of fixed block size. A segment is of variable size.

Fragmentation Paging may lead to internal Segmentation may lead to external


fragmentation. fragmentation.

Address The user specified address is The user specifies each address by
divided by CPU into a page two quantities a segment number and
number and offset. the offset (Segment limit).

Size The hardware decides the The segment size is specified by the
page size. user.
BASIS FOR
PAGING SEGMENTATION
COMPARISON

Table Paging involves a page table Segmentation involves the segment


that contains base address of table that contains segment number
each page. and offset (segment length).

DEFINITION OF PAGING

Paging is a memory management scheme. Paging allows a process to be stored in a


memory in a non-contiguous manner. Storing process in a non-contiguous manner solves
the problem of external fragmentation.
For implementing paging the physical and logical memory spaces are divided into the
same fixed-sized blocks. These fixed-sized blocks of physical memory are called frames, and
the fixed-sized blocks of logical memory are called pages.
When a process needs to be executed the process pages from logical memory space are
loaded into the frames of physical memory address space. Now the address generated by
CPU for accessing the frame is divided into two parts i.e. page number and page offset.
The page table uses page number as an index; each process has its separate page table that
maps logical address to the physical address. The page table contains base address of the
page stored in the frame of physical memory space. The base address defined by page table
is combined with the page offset to define the frame number in physical memory where the
page is stored.

DEFINITION OF SEGMENTATION

Like Paging, Segmentation is also a memory management scheme. It supports the user’s
view of the memory. The process is divided into the variable size segments and loaded to
the logical memory address space.
The logical address space is the collection of variable size segments. Each segment has its
name and length. For the execution, the segments from logical memory space are loaded to
the physical memory space.

The address specified by the


user contain two quantities the segment name and the Offset. The segments are numbered
and referred by the segment number instead of segment name. This segment number is
used as an index in the segment table, and offset value decides the length or limit of the
segment. The segment number and the offset together combinely generates the address of
the segment in the physical memory space.
Key Differences Between Paging and Segmentation

1. The basic difference between paging and segmentation is that a page is always of fixed
block size whereas, a segment is of variable size.
2. Paging may lead to internal fragmentation as the page is of fixed block size, but it
may happen that the process does not acquire the entire block size which will generate
the internal fragment in memory. The segmentation may lead to external
fragmentation as the memory is filled with the variable sized blocks.
3. In paging the user only provides a single integer as the address which is divided by
the hardware into a page number and Offset. On the other hands, in segmentation
the user specifies the address in two quantities i.e. segment number and offset.
4. The size of the page is decided or specified by the hardware. On the other hands, the
size of the segment is specified by the user.
5. In paging, the page table maps the logical address to the physical address, and it
contains base address of each page stored in the frames of physical memory space.
However, in segmentation, the segment table maps the logical address to the
physical address, and it contains segment number and offset (segment limit).

PAGED SEGMENTATION
In pure paging, each logical address space is considered a single segment that consists of
many fixed size pages. Paging does not alter the view of linearity of address spaces; it does
not achieve the objectives that motivated segmentation. On the other hand, segmentation
requires variable-sized segment blocks of contiguous physical addresses. Loading a segment
requires finding a free memory block large enough to contain the entire segment, whereas
loading a page requires finding a free frame, and any free frame will do. The former problem
is much more difficult than the latter. Application programmers preferred segmentation,
while system implementers preferred paging. For example, application processes have
arbitrary sized memory regions, different regions in different segments. External
fragmentation is a serious problem in segmentation. We would like to have schemes that
combine the benefits of both segmentation and paging, but without some of their problems.
In this section, we present a mixed address translation scheme, namely paged segmentation.

≫ The Intel 80386 processor supports paged segmentation.

As in the pure segmentation scheme, an address space in the paged segmentation scheme
consists of variable-sized segments. However, unlike in the pure segmentation scheme, a
segment may not be allocated to a single memory block. Instead, the segment is paged, and
the pages are distributed to memory frames. This avoids both, external fragmentation and
time overhead in searching for memory to allocate a segment

VIRTUAL MEMORY

Virtual Memory is a storage scheme that provides user an illusion of having a very big main
memory. This is done by treating a part of secondary memory as the main memory.

In this scheme, User can load the bigger size processes than the available main memory by
having the illusion that the memory is available to load the process.

Instead of loading one big process in the main memory, the Operating System loads the
different parts of more than one process in the main memory.

By doing this, the degree of multiprogramming will be increased and therefore, the CPU
utilization will also be increased.
How Virtual Memory Works?

In modern word, virtual memory has become quite common these days. In this scheme,
whenever some pages needs to be loaded in the main memory for the execution and the
memory is not available for those many pages, then in that case, instead of stopping the
pages from entering in the main memory, the OS search for the RAM area that are least
used in the recent times or that are not referenced and copy that into the secondary
memory to make the space for the new pages in the main memory.

Since all this procedure happens automatically, therefore it makes the computer feel like
it is having the unlimited RAM.

DEMAND PAGING

Demand Paging is a popular method of virtual memory management. In demand paging,


the pages of a process which are least used, get stored in the secondary memory.

A page is copied to the main memory when its demand is made or page fault occurs.
There are various page replacement algorithms which are used to determine the pages
which will be replaced. We will discuss each one of them later in detail.

SNAPSHOT OF A VIRTUAL MEMORY MANAGEMENT SYSTEM

Let us assume 2 processes, P1 and P2, contains 4 pages each. Each page size is 1 KB. The
main memory contains 8 frame of 1 KB each. The OS resides in the first two partitions.
In the third partition, 1st page of P1 is stored and the other frames are also shown as filled
with the different pages of processes in the main memory.

The page tables of both the pages are 1 KB size each and therefore they can be fit in one
frame each. The page tables of both the processes contain various information that is also
shown in the image.
The CPU contains a register which contains the base address of page table that is 5 in the
case of P1 and 7 in the case of P2. This page table base address will be added to the page
number of the Logical address when it comes to accessing the actual corresponding
entry.

Advantages of Virtual Memory

1. The degree of Multiprogramming will be increased.

2. User can run large application with less real RAM.

3. There is no need to buy more memory RAMs.


Disadvantages of Virtual Memory

1. The system becomes slower since swapping takes time.

2. It takes more time in switching between applications.

3. The user will have the lesser hard disk space for its use.
SECONDARY STORAGE MANAGEMENT

Secondary storage devices are those devices whose memory is non volatile, meaning, the
stored data will be intact even if the system is turned off. Here are a few things worth noting
about secondary storage.

• Secondary storage is also called auxiliary storage.


• Secondary storage is less expensive when compared to primary memory like RAMs.
• The speed of the secondary storage is also lesser than that of primary storage.
• Hence, the data which is less frequently accessed is kept in the secondary storage.
• A few examples are magnetic disks, magnetic tapes, removable thumb drives etc.

MAGNETIC DISK STRUCTURE


In modern computers, most of the secondary storage is in the form of magnetic disks. Hence,
knowing the structure of a magnetic disk is necessary to understand how the data in the disk
is accessed by the computer.
STRUCTURE OF A MAGNETIC DISK

A magnetic disk contains several platters. Each platter is divided into circular shaped
tracks. The length of the tracks near the centre is less than the length of the tracks farther
from the centre. Each track is further divided into sectors, as shown in the figure.
Tracks of the same distance from centre form a cylinder. A read-write head is used to read
data from a sector of the magnetic disk.
The speed of the disk is measured as two parts:

• Transfer rate: This is the rate at which the data moves from disk to the computer.
• Random access time: It is the sum of the seek time and rotational latency.
Seek time is the time taken by the arm to move to the required track. Rotational latency is
defined as the time taken by the arm to reach the required sector in the track.
Even though the disk is arranged as sectors and tracks physically, the data is logically
arranged and addressed as an array of blocks of fixed size. The size of a block can be 512 or
1024 bytes. Each logical block is mapped with a sector on the disk, sequentially. In this way,
each sector in the disk will have a logical address.

DISK SCHEDULING ALGORITHMS


On a typical multiprogramming system, there will usually be multiple disk access requests
at any point of time. So those requests must be scheduled to achieve good efficiency. Disk
scheduling is similar to process scheduling. Some of the disk scheduling algorithms are
described below.

First Come First Serve


This algorithm performs requests in the same order asked by the system. Let's take an
example where the queue has the following requests with cylinder numbers as follows:
98, 183, 37, 122, 14, 124, 65, 67
Assume the head is initially at cylinder 56. The head moves in the given order in the queue
i.e., 56→98→183→...→67.
Shortest Seek Time First (SSTF)
Here the position which is closest to the current head position is chosen first. Consider the
previous example where disk queue looks like,
98, 183, 37, 122, 14, 124, 65, 67
Assume the head is initially at cylinder 56. The next closest cylinder to 56 is 65, and then the
next nearest one is 67, then 37, 14, so on.
SCAN algorithm
This algorithm is also called the elevator algorithm because of it's behavior. Here, first the
head moves in a direction (say backward) and covers all the requests in the path. Then it
moves in the opposite direction and covers the remaining requests in the path. This behavior
is similar to that of an elevator. Let's take the previous example,
98, 183, 37, 122, 14, 124, 65, 67
Assume the head is initially at cylinder 56. The head moves in backward direction and
accesses 37 and 14. Then it goes in the opposite direction and accesses the cylinders as they
come in the path.
FREE SPACE MANAGEMENT

As we know that the memory space in the disk is limited. So we need to use the space of the
deleted files for the allocation of the new file. one optical disk allows only one write at a time
in the given sector and thus it is not physically possible to reuse it for other files. The system
maintains a free space list by keep track of the free disk space. The free space list contains all
the records of the free space disk block. Thee free blocks are those which are not allocated
to other file or directory. When we create a file we first search for the free space in the
memory and then check in the free space list for the required amount of space that we
require for our file. if the free space is available then allocate this space to the new file. After
that, the allocating space is deleted from the free space list. Whenever we delete a file then
its free memory space is added to the free space list.
The process of looking after and managing the free blocks of the disk is called free space
management. There are some methods or techniques to implement a free space list. These
are as follows:

• Bitmap
• Linked list
• Grouping
• Counting

1. BITMAP

This technique is used to implement the free space management. When the free space is
implemented as the bitmap or bit vector then each block of the disk is represented by a bit.
When the block is free its bit is set to 1 and when the block is allocated the bit is set to 0. The
main advantage of the bitmap is it is relatively simple and efficient in finding the first free
block and also the consecutive free block in the disk. Many computers provide the bit
manipulation instruction which is used by the users.

The calculation of the block number is done by the formula:

(number of bits per words) X (number of 0-value word) + Offset of first 1 bit

For Example: Apple Macintosh operating system uses the bitmap method to allocate the
disk space.

Assume the following are free. Rest are allocated:

Advantages:

• This technique is relatively simple.


• This technique is very efficient to find the free space on the disk.
Disadvantages:

• This technique requires a special hardware support to find the first 1 in a word it is
not 0.
• This technique is not useful for the larger disks.

For example: Consider a disk where blocks 2, 3, 4, 5, 8, 9, 10, 11, 12, 13, 17, 18, 25,26, and
27 are free and the rest of the blocks are allocated. The free-space bitmap would be:
001111001111110001100000011100000

2. LINKED LIST

This is another technique for free space management. In this linked list of all the free block
is maintained. In this, there is a head pointer which points the first free block of the list which
is kept in a special location on the disk. This block contains the pointer to the next block and
the next block contain the pointer of another next and this process is repeated. By using this
disk it is not easy to search the free list. This technique is not sufficient to traverse the list
because we have to read each disk block that requires I/O time. So traversing in the free list
is not a frequent action.

Advantages:

• Whenever a file is to be allocated a free block, the operating system can simply
allocate the first block in free space list and move the head pointer to the next free
block in the list.

Disadvantages:

• Searching the free space list will be very time consuming; each block will have to be
read from the disk, which is read very slowly as compared to the main memory.
• Not Efficient for faster access.
In our earlier example, we see that keep block 2 is the first free block which points to another
block which contains the pointer of the 3 blocks and 3 blocks contain the pointer to the 4
blocks and this contains the pointer to the 5 block then 5 block contains the pointer to the
next block and this process is repeated at the last .

3. Grouping

This is also the technique of free space management. In this, there is a modification of the
free-list approach which stores the address of the n free blocks. In this the first n-1 blocks
are free but the last block contains the address of the n blocks. When we use the standard
linked list approach the addresses of a large number of blocks can be found very quickly. In
this approach, we cannot keep a list of n free disk addresses but we keep the address of the
first free block.

4. Counting

Counting is another approach for free space management. Generally, some contiguous blocks
are allocated but some are free simultaneously. When the free space is allocated to a process
according to the contiguous allocation algorithm or clustering. So we cannot keep the list of
n free block address but we can keep the address of the first free block and then the numbers
of n free contiguous block which follows the first block. When there is an entry in the free
space list it consists the address of the disk and a count variable. This method of free space
management is similar to the method of allocating blocks. We can store these entries in the
B-tree in place of the linked list. So the operations like lookup, deletion, insertion are
efficient.
ALLOCATION METHODS

There are various methods which can be used to allocate disk space to the files. Selection of
an appropriate allocation method will significantly affect the performance and efficiency of
the system. Allocation method provides a way in which the disk will be utilized and the files
will be accessed.
There are following methods which can be used for allocation.

1. Contiguous Allocation.

2. Extents

3. Linked Allocation

4. Clustering

5. FAT

6. Indexed Allocation

7. Linked Indexed Allocation

8. Multilevel Indexed Allocation

9. Inode

CONTIGUOUS ALLOCATION

If the blocks are allocated to the file in such a way that all the logical blocks of the file get the
contiguous physical block in the hard disk then such allocation scheme is known as
contiguous allocation.

In the image shown below, there are three files in the directory. The starting block and the
length of each file are mentioned in the table. We can check in the table that the contiguous
blocks are assigned to each file as per its need.
Advantages

1. It is simple to implement.

2. We will get Excellent read performance.

3. Supports Random Access into files.

Disadvantages

1. The disk will become fragmented.

2. It may be difficult to have a file grow.

3.

LINKED LIST ALLOCATION

Linked List allocation solves all problems of contiguous allocation. In linked list allocation,
each file is considered as the linked list of disk blocks. However, the disks blocks allocated to
a particular file need not to be contiguous on the disk. Each disk block allocated to a file
contains a pointer which points to the next disk block allocated to the same file.
Advantages

1. There is no external fragmentation with linked allocation.

2. Any free block can be utilized in order to satisfy the file block requests.

3. File can continue to grow as long as the free blocks are available.

4. Directory entry will only contain the starting block address.

Disadvantages

1. Random Access is not provided.

2. Pointers require some space in the disk blocks.


3. Any of the pointers in the linked list must not be broken otherwise the file will get
corrupted.

4. Need to traverse each block.

FILE ALLOCATION TABLE

The main disadvantage of linked list allocation is that the Random access to a particular block
is not provided. In order to access a block, we need to access all its previous blocks.
File Allocation Table overcomes this drawback of linked list allocation. In this scheme, a file
allocation table is maintained, which gathers all the disk block links. The table has one entry
for each disk block and is indexed by block number.

File allocation table needs to be cached in order to reduce the number of head seeks. Now
the head doesn't need to traverse all the disk blocks in order to access one successive block.

It simply accesses the file allocation table, read the desired block entry from there and access
that block. This is the way by which the random access is accomplished by using FAT. It is
used by MS-DOS and pre-NT Windows versions.

Advantages

1. Uses the whole disk block for data.

2. A bad disk block doesn't cause all successive blocks lost.

3. Random access is provided although its not too fast.


4. Only FAT needs to be traversed in each file operation.

Disadvantages

1. Each Disk block needs a FAT entry.

2. FAT size may be very big depending upon the number of FAT entries.

3. Number of FAT entries can be reduced by increasing the block size but it will also
increase Internal Fragmentation.

4.

INDEXED ALLOCATION

Limitation of FAT

Limitation in the existing technology causes the evolution of a new technology. Till now, we
have seen various allocation methods; each of them was carrying several advantages and
disadvantages.

File allocation table tries to solve as many problems as possible but leads to a drawback. The
more the number of blocks, the more will be the size of FAT.

Therefore, we need to allocate more space to a file allocation table. Since, file allocation table
needs to be cached therefore it is impossible to have as many space in cache. Here we need
a new technology which can solve such problems.

INDEXED ALLOCATION SCHEME

Instead of maintaining a file allocation table of all the disk pointers, Indexed allocation
scheme stores all the disk pointers in one of the blocks called as indexed block. Indexed block
doesn't hold the file data, but it holds the pointers to all the disk blocks allocated to that
particular file. Directory entry will only contain the index block address.
Advantages

1. Supports direct access

2. A bad data block causes the lost of only that block.

Disadvantages

1. A bad index block could cause the lost of entire file.

2. Size of a file depends upon the number of pointers, a index block can hold.

3. Having an index block for a small file is totally wastage.

4. More pointer overhead


LINKED INDEX ALLOCATION

SINGLE LEVEL LINKED INDEX ALLOCATION

In index allocation, the file size depends on the size of a disk block. To allow large files, we
have to link several index blocks together. In linked index allocation,

o Small header giving the name of the file

o Set of the first 100 block addresses

o Pointer to another index block

For the larger files, the last entry of the index block is a pointer which points to another index
block. This is also called as linked schema.

Advantage: It removes file size limitations

Disadvantage: Random Access becomes a bit harder


MULTILEVEL INDEX ALLOCATION

In Multilevel index allocation, we have various levels of indices. There are outer level index
blocks which contain the pointers to the inner level index blocks and the inner level index
blocks contain the pointers to the file data.

o The outer level index is used to find the inner level index.

o The inner level index is used to find the desired data block.

Advantage: Random Access becomes better and efficient.

Disadvantage: Access time for a file will be higher.


INODE

In UNIX based operating systems, each file is indexed by an Inode. Inode are the special disk
block which is created with the creation of the file system. The number of files or directories
in a file system depends on the number of Inodes in the file system.

An Inode includes the following information

1. Attributes (permissions, time stamp, ownership details, etc) of the file

2. A number of direct blocks which contains the pointers to first 12 blocks of the file.

3. A single indirect pointer which points to an index block. If the file cannot be indexed
entirely by the direct blocks then the single indirect pointer is used.

4. A double indirect pointer which points to a disk block that is a collection of the
pointers to the disk blocks which are index blocks. Double index pointer is used if the
file is too big to be indexed entirely by the direct blocks as well as the single indirect
pointer.

5. A triple index pointer that points to a disk block that is a collection of pointers. Each
of the pointers is separately pointing to a disk block which also contains a collection
of pointers which are separately pointing to an index block that contains the pointers
to the file blocks.
STORAGE HIREACHERY
Computer storage has components that store computer data. The different storage types in
the storage hierarchy are as follows:

PRIMARY STORAGE
This is also known as the main memory and is the memory directly accessible by the CPU. All
the instructions are executed in the main memory by CPU and the data required by these
instructions is also stored in main memory.
Main memory primarily consists of the RAM which is volatile in nature. It is also quite small
compared to secondary memory and expensive as well.

SECONDARY STORAGE

Secondary or external storage is not directly accessible by the CPU. The data from secondary
storage needs to be brought into the primary storage before the CPU can use it.
Secondary storage is non volatile i.e. the data stored is not lost when power is switched off.
Mostly hard drives are used as secondary storage.
TERTIARY STORAGE
This is a third level of storage that mostly contains data that needs to be archived. This is
because it is quite slow. Data is normally retrieved from tertiary storage to primary storage
when it needs to be viewed.
Tertiary storage is mostly formed of tapes and disks.

CHARACTERISTICS OF STORAGE
The different characteristics of storage devices are as follows:

Volatility
Volatile memory needs power to work and loses its data when power is switched off.
However, it is quite fast so it is used as primary memory.
Non - volatile memory retains its data even when power is lost. So, it is used for secondary
memory.

Mutability
Mutable storage is both read and write storage and data can be overwritten as required.
Primary storage typically contains mutable storage and it is also available in secondary
storage nowadays.

Accessibility
Storage access can be random or sequential. In random access, all the data in the storage can
be accessed randomly and roughly in the same amount of time. In sequential storage, the
data needs to accessed in sequential order i.e. one after the other.

Addressability
Each storage location in memory has a particular memory address. The data in a particular
location can be accessed using its address.

Capacity
The capacity of any storage device is the amount of data it can hold. This is usually
represented in the form of bits or bytes.
Performance
Performance can be described in terms of latency or throughput.
• Latency is the time required to access the storage. It is specified in the form of read
latency and write latency.
• Throughput is the data reading rate for the memory. It can represented in the form
of megabytes per second.
UNIT IV

FILES AND PROTECTION


Introduction to File System
A file can be "free formed", indexed or structured collection of related bytes having meaning
only to the one who created it. Or in other words an entry in a directory is the file. The file
may have attributes like name, creator, date, type, permissions etc.

File Structure
A file has various kinds of structure. Some of them can be :

• Simple Record Structure with lines of fixed or variable lengths.


• Complex Structures like formatted document or reloadable load files.
• No Definite Structure like sequence of words and bytes etc.

Attributes of a File
Following are some of the attributes of a file :

• Name . It is the only information which is in human-readable form.


• Identifier. The file is identified by a unique tag(number) within file system.
• Type. It is needed for systems that support different types of files.
• Location. Pointer to file location on device.
• Size. The current size of the file.
• Protection. This controls and assigns the power of reading, writing, executing.
• Time, date, and user identification. This is the data for protection, security, and usage
monitoring.
FILE ACCESS METHODS
The way that files are accessed and read into memory is determined by Access methods.
Usually a single access method is supported by systems while there are OS's that support
multiple access methods.

1. Sequential Access

• Data is accessed one record right after another is an order.


• Read command cause a pointer to be moved ahead by one.
• Write command allocate space for the record and move the pointer to the new End Of
File.
• Such a method is reasonable for tape.

2. Direct Access

• This method is useful for disks.


• The file is viewed as a numbered sequence of blocks or records.
• There are no restrictions on which blocks are read/written, it can be dobe in any order.
• User now says "read n" rather than "read next".
• "n" is a number relative to the beginning of file, not relative to an absolute physical disk
location.

3. Indexed Sequential Access

• It is built on top of Sequential access.


• It uses an Index to control the pointer while accessing files.
What is a Directory?
Information about files is maintained by Directories. A directory can contain multiple files.
It can even have directories inside of them. In Windows we also call these directories as
folders.
Following is the information maintained in a directory :

• Name : The name visible to user.


• Type : Type of the directory.
• Location : Device and location on the device where the file header is located.
• Size : Number of bytes/words/blocks in the file.
• Position : Current next-read/next-write pointers.
• Protection : Access control on read/write/execute/delete.
• Usage : Time of creation, access, modification etc.
• Mounting : When the root of one file system is "grafted" into the existing tree of another
file system its called Mounting.

PREVENTION (SECURITY)
Security refers to providing a protection system to computer system resources such as CPU,
memory, disk, software programs and most importantly data/information stored in the
computer system. If a computer program is run by an unauthorized user, then he/she may
cause severe damage to computer or data stored in it. So a computer system must be
protected against unauthorized access, malicious access to system memory, viruses, worms
etc. We're going to discuss following topics in this chapter.

• Authentication
• One Time passwords
• Program Threats
• System Threats
• Computer Security Classifications
AUTHENTICATION
Authentication refers to identifying each user of the system and associating the executing
programs with those users. It is the responsibility of the Operating System to create a
protection system which ensures that a user who is running a particular program is
authentic. Operating Systems generally identifies/authenticates users using following three
ways −
• Username / Password − User need to enter a registered username and password
with Operating system to login into the system.
• User card/key − User need to punch card in card slot, or enter key generated by key
generator in option provided by operating system to login into the system.
• User attribute - fingerprint/ eye retina pattern/ signature − User need to pass
his/her attribute via designated input device used by operating system to login into
the system.
One Time passwords
One-time passwords provide additional security along with normal authentication. In One-
Time Password system, a unique password is required every time user tries to login into
the system. Once a one-time password is used, then it cannot be used again. One-time
password are implemented in various ways.
• Random numbers − Users are provided cards having numbers printed along with
corresponding alphabets. System asks for numbers corresponding to few alphabets
randomly chosen.
• Secret key − User are provided a hardware device which can create a secret id
mapped with user id. System asks for such secret id which is to be generated every
time prior to login.
• Network password − Some commercial applications send one-time passwords to
user on registered mobile/ email which is required to be entered prior to login.
PROGRAM THREATS
Operating system's processes and kernel do the designated task as instructed. If a user
program made these process do malicious tasks, then it is known as Program Threats. One
of the common example of program threat is a program installed in a computer which can
store and send user credentials via network to some hacker. Following is the list of some
well-known program threats.
• Trojan Horse − Such program traps user login credentials and stores them to send
to malicious user who can later on login to computer and can access system
resources.
• Trap Door − If a program which is designed to work as required, have a security hole
in its code and perform illegal action without knowledge of user then it is called to
have a trap door.
• Logic Bomb − Logic bomb is a situation when a program misbehaves only when
certain conditions met otherwise it works as a genuine program. It is harder to
detect.
• Virus − Virus as name suggest can replicate themselves on computer system. They
are highly dangerous and can modify/delete user files, crash systems. A virus is
generatlly a small code embedded in a program. As user accesses the program, the
virus starts getting embedded in other files/ programs and can make system
unusable for user

SYSTEM THREATS
System threats refers to misuse of system services and network connections to put user in
trouble. System threats can be used to launch program threats on a complete network called
as program attack. System threats creates such an environment that operating system
resources/ user files are misused. Following is the list of some well-known system threats.
• Worm − Worm is a process which can choked down a system performance by using
system resources to extreme levels. A Worm process generates its multiple copies
where each copy uses system resources, prevents all other processes to get required
resources. Worms processes can even shut down an entire network.
• Port Scanning − Port scanning is a mechanism or means by which a hacker can
detects system vulnerabilities to make an attack on the system.
• Denial of Service − Denial of service attacks normally prevents user to make
legitimate use of the system. For example, a user may not be able to use internet if
denial of service attacks browser's content settings.

COMPUTER SECURITY CLASSIFICATIONS


As per the U.S. Department of Defense Trusted Computer System's Evaluation Criteria there
are four security classifications in computer systems: A, B, C, and D. This is widely used
specifications to determine and model the security of systems and of security solutions.
Following is the brief description of each classification.

S.N. Classification Type & Description

1 Type A
Highest Level. Uses formal design specifications and verification techniques.
Grants a high degree of assurance of process security.

2 Type B
Provides mandatory protection system. Have all the properties of a class C2
system. Attaches a sensitivity label to each object. It is of three types.
• B1 − Maintains the security label of each object in the system. Label is used
for making decisions to access control.
• B2 − Extends the sensitivity labels to each system resource, such as storage
objects, supports covert channels and auditing of events.
• B3 − Allows creating lists or user groups for access-control to grant access
or revoke access to a given named object.

3 Type C
Provides protection and user accountability using audit capabilities. It is of two
types.
• C1 − Incorporates controls so that users can protect their private
information and keep other users from accidentally reading / deleting their
data. UNIX versions are mostly Cl class.
• C2 − Adds an individual-level access control to the capabilities of a Cl level
system.

4 Type D
Lowest level. Minimum protection. MS-DOS, Window 3.1 fall in this category.

FILE SYSTEM STRUCTURE

File System provide efficient access to the disk by allowing data to be stored, located and
retrieved in a convenient way. A file System must be able to store the file, locate the file and
retrieve the file.

Most of the Operating Systems use layering approach for every task including file systems.
Every layer of the file system is responsible for some activities.

The image shown below, elaborates how the file system is divided in different layers, and
also the functionality of each layer.
o When an application program asks for a file, the first request is directed to the logical
file system. The logical file system contains the Meta data of the file and directory
structure. If the application program doesn't have the required permissions of the file
then this layer will throw an error. Logical file systems also verify the path to the file.

o Generally, files are divided into various logical blocks. Files are to be stored in the
hard disk and to be retrieved from the hard disk. Hard disk is divided into various
tracks and sectors. Therefore, in order to store and retrieve the files, the logical blocks
need to be mapped to physical blocks. This mapping is done by File organization
module. It is also responsible for free space management.

o Once File organization module decided which physical block the application program
needs, it passes this information to basic file system. The basic file system is
responsible for issuing the commands to I/O control in order to fetch those blocks.

o I/O controls contain the codes by using which it can access hard disk. These codes are
known as device drivers. I/O controls are also responsible for handling interrupts.

MASTER BOOT RECORD (MBR)

Master boot record is the information present in the first sector of any hard disk. It contains
the information regarding how and where the Operating system is located in the hard disk
so that it can be booted in the RAM.

MBR is sometimes called master partition table because it includes a partition table which
locates every partition in the hard disk.

Master boot record (MBR) also includes a program which reads the boot sector record of the
partition that contains operating system.
What happens when you turn on your computer?

Due to the fact that the main memory is volatile, when we turn on our computer, CPU

cannot access the main memory directly. However, there is a special program called as BIOS
stored in ROM is accessed for the first time by the CPU.

BIOS contains the code, by executing which, the CPU access the very first partition of hard
disk that is MBR. It contains a partition table for all the partitions of the hard disk.

Since, MBR contains the information about where the operating system is being stored and
it also contains a program which can read the boot sector record of the partition, hence the
CPU fetches all this information and load the operating system into the main memory

ON DISK DATA STRUCTURES

There are various on disk data structures that are used to implement a file system. This
structure may vary depending upon the operating system.

1. Boot Control Block

Boot Control Block contains all the information which is needed to boot an operating
system from that volume. It is called boot block in UNIX file system. In NTFS, it is
called the partition boot sector.
2. Volume Control Block

Volume control block all the information regarding that volume such as number of
blocks, size of each block, partition table, pointers to free blocks and free FCB blocks.
In UNIX file system, it is known as super block. In NTFS, this information is stored
inside master file table.
3. Directory Structure (per file system)

A directory structure (per file system) contains file names and pointers to
corresponding FCBs. In UNIX, it includes inode numbers associated to file names.

4. File Control Block

File Control block contains all the details about the file such as ownership details,
permission details, file size,etc. In UFS, this detail is stored in inode. In NTFS, this
information is stored inside master file table as a relational database structure. A
typical file control block is shown in the image below.

IN MEMORY DATA STRUCTURE

Till now, we have discussed the data structures that are required to be present on the hard
disk in order to implement file systems. Here, we will discuss the data structures required to
be present in memory in order to implement the file system.
The in-memory data structures are used for file system management as well as performance
improvement via caching. This information is loaded on the mount time and discarded on
ejection.

1. In-memory Mount Table

In-memory mount table contains the list of all the devices which are being mounted
to the system. Whenever the connection is maintained to a device, its entry will be
done in the mount table.

2. In-memory Directory structure cache

This is the list of directory which is recently accessed by the CPU. The directories
present in the list can also be accessed in the near future so it will be better to store
them temporally in cache.

3. System-wide open file table

This is the list of all the open files in the system at a particular time. Whenever the
user open any file for reading or writing, the entry will be made in this open file table.

4. Per process Open file table

It is the list of open files subjected to every process. Since there is already a list which
is there for every open file in the system thereforeIt only contains Pointers to the
appropriate entry in the system wide table.

DIRECTORY IMPLEMENTATION

There is the number of algorithms by using which, the directories can be implemented.
However, the selection of an appropriate directory implementation algorithm may
significantly affect the performance of the system.

The directory implementation algorithms are classified according to the data structure they
are using. There are mainly two algorithms which are used in these days.
1. Linear List

In this algorithm, all the files in a directory are maintained as singly lined list. Each file
contains the pointers to the data blocks which are assigned to it and the next file in the
directory.

Characteristics

1. When a new file is created, then the entire list is checked whether the new file name
is matching to a existing file name or not. In case, it doesn't exist, the file can be
created at the beginning or at the end. Therefore, searching for a unique name is a big
concern because traversing the whole list takes time.

2. The list needs to be traversed in case of every operation (creation, deletion, updating,
etc) on the files therefore the systems become inefficient.

2. Hash Table

To overcome the drawbacks of singly linked list implementation of directories, there is an


alternative approach that is hash table. This approach suggests to use hash table along with
the linked lists.

A key-value pair for each file in the directory gets generated and stored in the hash table. The
key can be determined by applying the hash function on the file name while the key points
to the corresponding file stored in the directory.
Now, searching becomes efficient due to the fact that now, entire list will not be searched on
every operating. Only hash table entries are checked using the key and if an entry found then
the corresponding file will be fetched using the value.

FILE OPERATIONS

Creating a File

 Creation of a file is the first operation. For creating a file two steps are necessary.

1. Required space must be allocated.


2. An entry for new file must be made in the directory.

 The directory entry records the name of the file and the location in the file system.

Writing a File

 For file writing operation, we make a system call specifying both the name of a file and the
information to be written into the file.
 System searches the entire directory structure to find the location of the specified file.
 System keeps a write pointer that keeps track of writing at the location in the file.
 The system updates write pointer each time whenever a file write operation occurs.

Reading a File

 To perform file read operation, we use a system call that specifies name of the file and the
block of the file to read from it.

 Again the directory is searched for the specified file.

 System keeps a read pointer that keeps track of reading location in the file.
 The system updates read pointer each time whenever a file read operation occurs

Repositioning Within a File

 Repositioning within a file operation does not involve any actual input output.
 The directory is searched for the appropriate entry and the current file position is set to a
given value.
 It is also known as files seek operation.

Deleting a File

 File deletion operation also requires searching of a specified file entry within the directory
structure.
 As soon as the file is deleted, space allocated to that file becomes available for further use.

Truncating a File

 In some cases the user may want to erase the file contents but keep its attributes as it is. This
operation is called truncating a file.
 Instead of delete a file and recreating it with same attributes, this function allows all
attributes to remain unchanged except the file content.
 File length attribute is reset to a length zero and its file space is released.

FILE ACCESS METHODS


When file is used, information is read and accessed into computer memory and there are
several ways to accesses these information of the file. Some system provide only one access
method for files. Other system, such as those of IBM, support many access method, and
choosing the right one for a particular application is a major design problem.
There are three ways to access a file into computer system: Sequential Access, Direct Access,
Index sequential Method.

1. Sequential Access –

It is the simplest access method. Information in the file id processed in order, one
record after the other. This mode of access is by far the most common; for example,
editor and compiler usually access the file in this fashion.
Read and write make up the bulk of the operation on a file. A read operation -read next-
read the next position of the file and automatically advance a file pointer, which keep
track I/O location. Similarly for the writewrite next append to the end of the file and
advance to the newly written material.
Key points:
• Data is accessed one record right after another record in an order.
• When we use read command, it move ahead pointer by one
• When we use write command, it will allocate memory and move the pointer to the
end of the file
• Such a method is reasonable for tape.
Most of the operating systems access the file sequentially. In other words, we can say that
most of the files need to be accessed sequentially by the operating system.

In sequential access, the OS read the file word by word. A pointer is maintained which
initially points to the base address of the file. If the user wants to read first word of the file
then the pointer provides that word to the user and increases its value by 1 word. This
process continues till the end of the file.

Modern word systems do provide the concept of direct access and indexed access but the
most used method is sequential access due to the fact that most of the files such as text files,
audio files, video files, etc need to be sequentially accessed.

2. Direct Access –

Another method is direct access method also known as relative access method. A filed-
length logical record that allow program to read and write record rapidly. in no
particular order. The direct access is based on the disk model of a file since disk allows
random access to any file block. For direct access, the file is viewed as a numbered
sequence of block or record. Thus, we may read block 14 then block 59 and then we can
write block 17. There is no restriction on the order of reading and writing for direct
access file.
A block number provided by the user to the operating system is normally a relative
block number, the first relative block of the file is 0 and then 1 and so on.

The Direct Access is mostly required in the case of database systems. In most of the cases,
we need filtered information from the database. The sequential access can be very slow and
inefficient in such cases.

Suppose every block of the storage stores 4 records and we know that the record we needed
is stored in 10th block. In that case, the sequential access will not be implemented because
it will traverse all the blocks in order to access the needed record.
Direct access will give the required result despite of the fact that the operating system
has to perform some complex tasks such as determining the desired block number.
However, that is generally implemented in database applications.
3. Index sequential method –

It is the other method of accessing a file. Which is built on the top of he direct access
method. these methods we construct an index for the file. The index, like an index in
the back of a book, contains the pointer to the various blocks. To find a record in the
file, we first search the index and then by the help of pointer we access the file directly.
Key points:
• It is built on top of Sequential access.
• It control the pointer by using index.
CONSISTENCY SEMANTICS

_ Important criterion for evaluation of file systems that allows file sharing _ Specifies the
semantics of multiple users accessing a file simultaneously _ Specifies when modifications of
data by one user are observable by others _ File session  Series of accesses between an open
and close operation by the same user on the same file

_ Unix Semantics  Writes to an open file by a user are visible immediately to other users
that have this file open at the same time  There is a mode of sharing where users share the
pointer of current location into the file. Advancing of pointer by one user affects all
sharing users. A file has single image that interleaves all accesses, regardless of their origin

There should be consistency maintained by the file system for all users sharing the system :

Unix Semantics

• Writes performed by one user are immediately seen by other user(s)


• Can optionally share current location pointer :

User 1 reads block 510, User 2 then reads block 511 (Single File Image)

Session Semantics

• Writes to open file are not visible to other users


• When file closed, changes are made visible to new sessions, but are not seen by
current sessions

Multiple copies of files ?

Immutable Files

• Read only sharing - files cannot be changed


Locks

• Sharing of files locking to give exclusive access


• Prevent others reading while the file is being updated
• Locks can be : On whole files or Individual record of a file

STRUCTURES OF DIRECTORY
A directory is a container that is used to contain folders and file. It organises files and folders
into hierarchical manner.

There are several logical structures of directory, these are given as below.

1. Single-level directory –
Single level directory is simplest disectory structure.In it all files are contained in same
directory which make it easy to support and understand.
A single level directory has a significant limitation, however, when the number of files
increases or when the system has more than one user. Since all the files are in the same
directory, they must have the unique name . if two users call there dataset test, then the
unique name rule violated.

1. Advantages:
• Since it is a single directory, so its implementation is very easy.
• If files are smaller in size, searching will faster.
• The operations like file creation, searching, deletion, updating are very easy in
such a directory structure.
Disadvantages:
• There may chance of name collision because two files can not have the same name.
• Searching will become time taking if directory will large.
• In this can not group the same type of files together.

2. Two-level directory –
As we have seen, a single level directory often leads to confusion of files names among
different users. the solution to this problem is to create a separate directory for each
user.
In the two-level directory structure, each user has there own user files directory (UFD).
The UFDs has similar structures, but each lists only the files of a single user. system’s
master file directory (MFD) is searches whenever a new user id=s logged in. The MFD is
indexed by username or account number, and each entry points to the UFD for that
user.

Advantages:
• We can give full path like /User-name/directory-name/.
• Diffrent users can have same directory as well as file name.
• Searching of files become more easy due to path name and user-grouping.
Disadvantages:
• A user is not allowed to share files with other users.
• Still it not very scalable, two files of the same type cannot be grouped together in the
same user.

Tree-structured directory –
Once we have seen a two-level directory as a tree of height 2, the natural generalization is to
extend the directory structure to a tree of arbitrary height.
This generalization allows the user to create there own subdirectories and to organise on
their files accordingly.
1. A tree structure is the most common directory structure. The tree has a root directory,
and every file in the system have a unique path.
Advantages:
• Very generalize, since full path name can be given.
• Very scalable, the probability of name collision is less.
• Searching becomes very easy, we can use both absolute path as well as relative.
Disadvantages:
• Every file does not fit into the hierarchical model, files may be saved into multiple
directories.
• We can not share files.
• It is inefficient, because accessing a file may go under multiple directories.
2. Acyclic graph directory –

An acyclic graph is a graph with no cycle and allows 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 used in the situation like when two programmers are working on a joint project
and they need to access files. The associated files are stored in a subdirectory, separated
them from other projects and files of other programmers since they are working on a
joint project so they want to the subdirectories into there own directories. The common
subdirectories should be shared. So here we use Acyclic directories.
It is the point to note that shared file is not the same as copy file if any programmer
makes some changes in the subdirectory it will reflect in both subdirectories.
1. Advantages:
• We can share files.
• Searching is easy due to different-different paths.
Disadvantages:
• We share the files via linking, in case of deleting it may create the problem,
• If the link is softlink then after deleting the file we left with a dangling pointer.

• In case of hardlink, to delete a file we have to delete all the reference associated
with it.

2. General graph directory structure –

In general graph directory structure, cycles are allowed within a directory structure
where multiple directories can be derived from more than one parent directory.
The main problem with this kind of directory structure is to calculate total size or space
that have been taken by the files and directories.
Advantages:
• It allows cycles.
• It is more flexible than other directories structure.
Disadvantages:
• It is more costly than others.
• It needs garbage collection.

FILE PROTECTION:
Preventing accidental erasing of data. Physical file protection is provided on the storage
medium by turning a switch, moving a lever or covering a notch. Writing is prohibited even
if the software directs the computer to do so. For example, on the eariler half-inch tape, a
plastic ring in the center of the reel was removed (no ring-no write).

Logical file protection is provided by the operating system, which can designate files as read
only. This allows both regular (read/write) and read only files to be stored on the same disk
volume. Files can also be designated as hidden files, which makes them invisible to most
software programs.

The first level of protection is that only valid users are allowed to login to the system.
● The system admininstrator, called the superuser with user name root, sets up the valid
users with the adduser (or equivalent) command.
● If you don't know a valid user name, you cannot login.
● To see a list of all valid user names: cat /etc/passwd
● A typical, traditional user entry looks like
● Each user is given a unique user ID (called the uid), and is a put into a group.
● Each group has a unique group ID (called the gid); group information is stored in the
/etc/group file.
Password
● Since /etc/passwd is usually allowed to be seen by everyone, anyone could log in as you, if
they guess your password.
● To prevent this, give yourself a password: passwd old password: new password: retype
password:
Note: Passwords will not be echoed.
● Now no one can login as you, unless they know your password.

Your password is encrypted, and, traditionally, entered into /etc/passwd.


● Newer versions of UNIX place passwords into another file (such as /etc/shadow,
/etc/master.passwd, or /etc/secure), that only the superuser has access to. In such a system,
your password entry in /etc/passwd is replaced by a * or x character.

FILE SECURITY
● The following information is kept on each file that exists. It is stored on a special part of
the disk called the i-list or i-table.
➔ The user ID and group ID of its owner.

➔ Its protection bits (twelve in total).


➔ The physical disk or tape address of the file contents.

➔ Its size in bytes.


➔ Time of creation, last use, and last modification. Time is stored as the number of seconds
since midnight, January 1, 1970.
➔ The number of links to a file, that is, the number of times it appears in a directory.

➔ A code indicating whether the file is a directory, an ordinary file, or a special file.
Changing File Protection ● Use the chmod command. ● This command can only be executed
by the owner of the file or by root.
● You can change the protection for any of the three categories:
➔ owner - (u)

➔ group - (g)
➔ other - (o)
● You can add or take away permissions
➔ + [r][w][x] - Grant permission ➔ - [r][w][x] - Revoke permission ➔ = [r][w][x] - Set
permissions absolutely
FILE OWNERSHIP
● When you create a file, it is stamped with your user ID and group ID.
● You and the superuser are the only users allowed to change the mode (viachmod).
● Only the superuser can change the owner ID and group ID of a file.
chown username FILE.TXT chgrp groupname FILE.TXT
FILE SYSTEM SECURITY
 A file system is a method for storing and organizing computer files and the data they
contain to make it easy to find and access them.
 File systems exist on hard drives, pen drives, cd’s, dvd’s and any other form of data
storage medium
 Most data storage devices have array of fixed-size blocks, sometimes called sectors,
and file system is in charge of organizing these sectors into files and directories. It is
also in charge of indexing the media so it knows where and what each file is
file system handle security
 The file system is crucial to data integrity.
 Main method of protection is through access control
 Accessing file system operations (ex. modifying or deleting a file) are controlled
through access control lists or capabilities
 Capabilities are more secure so they tend to be used by operating systems on file
systems like NTFS or ext3.
 Secondary method of protection is through the use of backup and recovery systems
Attacks on the file system
 Race Condition Attacks
 Using ADS to hide files
 Directory traversal

Race Condition Attacks


 Occurs when a process performs a sequence of operations on a file, under the
assumption that they are executed atomically.
 Can be used by the attacker to change the characteristics of that file between two
successive operations on it resulting in the victim process to operate on the modified
file.
Using ADS to hide Files
 Alternate Data Streams(ADS) allows multiple data streams to be attached to a single
file.
 A file can be hidden behind a file as an attached stream that could be hundreds of
megabytes in size, however a directory listing will only display the file’s normal size.

Directory Traversal
 An exploit caused by lack of insufficient security validation of user supplied input file
names
 For example the attacker would pass this as input. ../../../../../../../../../etc/password
to retrieve the password file from the server.

file system data integrity in following ways.


 Access Controls
 Encryption
 RAID
 Recovery when data is corrupted
Access Control
 Access Control plays a huge part in file system security
 The system should only allow access to files that the user is permitted to access
 Almost all major file systems support ACL’s or capabilities in order to prevent
malicious activity on the file system
 Depending on the users rights they can be allowed to read, write and/or execute and
object. In some file systems schemes only certain users are allowed to alter the ACL
on a file or see if a file even exists.
 Ultimately the less the user has access to the less that can go wrong and the integrity
of the disk can be more guaranteed.

File System Encryption


 Encryption is also a method used by file systems to secure data, NTFS for example
offers file encryption using DESX
 Two method of disk encryption
 Full Disk Encryption
 File System Encryption
 File system encryption has a few advantages over full disk encryption for example
1. File based key management
2. Individual management of encrypted files
3. Access control can be further strengthened through the use of public
key cryptography
4. Keys are only held in memory while the file is being used
 Provides security beyond user authentication and access control lists. For example
when the attacker has physical access to the computer.
 EFS uses public key cryptography however it is susceptible to brute-force attacks
against the user account passwords.

RAID
 RAID stands for Redundant Array of Independent Disks
 Offers drawbacks and advantages over a single disk, each with different applications
 Types of RAID
 RAID 0 “Striping set without parity”
 RAID 1 “Mirrored set without parity”
 RAID 3 “Striped set with byte level parity”
 RAID 4 “Striped set with block level parity”
 RAID 5 “Striped set with distributed parity”
 RAID 6 “Striped set with dual distributed parity”

Encryption
Encryption is also a method used by file systems to secure data.
EFS Encryption
 EFS works by encrypting a file with a bulk symmetric key, aka File Encryption Key or
FEK.
 The FEK is encrypted with a public key that is associated with the user that encrypted
the file.

EFS Decryption
 The EFS uses the private key that matches the EFS digital certificate (that was
used to encrypt the file) to decrypt the symmetric key.
 The resulting symmetric key is then used to decrypt the file.
Case Study on Windows OS and Linux OS
An operating system (OS) is a program that makes it possible for you to interact with all of
the software and hardware on your computer. The most popular OS is Microsoft Windows,
but there is a cheaper, more flexible option: Unix.

Unix vs. Windows: How Do They Work?


There are two types of operating systems:
• With a command-line operating system (e.g., DOS), you type a text command and the
computer responds according to that command. The computer response is in text
format.
• With a graphical user interface (GUI) operating system (e.g., Windows), you interact
with the computer by selecting pictures and buttons in a graphical interface. You
select these graphical elements with a mouse, keyboard, or touch screen.
The Windows operating system is intended to be used with a GUI, its command line is for
those with advanced computer skills.
Unix gives you the option to use either the command line for more control and flexibility or
a GUI which many people find easier to use.

Unix and Windows: A Competitive History


Unix rose from the ashes of a failed attempt in the early 1960s to develop a reliable time-
sharing operating system. A few survivors from Bell Labs did not give up and created a
system that provided a development environment described as "of unusual simplicity,
power, and elegance."
Since the 1980s, Unix's main competitor, Windows, has gained popularity due to the
increasing power of microcomputers with Intel-compatible processors. At the time,
Windows was the only major OS designed for this type of processor; however, a new
version of Unix called Linux, also specifically developed for microcomputers, emerged in
the early 1990s. Since Linux can be obtained for free, it's a more cost-effective choice for
individuals and businesses.
On the server front, Unix has been closing in on Microsoft’s market share. In 1999, Linux
surpassed Novell Netware to become the top server operating system behind Windows NT.
On the client front, Microsoft dominates the operating system market with over 90 percent
market share.

Advantages of Unix
Unix is more flexible and can be installed on many different types of machines including
mainframe computers, supercomputers, and micro-computers. Other advantages of Unix
include:
• Unix is more stable and does not go down as often as Windows does, so it requires
less administration and maintenance.
• Unix has greater built-in security and permissions features than Windows.
• Unix possesses more processing power than Windows.
• Unix is the leader in serving the web. About 90 percent of the internet relies on Unix
operating systems running Apache, the world's most widely used web server.
• Software upgrades from Microsoft often require the user to purchase new hardware
or prerequisite software, this is not the case with Unix.
Unix also inspires novel approaches to software design, such as solving problems by
interconnecting simpler tools instead of creating large monolithic application programs.
Macintosh computers run a variant of UNIX that you can access by going to Applications >
Utilities > Terminal.app. You can even use your Mac to log into a UNIX server.

Advantages of Windows
While the Windows OS is more limited than Unix in terms of what it can do, there are still
many benefits to using Windows:
• Windows is easy enough for anyone to use.
• Windows software is supported by Microsoft and technical issues are easily
resolved.
• Windows supports a massive library of software programs, utilities, and games.
• Windows offers better backwards compatibility and extensive plug-and-play
support.
• Microsoft provides automatic updates that enable support for new features and
hardware; with Unix, such updates must be performed manually.

Unix vs. Windows: Which is Best for You?


Because of Microsoft’s aggressive marketing practices, millions of people use the Windows
OS that came with their PCs. These people either enjoy the convenience of the pre-installed
operating system or they're not aware that there are operating systems like Unix and
Linux.
If you're unsure which OS to use at home or for your business, consider one of the varieties
of Linux or Unix. The mostly free or inexpensive open-source operating systems, such as
Linux and BSD, are very attractive to aspiring and experienced computer wizards because
of their flexibility and control. Many of the smartest programmers are developing state-of-
the-art software free of charge for the fast-growing "open-source movement."
No single operating system offers universal solutions to all your computing needs. It is all
about having choices and making educated decisions.

Linux vs. Windows


Updated: 01/24/2018 by Computer Hope

Users who are considering making a change


from Windows to Linux or Linux to Windows commonly want to know the advantages and
disadvantages of each of the operating systems. Below is a chart to help illustrate the major
advantages and disadvantages of each of these operating systems.
Topic Linux Windows

Price The Linux kernel, and the GNU Microsoft Windows usually costs
utilities and libraries which between $99.00 and $199.00 USD
accompany it in most for each licensed copy. However,
distributions, are entirely free Windows 10 is being offered as a
and open source. You can free upgrade to current owners of
download and install Windows 7 or Windows 8.1 if
GNU/Linux distributions they upgrade before July 29,
without purchase. Some 2016.
companies offer paid support
for their Linux distributions,
but the underlying software is
still free to download and
install.

Ease Of GNU/Linux operating systems Windows is one of the easiest


Use have a steeper learning curve desktop operating systems to use.
for the average user. They One of its primary design
frequently require a deeper characteristics is user friendliness
understanding of the and simplicity of basic system
underlying system to perform tasks. Its ease lack of difficulty is
day-to-day functions. considered a positive by users
Additionally, troubleshooting who want their system to just
technical issues can be a more work. However, more proficient
intimidating and complicated users may be frustrated by
process than on Windows. oversimplification of system tasks
However, some distributions at the expense of fine-grained
such as Ubuntu and Linux Mint control over the system itself.
are designed specifically to ease
the transition from Windows to
a Linux environment.

Reliability Linux is notoriously reliable Although Microsoft Windows has


and secure. It has a strong focus made great improvements in
on process management, reliability in recent years, it's
system security, and uptime. considered less reliable than
Linux. Many of the sacrifices it
makes in the name of user-
friendliness can lead to security
vulnerabilities and system
instability.

Software There thousands of programs Windows commands the highest


available for Linux, and many number of desktop users, and
are available as easy-to-install therefore the largest selection of
software packages — all for commercial software. It also has
free. Also, many Windows the largest selection of video
programs can be run on Linux games by a wide margin.
using compatibility layers such
as WINE. Linux supports a
wider array of free software
than Windows.

Software Many of the available software Although there are many free
Cost programs, utilities, and games Windows programs, utilities, and
available on Linux are free and games, the majority of the
open source. Even complex programs are commercial.
applications such as GIMP,
OpenOffice, and Star Office are
available for free.

Hardware Fifteen years ago, Linux Windows has a massive user base,
struggled to support new so it would be madness for a
hardware. Manufacturers often consumer hardware
considered Linux support a manufacturer not to support
secondary concern (if they Windows. As a Windows user, you
considered supporting it at all). can rest assured that your
Furthermore, device drivers for operating system is compatible
Linux were created only by with any hardware you might
enthusiasts who devoted their buy.
own time and resources to
making Linux compatible with
new hardware. Since then, the
Linux user base has grown
exponentially. Today, the
majority of hardware
manufacturers give Linux
support the same priority as
Microsoft Windows.

Security Linux is a highly secure Microsoft has made great security


operating system. Although improvements over the years. But
attack vectors are still as the operating system with the
discovered, its source code is largest user base, especially
open and available for any user among novice computer users, it
to review, which makes it easier is the primary target for malicious
coders. As a result, of all major
to identify and repair operating systems, Microsoft
vulnerabilities. Windows is the most likely to be
the victim of viruses and
malware.

Support There is a massive amount of Microsoft Windows offers


online support available for integrated and online help
Linux, including here on systems, and there are thousands
Computer Hope. of informative books about
Windows available for every skill
level.

Use Cases Linux is used by corporate, Microsoft Windows is usually the


scientific, and academic operating system of choice for
organizations of every size. It's gamers, novice users, and
used to power the development business users who rely on
machines and servers at Google, Microsoft software. It doesn't run
Facebook, Twitter, NASA, and well on older hardware, however.
the New York Stock Exchange, Many Windows users are thrilled
just to name a few. On the with the changes that Microsoft
desktop, it is used by has introduced with Windows 10,
technically proficient users who so if you've never used it, now is a
prioritize system security and great time to try it out.
reliability, and by enthusiasts
who want to learn more about
computers and how they work.
It's also used to give new life to
older hardware, enable low-
budget computing projects to
succeed, and serve as the
operating system on single-
board computers such as the
Raspberry Pi.

Potrebbero piacerti anche