OPERATING SYSTEM
BSc IV SEMESTER
Prepared By,
Vinay Kumar V
Assistant Professor
Department of Computer Science
GFGC, Raibag
Operating System
UNIT I:
Introduction: Basics of Operating Systems: Definition, types of Operating
Systems, OS Service, System Calls, OS structure: Layered, Monolithic,
Microkernel Operating Systems – Concept of Virtual Machine.
UNIT II:
Process Management Process Definition , Process Relationship , Process
states , Process State transitions , Process Control Block , Context switching ,
Threads, Concept of multithreads , Benefits of threads, Types of threads.
Process Scheduling: Definition, Scheduling objectives, Types of Schedulers,
CPU scheduling algorithms, performance evaluation of the scheduling.
UNIT III:
Inter-process Communication, Race Conditions, Critical Section, Mutual
Exclusion, Hardware Solution, Strict Alternation, Peterson’s Solution, The
Producer Consumer Problem, Semaphores, Event Counters, Monitors,
Message Passing, and Classical IPC Problems.
Deadlocks: Definition, Deadlock characteristics, Deadlock Prevention,
Deadlock Avoidance (concepts only).
UNIT IV:
Memory Management: Logical and Physical address map, Memory
allocation, Internal and External fragmentation and Compaction, Paging.
Virtual Memory: Demand paging, Page Replacement policies.
UNIT V:
I/O Management Principles of I/O Hardware: Disk structure, Disk
scheduling algorithm
File Management: Access methods, File types, File operation, Directory
structure, File System structure, Allocation methods, Free-space
management, and directory implementation. Structure of Linux Operating
System, Exploring the Directory Structure, Naming Files and Directories,
Concept of shell, Types of shell, Editors for shell programming (e.g. vi),
basics of Shell programming.
UNIT I
Introduction: Basics of Operating Systems: Definition, types of Operating
Systems, OS Service, System Calls, OS structure: Layered, Monolithic,
Microkernel Operating Systems – Concept of Virtual Machine.
Introduction to Operating System:
An operating system is software that manages the basic resources of a
computer i.e Processor, Memory, Files, Peripheral devices, disk storage
units etc…
It provides an environment in which user can execute programs in a
convenient and efficient manner.
Purpose of OS is to transfer control automatically from one job to next
job.
OS performs the job of allocating resources for various tasks such as
program execution, file creation, command execution etc. and reclaims
the allocated resources after their use.
OS allocates Main Memory (MM) for the running program to the CPU,
Schedules them and manage the data transfer to and from peripheral
(I/O) devices to MM and Secondary storage (disks) units.
User Views:-
The user view of the computer depends on the interface used.
Some users may use PC’s. In this the system is designed so that only
one user can utilize the resources and mostly for ease of use where the
attention is mainly on performances and not on the resource
utilization.
Some users may use a terminal connected to a mainframe or
minicomputers.
Other users may access the same computer through other terminals.
These users may share resources and exchange information. In this
case the OS is designed to maximize resource utilization-so that all
available CPU time, memory & I/O are used efficiently.
Other users may sit at workstations, connected to the networks of
other workstation and servers. In this case OS is designed to
compromise between individual visibility & resource utilization.
System Views:
We can view system as resource allocator i.e. a computer system has
many resources that may be used to solve a problem. The OS acts as a
manager of these resources. The OS must decide how to allocate these
resources to programs and the users so that it can operate the
Computer system efficiently and fairly.
A different view of an OS is that it need to control various I/O devices &
user programs i.e. an OS is a control program used to manage the
execution of user program to prevent errors and improper use of the
computer.
Resources can be either CPU Time, memory space, file storage space,
I/O devices and so on. The OS must support the following tasks
Types of Operating Systems
1. Batch Operating System.
2. Multiprogrammed Operating System
3. Time-Sharing Operating Systems
4. Parallel Systems
5. Distributed Operating System
6. Real-Time Operating System
7. Network Operating System
1. Batch Operating System:
This type of operating system does not interact with the computer
directly. There is an operator which takes similar jobs having same
requirement and group them into batches. It is the responsibility of
operator to sort the jobs with similar needs.
Human operators would organize jobs into batches
Early computers where physically large machines.
The common I/P devices are card readers & tape drives.
The common O/P devices are line printers, tape drives & card punches.
The job was mainly in the form punched cards.
At later time the O/P appeared and it consisted of result along with dump
of memory and register content for debugging.
The OS of these computers was very simple.
Its major task was to transfer control from one job to the next.
The OS was always resident in the memory.
The processing of job was very slow. To improve the processing speed
operators batched together the jobs with similar needs and processed it
through the computers. This is called Batch Systems.
Drawback of First OS
The CPU may be idle for some time because the speed of the mechanical
devices slower compared to the electronic devices.
In order to eliminate this drawback, a batch OS was used to perform the task
of batching jobs.
Advantages:
Simple, Sequential job Scheduling.
Human interventions minimized.
Increased performance & System throughput due to batching of jobs.
Disadvantages:
Turn around time can be large from user point of view due to batching.
Difficult to debug the program.
A job can enter into infinite loop.
A job could corrupt the monitor.
Due to lack of protection scheme, one job may affect the pending jobs.
Example: Payroll System, Bank Statements etc
Note: Turn around time means time elapsed between the time of submission of a
process or job by a user and the time of completion of that process or job.
Spooling:
SPOOL(Simultaneous Peripheral Operation On-Line)
Spooling is a process in which data is temporarily held to be used and
executed by a device, program or the system.
Data is sent to and stored in memory of other volatile (Temporary
memory) storage until the program or computer requests it for
execution.
2. Multiprogramming Operating Systems
Multiprogramming concept increases CPU utilization by organization
jobs so that the CPU always has one job to execute.
The operating system keeps several jobs in memory simultaneously as
shown in below figure.
Memory Layout for Multiprogrammed System
This set of job is subset of the jobs kept in the job pool. The operating
system picks and begins to execute one of the jobs in the memory.
When a job needs to wait the CPU is simply switched to another job
and so on.
Functions:
The multiprogramming operating system is sophisticated because the
operating system makes decisions for the user. This is known as Job
scheduling.
If several jobs are ready to run at the same time the system choose one
among them. This is known as CPU scheduling.
Other functions are Memory management, Device and File
Management.
Advantages of Multiprogramming System:
Effective Resource Utilization (CPU, Memory, Peripheral devices).
Elimination or Minimizing of the waste CPU idle time.
Increased Throughput (The number of jobs executed against the
number of jobs submitted for execution in a given interval of time).
The Disadvantages of Multiprogramming System:
It does not provide user interaction with the computer system during
the program execution.
The introduction of disk technology solved these problems rather than
reading the cards from card reader into disk. This form of processing is
known as spooling.
Complex and quite expensive.
3. Time-Sharing Operating Systems
The time sharing system is also known as multi user systems.
The CPU executes multiple jobs by switching among them but the
switches occurs so frequently that the user can interact with each
program while it is running.
An interactive computer system provides direct communication
between a user and system.
The user gives instruction to the operating systems or to a program
directly using keyboard or mouse and wait for immediate results. So
the response time will be short.
The time sharing system allows many users to share the computer
simultaneously. Since each action in this system is short, only a little
CPU time is needed for each user.
The system switches rapidly from one user to the next so each user
feels as if the entire computer system is dedicated to his use, even
though it is being shared by many users.
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.
Advantages of time sharing system:
Effective sharing and utilization of computer resources.
Quick response time to many users.
CPU idle time is eliminated completely.
Suitable for on-line Data processing and user conversations.
Disadvantages of time sharing system:
It is more complex than multiprogrammed operating system.
The system must have memory management & protection, since
several jobs are kept in memory at the same time.
Time sharing system must also provide a file system, so disk
management is required.
It provides mechanism for concurrent execution which requires
complex CPU scheduling schemes.
Example: Multics, UNIX etc.
4. Multiprocessor/Parallel/Tightly coupled Systems
These Systems have more than one processor in close communications
which share the computer bus, clock, memory & peripheral devices. Ex:
UNIX, LINUX.
Multiprocessor Systems have 3 main advantages.
o Increased throughput: Number of processes computed per unit
time. By increasing the number of processors move work can be
done in less time. The speed up ratio with N processors is not N,
but it is less than N. Because a certain amount of overhead is
incurred in keeping all the parts working correctly.
o Increased Reliability: If functions can be properly distributed
among several processors, then the failure of one processor will
not halt the system, but slow it down. This ability to continue to
operate in spite of failure makes the system fault tolerant.
o Economic scale: Multiprocessor systems can save money as they
can share peripherals, storage & power supplies.
The various types of multiprocessing systems are:
Symmetric Multiprocessing (SMP): Each processor runs an
identical copy of the operating system & these copies communicate
with one another as required. Ex: Encore’s version of UNIX for multi
max computer. Virtually, all modern operating system including
Windows NT, Solaris, Digital UNIX, OS/2 & LINUX now provide
support for SMP.
Asymmetric Multiprocessing (Master – Slave Processors): Each
processor is designed for a specific task. A master processor controls
the system & schedules & allocates the work to the slave processors.
Ex- Sun’s Operating system SUNOS version 4 provides asymmetric
multiprocessing
5. Distributed/Loosely Coupled System
In contrast to tightly coupled systems, the processors do not share
memory or a clock. Instead, each processor has its own local memory.
The processors communicate with each other by various
communication lines such as high speed buses or telephone lines.
Distributed systems depend on networking for their functionalities. By
being able to communicate distributed systems are able to share
computational tasks and provide a rich set of features to the users.
Networks vary by the protocols used, the distances between the nodes
and transport media.
TCP/IP is the most common network protocol.
The processor in a distributed system varies in size and function. It
may be microprocessors, work stations, minicomputer, and large
general purpose computers.
Network types are based on the distance between the nodes such as
LAN (within a room, floor or building) and WAN (between buildings,
cities or countries).
The advantages of distributed system are:
Resource sharing.
Computations speed up.
Load sharing/Load Balancing.
Reliability.
Communication.
Disadvantages of Distributed Operating System:
Failure of the main network will stop the entire communication.
To establish distributed systems the language which are used are not
well defined yet.
These types of systems are not readily available as they are very
expensive. Not only that the underlying software is highly complex and
not understood well yet
Examples- LOCUS etc
6. Real-Time Operating System
Real time system is used when there are rigid time requirements on
the operation of a processor or flow of data. Sensors bring data to the
computers. The computer analyzes data and adjusts controls to modify
the sensors inputs.
System that controls scientific experiments, medical imaging systems
and some display systems are real time systems.
The disadvantages of real time system are:
A real time system is considered to function correctly only if it returns
the correct result within the time constraints.
Secondary storage is limited or missing instead data is usually stored in
short term memory or ROM.
Advanced OS features are absent.
Real time system is of two types such as:
1. Hard real time systems:
It guarantees that the critical task has been completed on time.
The sudden task is takes place at a sudden instant of time.
2. Soft real time systems:
It is a less restrictive type of real time system where a critical task
gets priority over other tasks and retains that priority until it
computes.
These have more limited utility than hard real time systems.
Missing an occasional deadline is acceptable
Example: QNX, VX works. Digital audio or multimedia is included in this
category.
It is a special purpose OS in which there are rigid time requirements on
the operation of a processor.
A real time OS has well defined fixed time constraints.
Processing must be done within the time constraint or the system will
fail.
A real time system is said to function correctly only if it returns the
correct result within the time constraint.
These systems are characterized by having time as a key parameter.
Advantages of RTOS:
Maximum Consumption: Maximum utilization of devices and system,
thus more output from all the resources
Task Shifting: Time assigned for shifting tasks in these systems are
very less. For example in older systems it takes about 10 micro seconds
in shifting one task to another and in latest systems it takes 3 micro
seconds.
Focus on Application: Focus on running applications and less
importance to applications which are in queue.
Real time operating system in embedded system: Since sizes of
programs are small, RTOS can also be used in embedded systems like in
transport and others.
Error Free: These types of systems are error free.
Memory Allocation: Memory allocation is best managed in these type
of systems.
Disadvantages of RTOS:
Limited Tasks: Very few tasks run at the same time and their
concentration is very less on few applications to avoid errors.
Use heavy system resources: Sometimes the system resources are
not so good and they are expensive as well.
Complex Algorithms: The algorithms are very complex and difficult
for the designer to write on.
Device driver and interrupt signals: It needs specific device drivers
and interrupts signals to response earliest to interrupts.
Thread Priority: It is not good to set thread priority as these systems
are very less pron to switching tasks.
Examples of Real-Time Operating Systems are: Scientific experiments,
medical imaging systems, industrial control systems, weapon systems,
robots, air traffic control systems, etc.
7. Network Operating System
These systems run on a server and provide the capability to manage
data, users, groups, security, applications, and other networking
functions.
This type of operating systems allows shared access of files, printers,
security, applications, and other networking functions over a small
private network.
One more important aspect of Network Operating Systems is that all
the users are well aware of the underlying configuration, of all other
users within the network, their individual connections etc. and that’s
why these computers are popularly known as tightly coupled
systems.
Advantages of Network Operating System:
Highly stable centralized servers
Security concerns are handled through servers
New technologies and hardware up-gradation are easily integrated to
the system
Server access are possible remotely from different locations and types
of systems
Disadvantages of Network Operating System:
Servers are costly
User has to depend on central location for most operations
Maintenance and updates are required regularly
Examples of Network Operating System are: Microsoft Windows Server
2003, Microsoft Windows Server 2008, UNIX, Linux, Mac OS X, Novell
NetWare, and BSD etc.
Operating System Services
An operating system provides an environment for the execution of the
program. It provides some services to the programs.
The services provided by one OS may be different from other OS.
OS makes the programming task easier.
The various services provided by an operating system are as follows:
Program Execution:
The system must be able to load a program into main memory
partitions and to run that program.
The program must be able to terminate this execution normally for
successful execution or abnormally for displaying errors.
I/O Operation:
Accomplish the task of Device allocation and control I/O devices.
Provide for notifying device errors, device status, etc.
File System Manipulation:
Accomplish the task of Opening a File, Closing a File.
The programs need to create and delete files by name
Allows file manipulations such as Reading a file, Writing a File,
Appending a File.
Communication:
Accomplish the task of inter-process communication either on the
same computer system or between different computer systems on a
computer network.
Provide for Message passing and shared memory access in safe
mode.
Error detection:
The operating system should take the appropriate actions for the
occurrences of any type like arithmetic overflow; divide by zero
error, access to the illegal memory location and too large user CPU
time.
Accomplish the task of error detection and recovery if any. Ex: Paper
jam or out of paper in a printer.
Keep track status of CPU, Memory, I/O devices, Storage devices, File
system, networking, etc.
Abort execution in case of fatal errors such as RAM parity errors,
power fluctuations, if any.
Resource Allocation:
Accomplish the task of resource allocation to multiple jobs.
Reclaim the allocated resources after their use or as and when the
job terminates.
When multiple users are logged on to the system the resources must
be allocated to each of them.
For current distribution of the resource among the various
processes the operating system uses the CPU scheduling run times
which determine which process will be allocated with the resource.
Accounting:
The operating system keep track of which users use how many and
which kind of computer resources.
Maintain logs of system activities for performance analysis and error
recovery.
Protection:
Accomplish the task of protecting the system resources against
malicious use.
Provide for safe computing by employing security scheme against
unauthorized access/users.
Authenticate legitimate users with login passwords and
registrations.
The operating system is responsible for both hardware as well as
software protection.
The operating system protects the information stored in a multiuser
computer system.
System Calls
System calls provide the interface between a process & the OS. These
are usually available in the form of assembly language instruction.
Some systems allow system calls to be made directly from a high level
language program like C, BCPL and PERL etc.
System calls occur in different ways depending on the computer in use.
System calls can be roughly grouped into 5 major categories.
1. Process Management and Control:
End, abort: A running program needs to be able to has its execution
either normally (end) or abnormally (abort).
Load, execute: A process or job executing one program may want to
load and executes another program.
Create Process, terminate process: There is a system call specifying
for the purpose of creating a new process or job (create process or
submit job). We may want to terminate a job or process that we
created (terminates process, if we find that it is incorrect or no longer
needed).
Get process attributes, set process attributes: If we create a new job
or process we should able to control its execution. This control
requires the ability to determine & reset the attributes of a job or
processes (get process attributes, set process attributes).
Wait time: After creating new jobs or processes, we may need to wait
for them to finish their execution (wait time).
Wait event, signal event: We may wait for a specific event to occur
(wait event). The jobs or processes then signal when that event has
occurred (signal event).
Allocate, Release Memory.
2. File Management/File Manipulation/File Handling:
Create file, delete file: We first need to be able to create & delete files.
Both the system calls require the name of the file & some of its
attributes.
Open file, close file: Once the file is created, we need to open it & use
it. We close the file when we are no longer using it.
Read, write, reposition file: After opening, we may also read, write or
reposition the file (rewind or skip to the end of the file).
Get file attributes, set file attributes: For either files or directories,
we need to be able to determine the values of various attributes & reset
them if necessary. Two system calls get file attribute & set file
attributes are required for their purpose.
3. Device Management:
Request device, release device: If there are multiple users of the
system, we first request the device. After we finished with the device,
we must release it.
Read, write, reposition: Once the device has been requested &
allocated to us, we can read, write & reposition the device.
4. Information Maintenance/Management:
Get time or date, set time or date: Most systems have a system call to
return the current date & time or set the current date & time.
Get system data, set system data: Other system calls may return
information about the system like number of current users, version
number of OS, amount of free memory etc.
Get process attributes, set process attributes: The OS keeps
information about all its processes & there are system calls to access
this information.
5. Communication Management:
Create, Delete Connection.
Send, Receive Message.
Attach, Detach remote device(Mounting/Remote Login)
Transfer status information(byte)
There are two modes of communication such as:
Message passing model:
Information is exchanged through an inter process communication
facility provided by operating system.
Each computer in a network has a name by which it is known.
Similarly, each process has a process name which is translated to an
equivalent identifier by which the OS can refer to it.
The get host id and get processed systems calls to do this translation.
These identifiers are then passed to the general purpose open &
close calls provided by the file system or to specific open connection
system call.
The recipient process must give its permission for communication to
take place with an accept connection call.
The source of the communication known as client & receiver known
as server exchange messages by read message & write message
system calls.
The close connection call terminates the connection.
Shared memory model:
Processes use map memory system calls to access regions of
memory owned by other processes.
They exchange information by reading & writing data in the shared
areas.
The processes ensure that they are not writing to the same location
simultaneously.
OPERATING SYSTEM STRUCTURES
The design of operating system architecture traditionally follows
the separation of concerns principle. This principle suggests structuring the
operating system into relatively independent parts that provide simple
individual features, thus keeping the complexity of the design manageable.
1. Simple Structure
In MS-DOS, applications may bypass the operating system.
There are several commercial system that don’t have a well- defined
structure such operating systems begins as small, simple & limited
systems and then grow beyond their original scope.
MS-DOS is an example of such system. It was not divided into modules
carefully.
Another example of limited structuring is the UNIX operating system.
There was no CPU Execution Mode (user and kernel), so errors in
applications could cause the whole system to crash.
2. Monolithic Approach
A monolithic design of the operating system architecture makes no
special accommodation for the special nature of the operating system.
Although the design follows the separation of concerns, no attempt is
made to restrict the privileges granted to the individual parts of the
operating system.
The entire operating system executes with maximum privileges.
The communication overhead inside the monolithic operating system
is the same as the communication overhead inside any other software,
considered relatively low.
CP/M and DOS are simple examples of monolithic operating systems.
Both CP/M and DOS are operating systems that share a single address
space with the applications. In CP/M, the 16 bit address space starts with
system variables and the application area and ends with three parts of the
operating system, namely CCP (Console Command Processor), BDOS
(Basic Disk Operating System) and BIOS (Basic Input/Output System). In
DOS, the 20 bit address space starts with the array of interrupt vectors
and the system variables, followed by the resident part of DOS and the
application area and ending with a memory block used by the video card
and BIOS.
Simple Monolithic Operating Systems Example
3. Layered approach:
In the layered approach, the OS is broken into a number of layers
(levels) each built on top of lower layers. The bottom layer (layer 0) is
the hardware & top most layer (layer N) is the user interface.
The main advantage of the layered approach is modularity.
The layers are selected such that each users functions (or operations)
& services of only lower layer.
This approach simplifies debugging & system verification, i.e. the first
layer can be debugged without concerning the rest of the system. Once
the first layer is debugged, its correct functioning is assumed while the
2nd layer is debugged & so on.
If an error is found during the debugging of a particular layer, the error
must be on that layer because the layers below it are already debugged.
Thus the design & implementation of the system are simplified when
the system is broken down into layers.
Each layer is implemented using only operations provided by lower
layers. A layer doesn’t need to know how these operations are
implemented; it only needs to know what these operations do.
The layer approach was first used in the operating system. It was
defined in six layers.
The main disadvantage of the layered approach is:
The main difficulty with this approach involves the careful definition of
the layers, because a layer can use only those layers below it. For
example, the device driver for the disk space used by virtual memory
algorithm must be at a level lower than that of the memory
management routines, because memory management requires the
ability to use the disk space.
It is less efficient than a non layered system (Each layer adds overhead
to the system call & the net result is a system call that take longer time
than on a non layered system).
4. Microkernel approach:
This structures the operating system by removing all nonessential portions
of the kernel and implementing them as system and user level programs.
Generally they provide minimal process and memory
management, and a communications facility.
Communication between components of the OS is provided by
message passing.
The benefits of the microkernel are as follows:
Extending the operating system becomes much easier.
Any changes to the kernel tend to be fewer, since the kernel is
smaller.
The microkernel also provides more security and reliability.
Main disadvantage is poor performance due to increased system overhead
from message passing.
Virtual Machines
A virtual machine takes the layered approach to its logical conclusion.
It treats hardware and the operating system kernel as though they
were all hardware.
A virtual machine provides an interface identical to the underlying bare
hardware.
The operating system creates the illusion of multiple processes, each
executing on its own processor with its own (virtual) memory.
The resources of the physical computer are shared to create the virtual
machines.
CPU scheduling can create the appearance that users have their own
processor.
Spooling and a file system can provide virtual card readers and virtual
line printers.
A normal user time-sharing terminal serves as the virtual machine
operator’s console.
Advantages and Disadvantages of Virtual Machines:
The virtual-machine concept provides complete protection of system
resources since each virtual machine is isolated from all other virtual
machines. This isolation, however, permits no direct sharing of
resources.
A virtual-machine system is a perfect vehicle for operating-systems
research and development. System development is done on the virtual
machine, instead of on a physical machine and so does not disrupt
normal system operation.
The virtual machine concept is difficult to implement due to the effort
required to provide an exact duplicate to the underlying machine.
Drawback:
Virtual Machine includes the increased system overhead which is due
to simulation of virtual machine operation heavily.
The efficiency of VM OS depends upon the number of operations that
must be simulated by VM monitor.
UNIT II
Process Management Process Definition , Process Relationship ,
Process states , Process State transitions , Process Control Block ,
Context switching , Threads, Concept of multithreads , Benefits of
threads, Types of threads.
Process Scheduling: Definition, Scheduling objectives, Types of
Schedulers, CPU scheduling algorithms, performance evaluation of the
scheduling.
Process Management:
A program does nothing unless their instructions are executed by a
CPU.A process is a program in execution. A time shared user program
such as a complier is a process. A word processing program being run
by an individual user on a pc is a process.
A system task such as sending output to a printer is also a process. A
process needs certain resources including CPU time, memory files &
I/O devices to accomplish its task.
These resources are either given to the process when it is created or
allocated to it while it is running.
The OS is responsible for the following activities of process
management.
o Creating & deleting both user & system processes.
o Suspending & resuming processes.
o Providing mechanism for process synchronization.
o Providing mechanism for process communication.
o Providing mechanism for deadlock handling.
Process:
A process or task is an instance of a program in execution.
The execution of a process must programs in a sequential manner.
At any time at most one instruction is executed.
The process includes the current activity as represented by the value of
the program counter and the content of the processors registers. Also it
includes the process stack which contain temporary data (such as
method parameters return address and local variables) & a data
section which contain global variables. A process may also include a
heap, which is memory that is dynamically allocated during process
run time.
Difference between process & program:
A program by itself is not a process.
A program in execution is known as a process.
A program is a passive entity, such as the contents of a file stored on
disk whereas process is an active entity with a program counter
specifying the next instruction to execute and a set of associated
resources may be shared among several process with some scheduling
algorithm being used to determinate when the stop work on one
process and service a different one.
Process Relationship:
Process States
As a process executes, it changes state. The state of a process is defined by
the correct activity of that process. Each process may be in one of the
following states.
New: The process is being created.
Ready: The process is waiting to be assigned to a processor.
Running: Instructions are being executed.
Waiting: The process is waiting for some event to occur.
Terminated: The process has finished execution.
Process State transitions
Many processes may be in ready and waiting state at the same time.
But only one process can be running on any processor at any instant.
Process Control Block
Each process is represented in the OS by a process control block. It is
also by a process control block.
It is also known as task control block.
A process control block contains many pieces of information associated
with a specific process.
It includes the following information’s.
o Process state: The state may be new, ready, running, waiting or
terminated state.
o Program counter: It indicates the address of the next instruction
to be executed for this purpose.
o CPU registers: The registers vary in number & type depending on
the computer architecture. It includes accumulators, index
registers, stack pointer & general purpose registers, plus any
condition- code information must be saved when an interrupt
occurs to allow the process to be continued correctly after- ward.
o CPU scheduling information: This information includes process
priority pointers to scheduling queues & any other scheduling
parameters.
o Memory management information: This information may
include such information as the value of the bar & limit registers,
the page tables or the segment tables, depending upon the
memory system used by the operating system.
o Accounting information: This information includes the amount
of CPU and real time used, time limits, account number, job or
process numbers and so on.
o I/O Status Information: This information includes the list of I/O
devices allocated to this process, a list of open files and so on. The
PCB simply serves as the repository for any information that may
vary from process to process.
Context Switching
1. When CPU switches to another process, the system must save the state
of the old process and load the saved state for the new process. This is
known as context switch.
2. Context-switch time is overhead; the system does no useful work while
switching.
3. Switching speed varies from machine to machine, depending on the
memory speed, the number of registers that must be copied, and the
existence of special instructions. A typical speed is a few milliseconds.
4. Context switch times are highly dependent on hardware support.
Threads
A thread is a flow of execution through the process code, with its own
program counter, system registers and stack.
Threads are a popular way to improve application performance
through parallelism.
A thread is sometimes called a light weight process.
Threads represent a software approach to improving performance of
operating system by reducing the over head thread is equivalent to a
classical process.
Each thread belongs to exactly one process and no thread can exist
outside a process.
Each thread represents a separate flow of control.
Threads have been successfully used in implementing network servers. They
also provide a suitable foundation for parallel execution of applications on
shared memory multiprocessors.
Concept of Multithreads
Some operating systems provide a combined user level thread and
Kernel level thread facility.
Solaris is a good example of this combined approach. In a combined
system, multiple threads within the same application can run in
parallel on multiple processors and a blocking system call need not
block the entire process.
Multithreading models are three types:
1. Many to many relationship.
2. Many to one relationship.
3. One to one relationship.
1. Many to Many Model
In this model, many user level threads multiplexes to the Kernel thread
of smaller or equal numbers.
The number of Kernel threads may be specific to either a particular
application or a particular machine.
In this model, developers can create as many user threads as necessary and
the corresponding Kernel threads can run in parallels on a multiprocessor.
2. Many to One Model
Many to one model maps many user level threads to one Kernel level
thread.
Thread management is done in user space. When thread makes a
blocking system call, the entire process will be blocks.
Only one thread can access the Kernel at a time, so multiple threads are
unable to run in parallel on multiprocessors.
If the user level thread libraries are implemented in the operating system,
that system does not support Kernel threads use the many to one
relationship modes.
3. One to One Model
There is one to one relationship of user level thread to the kernel level
thread.
This model provides more concurrency than the many to one model.
It allows another thread to run when a thread makes a blocking system call.
It supports multiple threads to execute in parallel on microprocessors.
Disadvantage of this model is that creating user thread requires the
corresponding Kernel thread. OS/2, Windows NT and windows 2000 use
one to one relationship model.
Benefits of Threads
The benefits of multithreaded programming can be broken down into four
major categories:
1. Responsiveness. Multithreading an interactive application may allow a
program to continue running even if part of it is blocked or is performing a
lengthy operation, thereby increasing responsiveness to the user. This
quality is especially useful in designing user interfaces. For instance,
consider what happens when a user clicks a button that results in the
performance of a time-consuming operation. A single-threaded application
would be unresponsive to the user until the operation had completed. In
contrast, if the time-consuming operation is performed in a separate thread,
the application remains responsive to the user.
2. Resource sharing. Processes can only share resources through
techniques such as shared memory and message passing. Such techniques
must be explicitly arranged by the programmer. However, threads share the
memory and the resources of the process to which they belong by default.
The benefit of sharing code and data is that it allows an application to have
several different threads of activity within the same address space.
3. Economy. Allocating memory and resources for process creation is
costly. Because threads share the resources of the process to which they
belong, it is more economical to create and context-switch threads.
Empirically gauging the difference in overhead can be difficult, but in
general it is significantly more time consuming to create and manage
processes than threads. In Solaris, for example, creating a process is about
thirty time’s slower than is creating a thread, and context switching is about
five times slower.
4. Scalability. The benefits of multithreading can be even greater in a
multiprocessor architecture, where threads may be running in parallel on
different processing cores. A single-threaded process can run on only one
processor, regardless how many are available. We explore this issue further
in the following section.
Types of Threads
Thread is implemented in two ways:
1. User Level
2. Kernel Level
1. User Level Thread
In a user thread, all of the work of thread management is done by the
application and the kernel is not aware of the existence of threads.
The thread library contains code for creating and destroying threads,
for passing message and data between threads, for scheduling thread
execution and for saving and restoring thread contexts.
The application begins with a single thread and begins running in that
thread.
User level threads are generally fast to create and manage.
Advantage of user level thread over Kernel level thread:
Thread switching does not require Kernel mode privileges.
User level thread can run on any operating system.
Scheduling can be application specific.
User level threads are fast to create and manage.
Disadvantages of user level thread:
In a typical operating system, most system calls are blocking.
Multithreaded application cannot take advantage of multiprocessing.
2. Kernel Level Threads
In Kernel level thread, thread management done by the Kernel.
There is no thread management code in the application area.
Kernel threads are supported directly by the operating system.
Any application can be programmed to be multithreaded. All of the
threads within an application are supported within a single process.
The Kernel maintains context information for the process as a whole
and for individual threads within the process.
Scheduling by the Kernel is done on a thread basis.
The Kernel performs thread creation, scheduling and management in
Kernel space.
Kernel threads are generally slower to create and manage than the
user threads.
Advantages of Kernel level thread:
Kernel can simultaneously schedule multiple threads from the same
process on multiple processes.
If one thread in a process is blocked, the Kernel can schedule another
thread of the same process.
Kernel routines themselves can multithreaded.
Disadvantages:
Kernel threads are generally slower to create and manage than the
user threads.
Transfer of control from one thread to another within same process
requires a mode switch to the Kernel.
Benefits/Advantages of Thread
Thread minimizes context switching time.
Use of threads provides concurrency within a process.
Efficient communication.
Economy- It is more economical to create and context switch threads.
Utilization of multiprocessor architectures –
The benefits of multithreading can be greatly increased in a multiprocessor
architecture.
Difference between User Level & Kernel Level Thread
User level threads are faster to Kernel level threads are slower to
1 create and manage create and manage.
Implemented by a thread library at Operating system support directly
2 the user level to Kernel threads
User level thread can run on any Kernel level threads are specific to
3 operating system the operating system
Support provided at the user level Support may be provided by kernel
4 called user level thread is called Kernel level threads
Multithread application cannot Kernel routines themselves can be
5 take advantage of multiprocessing multithreaded
Difference between Process and Thread
1 Process is called heavy weight Thread is called light weight
process Process
2 Process switching needs interface Thread switching does not need to
with operating system call a operating system and cause an
interrupt to the Kernel
3 In multiple process All threads can share same set of
implementation each process open files, child processes
executes the same code but has its
own memory and file resources
4 If one server process is blocked no While one server thread is blocked
other server process can execute and waiting, second thread in the
until the first process unblocked same task could run
5 Multiple redundant process uses Multiple threaded process uses
more resources than multiple fewer resources than multiple
threaded redundant process
6 In multiple process each process One thread can read, write or even
operates independently of the completely wipe out another
others threads stack
Process Scheduling:
Scheduling is a fundamental function of OS. When a computer is
Multiprogrammed, it has multiple processes competing for the CPU at the
same time. If only one CPU is available, then a choice has to be made
regarding which process to execute next. This decision making process is
known as scheduling and the part of the OS that makes this choice is called
a scheduler. The algorithm it uses in making this choice is called
scheduling algorithm.
Scheduling queues: As processes enter the system, they are put into a job
queue. This queue consists of all process in the system. The process that are
residing in main memory and are ready & waiting to execute or kept on a list
called ready queue.
This queue is generally stored as a linked list. A ready queue header contains
pointers to the first & final PCB in the list. The PCB includes a pointer field
that points to the next PCB in the ready queue. The lists of processes waiting
for a particular I/O device are kept on a list called device queue. Each device
has its own device queue. A new process is initially put in the ready queue. It
waits in the ready queue until it is selected for execution & is given the CPU.
SCHEDULERS:
A process migrates between the various scheduling queues throughout
its life-time purposes. The OS must select for scheduling processes
from these queues in some fashion. This selection process is carried
out by the appropriate scheduler.
In a batch system, more processes are submitted and then executed
immediately. So these processes are spooled to a mass storage device
like disk, where they are kept for later execution.
Types of schedulers:
There are 3 types of schedulers mainly used:
1. Long term scheduler:
Long term scheduler selects process from the disk & loads them into
memory for execution.
It controls the degree of multi-programming i.e. no. of processes in
memory.
It executes less frequently than other schedulers.
If the degree of multiprogramming is stable than the average rate of
process creation is equal to the average departure rate of processes
leaving the system.
So, the long term scheduler is needed to be invoked only when a
process leaves the system.
Due to longer intervals between executions it can afford to take more
time to decide which process should be selected for execution.
Most processes in the CPU are either I/O bound or CPU bound.
An I/O bound process (an interactive ‘C’ program) is one that spends
most of its time in I/O operation than it spends in doing I/O operation.
A CPU bound process is one that spends more of its time in doing
computations than I/O operations (complex sorting program).
It is important that the long term scheduler should select a good mix of
I/O bound & CPU bound processes.
2. Short - term scheduler:
The short term scheduler selects among the process that are ready to
execute & allocates the CPU to one of them.
The primary distinction between these two schedulers is the frequency
of their execution.
The short-term scheduler must select a new process for the CPU quite
frequently.
It must execute at least one in 100ms. Due to the short duration of time
between executions, it must be very fast.
3. Medium - term scheduler:
Some operating systems introduce an additional intermediate level of
scheduling known as medium - term scheduler.
The main idea behind this scheduler is that sometimes it is
advantageous to remove processes from memory & thus reduce the
degree of multiprogramming.
At some later time, the process can be reintroduced into memory & its
execution can be continued from where it had left off. This is called as
swapping.
The process is swapped out & swapped in later by medium term
scheduler.
Swapping is necessary to improve the process miss or due to some
change in memory requirements, the available memory limit is
exceeded which requires some memory to be freed up.
Scheduling objectives:
1. CPU utilization – keep the CPU as busy as possible
2. Throughput – Number of processes that complete their execution per
time unit
3. Turnaround time – amount of time to execute a particular process
4. Waiting time – amount of time a process has been waiting in the ready
queue
5. Response time – amount of time it takes from when a request was
submitted until the first response is produced, not output (for time-
sharing environment)
General Goals
Fairness
Fairness is important under all circumstances. A scheduler makes sure
that each process gets its fair share of the CPU and no process can
suffer indefinite postponement.
Note that giving equivalent or equal time is not fair. Think of safety
control and payroll at a nuclear plant.
Policy Enforcement
The scheduler has to make sure that system's policy is enforced. For
example, if the local policy is safety then the safety control processes
must be able to run whenever they want to, even if it means delay in
payroll processes.
Efficiency
Scheduler should keep the system (or in particular CPU) busy cent
percent of the time when possible. If the CPU and all the Input/Output
devices can be kept running all the time, more work gets done per
second than if some components are idle.
Response Time
A scheduler should minimize the response time for interactive user.
Turnaround
A scheduler should minimize the time batch users must wait for an
output.
Throughput
A scheduler should maximize the number of jobs processed per unit
time.
A little thought will show that some of these goals are contradictory. It
can be shown that any scheduling algorithm that favors some class of
jobs hurts another class of jobs. The amount of CPU time available is
finite, after all.
Preemptive Vs Non-preemptive Scheduling
The Scheduling algorithms can be divided into two categories with respect
to how they deal with clock interrupts.
Non-preemptive Scheduling
A scheduling discipline is non-preemptive if, once a process has been given
the CPU, the CPU cannot be taken away from that process.
Following are some characteristics of non-preemptive scheduling
In non-preemptive system, short jobs are made to wait by longer jobs
but the overall treatment of all processes is fair.
In non-preemptive system, response times are more predictable
because incoming high priority jobs cannot displace waiting jobs.
In non-preemptive scheduling, a scheduler executes jobs in the
following two situations.
o When a process switches from running state to the waiting state.
o When a process terminates.
Preemptive Scheduling
A scheduling discipline is preemptive if, once a process has been given
the CPU can taken away.
The strategy of allowing processes that are logically runnable to be
temporarily suspended is called Preemptive Scheduling and it is
contrast to the "run to completion" method.
CPU scheduling algorithms:
CPU Scheduling deals with the problem of deciding which of the processes in
the ready queue is to be allocated the CPU.
Following are some scheduling algorithms we will study.
1. FCFS Scheduling.
2. Round Robin Scheduling.
3. SJF Scheduling.
4. SRT Scheduling.
5. Priority Scheduling.
6. Multilevel Queue Scheduling.
7. Multilevel Feedback Queue Scheduling.
1. First Come, First Served Scheduling (FCFS) Algorithm:
This is the simplest CPU scheduling algorithm. In this scheme, the
process which requests the CPU first, that is allocated to the CPU first.
The implementation of the FCFS algorithm is easily managed with a
FIFO queue.
When a process enters the ready queue its PCB is linked onto the rear
of the queue. The average waiting time under FCFS policy is quiet long.
Consider the following example:
Process CPU time
P1 3
P2 5
P3 2
P4 4
Using FCFS algorithm find the average waiting time and average turnaround
time if the order is P1, P2, P3, P4.
Solution: If the process arrived in the order P1, P2, P3, P4 then according to
the FCFS the Gantt chart will be:
P1 P2 P3 P4
0 3 8 10 14
Waiting time for process P1 = 0, P2 = 3, P3 = 8, P4 = 10
Turnaround time for process P1 = 0 + 3 = 3, P2 = 3 + 5 = 8,
P3 = 8 + 2 = 10, P4 = 10 + 4 =14.
Average waiting time = (0 + 3 + 8 + 10)/4
= 21/4
= 5.25
Average Turnaround time = (3 + 8 + 10 + 14)/4
= 35/4
= 8.75
The FCFS algorithm is non preemptive means once the CPU has been
allocated to a process then the process keeps the CPU until the release the
CPU either by terminating or requesting I/O.
2. Shortest Job First Scheduling (SJF) Algorithm:
Other name of this algorithm is Shortest-Process-Next (SPN).
This algorithm associates with each process if the CPU is available.
This scheduling is also known as shortest next CPU burst, because the
scheduling is done by examining the length of the next CPU burst of the
process rather than its total length.
Consider the following example:
Process CPU time
P1 3
P2 5
P3 2
P4 4
Solution: According to the SJF the Gantt chart will be
P3 P1 P2 P4
0 2 5 9 14
Waiting time for process P1 = 0, P2 = 2, P3 = 5, P4 = 9
Turnaround time for process P3 = 0 + 2 = 2, P1 = 2 + 3 = 5,
P4 = 5 + 4 = 9, P2 = 9 + 5 =14.
Average waiting time = (0 + 2 + 5 + 9)/4 = 16/4 = 4
Average Turnaround time = (2 + 5 + 9 + 14)/4 = 30/4 = 7.5
The SJF algorithm may be either preemptive or non preemptive algorithm.
The preemptive SJF is also known as shortest remaining time first.
Consider the following example.
Process Arrival Time CPU Time
P1 0 8
P2 1 4
P3 2 9
P4 3 5
In this case the Gantt chart will be
P1 P2 P4 P1 P3
0 1 5 10 17 26
The Waiting time for process
P1 = 10 - 1 = 9
P2 = 1 – 1 = 0
P3 = 17 – 2 = 15
P4 = 5 – 3 = 2
The Average Waiting time = (9 + 0 + 15 + 2)/4 = 26/4 = 6.5
3. Priority Scheduling Algorithm:
In this scheduling a priority number (integer) is associated with each
process and the CPU is allocated to the process with the highest
priority (smallest integer, highest priority).
o Preemptive
o Non-preemptive
Equal priority processes are scheduled in FCFS manner.
SJF is a priority scheduling where priority is the predicted next CPU
burst time.
Problem {Starvation – low priority processes may never execute.
Solution { Aging – as time progresses increase the priority of the
process
Priority can be defined either internally or externally.
Examples of Internal priorities are
Time limits.
Memory requirements.
File requirements, for example, number of open files.
CPU Vs I/O requirements.
Externally defined priorities are set by criteria that are external to operating
system such as
The importance of process.
Type or amount of funds being paid for computer use.
The department sponsoring the work.
Politics.
Consider the following example:
Process Arrival Time CPU Time
P1 10 3
P2 1 1
P3 2 3
P4 1 4
P5 5 2
According to the priority scheduling the Gantt chart will be
P2 P5 P1 P3 P4
0 1 6 16 18 19
The Waiting time for process
P1 = 6
P2 = 0
P3 = 16
P4 = 18
P5 = 1
The Average Waiting time = (0 + 1 + 6 + 16 + 18)/5 = 41/5 = 8.2
4. Round Robin Scheduling Algorithm:
This type of algorithm is designed only for the time sharing system.
It is similar to FCFS scheduling with preemption condition to switch
between processes.
A small unit of time called quantum time or time slice is used to switch
between the processes.
The average waiting time under the round robin policy is quiet long.
Consider the following example:
Process CPU time
P1 3
P2 5
P3 2
P4 4
Time Slice = 1 millisecond.
P1 P2 P3 P4 P1 P2 P3 P4 P1 P2 P4 P2 P4 P2
0 1 2 3 4 5 6 7 8 9 10 11 12 13 14
The Waiting Time for process
P1 = 0 + (4 – 1) + (8 – 5)
=0+3+3
=6
P2 = 1 + (5 – 2) + (9 – 6) + (11 – 10) + (13 – 12)
=1+3+3+1+1
=9
P3 = 2 + (6 – 3)
=2+3
=5
P4 = 3 + (7 – 4) + (10 – 8) + (12 – 11)
=3+3+2+1
=9
The Average Waiting time = (6 + 9 + 5 + 9)/4
= 7.2
5. Shortest-Remaining-Time (SRT) Scheduling
The SRT is the preemptive counterpart of SJF and useful in time-
sharing environment.
In SRT scheduling, the process with the smallest estimated run-time to
completion is run next, including new arrivals.
In SJF scheme, once a job begins executing, it run to completion.
In SJF scheme, a running process may be preempted by a new arrival
process with shortest estimated run-time.
The algorithm SRT has higher overhead than its counterpart SJF.
The SRT must keep track of the elapsed time of the running process
and must handle occasional preemptions.
In this scheme, arrival of small processes will run almost immediately.
However, longer jobs have even longer mean waiting time.
6. Multilevel Queue Scheduling
A multilevel queue scheduling algorithm partitions the ready queue in
several separate queues, for instance, in a multilevel queue scheduling
processes are permanently assigned to one queues.
The processes are permanently assigned to one another, based on
some property of the process, such as Memory size, Process priority,
Process type.
Algorithm choose the process from the occupied queue that has the
highest priority, and run that process either
7. Multilevel Feedback Queue Scheduling.
Multilevel feedback queue-scheduling algorithm allows a process to
move between queues.
It uses many ready queues and associates a different priority with each
queue.
The Algorithm chooses to process with highest priority from the
occupied queue and run that process either preemptively or non-
preemptively.
If the process uses too much CPU time it will moved to a lower-priority
queue.
Similarly, a process that wait too long in the lower-priority queue may
be moved to a higher-priority queue may be moved to a highest
priority queue.
Note that this form of aging prevents starvation.
Example:
A process entering the ready queue is placed in queue 0.
If it does not finish within 8 milliseconds time, it is moved to the tail
of queue 1.
If it does not complete, it is preempted and placed into queue 2.
Processes in queue 2 run on a FCFS basis, only when 2 run on a FCFS
basis queue, only when queue 0 and queue 1 are empty.
Three queues:
Q0 – RR with time quantum 8 milliseconds
Q1 – RR time quantum 16 milliseconds
Q2 – FCFS
In general, a multilevel feedback queue scheduler is defined by the following
parameters:
The number of queues.
The scheduling algorithm for each queue.
The method used to determine when to upgrade a process to a higher
priority queue.
The method used to determine when to demote a process to a lower
priority queue.
The method used to determine which queue a process will enter when
that process needs service
Performance evaluation of the scheduling:
UNIT III
Inter-process Communication, Race Conditions, Critical Section, Mutual
Exclusion, Hardware Solution, Strict Alternation, Peterson’s Solution,
The Producer Consumer Problem, Semaphores, Event Counters,
Monitors, Message Passing, and Classical IPC Problems.
Deadlocks: Definition, Deadlock characteristics, Deadlock Prevention,
Deadlock Avoidance (concepts only).
Inter-process Communication:
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 executing concurrently in the operating system may be
either independent processes or cooperating processes.
A process is independent if it cannot affect or be affected by the other
processes executing in the system. Any process that does not share
data with any other process is independent.
A process is cooperating if it can affect or be affected by the other
processes executing in the system. Clearly, any process that shares data
with other processes is a cooperating process.
Reasons for co-operating processes:
o Information sharing: Since several users may be interested in
the same piece of information (for instance, a shared file), we
must provide an environment to allow concurrent access to such
information.
o Computation speedup: If we want a particular task to run faster,
we must break it into subtasks, each of which will be executing in
parallel with the others. Notice that such a speedup can be
achieved only if the computer has multiple processing cores.
o Modularity: We may want to construct the system in a modular
fashion, dividing the system functions into separate processes or
threads.
o Convenience: Even an individual user may work on many tasks
at the same time. For instance, a user may be editing, listening to
music and compiling in parallel.
There are two fundamental models of inter-process communication:
o Shared Memory –
In the shared-memory model, a region of memory that is
shared by cooperating processes is established.
Processes can then exchange information by reading and
writing data to the shared region.
Shared memory can be faster than message passing, since
message-passing systems are typically implemented using
system calls and thus require the more time-consuming task
of kernel intervention.
o Message Passing –
In the message-passing model, communication takes place
by means of messages exchanged between the cooperating
processes.
Message passing is useful for exchanging smaller amounts of
data, because no conflicts need be avoided.
Message passing is also easier to implement in a distributed
system than shared memory.
Shared-Memory Systems
Inter-process communication using shared memory requires
communicating processes to establish a region of shared memory.
Typically, a shared-memory region resides in the address space of the
process creating the shared-memory segment.
Other processes that wish to communicate using this shared-memory
segment must attach it to their address space.
Recall that, normally, the operating system tries to prevent one process
from accessing another process’s memory.
Shared memory requires that two or more processes agree to remove this
restriction.
They can then exchange information by reading and writing data in the
shared areas.
The form of the data and the location are determined by these processes
and are not under the operating system’s control.
The processes are also responsible for ensuring that they are not writing
to the same location simultaneously.
Race Conditions:
A situation like this, where several processes access and manipulate the
same data concurrently and the outcome of the execution depends on the
particular order in which the access takes place, is called a race
condition.
Suppose that two processes, P1 and P2, share the global variable a.
At some point in its execution, P1 updates a to the value 1, and at
some point in its execution, P2 updates a to the value 2. Thus, the
two tasks are in a race to write variable a.
In this example the "loser" of the race (the process that updates last)
determines the final value of a.
Therefore Operating System Concerns of following things
1. The operating system must be able to keep track of the various
processes.
2. The operating system must allocate and deallocate various
resources for each active process.
3. The operating system must protect the data and physical resources
of each process against unintended interference by other processes.
4. The functioning of a process, and the output it produces, must be
independent of the speed at which its execution is carried out
relative to the speed of other concurrent processes.
Process Interaction can be defined as
• Processes unaware of each other
• Processes indirectly aware of each other
• Processes directly aware of each other
Concurrent processes come into conflict with each other when
they are
Competing for the use of the same resource.
Two or more processes need to access a resource during the course
of their execution.
Each process is unaware of the existence of the other processes.
There is no exchange of information between the competing
processes.
Critical Section Problem:
Consider a system consisting of n processes (P0, P1, ……… ,Pn-1) each
process has a segment of code which is known as critical section in which
the process may be changing common variable, updating a table, writing a
file and so on.
The important feature of the system is that when the process is
executing in its critical section no other process is to be allowed to
execute in its critical section.
The execution of critical sections by the processes is a mutually
exclusive.
The critical section problem is to design a protocol that the process can
use to co-operate each process must request permission to enter its
critical section.
The section of code implementing this request is the entry section. The
critical section is followed on exit section. The remaining code is the
remainder section.
Example:
While (1)
{
Entry Section;
Critical Section;
Exit Section;
Remainder Section;
}
A solution to the critical section problem must satisfy the following three
conditions.
1. Mutual Exclusion: If process Pi is executing in its critical section
then no any other process can be executing in their critical section.
2. Progress: If no process is executing in its critical section and some
process wish to enter their critical sections then only those process
that are not executing in their remainder section can enter its critical
section next.
3. Bounded waiting: There exists a bound on the number of times that
other processes are allowed to enter their critical sections after a
process has made a request.
Mutual Exclusion:
Requirements for Mutual Exclusion:
1. Mutual exclusion must be enforced: Only one process at a time is
allowed into its critical section, among all processes that have critical
sections for the same resource or shared object.
2. A process that halts in its non critical section must do so without
interfering with other processes.
3. It must not be possible for a process requiring access to a critical
section to be delayed indefinitely: no deadlock or starvation.
4. When no process is in a critical section, any process that requests
entry to its critical section must be permitted to enter without delay.
5. No assumptions are made about relative process speeds or number of
processors.
6. A process remains inside its critical section for a finite time only.
Hardware Solution:
Hardware approaches to mutual exclusion.
Interrupt Disabling:
In a uniprocessor machine, concurrent processes cannot be overlapped; they
can only be interleaved. Furthermore, a process will continue to run until it
invokes an operating system service or until it is interrupted. Therefore, to
guarantee mutual exclusion, it is sufficient to prevent a process from being
interrupted. This capability can be provided in the form of primitives
defined by the system kernel for disabling and enabling interrupts.
Solution to Critical-section Problem Using Locks
do
{
acquire lock
critical section;
release lock
remainder section;
} while (TRUE);
Because the critical section cannot be interrupted, mutual exclusion is
guaranteed.
Disadvantages
It works only in a single processor environment.
Interrupts can be lost if not serviced promptly.
A process waiting to enter its critical section could suffer from
starvation.
Test and Set Instruction
It is special machine instruction used to avoid mutual exclusion.
The test and set instruction can be defined as follows:
Definition:
boolean TestAndSet (boolean *target)
{
boolean rv = *target;
*target = TRUE;
return rv:
}
The above function is carried out automatically.
Solution using TestAndSet
Shared boolean variable lock initialized to false.
Solution:
do {
while ( TestAndSet (&lock ))
; // do nothing
// critical section
lock = FALSE;
// remainder section
} while (TRUE);
Advantages
1. It is simple and easy to verify.
2. It is applicable to any number of processes.
3. It can b used to support multiple critical section.
Disadvantages
1. Busy waiting is possible.
2. Starvation is also possible.
3. There may be deadlock.
Swap Instruction:
Definition:
void Swap (boolean *a, boolean *b)
{
boolean temp = *a;
*a = *b;
*b = temp:
}
Solution using Swap
Shared Boolean variable lock initialized to FALSE;
Each process has a local Boolean variable key
Solution:
do {
key = TRUE;
while ( key == TRUE)
Swap (&lock, &key);
// critical section
lock = FALSE;
// remainder section
} while (TRUE);
Bounded-waiting Mutual Exclusion with TestandSet()
do {
waiting[i] = TRUE;
key = TRUE;
while (waiting[i] && key)
key = TestAndSet(&lock);
waiting[i] = FALSE;
// critical section
j = (i + 1) % n;
while ((j != i) && !waiting[j])
j = (j + 1) % n;
if (j == i)
lock = FALSE;
else
waiting[j] = FALSE;
// remainder section
} while (TRUE);
Peterson’s Solution:
The mutual exclusion problem is to devise a pre-protocol (or entry protocol)
and a post-protocol (or exist protocol) to keep two or more threads from
being in their critical sections at the same time. Tanenbaum examine
proposals for critical-section problem or mutual exclusion problem.
Problem
When one process is updating shared modifiable data in its critical section,
no other process should allowed to enter in its critical section.
Proposal 1 -Disabling Interrupts (Hardware Solution)
Each process disables all interrupts just after entering in its critical section
and re-enable all interrupts just before leaving critical section. With
interrupts turned off the CPU could not be switched to other process.
Hence, no other process will enter its critical and mutual exclusion achieved.
Conclusion
Disabling interrupts is sometimes a useful interrupts is sometimes a useful
technique within the kernel of an operating system, but it is not appropriate
as a general mutual exclusion mechanism for user process. The reason is
that it is unwise to give user process the power to turn off interrupts.
Proposal 2 -Lock Variable (Software Solution)
In this solution, we consider a single, shared, (lock) variable, initially 0.
When a process wants to enter in its critical section, it first test the lock. If
lock is 0, the process first sets it to 1 and then enters the critical section. If
the lock is already 1, the process just waits until (lock) variable becomes 0.
Thus, a 0 means that no process in its critical section, and 1 means hold your
horses -some process is in its critical section.
Conclusion
The flaw in this proposal can be best explained by example. Suppose process
A sees that the lock is 0. Before it can set the lock to 1 another process B is
scheduled, runs, and sets the lock to 1. When the process A runs again, it will
also set the lock to 1, and two processes will be in their critical section
simultaneously.
Proposal 1 -Strict Alternation
In this proposed solution, the integer variable 'turn' keeps track of whose
turn is to enter the critical section. Initially, process A inspects turn, finds it
to be 0, and enters in its critical section. Process B also finds it to be 0 and
sits in a loop continually testing 'turn' to see when it becomes 1.
Continuously testing a variable waiting for some value to appear is called the
Busy-Waiting.
Conclusion:
Taking turns is not a good idea when one of the processes is much slower
than the other. Suppose process 0 finishes its critical section quickly, so both
processes are now in their noncritical section. This situation violates above
mentioned condition 3.
The Producer Consumer Problem:
A producer process produces information that is consumed by a
consumer process.
For example, a compiler may produce assembly code that is consumed
by an assembler. The assembler, in turn, may produce object modules
that are consumed by the loader.
The producer–consumer problem also provides a useful metaphor for
the client–server paradigm.
One solution to the producer–consumer problem uses shared memory
To allow producer and consumer processes to run concurrently, we
must have available a buffer of items that can be filled by the producer
and emptied by the consumer.
This buffer will reside in a region of memory that is shared by the
producer and consumer processes. A producer can produce one item
while the consumer is consuming another item.
The producer and consumer must be synchronized, so that the
consumer does not try to consume an item that has not yet been
produced.
Two types of buffers can be used.
o Unbounded buffer
o Bounded buffer
The unbounded buffer places no practical limit on the size of the buffer.
The consumer may have to wait for new items, but the producer can
always produce new items.
The bounded buffer assumes a fixed buffer size. In this case, the
consumer must wait if the buffer is empty, and the producer must wait
if the buffer is full.
Let’s look more closely at how the bounded buffer illustrates inter-
process communication using shared memory. The following variables
reside in a region of memory shared by the producer and consumer
processes.
#define BUFFER SIZE 10
typedef struct {
...
}item;
item buffer[BUFFER SIZE];
int in = 0;
int out = 0;
The shared buffer is implemented as a circular array with two logical
pointers: in and out.
The variable in points to the next free position in the buffer;
The variable out points to the first full position in the buffer.
The buffer is empty when in ==out;
The buffer is full when ((in + 1) % BUFFER SIZE) == out.
The producer process has a local variable next produced in which the
new item to be produced is stored.
The consumer process has a local variable next consumed in which
the item to be consumed is stored.
item next produced;
while (true) {
/* produce an item in next produced */
while (((in + 1) % BUFFER SIZE) == out)
; /* do nothing */
buffer[in] = next produced;
in = (in + 1) % BUFFER SIZE;
}
The producer process using shared memory.
item next consumed;
while (true) {
while (in == out)
; /* do nothing */
next consumed = buffer[out];
out = (out + 1) % BUFFER SIZE;
/* consume the item in next consumed */
}
The consumer process using shared memory.
To write a C program to implement the Producer & consumer
Problem (Semaphore)
ALGORITHM:
Step 1: The Semaphore mutex, full & empty are initialized.
Step 2: In the case of producer process
i) Produce an item in to temporary variable.
ii) If there is empty space in the buffer check the mutex value for
enter into the critical section.
iii) If the mutex value is 0, allow the producer to add value in the
temporary variable to the buffer.
Step 3: In the case of consumer process
i) It should wait if the buffer is empty
ii) If there is any item in the buffer check for mutex value, if the
mutex==0, remove item from buffer
iii) Signal the mutex value and reduce the empty value by 1.
iv) Consume the item.
Step 4: Print the result
PROGRAM CODING
#include<stdio.h>
int mutex=1,full=0,empty=3,x=0; main()
{
int n;
void producer();
void consumer();
int wait(int);
int signal(int);
printf("\n 1.producer\n2.consumer\n3.exit\n"); while(1)
{
printf(" \nenter ur choice");
scanf("%d",&n);
switch(n)
{
case 1: if((mutex==1)&&(empty!=0))
producer();
else
printf("buffer is full\n");
break;
case 2: if((mutex==1)&&(full!=0))
consumer();
else
printf("buffer is empty");
break;
case 3: exit(0);
break;
}
}
}
int wait(int s)
{
return(--s);
}
int signal(int s)
{
return (++s);
}
void producer()
{
mutex=wait(mutex);
full=signal(full);
empty=wait(empty);
x++;
printf("\n producer produces the items %d",x);
mutex=signal(mutex);
}
void consumer()
{
mutex=wait(mutex);
full=wait(full);
empty=signal(empty);
printf("\n consumer consumes the item %d",x);
x--;
mutex=signal(mutex);
}
Semaphores:
A semaphore S is an integer variable that, apart from initialization, is
accessed only through two standard atomic operations: wait() and
signal().
The wait() operation was originally termed P (from the Dutch
proberen, “to test”); signal() was originally called V (from verhogen, “to
increment”).
The definition of wait() is as follows:
wait(S) {
while (S <= 0)
; // busy wait
S--;
}
The definition of signal() is as follows:
signal(S) {
S++;
}
The integer value of the semaphore in the wait and signal operations must
be executed indivisibly. That is, when one process modifies the semaphore
value, no other process can simultaneously modify that same semaphore
value.
In addition, in the case of the wait(S), the testing of the integer value of S (S
0) and its possible modification (S := S – 1), must also be executed without
interruption.
Semaphore as General Synchronization Tool
Counting semaphore – integer value can range over an unrestricted
domain
Binary semaphore – integer value can range only between 0 and 1;
Can be simpler to implement
Also known as mutex locks Can implement a counting semaphore S as a
binary semaphore
Provides mutual exclusion Semaphore mutex;
// initialized to
do {
wait (mutex);
// Critical Section
signal (mutex);
} while (TRUE);
Semaphore Implementation
Must guarantee that no two processes can execute wait () and signal ()
on the same semaphore at the same time
Thus, implementation becomes the critical section problem where the
wait and signal code are placed in the critical section.
Could now have busy waiting in critical section implementation But
implementation code is short Little busy waiting if critical section
rarely occupied
Note that applications may spend lots of time in critical sections and
therefore this is not a good solution.
Semaphore Implementation with no Busy waiting:
With each semaphore there is an associated waiting queue.
Each entry in a waiting queue has two data items:
o value (of type integer)
o pointer to next record in the list
Two operations:
o block – place the process invoking the operation on the
appropriate waiting queue.
o wakeup – remove one of processes in the waiting queue and place
it in the ready queue.
Implementation of wait:
wait(semaphore *S) {
S->value--;
if (S->value < 0) {
add this process to S->list;
block();
}
}
Implementation of signal:
signal(semaphore *S) {
S->value++;
if (S->value <= 0) {
remove a process P from S->list;
wakeup(P);
}
}
Semaphores are not provided by hardware. But they have several attractive
properties:
1. Semaphores are machine independent.
2. Semaphores are simple to implement.
3. Correctness is easy to determine.
4. Can have many different critical sections with different semaphores.
5. Semaphore acquires many resources simultaneously.
Drawback of Semaphore
1. They are essentially shared global variables.
2. Access to semaphores can come from anywhere in a program.
3. There is no control or guarantee of proper usage.
4. There is no linguistic connection between the semaphore and the data
to which the semaphore controls access.
5. They serve two purposes, mutual exclusion and scheduling constraints.
Monitors:
Although semaphores provide a convenient and effective mechanism for
process synchronization, using them incorrectly can result in timing errors
that are difficult to detect, since these errors happen only if particular
execution sequences take place and these sequences do not always occur.
The monitor is a programming-language construct that provides
equivalent functionality to that of semaphores and that is easier to
control.
The monitor construct has been implemented in a number of
programming languages, including Concurrent Pascal, Pascal-Plus,
Modula-2, Modula-3, and Java.
An abstract data type—or ADT—encapsulates data with a set of
functions to operate on that data that are independent of any specific
implementation of the ADT.
A monitor type is an ADT that includes a set of programmer defined
operations that are provided with mutual exclusion within the monitor.
The monitor type also declares the variables whose values define the
state of an instance of that type, along with the bodies of functions that
operate on those variables.
It has also been implemented as a program library.
This allows programmers to put monitor locks on any object.
monitor monitor_name
{
/* shared variable declarations */
function P1 ( . . . ) {
...
}
function P2 ( . . . ) {
...
}
.
.
.
function Pn ( . . . ) {
...
}
initialization code ( . . . ) {
...
}
}
Figure: Syntax of a monitor.
Thus, a function defined within a monitor can access only those
variables declared locally within the monitor and its formal
parameters.
Similarly, the local variables of a monitor can be accessed by only the
local functions.
The monitor construct ensures that only one process at a time is active
within the monitor
However, the monitor construct, as defined so far, is not sufficiently
powerful for modeling some synchronization schemes.
For this purpose, we need to define additional synchronization
mechanisms.
condition x, y;
The only operations that can be invoked on a condition variable are
wait() and signal().
x.wait();
The operation means that the process invoking this operation is
suspended until another process invokes
x.signal();
The x.signal() operation resumes exactly one suspended process.
If no process is suspended, then the signal() operation has no effect;
Now suppose that, when the x.signal() operation is invoked by a
process P, there exists a suspended process Q associated with
condition x.
Clearly, if the suspended process Q is allowed to resume its execution,
the signaling process P must wait.
Otherwise, both P and Q would be active simultaneously within the
monitor. Note, however, that conceptually both processes can continue
with their execution.
Two possibilities exist:
1. Signal and wait. P either waits until Q leaves the monitor or waits for
another condition.
2. Signal and continue. Q either waits until P leaves the monitor or waits
for another condition.
On the one hand, since P was already executing in the monitor, the
signal and continue method seems more reasonable. On the other, if
we allow thread P to continue, then by the time Q is resumed, the
logical condition for which Q was waiting may no longer hold. When
thread P executes the signal operation, it immediately leaves the
monitor. Hence, Q is immediately resumed.
Message Passing:
Message passing provides a mechanism to allow processes to
communicate and to synchronize their actions without sharing the
same address space.
It is particularly useful in a distributed environment, where the
communicating processes may reside on different computers
connected by a network.
A message-passing facility provides at least two operations:
o send(message)
o receive(message)
If P and Q wish to communicate, they need to establish a
communication link between them exchange messages via
send/receive Implementation of communication link physical (e.g.,
shared memory, hardware bus) logical (e.g., logical properties)
Direct Communication
Processes must name each other explicitly:
o send (P, message) – send a message to process P
o receive(Q, message) – receive a message from process Q
Properties of communication link
Links are established automatically
A link is associated with exactly one pair of communicating processes
Between each pair there exists exactly one link
The link may be unidirectional, but is usually bi-directional
Indirect Communication
Messages are directed and received from mailboxes (also referred to as
ports)
Each mailbox has a unique id
Processes can communicate only if they share a mailbox
Properties of communication link
Link established only if processes share a common mailbox
A link may be associated with many processes
Each pair of processes may share several communication links
Link may be unidirectional or bi-directional
Operations
create a new mailbox
send and receive messages through mailbox
destroy a mailbox
Primitives are defined as:
o send(A, message) – send a message to mailbox A
o receive(A, message) – receive a message from mailbox A
Mailbox sharing
Problem: Now suppose that processes P1, P2, and P3 all share mailbox A.
Process P1 sends a message to A, while both P2 and P3 execute a receive()
from A. Which process will receive the message sent by P1?
Solutions
Allow a link to be associated with at most two processes
Allow only one process at a time to execute a receive operation
Allow the system to select arbitrarily the receiver. Sender is notified
who the receiver was.
Synchronization
Message passing may be either blocking or non-blocking
Blocking is considered synchronous
Blocking send has the sender block until the message is received
Blocking receive has the receiver block until a message is available
Non-blocking is considered asynchronous
Non-blocking send has the sender send the message and continue
Non-blocking receive has the receiver receive a valid message or null
Buffering
Queue of messages attached to the link; implemented in one of three ways
1. Zero capacity – 0 messages Sender must wait for receiver (rendezvous)
2. Bounded capacity – finite length of n messages Sender must wait if link
full
3. Unbounded capacity – infinite length Sender never waits
Classical IPC Problems:
These problems are used for testing nearly every newly proposed
synchronization scheme. In our solutions to the problems, we use
semaphores for synchronization, since that is the traditional way to present
such solutions. However, actual implementations of these solutions could
use mutex locks in place of binary semaphores.
1. The Bounded-Buffer Problem
2. The Readers–Writers Problem
3. The Dining-Philosophers Problem
1. The Bounded-Buffer Problem:
It is commonly used to illustrate the power of synchronization
primitives
In our problem, the producer and consumer processes share the
following data structures:
int n;
semaphore mutex = 1;
semaphore empty = n;
semaphore full = 0
We assume that the pool consists of n buffers, each capable of holding
one item.
The mutex semaphore provides mutual exclusion for accesses to the
buffer pool and is initialized to the value 1.
The empty and full semaphores count the number of empty and full
buffers.
The semaphore empty is initialized to the value n; the semaphore full is
initialized to the value 0.
do {
...
/* produce an item in next produced */
...
wait(empty);
wait(mutex);
...
/* add next produced to the buffer */
...
signal(mutex);
signal(full);
} while (true);
Fig: The structure of the producer process.
do {
wait(full);
wait(mutex);
...
/* remove an item from buffer to next consumed */
...
signal(mutex);
signal(empty);
...
/* consume the item in next consumed */
...
} while (true);
Fig: The structure of the consumer process.
2. The Readers–Writers Problem:
Suppose that a database is to be shared among several concurrent
processes. Some of these processes may want only to read the
database, whereas others may want to update (that is, to read and
write) the database.
If two readers access the shared data simultaneously, no adverse
effects will result.
If a writer and some other process (either a reader or a writer) access
the database simultaneously, problem may arise.
No reader should wait for other readers to finish simply because a
writer is waiting.
Once a writer is ready, that writer performs its write as soon as
possible.
A solution to either problem may result in starvation. In the first case,
writers may starve; in the second case, readers may starve.
In the solution to the first readers–writers problem, the reader
processes share the following data structures:
semaphore rw mutex = 1;
semaphore mutex = 1;
int read count = 0;
The semaphore rw_mutex is common to both reader and writer
processes. The mutex semaphore is used to ensure mutual exclusion
when the variable read count is updated.
The read_count variable keeps track of how many processes are
currently reading the object.
The semaphore rw_mutex functions as a mutual exclusion semaphore
for the writers
The code for a writer process:
do {
wait(rw mutex);
...
/* writing is performed */
...
signal(rw mutex);
} while (true);
The code for a reader process:
do {
wait(mutex);
read count++;
if (read count == 1)
wait(rw mutex);
signal(mutex);
...
/* reading is performed */
...
wait(mutex);
read count--;
if (read count == 0)
signal(rw mutex);
signal(mutex);
} while (true);
If a writer is in the critical section and n readers are waiting, then one
reader is queued on rw_mutex, and n − 1 readers are queued on
mutex.
When a writer executes signal (rw_mutex), we may resume the
execution of either the waiting readers or a single waiting writer. The
selection is made by the scheduler.
3. The Dining-Philosophers Problem:
The dining-philosophers problem is considered a classic
synchronization problem.
Consider five philosophers who spend their lives thinking and eating.
The philosophers share a circular table surrounded by five chairs, each
belonging to one philosopher. In the center of the table is a bowl of rice,
and the table is laid with five single chopsticks
When a philosopher thinks, she does not interact with her colleagues.
From time to time, a philosopher gets hungry and tries to pick up the
two chopsticks that are closest to her (the chopsticks that are between
her and her left and right neighbors).
A philosopher may pick up only one chopstick at a time.
She cannot pick up a chopstick that is already in the hand of a neighbor.
When a hungry philosopher has both her chopsticks at the same time,
she eats without releasing the chopsticks.
When she is finished eating, she puts down both chopsticks and starts
thinking again.
A philosopher tries to grab a chopstick by executing a wait() operation
on that semaphore.
She releases her chopsticks by executing the signal() operation on the
appropriate semaphores.
Thus, the shared data are where all the elements of chopstick are
initialized to 1.
semaphore chopstick[5];
do {
wait(chopstick[i]);
wait(chopstick[(i+1) % 5]);
...
/* eat for awhile */
...
signal(chopstick[i]);
signal(chopstick[(i+1) % 5]);
...
/* think for awhile */
...
} while (true);
Fig: The structure of philosopher i.
Although this solution guarantees that no two neighbors are eating
simultaneously, it nevertheless must be rejected because it could
create a deadlock.
Suppose that all five philosophers become hungry at the same time and
each grabs her left chopstick.
All the elements of chopstick will now be equal to 0.
When each philosopher tries to grab her right chopstick, she will be
delayed forever.
Several possible remedies to the deadlock problem are replaced by:
o Allow at most four philosophers to be sitting simultaneously at
the table.
o Allow a philosopher to pick up her chopsticks only if both
chopsticks are available (to do this, she must pick them up in a
critical section).
o Use an asymmetric solution—that is, an odd-numbered
philosopher picks up first her left chopstick and then her right
chopstick, whereas an even numbered philosopher picks up her
right chopstick and then her left chopstick.
Deadlocks
Definition: In a multiprogramming environment, several processes may
compete for a finite number of resources. A process requests resources; if
the resources are not available at that time, the process enters a waiting
state. Sometimes, a waiting process is never again able to change state,
because the resources it has requested are held by other waiting processes.
This situation is called a deadlock.
SYSTEM MODEL
• A system may consist of finite number of resources and is distributed
among number of processes. There resources are partitioned into several
instances each with identical instances.
• A process must request a resource before using it and it must release the
resource after using it. It can request any number of resources to carry out a
designated task. The amount of resource requested may not exceed the total
number of resources available.
A process may utilize the resources in only the following sequences:
1. Request:- If the request is not granted immediately then the
requesting process must wait it can acquire the resources.
2. Use:- The process can operate on the resource.
3. Release:- The process releases the resource after using it.
Deadlock may involve different types of resources.
Example:-
Consider a system with one printer and one tape drive. If a process Pi
currently holds a printer and a process Pj holds the tape drive. If process Pi
request a tape drive and process Pj request a printer then a deadlock occurs.
Multithread programs are good candidates for deadlock because they
compete for shared resources.
Deadlock characteristics:
Necessary Conditions: A deadlock situation can occur if the following 4
conditions occur simultaneously in a system:-
1. Mutual Exclusion: Only one process must hold the resource at a time.
If any other process requests for the resource, the requesting process
must be delayed until the resource has been released.
2. Hold and Wait:- A process must be holding at least one resource and
waiting to acquire additional resources that are currently being held by
the other process.
3. No Preemption:- Resources can’t be preempted i.e., only the process
holding the resources must release it after the process has completed
its task.
4. Circular Wait:- A set {P0,P1……..Pn} of waiting process must exist such
that P0 is waiting for a resource i.e., held by P1, P1 is waiting for a
resource i.e., held by P2. Pn-1 is waiting for resource held by process
Pn and Pn is waiting for the resource i.e., held by P1. All the four
conditions must hold for a deadlock to occur.
Resource Allocation Graph:
1. Deadlocks are described by using a directed graph called system
resource allocation graph. The graph consists of set of vertices (v) and
set of edges (e).
2. The set of vertices (v) can be described into two different types of
nodes P={P1,P2……..Pn} i.e., set consisting of all active processes and
R={R1,R2……….Rn} i.e., set consisting of all resource types in the
system.
3. A directed edge from process Pi to resource type Rj denoted by Pi->Ri
indicates that Pi requested an instance of resource Rj and is waiting.
This edge is called Request edge.
4. A directed edge Ri-> Pj signifies that resource Rj is held by process Pi.
This is called Assignment edge.
5. If the graph contain no cycle, then no process in the system is deadlock.
If the graph contains a cycle then a deadlock may exist.
6. If each resource type has exactly one instance than a cycle implies that
a deadlock has occurred. If each resource has several instances then a
cycle do not necessarily implies that a deadlock has occurred.
The sets P, R, and E:
P = {P1, P2, P3}
R = {R1, R2, R3, R4}
E = {P1 → R1, P2 → R3, R1 → P2, R2 → P2, R2 → P1, R3 → P3}
Resource instances:
One instance of resource type R1
Two instances of resource type R2
One instance of resource type R3
Three instances of resource type R4
Process states:
Process P1 is holding an instance of resource type R2 and is waiting for
an instance of resource type R1.
Process P2 is holding an instance of R1 and an instance of R2 and is
waiting for an instance of R3.
Process P3 is holding an instance of R3.
Methods for Handling Deadlocks:
There are three ways to deal with deadlock problem
We can use a protocol to prevent deadlocks ensuring that the system
will never enter into the deadlock state.
We allow a system to enter into deadlock state, detect it and recover
from it.
We ignore the problem and pretend that the deadlock never occur in
the system. This is used by most OS including UNIX.
To ensure that the deadlock never occur the system can use either
deadlock avoidance or a deadlock prevention.
Deadlock prevention is a set of method for ensuring that at least one of
the necessary conditions does not occur.
Deadlock avoidance requires the OS is given advance information
about which resource a process will request and use during its lifetime.
If a system does not use either deadlock avoidance or deadlock
prevention then a deadlock situation may occur. During this it can
provide an algorithm that examines the state of the system to
determine whether a deadlock has occurred and algorithm to recover
from deadlock.
Undetected deadlock will result in deterioration of the system
performance.
Deadlock Prevention:
For a deadlock to occur each of the four necessary conditions must hold. If at
least one of the conditions does not hold then we can prevent occurrence of
deadlock.
1. Mutual Exclusion: This holds for non-sharable resources.
Eg:-A printer can be used by only one process at a time.
Mutual exclusion is not possible in sharable resources and thus they
cannot be involved in deadlock. Read-only files are good examples for
sharable resources. A process never waits for accessing a sharable resource.
So we cannot prevent deadlock by denying the mutual exclusion condition in
non-sharable resources.
2. Hold and Wait: This condition can be eliminated by forcing a process to
release all its resources held by it when it request a resource i.e., not
available.
One protocol can be used is that each process is allocated with all of
its resources before its start execution.
Eg:-consider a process that copies the data from a tape drive to the
disk, sorts the file and then prints the results to a printer. If all the
resources are allocated at the beginning then the tape drive, disk
files and printer are assigned to the process. The main problem with
this is it leads to low resource utilization because it requires printer
at the last and is allocated with it from the beginning so that no
other process can use it.
Another protocol that can be used is to allow a process to request a
resource when the process has none. i.e., the process is allocated
with tape drive and disk file. It performs the required operation and
releases both. Then the process once again request for disk file and
the printer and the problem and with this is starvation is possible.
3. No Preemption: To ensure that this condition never occurs the resources
must be preempted. The following protocol can be used.
If a process is holding some resource and request another resource
that cannot be immediately allocated to it, then all the resources
currently held by the requesting process are preempted and added to
the list of resources for which other processes may be waiting. The
process will be restarted only when it regains the old resources and the
new resources that it is requesting.
When a process request resources, we check whether they are
available or not. If they are available we allocate them else we check
that whether they are allocated to some other waiting process. If so we
preempt the resources from the waiting process and allocate them to
the requesting process. The requesting process must wait.
4. Circular Wait: The fourth and the final condition for deadlock is the
circular wait condition. One way to ensure that this condition never, is to
impose ordering on all resource types and each process requests resource
in an increasing order.
Let R={R1,R2,………Rn} be the set of resource types. We assign each
resource type with a unique integer value. This will allows us to compare
two resources and determine whether one precedes the other in ordering.
Eg:-we can define a one to one function
F:R->N as follows :-F(disk drive)=5 F(printer)=12 F(tape drive)=1
Deadlock can be prevented by using the following protocol:
Each process can request the resource in increasing order. A process
can request any number of instances of resource type say Ri and it can
request instances of resource type Rj only F(Rj) > F(Ri).
Alternatively when a process requests an instance of resource type Rj,
it has released any resource Ri such that F(Ri) >= F(Rj). If these two
protocols are used then the circular wait can’t hold.
Deadlock Avoidance (concepts only):
Deadlock prevention algorithm may lead to low device utilization and
reduces system throughput.
Avoiding deadlocks requires additional information about how
resources are to be requested. With the knowledge of the complete
sequences of requests and releases we can decide for each requests
whether or not the process should wait.
For each requests it requires to check the resources currently available,
resources that are currently allocated to each processes future
requests and release of each process to decide whether the current
requests can be satisfied or must wait to avoid future possible
deadlock.
A deadlock avoidance algorithm dynamically examines the resources
allocation state to ensure that a circular wait condition never exists.
The resource allocation state is defined by the number of available and
allocated resources and the maximum demand of each process.
Safe State:
A state is a safe state in which there exists at least one order in which
all the process will run completely without resulting in a deadlock.
A system is in safe state if there exists a safe sequence.
A sequence of processes <P1,P2,………..Pn> is a safe sequence for the
current allocation state if for each Pi the resources that Pi can request
can be satisfied by the currently available resources.
If the resources that Pi requests are not currently available then Pi can
obtain all of its needed resource to complete its designated task.
A safe state is not a deadlock state.
Whenever a process request a resource i.e., currently available, the
system must decide whether resources can be allocated immediately or
whether the process must wait. The request is granted only if the
allocation leaves the system in safe state.
In this, if a process requests a resource i.e., currently available it must
still have to wait. Thus resource utilization may be lower than it would
be without a deadlock avoidance algorithm.
Resource Allocation Graph Algorithm:
This algorithm is used only if we have one instance of a resource type. In
addition to the request edge and the assignment edge a new edge called
claim edge is used.
Ex :- A claim edge Pi->Rj indicates that process Pi may request Rj in future.
The claim edge is represented by a dotted line.
When a process Pi requests the resource Rj, the claim edge is converted
to a request edge.
When resource Rj is released by process Pi, the assignment edge Rj->Pi
is replaced by the claim edge Pi->Rj.
When a process Pi requests Rj the request is granted only if converting the
request edge Pi->Rj to as assignment edge Rj->Pi do not result in a cycle.
Cycle detection algorithm is used to detect the cycle. If there are no cycles
then the allocation of the resource to process leave the system in safe state
Banker’s Algorithm:
This algorithm is applicable to the system with multiple instances of
each resource types, but this is less efficient then the resource
allocation graph algorithm.
When a new process enters the system it must declare the maximum
number of resources that it may need. This number may not exceed the
total number of resources in the system. The system must determine
that whether the allocation of the resources will leave the system in a
safe state or not. If it is so resources are allocated else it should wait
until the process release enough resources.
Several data structures are used to implement the banker’s algorithm.
Let ‘n’ be the number of processes in the system and ‘m’ be the number
of resources types.
We need the following data structures:
Available:-A vector of length m indicates the number of available
resources.
If Available[i]=k, then k instances of resource type Rj is available.
Max:-An n*m matrix defines the maximum demand of each process if
Max[i,j]=k, then Pi may request at most k instances of resource type Rj.
Allocation:-An n*m matrix defines the number of resources of each
type currently allocated to each process.
If Allocation[i,j]=k, then Pi is currently k instances of resource type Rj.
Need:-An n*m matrix indicates the remaining resources need of each
process.
If Need[i,j]=k, then Pi may need k more instances of resource type Rj to
compute its task.
So Need[i,j]=Max[i,j]-Allocation[i]
Safety Algorithm:-
This algorithm is used to find out whether or not a system is in safe state or
not.
Step 1: Let Work and Finish be vectors of length m and n, respectively
Initialize:
Work = Available
For i = 1,2, …, n,
if Allocationi¹ 0, then
Finish[i] = false;otherwise, Finish[i] = true.
Step 2: Find an index i such that both:
Finish[i] == false
Requesti <= Work
If no such i exists, go to step 4.
Step 3: Work = Work + Allocation
Finish [i] = true
Go to step 2
Step 4: If Finish [i] = false, for some i, 1<=i<= n, then the system is in a
deadlock state.
Moreover, if Finish[i] = false, then process Pi is deadlocked.
Resource Allocation Algorithm:
Request = request vector for process Pi. If Requesti [j] = k then process Pi
wants k instances of resource type Rj.
Step 1: If Requesti <= Needi go to step 2. Otherwise, raise error condition,
since process has exceeded its maximum claim.
Step 2: If Requesti <= Available, go to step 3. Otherwise Pi must wait, since
resources are not available.
Step 3: Pretend to allocate requested resources to Pi by modifying the state
as follows:
Available = Available – Request;
Allocationi = Allocationi + Requesti;
Needi = Needi – Requesti;
· If safe ⇒ the resources are allocated to Pi.
· If unsafe ⇒ Pi must wait and the old resource-allocation state is restored
Example 1:
5 processes P0 through P4;
3 resource types: A (10 instances), B (5instances), and C (7 instances).
Snapshot at time T0:
Allocation Max
ABC ABC
P0 010 753
P1 200 322
P2 302 902
P3 211 222
P4 002 433
Check that Needi <= Available
To calculate first Available matrix is (10 5 7) – (7 2 5) = 3 3 2
Allocation Max Request/Need Available/Work
(Max-Allocation) (Total-Allocation)
ABC ABC ABC ABC
P0 010 753 743 332
P1 302 322 020 532
P2 301 902 600 743
P3 211 422 011 745
P4 002 533 431 755
______ 10 5 7
Total = 725
To find safe state using Banker’s Algorithm:
Needi <= Available => Work = Work+Allocation
P0 743 <= 332 X
P1 122 <= 332 Work = 3 3 2 + 2 0 0 = 5 3 2
P2 600 <= 532 X
P3 211 <= 532 Work = 5 3 2 + 2 1 1 = 7 4 3
P4 531 <= 743 Work = 7 4 3 + 0 0 2 = 7 4 5
P0 743 <= 745 Work = 7 4 5 + 0 1 0 = 7 5 5
P1 600 <= 755 Work = 7 5 5 + 3 0 2 = 10 5 7
Executing safety algorithm shows that sequence <P1, P3, P4, P0, P2>
satisfies safety requirement.
Can request for (3,3,0) by P4 be granted? –NO
Can request for (0,2,0) by P0 be granted? –NO (Results Unsafe)
Recovery from Deadlock
Abort all deadlocked processes.
Abort one process at a time until the deadlock cycle is eliminated.
In which order should we choose to abort?
Priority of the process.
How long process has computed, and how much longer to completion.
Resources the process has used.
Resources process needs to complete.
How many processes will need to be terminated?
Is process interactive or batch?.
Resource Preemption
Selecting a victim – minimize cost.
Rollback – return to some safe state, restart process for that state.
Starvation – same process may always be picked as victim, include number of
rollback in cost factor.
UNIT IV
UNIT IV:
Memory Management:
Memory Management: Logical and and Physical
Physical address
address map,
map,Memory
Memory
allocation,Internal
allocation, Internaland
andExternal
Externalfragmentation
fragmentationand
andCompaction,
Compaction,Paging.
Paging.
VirtualMemory:
Virtual Memory:Demand
Demandpaging,
paging,Page
PageReplacement
Replacementpolicies.
policies.
Background:
Memory management is concerned with managing the primary
memory.
Memory consists of array of bytes or words each with their own
address.
The instructions are fetched from the memory by the CPU based on the
value program counter. These instructions may cause additional
loading from and storing to specific memory addresses.
Memory unit sees only a stream of memory addresses. It does not
know how they are generated.
Program must be brought into memory and placed within a process for
it to be run.
Input queue – collection of processes on the disk that are waiting to be
brought into memory for execution.
User programs go through several steps before being run.
Functions of memory management:-
Keeping track of status of each memory location.
Determining the allocation policy - Memory allocation technique & De-
allocation technique.
Address Binding:-
Programs are stored on the secondary storage disks as binary executable
files.
When the programs are to be executed they are brought in to the main
memory and placed within a process.
The collection of processes on the disk waiting to enter the main memory
forms the input queue.
One of the processes which are to be executed is fetched from the queue
and placed in the main memory.
During the execution it fetches instruction and data from main memory.
After the process terminates it returns back the memory space.
During execution the process will go through different steps and in each
step the address is represented in different ways.
In source program the address is symbolic.
The compiler converts the symbolic address to re-locatable address.
The loader will convert this re-locatable address to absolute address.
Binding of instructions and data can be done at any step along the
way:-
1. Compile time:- If we know whether the process resides in memory
then absolute code can be generated. If the static address changes then
it is necessary to re-compile the code from the beginning.
2. Load time:- If the compiler doesn’t know whether the process resides
in memory then it generates the re-locatable code. In this the binding is
delayed until the load time.
3. Execution time:- If the process is moved during its execution from one
memory segment to another then the binding is delayed until run time.
Special hardware is used for this. Most of the general purpose
operating system uses this method.
Logical and Physical address map
The address generated by the CPU is called logical address or virtual
address.
The address seen by the memory unit i.e., the one loaded in to the
memory register is called the physical address.
Compile time and load time address binding methods generate some
logical and physical address. The execution time address binding
generate different logical and physical address.
Set of logical address space generated by the programs is the logical
address space.
Set of physical address corresponding to these logical addresses is the
physical address space.
The mapping of virtual address to physical address during run time is
done by the hardware device called memory management unit (MMU).
The base register is also called re-location register. Value of the re-
location register is added to every address generated by the user
process at the time it is sent to memory.
User program never sees the real physical address.
The figure below shows the dynamic relation. Dynamic relation implies
mapping from virtual address space to physical address space and is
performed at run time usually with some hardware assistance.
Relocation is performed by the hardware and is invisible to the user.
Dynamic relocation makes it possible to move a partially executed process
from one area of memory to another without affecting.
Dynamic re-location using a re-location registers The above figure
shows that dynamic re-location which implies mapping from virtual
addresses space to physical address space and is performed by the
hardware at run time.
Re-location is performed by the hardware and is invisible to the user
dynamic relocation makes it possible to move a partially executed process
from one area of memory to another without affecting.
Example:
If the base is at 14000, then an attempt by the user to address location
0 is dynamically relocated to location 14000; an access to location 346
is mapped to location 14346.
The user program never sees the real physical addresses.
The program can create a pointer to location 346, store it in memory,
manipulate it, and compare it with other addresses—all as the number
346.
Only when it is used as a memory address (in an indirect load or store,
perhaps) is it relocated relative to the base register.
The user program deals with logical addresses.
The memory-mapping hardware converts logical addresses into
physical addresses.
We now have two different types of addresses: logical addresses (in the
range 0 to max) and physical addresses (in the range R + 0 to R + max
for a base value R).
The user program generates only logical addresses and thinks that the
process runs in locations 0 to max. However, these logical addresses
must be mapped to physical addresses before they are used.
The concept of a logical address space that is bound to a separate
physical address space is central to proper memory management.
Dynamic Loading:-
For a process to be executed it should be loaded in to the physical
memory. The size of the process is limited to the size of the physical
memory.
Dynamic loading is used to obtain better memory utilization.
In dynamic loading the routine or procedure will not be loaded until it
is called.
Whenever a routine is called, the calling routine first checks whether
the called routine is already loaded or not. If it is not loaded it cause the
loader to load the desired program in to the memory and updates the
programs address table to indicate the change and control is passed to
newly called routine.
Advantage:-
Gives better memory utilization.
Unused routine is never loaded.
Do not need special operating system support.
This method is useful when large amount of codes are needed to
handle in frequently occurring cases.
Swapping:
Swapping is a technique of temporarily removing inactive programs from
the memory of the system. A process can be swapped temporarily out of the
memory to a backing store and then brought back in to the memory for
continuing the execution. This process is called swapping.
Example:-
In a multi-programming environment with a round robin CPU
scheduling whenever the time quantum expires then the process that
has just finished is swapped out and a new process swaps in to the
memory for execution.
A variation of swap is priority based scheduling. When a low priority is
executing and if a high priority process arrives then a low priority will
be swapped out and high priority is allowed for execution. This process
is also called as Roll out and Roll in.
Normally the process which is swapped out will be swapped back to
the same memory space that is occupied previously. This depends
upon address binding.
If the binding is done at load time, then the process is moved to same
memory location. If the binding is done at run time, then the process is
moved to different memory location.
This is because the physical address is computed during run time.
Swapping requires backing store and it should be large enough to
accommodate the copies of all memory images. The system maintains a
ready queue consisting of all the processes whose memory images are
on the backing store or in memory that are ready to run.
Swapping is constant by other factors:-
To swap a process, it should be completely idle.
A process may be waiting for an I/O operation. If the I/O is
asynchronously accessing the user memory for I/O buffers, then the
process cannot be swapped.
Sl. No Logical Address Physical Address
1 Address generated by the Address seen by the memory unit
CPU
2 The set of all logical The set of all physical addresses
addresses generated by a corresponding to these logical
program is a logical address addresses is a physical address
space. space.
3 Logical addresses (in the Physical addresses (in the range R
range 0 to max) + 0 to R + max for a base value R)
4 User can view the logical User can never view the Physical
address of a program address of a program
5 User can use the logical User can indirectly access physical
address to access the address but not directly
physical address
6 Logical address is variable Physical address of that object
hence will changing with always remains constant.
the system
Contiguous Memory Allocation :
One of the simplest method for memory allocation is to divide memory
in to several fixed partition.
Main memory is usually divided into two partitions:
o Resident operating system, usually held in low memory with
interrupt vector.
o User processes, held in high memory.
Each partition contains exactly one process.
The degree of multi-programming depends on the number of
partitions.
Memory mapping and Protection:-
Memory protection means protecting the OS from user process and
protecting process from one another.
Memory protection is provided by using a re-location register, with a
limit register.
Re-location register contains the values of smallest physical address
and limit register contains range of logical addresses. (Re-location =
100040 and limit = 74600).
The logical address must be less than the limit register, the MMU maps
the logical address dynamically by adding the value in re-location
register. When the CPU scheduler selects a process for execution, the
dispatcher loads the re-location and limit register with correct values
as a part of context switch.
Since every address generated by the CPU is checked against these
register we can protect the OS and other users programs and data from
being modified.
Memory Allocation
Multiple-partition allocation:
Hole – block of available memory; holes of various size are scattered
throughout memory.
When a process arrives, it is allocated memory from a hole large
enough to accommodate it.
Operating system maintains information about:
1. Allocated partitions
2. Free partitions (hole)
A set of holes of various sizes, is scattered throughout memory at any
given time. When a process arrives and needs memory, the system
searches this set for a hole that is large enough for this process.
If the hole is too large, it is split into two:
o One part is allocated to the arriving process;
o The other is returned to the set of holes.
When a process terminates, it releases its block of memory, which is
then placed back in the set of holes.
If the new hold is adjacent to other holes, these adjacent holes are
merged to form one larger hole.
This procedure is a particular instance of the general dynamic storage
allocation problem, which is how to satisfy a request of size n from a
list of free holes.
There are many solutions to this problem. The set of holes is searched
to determine which hole is best to allocate. The first-fit, best-fit and
worst-fit strategies are the most common ones used to select a free
hole from the set of available holes.
The three strategies to select a free hole:-
First fit:- Allocates first hole that is big enough. This algorithm scans
memory from the beginning and selects the first available block that is
large enough to hold the process.
Best fit:- It chooses the hole i.e., closest in size to the request. It
allocates the smallest hole i.e., big enough to hold the process.
Worst fit:- It allocates the largest hole to the process request. It
searches for the largest hole in the entire list.
First fit and best fit are the most popular algorithms for dynamic
memory allocation.
First fit is generally faster.
Best fit searches for the entire list to find the smallest hole i.e., large
enough.
Worst fit reduces the rate of production of smallest holes.
All these algorithms suffer from fragmentation.
Internal and External fragmentation and Compaction
As processes are loaded and removed from memory, the free memory
space is broken into little pieces. After some time that process which we
removed from memory cannot be allocated because of small pieces of
memory and memory block remains unused. This problem is called
Fragmentation.
(OR)
Fragmentation occurs when memory is allocated
sequentially/contiguously in the system. Different files are stored as chunks
in the memory. When these files are frequently modified or deleted,
gaps/free spaces are left in between, this is called fragmentation.
Memory fragmentation can be of two types:-
Internal Fragmentation
External Fragmentation
Internal Fragmentation:
There is wasted space internal to a portion due to the fact that block of
data loaded is smaller than the partition.
The scenario is where a free space is too small for any file to fit into.
The overhead to keep a track of that free space is too much for the operating
system. This is called internal fragmentation.
Example:- If there is a block of 50kb and if the process requests 40kb and if
the block is allocated to the process then there will be 10kb of memory left.
External Fragmentation
Exists when there is enough memory space exists to satisfy the request,
but it not contiguous i.e., storage is fragmented in to large number of small
holes. External Fragmentation may be either minor or a major problem.
As shown in the diagram, if we want a file to fit into the memory which
is equal to the sum of the freed space, we cannot as the space is not
contiguous. Therefore even though there is enough memory to hold the file
we cannot fit it due to the memory being scattered at different places. This
is external fragmentation.
One solution for over-coming external fragmentation is compaction.
o The goal is to move all the free memory together to form a large
block.
o Compaction is not possible always. If the re-location is static and
is done at load time then compaction is not possible.
o Compaction is possible if the re-location is dynamic and done at
execution time.
Another possible solution to the external fragmentation problem is to
permit the logical address space of a process to be non-contiguous,
thus allowing the process to be allocated physical memory whenever the
latter is available.
Paging
Paging is a memory management scheme that permits the physical
address space of a process to be non-contiguous. Support for paging is
handled by hardware. It is used to avoid external fragmentation.
Paging avoids the considerable problem of fitting the varying sized
memory chunks on to the backing store. When some code or date residing in
main memory need to be swapped out, space must be found on backing
store.
Basic Method:-
Physical memory is broken in to fixed sized blocks called frames (f).
Logical memory is broken in to blocks of same size called pages (p).
When a process is to be executed its pages are loaded in to available
frames from backing store.
The blocking store is also divided in to fixed-sized blocks of same size
as memory frames.
Logical address generated by the CPU is divided in to two parts: page
number (p) and page offset (d).
The page number (p) is used as index to the page table. The page table
contains base address of each page in physical memory. This base
address is combined with the page offset to define the physical
memory i.e., sent to the memory unit.
The following figure shows paging hardware:-
The page size is defined by the hardware. The size of a power of 2,
varying between 512 bytes and 10Mb per page.
If the size of logical address space is 2m address unit and page size is
2n, then high order m-n designates the page number and n low order
bits represents page offset.
Example:-
To show how to map logical memory in to physical memory consider a page
size of 4 bytes and physical memory of 32 bytes (8 pages).
Logical address 0 is page 0 and offset 0. Page 0 is in frame 5. The logical
address 0 maps to physical address 20. [(5*4) + 0].
Logical address 3 is page 0 and offset 3 maps to physical address 23
[(5*4) + 3].
Logical address 4 is page 1 and offset 0 and page 1 is mapped to frame 6.
So logical address 4 maps to physical address 24 [(6*4) + 0].
Logical address 13 is page 3 and offset 1 and page 3 is mapped to frame
2. So logical address 13 maps to physical address 9 [(2*4) + 1].
When a process arrives in the system to be executed, its size, expressed in
pages, is examined. Each page of the process needs one frame. Thus, if the
process requires n pages, at least n frames must be available in memory. If n
frames are available, they are allocated to this arriving process. The first
page of the process is loaded into one of the allocated frames, and the frame
number is put in the page table for this process. The next page is loaded into
another frame, its frame number is put into the page table, and so on
Since the operating system is managing physical memory, it must be aware
of the allocation details of physical memory—which frames are allocated,
which frames are available, how many total frames there are, and so on. This
information is generally kept in a data structure called a frame table. The
frame table has one entry for each physical page frame, indicating whether
the latter is free or allocated and, if it is allocated, to which page of which
process or processes.
Address Translation:
Page address is called logical address and represented by page number
and the offset.
Logical Address = Page number + page offset
Frame address is called physical address and represented by a frame
number and the offset.
Physical Address = Frame number + page offset
A data structure called page map table is used to keep track of the relation
between a page of a process to a frame in physical memory.
When the system allocates a frame to any page, it translates this logical
address into a physical address and create entry into the page table to be
used throughout execution of the program.
When a process is to be executed, its corresponding pages are loaded into
any available memory frames. Suppose you have a program of 8Kb but your
memory can accommodate only 5Kb at a given point in time, then the
paging concept will come into picture. When a computer runs out of RAM,
the operating system (OS) will move idle or unwanted pages of memory to
secondary memory to free up RAM for other processes and brings them
back when needed by the program.
This process continues during the whole execution of the program where
the OS keeps removing idle pages from the main memory and write them
onto the secondary memory and bring them back when required by the
program.
Advantages and Disadvantages of Paging
Paging reduces external fragmentation, but still suffer from internal
fragmentation.
Paging is simple to implement and assumed as an efficient memory
management technique.
Due to equal size of the pages and frames, swapping becomes very
easy.
Page table requires extra memory space, so may not be good for a
system having small RAM.
Virtual Memory
It is a technique which allows execution of process that may not be compiled
within the primary memory.
It separates the user logical memory from the physical memory.
This separation allows an extremely large memory to be provided for
program when only a small physical memory is available.
Virtual memory makes the task of programming much easier because
the programmer no longer needs to working about the amount of the
physical memory is available or not.
The virtual memory allows files and memory to be shared by different
processes by page sharing.
It is most commonly implemented by demand paging.
The virtual address space of a process refers to the logical (or virtual) view
of how a process is stored in memory. Typically, this view is that a process
begins at a certain logical address—say, address 0—and exists in contiguous
memory
Demand paging
A demand paging system is similar to the paging system with swapping
feature.
When we want to execute a process we swap it into the memory. A
swapper manipulates entire process where as a pager is concerned
with the individual pages of a process.
The demand paging concept is using pager rather than swapper.
When a process is to be swapped in, the pager guesses which pages will
be used before the process is swapped out again.
Instead of swapping in a whole process, the pager brings only those
necessary pages into memory.
The transfer of a paged memory to contiguous disk space is shown in below
figure.
Thus it avoids reading into memory pages that will not used any way
decreasing the swap time and the amount of physical memory needed.
In this technique we need some hardware support to distinct between
the pages that are in memory and those that are on the disk.
A valid and invalid bit is used for this purpose.
When this bit is set to valid it indicates that the associate page is in
memory.
If the bit is set to invalid it indicates that the page is either not valid or
is valid but currently not in the disk.
Marking a page invalid will have no effect if the process never attempts
to access that page.
So while a process executes and access pages that are memory
resident, execution proceeds normally.
Access to a page marked invalid causes a page fault trap.
It is the result of the OS’s failure to bring the desired page into memory.
Procedure to handle page fault
If a process refers to a page that is not in physical memory then
We check an internal table (page table) for this process to determine
whether the reference was valid or invalid.
If the reference was invalid, we terminate the process, if it was valid
but not yet brought in, we have to bring that from main memory.
Now we find a free frame in memory.
Then we read the desired page into the newly allocated frame.
When the disk read is complete, we modify the internal table to
indicate that the page is now in memory.
We restart the instruction that was interrupted by the illegal address
trap. Now the process can access the page as if it had always been in
memory.
Hardware support:-
For demand paging the same hardware is required as paging and swapping.
1. Page table:- Has the ability to mark an entry invalid through valid-
invalid bit.
2. Secondary memory:- This holds the pages that are not present in
main memory. It’s a high speed disk.
Performance of demand paging:-
Demand paging can have significant effect on the performance of the
computer system.
Let P be the probability of the page fault (0<=P<=1) Effective access
time = (1-P) * ma + P * page fault. Where P = page fault and ma =
memory access time.
Effective access time is directly proportional to page fault rate. It is
important to keep page fault rate low in demand paging.
A page fault causes the following sequence to occur:-
1. Trap to the OS.
2. Save the user registers and process state.
3. Determine that the interrupt was a page fault.
4. Checks the page references were legal and determine the location of
page on disk.
5. Issue a read from disk to a free frame.
6. If waiting, allocate the CPU to some other user.
7. Interrupt from the disk.
8. Save the registers and process states of other users.
9. Determine that the interrupt was from the disk.
10. Correct the page table and other table to show that the desired page
is now in memory.
11. Wait for the CPU to be allocated to this process again.
12. Restore the user register process state and new page table then resume
the interrupted instruction.
Page Replacement policies
Each process is allocated frames (memory) which hold the process’s
pages (data)
Frames are filled with pages as needed – this is called demand paging
Over-allocation of memory is prevented by modifying the page-fault
service routine to replace pages
The job of the page replacement algorithm is to decide which page gets
victimized to make room for a new page
Page replacement completes separation of logical and physical memory
Page Replacement
Demand paging shares the I/O by not loading the pages that are never
used.
Demand paging also improves the degree of multiprogramming by
allowing more process to run at the some time.
Page replacement policy deals with the solution of pages in memory to
be replaced by a new page that must be brought in. When a user
process is executing a page fault occurs.
The hardware traps to the operating system, which checks the internal
table to see that this is a page fault and not an illegal memory access.
The operating system determines where the derived page is residing
on the disk, and this finds that there are no free frames on the list of
free frames.
When all the frames are in main memory, it is necessary to bring a new
page to satisfy the page fault, replacement policy is concerned with
selecting a page currently in memory to be replaced.
The page i,e to be removed should be the page i,e least likely to be
referenced in future.
When a user process is executing a page fault occurs. The OS
determines where the desired pages is residing on disk but finds that
there are no free frames on the free frame list i.e. all memory is in use
as shown in the above figure.
The OS has several options at this point. It can terminate the process.
The OS could instead swap out a process by freeing all its frames and
reducing the degree of multiprogramming.
Working of Page Replacement Algorithm
1. Find the location of derived page on the disk.
2. Find a free frame
a. If there is a free frame, use it.
b. Otherwise, use a replacement algorithm to select a victim.
c. Write the victim page to the disk; change the page and frame
tables accordingly.
3. Read the desired page into the free frame; change the page and
frame tables.
4. Restart the user process.
Victim Page
The page that is supported out of physical memory is called victim page.
If no frames are free, the two page transforms come (out and one in)
are read. This will see the effective access time.
Each page or frame may have a dirty (modify) bit associated with the
hardware. The modify bit for a page is set by the hardware whenever
any word or byte in the page is written into, indicating that the page
has been modified.
When we select the page for replacement, we check its modify bit. If the
bit is set, then the page is modified since it was read from the disk.
If the bit was not set, the page has not been modified since it was read
into memory. Therefore, if the copy of the page has not been modified
we can avoid writing the memory page to the disk, if it is already there.
Some pages cannot be modified.
We must solve two major problems to implement demand paging:
We must develop a frame allocation algorithm and a page replacement
algorithm.
If we have multiple processors in memory, we must decide how many
frames to allocate and page replacement is needed.
Page Replacement Algorithm
It decides which memory page to page will be swapped out when a page of
memory needs to be allocated.
Page Fault: The CPU demanded page is not present in memory.
Page Hit: The CPU demanded page is present in memory.
There are 3 different types:
1. FIFO (First In First Out)
2. Optimal
3. LRU (Least Recently Used)
FIFO (First In First Out) algorithm:
This is the simplest page replacement algorithm. A FIFO replacement
algorithm associates each page the time when that page was brought
into memory.
When a Page is to be replaced the oldest one is selected.
We replace the queue at the head of the queue. When a page is brought
into memory, we insert it at the tail of the queue.
Example: Consider the following references string with frames initially
empty.
The first three references (7,0,1) cases page faults and are brought into
the empty frames.
The next references 2 replaces page 7 because the page 7 was brought
in first.
Since 0 is the next references and 0 is already in memory e has no page
faults.
The next references 3 results in page 0 being replaced so that the next
references to 0 causer page fault. This will continue till the end of
string. There are 15 faults all together.
Analysis:
Number of Page references = 20
Number of Page faults = 15
Number of Page Hits = 5
Page Hit Ratio = Number of Page Hits / Total Number of Page references
= (5 / 20)*100
= 25%
Page Fault Ratio = Number of Page Faults / Total Number of Page references
= (15 / 20)*100
= 75%
Belady’s Anamoly:
For some page replacement algorithm, the page fault may increase as the
number of allocated frames increases. FIFO replacement algorithm may face
this problem.
Optimal Algorithm:
Optimal page replacement algorithm is mainly to solve the problem of
Belady’s Anamoly.
Ideally we want to select an algorithm with the lowest page-fault rate
Such an algorithm exists, and is called (unsurprisingly) the optimal
algorithm:
Procedure: replace the page that will not be used for the longest time
(or at all) – i.e. replace the page with the greatest forward distance in
the reference string
Example: Consider the following references string with frames initially
empty.
The first three references cause faults that fill the three empty frames.
The references to page 2 replaces page 7, because 7 will not be used
until reference 18.
The page 0 will be used at 5 and page 1 at 14.
With only 9 page faults, optimal replacement is much better than a
FIFO, which had 15 faults.
This algorithm is difficult t implement because it requires future
knowledge of reference strings.
Analysis:
Number of Page references = 20
Number of Page faults = 09
Number of Page Hits = 11
Page Hit Ratio = Number of Page Hits / Total Number of Page references
= (11 / 20)*100
= 55%
Page Fault Ratio = Number of Page Faults / Total Number of Page references
= (9 / 20)*100
= 45%
Least Recently Used (LRU) Algorithm:
If the optimal algorithm is not feasible, an approximation to the
optimal algorithm is possible.
The main difference b/w OPTS and FIFO is that; FIFO algorithm uses
the time when the pages was built in and OPT uses the time when a
page is to be used.
The LRU algorithm replaces the pages that have not been used for
longest period of time.
The LRU associated its pages with the time of that pages last use.
This strategy is the optimal page replacement algorithm looking
backward in time rather than forward.
Example: Consider the following references string with frames initially
empty.
The first 5 faults are similar to optimal replacement.
When reference to page 4 occurs, LRU sees that of the three frames,
page 2 as used least recently.
The most recently used page is page 0 and just before page 3 was used.
The LRU policy is often used as a page replacement algorithm and
considered to be good.
Analysis:
Number of Page references = 20
Number of Page faults = 12
Number of Page Hits = 08
Page Hit Ratio = Number of Page Hits / Total Number of Page references
= (8 / 20)*100
= 40%
Page Fault Ratio = Number of Page Faults / Total Number of Page references
= (12 / 20)*100
= 60%
UNIT V
I/O Management Principles of I/O Hardware: Disk structure, Disk
scheduling algorithm
File Management: Access methods, File types, File operation, Directory
structure, File System structure, Allocation methods, Free-space
management, and directory implementation.
Structure of Linux Operating System, Exploring the Directory
Structure, Naming Files and Directories, Concept of shell, Types of shell,
Editors for shell programming (e.g. vi), basics of Shell programming.
Principles of I/O Hardware:
Computers operate a great many kinds of devices. General types
include storage devices (disks, tapes), transmission devices (network
cards, modems), and human-interface devices (screen, keyboard,
mouse).
A device communicates with a computer system by sending signals
over a cable or even through the air.
The device communicates with the machine via a connection point
termed a port (for example, a serial port).
If one or more devices use a common set of wires, the connection is
called a bus.
When device A has a cable that plugs into device B, and device B has a
cable that plugs into device C, and device C plugs into a port on the
computer, this arrangement is called a daisy chain. It usually operates
as a bus.
A typical PC bus structure appears in the Figure, a PCI bus (the
common PC system bus) connects the processor–memory subsystem
to fast devices, and an expansion bus connects relatively slow devices,
such as the keyboard and serial and USB ports.
In the upper-right portion of the figure, four disks are connected
together on a Small Computer System Interface (SCSI) bus plugged into
a SCSI controller. Other common buses used to interconnect main parts
of a computer include PCI Express (PCIe), with throughput of up to 16
GB per second, and Hyper Transport, with throughput of up to 25 GB
per second.
A controller is a collection of electronics that can operate a port, a bus,
or a device.
A serial-port controller is an example of a simple device controller. It
is a single chip in the computer that controls the signals on the wires of
a serial port.
The SCSI bus controller is often implemented as a separate circuit
board (a host adapter) that plugs into the computer. It typically
contains a processor, microcode and some private memory to enable it
to process the SCSI protocol messages.
Some devices have their own built-in controllers.
An I/O port typically consists of four registers, called:
o Status Register
o Control Register
o Data-In Register
o Data-Out Register.
The status register contains bits that can be read by the host. These
bits indicate states such as :
o Whether the current command has completed
o Whether a byte is available to be read from the data-in register.
o Whether there has been a device error.
The control register can be written by the host to start a command or
to change the mode of a device.
o For instance, a certain bit in the control register of a serial port
chooses between full-duplex and half-duplex communication,
o Another enables parity checking.
o A third bit sets the word length to 7 or 8 bits.
o Other bits select one of the speeds supported by the serial port.
The data-in register is read by the host to get input
The data out register is written by the host to send output.
The data registers are typically 1 to 4 bytes.
Some controllers have FIFO chips that can hold several bytes of input
or output data to expand the capacity of the controller beyond the size
of the data register.
A FIFO chip can hold a small burst of data until the device or host is
able to receive those data.
POLLING:
Incomplete protocol for interaction between the host and a controller
can be intricate, but the basic handshaking notion is simple.
The controller indicates its state through the busy bit in the status
register. (Recall that to set a bit means to write a 1 into the bit, and to
clear a bit mean to write a 0 into it.)
The controller sets the busy bit when it is busy working and clears the
busy bit when it is ready to accept the next command.
The host signals its wishes via the command-ready bit in the command
register.
The host sets the command-ready bit when a command is available for
the controller to execute.
For this example, the host writes output through a port, coordinating
with the controller by handshaking as follows.
1. The host repeatedly reads the busy bit until that bit becomes clear.
2. The host sets the write bit in the command register and writes a
byte into the data-out register.
3. The host sets the command-ready bit.
4. When the controller notices that the command-ready bit is set, it
sets the Busy.
5. The controller reads the command register and sees the write
command.
6. It reads the data-out register to get the byte, and does the I/O to the
device.
7. The controller clears the command-ready bit, clears the error bit in
the status register to indicate that the device I/O succeeded, and
clears the busy bit to indicate that it is finished.
The host is busy-waiting or polling: It is in a loop, reading the status register
over and over until the busy bit becomes clear. If the controller and device
are fast, this method is a reasonable one. But if the wait may be long, the
host should probably switch to another task
I/O Devices
Categories of I/O Devices
1. Human readable
2. Machine readable
3. Communication
1. Human Readable is suitable for communicating with the computer
user. Examples are printers, video display terminals, keyboard etc.
2. Machine Readable is suitable for communicating with electronic
equipment. Examples are disk and tape drives, sensors, controllers and
actuators.
3. Communication is suitable for communicating with remote devices.
Examples are digital line drivers and modems.
Differences between I/O Devices
1. Data Rate: there may be differences of several orders of magnitude
between the data transfer rates.
2. Application: Different devices have different use in the system.
3. Complexity of Control: A disk is much more complex whereas printer
requires simple control interface.
4. Unit of transfer: Data may be transferred as a stream of bytes or
characters or in larger blocks.
5. Data representation: Different data encoding schemes are used for
different devices.
6. Error Conditions: The nature of errors differs widely from one device
to another.
Direct Memory Access
A special control unit may be provided to allow transfer of a block of
data directly between an external device and the main memory,
without continuous intervention by the processor. This approach is
called Direct Memory Access (DMA).
DMA can be used with either polling or interrupt software.
DMA is particularly useful on devices like disks, where many bytes of
information can be transferred in single I/O operations.
When used in conjunction with an interrupt, the CPU is notified only
after the entire block of data has been transferred.
For each byte or word transferred, it must provide the memory
address and all the bus signals that control the data transfer.
Interaction with a device controller is managed through a device
driver.
Device drivers are part of the operating system, but not necessarily
part of the OS kernel.
The operating system provides a simplified view of the device to user
applications (e.g., character devices vs. block devices in UNIX).
In some operating systems (e.g., Linux), devices are also accessible
through the /dev file system.
In some cases, the operating system buffers data that are transferred
between a device and a user space program (disk cache, network
buffer).
This usually increases performance, but not always.
Device Controllers
A computer system contains a multitude of I/O devices and their respective
controllers:
Network card
Graphics adapter
Disk controller
DVD-ROM controller
Serial port
USB
Sound card
Disk structure:
Disk drives are addressed as large 1-dimensional arrays of logical
blocks, where the logical block is the smallest unit of transfer.
The size of a logical block is usually 512 bytes.
The 1-dimensional array of logical blocks is mapped into the sectors of
the disk sequentially.
o Sector 0 is the first sector of the first track on the outermost
cylinder.
o Mapping proceeds in order through that track, then the rest of the
tracks in that cylinder, and then through the rest of the cylinders
from outermost to innermost.
o Logical to physical address should be easy. Except for bad sectors.
o Non-constant # of sectors per track via constant angular velocity.
Disks provide bulk of secondary storage of computer system.
The disk can be considered the one I/O device that is common to each
and every computer.
Disks come in many size and speeds, and information may be stored
optically or magnetically.
Magnetic tape was used as an early secondary storage medium, but the
access time is much slower than for disks.
For backup, tapes are currently used.
Modern disk drives are addressed as large one dimensional arrays of
logical blocks, where the logical block is the smallest unit of transfer.
The actual details of disk I/O operation depends on the computer
system, the operating system and the nature of the I/O channel and
disk controller hardware.
The basic unit of information storage is a sector.
The sectors are stored on a flat, circular, media disk.
This media spins close to one or more read/write heads.
The heads can move from the inner portion of the disk to the outer
portion.
When the disk drive is operating, the disk is rotating at constant speed.
To read or write, the head must be positioned at the desired track and
at the beginning of the desired sector on that track.
Track selection involves moving the head in a movable head system or
electronically selecting one head on a fixed head system.
These characteristics are common to floppy disks, hard disks, CD-ROM
and DVD.
Disk Performance Parameters
When the disk drive is operating, the disk is rotating at constant speed.
To read or write, the head must be positioned at the desired track and
at the beginning of the desired sector on that track.
Track selection involves moving the head in a movable-head system or
electronically selecting one head on a fixed-head system.
On a movable head system, the time it takes to position the head at the
track is known as seek time.
When once the track is selected, the disk controller waits until the
appropriate sector rotates to line up with the head. The time it takes
for the beginning of the sector to reach the head is known as
rotational delay or rotational latency.
The sum of the seek time, if any, and the rotational delay equals the
access time, which is the time it takes to get into position to read or
write.
Once the head is in position, the read or write operation is then
performed as the sector moves under the head; this is the data transfer
portion of the operation; the time required for the transfer is the
transfer time.
Seek Time:
o Seek time is the time required to move the disk arm to the
required track. It turns out that this is a difficult quantity to pin
down.
o The seek time consists of two key components:
The initial startup time.
The time taken to traverse the tracks that have to be crossed
once the access arm is up to speed.
Ts = m * n + s
Where,
Ts = seek time.
n = number of track traversed.
m = constant that depends on the disk drive
s = startup time.
Rotational Latency:
o Rotational latency is the additional addition time for waiting for
the disk to rotate to the desired sector to the disk head.
Rotational Delay:
o Disks, other than floppy disks, rotate at speeds ranging from 3600
rpm up to, as of this writing, 15,000 rpm; at this latter speed,
there is one revolution per 4 ms. Thus, on the average, the
rotational delay will be 2 ms.
o Floppy disks typically rotate at between 300 and 600 rpm. Thus
the average delay will be between 100 and 50 ms.
Disk Bandwidth:
o Disk bandwidth is the total number of bytes transferred divided
by total time between the first request for service and the
completion of last transfer.
Transfer Time:
o The transfer time to or from the disk depends on the rotation
speed of the disk in the following fashion:
T= b/rN
Where,
T = Transfer time
b = Number of bytes to be transferred
N = Number of bytes on a track
r = Rotation speed, in revolutions per second
Thus the total average access time can be expressed as
Ta = Ts + 1/ 2r + b/ rN
Total capacity of the disk:
o It is calculated by using following formula:
Number of Number of Number of Number of
Cylinders * heads * sector/track * bytes/sector
Disk Scheduling:
The amount of head needed to satisfy a series of I/O request can affect
the performance.
If desired disk drive and controller are available, the request can be
serviced immediately.
If a device or controller is busy, any new requests for service will be
placed on the queue of pending requests for that drive.
When one request is completed, the operating system chooses which
pending request to service next.
Different types of scheduling algorithms are as follows.
1. First Come, First Served scheduling algorithm (FCFS).
2. Shortest Seek Time First (SSTF) algorithm
3. SCAN algorithm
4. Circular SCAN (C-SCAN) algorithm
5. Look Scheduling Algorithm
First Come, First Served scheduling algorithm (FCFS).
The simplest form of scheduling is first-in-first-out (FIFO) scheduling,
which processes items from the queue in sequential order.
This services the request in the order they are received.
This algorithm is fair but do not provide fastest service.
It takes no special time to minimize the overall seek time.
This strategy has the advantage of being fair, because every request is
honored and the requests are honored in the order received.
With FIFO, if there are only a few processes that require access and if
many of the requests are to clustered file sectors, then we can hope for
good performance.
Example: consider a disk queue with request for I/O to blocks on cylinders.
98, 183, 37, 122, 14, 124, 65, 67.
If the disk head is initially at 53, it will first move from 53 to 98 then to
183 and then to 37, 122, 14, 124, 65, 67 for a total head movement of
640 cylinders.
The wild swing from 122 to 14 and then back to 124 illustrates the
problem with this schedule.
If the requests for cylinders 37 and 14 could be serviced together
before or after 122 and 124 the total head movement could be
decreased substantially and performance could be improved.
Shortest Seek Time First (SSTF) algorithm
Shortest Seek Time First selects the request with the minimum seek
time from the current head position.
SSTF scheduling is a form of SJF scheduling; may cause starvation of
some requests.
Since seek time increases with the number of cylinders traversed by
head, SSTF chooses the pending request closest to the current head
position.
Illustration shows total head movement of 236 cylinders.
Example: consider a disk queue with request for I/O to blocks on cylinders.
98, 183, 37, 122, 14, 124, 65, 67.
If the disk head is initially at 53, the closest is at cylinder 65, then 67,
then 37 is closer than 98 to 67.
So it services 37, continuing we service 14, 98, 122, 124 and finally
183.
The total head movement is only 236 cylinders.
SSTF is essentially a form of SJF and it may cause starvation of some
requests. SSTF is a substantial improvement over FCFS, it is not
optimal.
SCAN algorithm
SCAN algorithm Sometimes called the elevator algorithm.
The scan algorithm has the head start at track 0 and move towards the
highest numbered track, servicing all requests for a track as it passes
the track.
The disk arm starts at one end of the disk, and moves toward the other
end, servicing requests until it gets to the other end of the disk, where
the head movement is reversed and servicing continues.
Illustration shows total head movement of 208 cylinders.
But note that if requests are uniformly dense, largest density at other
end of disk and those wait the longest.
Example: consider a disk queue with request for I/O to blocks on cylinders.
98, 183, 37, 122, 14, 124, 65, 67.
If the disk head is initially at 53 and if the head is moving towards 0, it
services 37 and then 14.
At cylinder 0 the arm will reverse and will move towards the other end
of the disk servicing 65, 67, 98, 122, 124 and 183.
If a request arrives just in from of head, it will be serviced immediately
and the request just behind the head will have to wait until the arms
reach other end and reverses direction.
Circular SCAN (C-SCAN) algorithm:
C-SCAN is a variant of SCAN designed to provide a more uniform wait
time.
The C-SCAN policy restricts scanning to one direction only.
Like SCAN, C-SCAN moves the head from end of the disk to the other
servicing the request along the way.
When the head reaches the other end, it immediately returns to the
beginning of the disk, without servicing any request on the return.
The C-SCAN treats the cylinders as circular list that wraps around from
the final cylinder to the first one.
This reduces the maximum delay experienced by new requests.
Example: consider a disk queue with request for I/O to blocks on cylinders.
98, 183, 37, 122, 14, 124, 65, 67.
Look Scheduling Algorithm:
Both SCAN and C-SCAN move the disk arm across the full width of the
disk.
Start the head moving in one direction. Satisfy the request for the
closest track in that direction when there is no more request in the
direction, the head is traveling, reverse direction and repeat. This
algorithm is similar to innermost and outermost track on each circuit.
In practice neither of the algorithms is implemented in this way.
The arm goes only as far as the final request in each direction. Then it
reverses, without going all the way to the end of the disk.
These versions of SCAN and CSCAN are called Look and C-Look
scheduling because they look for a request before continuing to move
in a given direction.
Example: consider a disk queue with request for I/O to blocks on cylinders.
98, 183, 37, 122, 14, 124, 65, 67.
Selecting a Disk-Scheduling Algorithm
SSTF is common and it increases performance over FCFS.
SCAN and C-SCAN algorithm is better for systems that place a heavy
load on disk.
SCAN and C-SCAN have less starvation problem.
SSTF or Look is a reasonable choice for a default algorithm.
Performance depends on the number and types of requests.
Requests for disk service can be influenced by the file-allocation
method.
The disk-scheduling algorithm should be written as a separate module
of the operating system, allowing it to be replaced with a different
algorithm if necessary Either SSTF or LOOK is a reasonable choice for
the default algorithm.
File Management
A file is a collection of similar records.
The data can’t be written on to the secondary storage unless they are
within a file.
Files represent both the program and the data. Data can be numeric,
alphanumeric, alphabetic or binary.
Many different types of information can be stored on a file ---Source
program, object programs, executable programs, numeric data, payroll
recorder, graphic images, sound recordings and so on.
File Attributes
File attributes varies from one OS to other. The common file attributes are:
1. Name:- The symbolic file name is the only information kept in human
readable form.
2. Identifier:- The unique tag, usually a number, identifies the file within
the file system. It is the non-readable name for a file.
3. Type:- This information is needed for those systems that supports
different types.
4. Location:- This information is a pointer to a device and to the location
of the file on that device.
5. Size:- The current size of the file and possibly the maximum allowed
size are included in this attribute.
6. Protection:- Access control information determines who can do
reading, writing, execute and so on.
7. Time, data and User Identification:- This information must be kept
for creation, last modification and last use. These data are useful for
protection, security and usage monitoring.
File types:
A file has a certain defined structures according to its type:-
1. Text file:- Text file is a sequence of characters organized in to lines.
2. Object file:- Object file is a sequence of bytes organized in to blocks
understandable by the systems linker.
3. Executable file:- Executable file is a series of code section that the
loader can bring in to memory and execute.
4. Source File:- Source file is a sequence of subroutine and function, each
of which are further organized as declaration followed by executable
statements.
File operation:
File is an abstract data type. To define a file we need to consider the
operation that can be performed on the file.
Basic operations of files are:-
1. Creating a file:- Two steps are necessary to create a file. First space in
the file system for file is found. Second an entry for the new file must be
made in the directory. The directory entry records the name of the file
and the location in the file system.
2. Writing a file:- System call is mainly used for writing in to the file.
System call specify the name of the file and the information i.e., to be
written on to the file. Given the name the system search the entire
directory for the file. The system must keep a write pointer to the
location in the file where the next write to be taken place.
3. Reading a file:- To read a file system call is used. It requires the name
of the file and the memory address. Again the directory is searched for
the associated directory and system must maintain a read pointer to
the location in the file where next read is to take place.
4. Delete a file:- System will search for the directory for which file to be
deleted. If entry is found it releases all free space. That free space can
be reused by another file.
5. Truncating the file:- User may want to erase the contents of the file
but keep its attributes. Rather than forcing the user to delete a file and
then recreate it, truncation allows all attributes to remain unchanged
except for file length.
6. Repositioning within a file:- The directory is searched for
appropriate entry and the current file position is set to a given value.
Repositioning within a file does not need to involve actual i/o. The file
operation is also known as file seeks.
In addition to this basis 6 operations the other two operations include
appending new information to the end of the file and renaming the existing
file. These primitives can be combined to perform other two operations.
Most of the file operation involves searching the entire directory for the
entry associated with the file. To avoid this OS keeps a small table containing
information about an open file (the open table). When a file operation is
requested, the file is specified via index in to this table. So searching is not
required.
Several piece of information are associated with an open file:-
File pointer:- on systems that does not include offset an a part of the
read and write system calls, the system must track the last read-write
location as current file position pointer. This pointer is unique to each
process operating on a file.
File open count:- As the files are closed, the OS must reuse its open file
table entries, or it could run out of space in the table. Because multiple
processes may open a file, the system must wait for the last file to close
before removing the open file table entry. The counter tracks the
number of copies of open and closes and reaches zero to last close.
Disk location of the file:- The information needed to locate the file on
the disk is kept in memory to avoid having to read it from the disk for
each operation.
Access rights:- Each process opens a file in an access mode. This
information is stored on per-process table the OS can allow OS deny
subsequent I/O request.
Access Methods:
The information in the file can be accessed in several ways. Different file
access methods are:-
1. Sequential Access:-
Sequential access is the simplest access method. Information in the file is
processed in order, one record after another. Editors and compilers access
the files in this fashion.
Normally read and write operations are done on the files. A read operation
reads the next portion of the file and automatically advances a file pointer,
which track next i/I track.
Write operation appends to the end of the file and such a file can be next to
the beginning.
Sequential access depends on a tape model of a file.
2. Direct Access OR relative access
Direct access allows random access to any file block.
This method is based on disk model of a file.
A file is made up of fixed length logical records. It allows the program
to read and write records rapidly in any order.
A direct access file allows arbitrary blocks to be read or written.
Example:-User may need block 13, then read block 99 then write block 12.
For searching the records in large amount of information with immediate
result, the direct access method is suitable. Not all OS support sequential and
direct access. Few OS use sequential access and some OS uses direct access.
It is easy to simulate sequential access on a direct access but the reverse is
extremely inefficient.
Direct access is based on disk model.
Indexing Method:-
The index is like an index at the end of a book which contains pointers
to various blocks.
To find a record in a file, we search the index and then use the pointer
to access the file directly and to find the desired record.
With large files index file itself can be very large to be kept in memory. One
solution to create an index to the index files itself. The primary index file
would contain pointer to secondary index files which would point to the
actual data items.
Two types of indexes can be used:-
Exhaustive index:- Contain one entry for each of record in the main
file. An index itself is organized as a sequential file.
Partial index:- Contains entries to records where the field of interest
exists with records of variable length, some record will not contain an
fields. When a new record is added to the main file, all index files must
be updated.
Directory structure:
File System structure:
Allocation methods:
Free-space management and Directory implementation:
Structure of Linux Operating System:
Exploring the Directory Structure:
Naming Files and Directories:
Concept of shell:
Types of shell:
Editors for shell programming (e.g. vi):
Basics of Shell programming: