0% found this document useful (0 votes)
688 views78 pages

Operating System

The document provides information about operating systems including: 1) It discusses the functions and types of operating systems such as multi-user, single-user, batch systems, and real-time systems. 2) It describes operating system concepts such as memory management, process management, CPU scheduling, and deadlock handling. 3) It outlines system software including assemblers, loaders, compilers, and their basic functions.

Uploaded by

abcd
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
688 views78 pages

Operating System

The document provides information about operating systems including: 1) It discusses the functions and types of operating systems such as multi-user, single-user, batch systems, and real-time systems. 2) It describes operating system concepts such as memory management, process management, CPU scheduling, and deadlock handling. 3) It outlines system software including assemblers, loaders, compilers, and their basic functions.

Uploaded by

abcd
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 78

OPERATING SYSTEM

SEMESTER : 5
PAPER 6 : Operating Systems And System Software
THEORY : 80 (EXAM) + 10 (IA) PRACTICALS :20 (EXAM)+10 (IA)

UNIT – 1

Fundamentals of operating system: An introduction, History,Types of operating system,


multi-user,single user,batch systems, multitasking, multiprogramming, real time systems

Operating system functions/services: System calls, System programs

Memory management : Swapping, Single contiguous technique, Partitioned allocation,


Paging, Segmentation with paging, Demand paging

UNIT – 2

Process management : Process concept,Process state, Process control block


CPU scheduling : CPU-I/O burst cycle, CPU schedulers,Scheduling queues,
Scheduling criteria

Scheduling algorithms : FCFS scheduling,SJF scheduling, Priority scheduling,


Round robin scheduling

Process synchronisation : Spooling, Semaphores

Deadlocks : Characterisation,Methods for handling deadlocks, Deadlock


Prevention, mutual exclusion , hold and wait,no pre-emption, circular wait,
Deadlock avoidance, safe state ,Banker’s algorithm, Deadlock detection, single
and several instances of resources type

Device scheduling : Disk scheduling,FCFS,SSTF(Shortest seek time first) ,Disk


management, formatting,Swap space management,use,location

UNIT - 3

System software: An introduction

Assembler: Functions, Pass1 and pass2 (no designing; only purposes)

Loaders: General loading scheme, Types of loading scheme, compile-and-go


(without designing),absolute loaders( with design),direct-linking loaders(without
designing)

Compilers: Functions, Phases and compilers, lexical analysis ,interpretation,


syntax analysis, optimisation, storage assignment , code generation , assembly
and output, Lexical and syntax analysis (in detail)

Dept of CS, GSC,Hassan Page 1


OPERATING SYSTEM

References :

• Silberschatz and Galvin- operating system concepts , John wiley and sons publication.
Chapters:
1.1,1.2,1.3,1.4,1.8,3.2,3.3,3.4,4.1,4.2.1,5.1,5.2,5.3,6.4,7.2,7.3,7.4,7.5,7.6,8.3,
8.4,8.5,8.7,9.2,13.2,13.3,13.4)
• Operating system and system programming by Dhamdhere, Tata McGraw-Hill
publishing company.
• Operating system by Tannebaum
• Operating systems by Godbole, publications:TataMcGraw-Hill.
• John J Donovan - system programming , Tata McGraw-Hill.
(Chapters: 3.2.1,5.1,5.1.1,5.1.3,5.1.6,5.2,8,1,8.1.6,8.2.1,8.2.2).

Dept of CS, GSC,Hassan Page 2


OPERATING SYSTEM

OPERATING SYSTEM
Computers need clear cut instructions to tell them what to do, how to do, and when to
do. A set of instructions to carry out these functions is called a program. A group of such
programs which are put into computer to operate and control its activities is called software.

Software is an essential requirement of computer systems. As a car cannot run without


fuel, a computer cannot work without software. Software is intangible but resides on something
tangible such as floppy disks, magnetic tapes.

Software may be broadly classified into two major categories:


System software: These are designed to help users to manage computer resources and to run
their application programs effectively. These are complex programs developed and supplied
by computer vendors. System software may include:
• Operating system
• Language translators
• Linker programs
• Loader programs
• Editor programs
• Utility programs

Application software: These are the software developed to carry out specific applications that
are used in many organizations. Application programs make the hardware to do useful work,
which include,
• Word processing packages
• Electronic spread sheet (ESS) programs
• DBMS
• Graphics software

Language translators-these are the programs required to translate the program written in
assembly or high level language to machine language.

Linker program: a linker program is system software that links program modules or segments
together so that they can refer to one another. It is responsible for producing an executable code
by linking the object code with other library functions.

Object code
Library function Linker Executable code

Loader program: a loader is a system program that loads programs stored in program library
into memory for execution.
Ex: bootstrap loader brings the O.S from the disk and loads it into the memory.

Editor programs: is a system program that is used to type in and edit text or program
statements. All operating systems contain their own editors.

Utility programs: there are some tasks that are common to a variety of applications. For
example, sorting a list in desired order, merging of two programs, copying a program from one

Dept of CS, GSC,Hassan Page 3


OPERATING SYSTEM

place to another. One need not write programs for these tasks. Such standard programs are
known as utility programs.

Operating System:

An operating system is a set of programs that controls a computer. It acts as an interface


between the computer’s hardware and the user program.

Some examples of operating systems are UNIX, MS-DOS, MS-Windows,


Windows/NT, OS/2,LINUX, XENIX, …...

User 1 User 2 User 3 User n

Compiler assembler text editor database systems

System programs and application programs

Operating system

Computer hardware

Controlling the computer involves software at different levels. We will differentiate


kernel services, library services, and application-level services, all of which are part of the
operating system.

Operating Systems are resource managers. The main resource is computer hardware in
the form of processors, storage, input/output devices, communication devices, and data. Some
of the operating system functions are: implementing the user interface, sharing hardware
among users, allowing users to share data among themselves, preventing users from interfering
with one another, scheduling resources among users, facilitating input/output, recovering from
errors, accounting for resource usage, facilitating parallel operations, organizing data for secure
and rapid access, and handling network communications.

One can view Operating Systems from two points of views: Resource manager and
Extended machines. From Resource manager point of view Operating Systems manage the
different parts of the system efficiently and from extended machines point of view Operating
Systems provide a virtual machine to users that is more convenient to use.

Objectives of Operating System: Modern Operating systems generally have following three
major goals.

▪ To make the computer system easy to use whether the user has got technical
• knowledge about the machine or not.
▪ To allocate resources to processes (Manage resources)
▪ Provide a pleasant and effective user interface.

Dept of CS, GSC,Hassan Page 4


OPERATING SYSTEM

History of Operating Systems

Historically operating systems have been tightly related to the computer architecture,
Operating systems have evolved through a number of distinct phases or generations which
corresponds roughly to the decades.

The 1940's - First Generations

The earliest electronic digital computers had no operating systems. Machines of the
time were so primitive that programs were often entered one bit at time on rows of mechanical
switches (plug boards). Programming languages were unknown (not even assembly
languages). Operating systems were unheard of.

The 1950's - Second Generation

The General Motors Research Laboratories implemented the first operating systems in
early 1950's for their IBM 701. The system of the 50's generally ran one job at a time. These
were called single-stream batch processing systems because programs and data were submitted
in groups or batches.

The 1960's - Third Generation

The systems of the 1960's were also batch processing systems, but they were able to
take better advantage of the computer's resources by running several jobs at once. So operating
systems designers developed the concept of multiprogramming in which several jobs are in
main memory at once; a processor is switched from job to job as needed to keep several jobs
advancing while keeping the peripheral devices in use.

Another major feature in third-generation operating system was the technique called
spooling (simultaneous peripheral operations on line). In spooling, a high-speed device like a
disk interposed between a running program and a low-speed device involved with the program
in input/output.

Another feature present in this generation was time-sharing technique, a variant of


multiprogramming technique, in which each user has an on-line (i.e., directly connected)
terminal. Because the user is present and interacting with the computer, the computer system
must respond quickly to user requests, otherwise user productivity could suffer. Timesharing
systems were developed to multiprogramming large number of simultaneous interactive user.

Fourth Generation

With the development of LSI (Large Scale Integration) circuits, chips, operating system
entered in the personal computer and the workstation age.

Operating Systems Services:

Following are the services provided by operating systems to the convenience of the users.
• Program Execution - The purpose of a computer system is to allow the user to execute
programs. So the operating system provides an environment where the user can
conveniently run programs. The user does not have to worry about the memory

Dept of CS, GSC,Hassan Page 5


OPERATING SYSTEM

allocation or multitasking or anything. These things are taken care of by the operating
systems.
• I/O Operations - Each program requires an input and produces output. This involves
the use of I/O. The operating systems hides the user the details of underlying hardware
for the I/O. Thus operating system by providing I/O makes it convenient for the users
to run programs.
• Data Management – it provides data management facilities such as data organization
and retrieval of data from secondary storage devices.
• File System Manipulation - The output of a program may need to be written into new
files or input taken from some files. The operating system provides this service. The
user does not have to worry about secondary storage management
• Communication -There are instances where processes need to communicate with each
other to exchange information. It may be between processes running on the same
computer or running on the different computers which tied together by an internet. By
providing this service the operating system establishes the communication between the
processes.
• Protection – O.S provides security mechanism to protect the owner’s information from
illegal or unauthorized usage in a multi-user system. Protection involves ensuring that
all access to system resources is controlled. Security of the system from outsiders is
also important.
• Error Detection – O.S needs to be aware of all possible errors. Errors may occur in
CPU or in memory or in I/O devices or error may due to power failure, arithmetic
overflow or attempt to access an illegal memory, error may in the user program etc. The
O.S should take the appropriate action to ensure the correct and consistent computing.
• Accounting – O.S provide means to keep track of which user’s use how many and
which kinds of computer resources. This record keeping may be used for accounting,
which is necessary to re-configure the system to improve computing services.

Types of Operating Systems:

Operating systems are designed not only to manage computer resources but also to
serve the users in a variety of environments. Depending on how they handle user programs,
operating systems are categorized into the following types:

• Single-user, single task - As the name implies, this operating system is designed to
manage the computer so that one user can effectively do one thing at a time. The Palm
OS for Palm handheld computers is a good example of a modern single-user, single-
task operating system.
• Single-user, multi-tasking - This is the type of operating system most people use on
their desktop and laptop computers today. Microsoft's Windows and Apple's MacOS
platforms are both examples of operating systems that will let a single user have several
programs in operation at the same time. For example, it's entirely possible for a
Windows user to be writing a note in a word processor while downloading a file from
the Internet while printing the text of an e-mail message.
• Multi-user - A multi-user operating system allows many different users to take
advantage of the computer's resources simultaneously. The operating system must
make sure that the requirements of the various users are balanced, and that each of the
programs they are using has sufficient and separate resources so that a problem with
one user doesn't affect the entire community of users.

Dept of CS, GSC,Hassan Page 6


OPERATING SYSTEM

Unix, VMS and mainframe operating systems, such as MVS, are examples of multi-
user operating systems.

Batch systems: Jobs with similar needs are batched together and run through the computer as
a group, by an operator or automatic job sequencer where no human intervention is needed.
Typically the processing of payrolls, electricity bills, invoices and daily transactions are dealt
with this way. Performance is increased by attempting to keep CPU and I/O devices busy at all
times through buffering, off-line operation, spooling, and multiprogramming.

Advantages:
1. Increases the utilization of cpu and i/o devices.
2. Operator intervention not required while running.

Disadvantages:
1. Increases turn around time of each job.
2. User cannot interact with the program during execution.

Multiprogramming OS: OS that can run two or more indepenent jobs concurrently by a single
processor called multiprogramming os .

This system keeps many jobs in memory simultaneously, picks up one and begins to
execute the job.If the job reads any i/o operation, the o.s. simply switches the cpu to another
job and starts executing it. Eventually, if the job(second job) needs to wait, the cpu is again
switched to another job and so on.In the mean while, if the first job finishes its i/o operation, it
gets the cpu back.Thus, the cpu switches back and forth among the jobs and is never idle. Thus
multiprogramming increases cpu utilization. Multiprogramming o.s. must make decisions for
the users,therefore they are fairly sophisticated.Multiprogramming systems make decisions for
job scheduling and cpu scheduling.

Note : If several jobs are ready to be bought into memory, and if there is no enough space for
all of them ,then the o.s. has to make decision to choose among them by ‘job scheduling’.
If several jobs are ready to run at the same time, the os must choose among them by ‘cpu
scheduling’.

Memory layout for a multiprogramming system is as shown :

OS

Job 1

Job2

Job n

Different states of a process in multiprogramming environment is as shown below:

Dept of CS, GSC,Hassan Page 7


OPERATING SYSTEM

IO Completion

New Ready Blocked

Resume CPU IO Wait

Running Terminate
Fig. 1

Advantages :
1. Efficient utilization of cpu and other resources
2. High throughput
3. Waiting time of each job is less

Disadvantage:
1. MPOS is considerably complex and expert engineers are required to design.

Time sharing or Multitasking system : Time sharing system is a logical extension of


multiprogramming system, allow many users to share the computer simultaneously by
switching the cpu among the users.The cpu is switched so frequently such that user can interact
with each program while it is running.

In time sharing system, the cpu will be allocated to each user for a small, fixed period
of time(generally the timeslots are defined by the o.s.) and it is switched from one job to another
either when there a natural break or when the allocated timeslot is over.Time shared systems
are much more complex than m.p.o.s. ,however multiprogramming and time sharing are the
central themes of modern o.s.
Ex. Unix,Multics

Different states of a process in time shared environment is as shown :


IO Completion

New Ready Blocked


Resume CPU
Expiry of IO Wait
time Slot
Running Terminate
Fig. 2

Real-time Operating Systems (RTOS) :RTOS is used when rigid time requirements have
been placed on the operation of a processor or flow of data. This system is often used as a
control device in dedicated applications. RTOS has well defined, fixed time constraints.
Processing must be done within the defined constraints otherwise the system fails.
RTOS are of two types:

Hard real-time systems: They are highly stringent systems which guarantees that critical tasks
be completed on-time. Here, the time constraint dictate the facilities available in the system.
Soft real-time systems: These are less stringent.Here, cirtical tasks get priority over other tasks
and retain that priority till they complete.Thus, the prime objective of this system is to provide
quick event response.

Dept of CS, GSC,Hassan Page 8


OPERATING SYSTEM

Advantages :
• Sytems that control scientific experiments , medical imaging systems , industrial control
systems, home appliance controllers , weapon systems are real-time systems.

Disadvantages :
• Hard real-time systems are highly stringent and fails if they are unable to respond within
the defined time constraint.
• Soft real-time systems are less restrictive and do not support deadline scheduling.

System Calls and System Programs: System calls provide an interface between the
process and the operating system. System calls allow user-level processes to request some
services from the operating system. In handling the trap, the operating system will enter in the
kernel mode, where it has access to privileged instructions, and can perform the desired service
on the behalf of user-level process. System calls are generally available as assembly language
instructions. They can also be made directly from a higher language program.
For ex. Unix system calls may be invoked directly from c or c++ programs.
System calls can be grouped into five major categories :

System calls for :


• process control
• file management
• device management
• information maintenance
• communication

Process control :

• end, abort : a process needs to be able to halt its execution either normally (end)or
abnormally(abort) .Under any circumstances the o.s. must transfer the control to the
command interpreter. This reads the next command.
• Load, execute : A process executing one job may want to load and execute another
process.
• Create process, terminate process: An effective mechanism is established to
terminate one process and to create another process.
• get process attributes, set process attributes : If we create a new process we should
be able to control its execution. The control requires the ability to determine and reset
the attributes of the process such as process priority, maximum allowable execution
time, etc .
• wait for time, wait for event, signal the event : A new process may need to wait for
cpu or wait for an event to occur and the process must signal the event that has
occurred.
• Allocate and freeing memory: Certain memory is to be allocated for a process and
reclaim the memory when it finishes its task.

File management:
• create file, delete file
• open, close
• read, write, reposition’

Dept of CS, GSC,Hassan Page 9


OPERATING SYSTEM

• get file attributes(filename, file type, protection code, accounting information),set file
attributes.

Device management:
• Request device, release device: if the system has multiple users, one must request the
device for the exclusive use of it and release it after finishing the task.
• read, write, reposition: Once the device has been requested, we can read , write and
reposition the device
• get device attributes, set device attributes
• logically attach or detach devices

Information maintenance
• get time or date, set time or date : System calls to return the current time and date.
• get system data or set system data: system calls which return information about the
system such as no: of current users, version no: of the OS , amount of free memory or
disk space, …
• get process, file or device attributes:
• set process, file or device attributes:
Communications :
• create, delete communication connection
• send, receive messages
• transfer status information
• attach or detach remote devices

There are two common models of communication

1. Message passing model: In this case message is exchanged through an inter process
communication facilities provided by the o.s.
2. Shared memory model: In this case, message is exchanged between processes through a
shared memory.

Process A Process A
Process B Shared
Memory
Kernel
Process B

Message Passing Model Shared Memory Model

System Programs: System programs provide a convenient environment for program


development and execution .System programs can be divided into the following categories:
• File management: These programs create, delete, copy, rename, print, list and
generally manipulate files and directories.
• File manipulation: Several text editors are available to create and modify the contents
of the files stored on disk or tape.
• Status information: Programs ask the system the date, time, available memory or disk
space , number of users , status of each processes etc.

Dept of CS, GSC,Hassan Page 10


OPERATING SYSTEM

• Programming language support programs. The o.s. provides the language


translators like assembler, compiler and interpreter to run programs written in higher
languages.
• Program loading and execution: Once a program is assembled or compiled it must be
loaded into memory for execution. The o.s. provides various loaders, editors, linkers
etc.
• Communication: These programs provide the mechanism for creating virtual
connections among processes.

Memory Management:

Memory management is the act of managing computer memory, this provides way to
allocate portions of memory to programs at their request, and freeing it for reuse when no
longer needed. The management of main memory is critical to the computer system.
The main functions of memory management are

• Keep track of each memory location whether it is free or allocated.


• Enforcing policies to determine to which processes the specified memory area is to be
allocate.
• Allocation of memory to a process as per the policy.
• Reclaim the memory from the process when it finishes its task.

All programs and data referenced by those programs are stored in main memory so that
they can be accessed during execution. Main memory could be thought of as a big continuous
chunk of space divided into groups of 8 or 16 or 32 bits. Each byte or word of memory has
corresponding address which is an integer that uniquely identifies that particular part of
memory.

Operating system must employ techniques to


• track where and how a program resides in memory
• converting logical program addresses into actual memory addressess.

when the program is compiled the variable references are changed into the addressess
in memory where the data and code reside. But we don’t know exactly where a program will
be loaded into main memory, how can we know what addresses to use?

The solution is to use two kinds of addressess logical and physical addressess.

A logical address ( sometimes called virtual or relative address) is a value that specifies a
generic location relative to the program but not to the reality of main memory.
A physical address is an actual address in the main memory device.
When a program is compiled, a referece to an identifier is changed to a logical address when
program is eventuallly loaded into memory, each logical address finally corrresponds to a
physical address. This mapping of logical address to a physical address is called addresss
binding.
Logical addressess allow a program to be moved around in memory or loaded in different
places at different times. As long as we keep track of where the program is stored, we are
always able to determine the physical address that corresponds to any given logical address.

Dept of CS, GSC,Hassan Page 11


OPERATING SYSTEM

Single contiguous memory management:

In this technique, the main memory is conceptually divided into two sections. One for
the O.S and another for the application program we want to execute. This approach is called
single contiguous memory management because the entire application program, is loaded into
one large chaunk of memory. Only one program other than the O.S can be processed at one
time.

O.S
A→starting address of the application program
A L→Logical address
Program A+L→Physical address
A+L

Advantages:
• Simple to implement and manage.

Disadvantages:
• Wastage of memory space and CPU time.
• Makes no provision for multiprogramming.

Partitioned memory management:

This technique allows multiprogramming by dividing the physical memory into


multiple partitions and allocating each partition to a separate job. Partitioning could be done
either statically or dynamically.

Static Partition memory management.

In this scheme physical memory is divided into partitions of different size prior to
processing of jobs and the size of each partition remains fixed there after . the size and number
of partitions are usually determined by taking into account the degree of multiprogramming,
available physical memory and typical size of the jobs to be processed. Status of each partition
and its attributes such as partition number,location and size are maintained in a table called
static partition status table.

Dept of CS, GSC,Hassan Page 12


OPERATING SYSTEM

O.S

J1

J3

J2

J4

1000

Static Partition Status Table.

Partition no: location size status


0 0 KB 100 KB allocated
1 100 KB 100 KB allocated
2 200 KB 300 KB available
3 500 KB 100 KB allocated
4 600 KB 400 KB available

Advantage:
• This technique is appropriate when the number and size of jobs are known.

Disadvantages :
• If the size of the job is not matched with the partition of memory, it will be unused.
• Job size is restricted to the size of the available partition.

Dynamic Partitioned Memory Management :

In this scheme memory is divided into partitions during processing of jobs, so that the
size of memory partition matches with the size of the job.
Here, two separate tables are maintained ,namely
• Allocated Partition Status Table :Used to keep track of the allocated area
• Free Area Status Table :To keep track of free areas.

Allocated Partition Status Table

Partition no: location size status


0 0 KB 100 KB allocated
1 100 KB 100 KB allocated

3 500 KB 100 KB allocated

Dept of CS, GSC,Hassan Page 13


OPERATING SYSTEM

Free Area Status Table :

Partition no: location size status


0 200 KB 300 KB available
1 600 KB 400 KB available

When new jobs say j1 of size 150KB and j2 of size 350k are admitted ,then the entries in both
the tables are changed as shown.

Allocated Partition Status Table

Partition no: location size status


0 0 KB 100 KB allocated
1 100 KB 100 KB allocated
2 200 KB 150 KB allocated
3 500 KB 100 KB allocated
4 600 KB 350 KB allocated

Free Area Status Table :

Partition no: location size status


0 350 KB 150 KB available
1 9500 KB 50 KB available

Advantages :
• Partitioned technique facilitate the degree of multi-program and increases the utility of
processor and input-output devices.
• It requires no special and costly hardware.
• Algorithms used are simple to design and easy to implement.

Disadvantage:
• Fragmentation could be a major problem.

Rellocatable Partition Memory Management :

Programs in memory must be able to reside in different parts of the memory at different
times. This is because when the program is swapped back into memory after being swapped
out for a while it cannot always be placed in the same location. Therefore memory management
in the operating system should be able to relocate programs in memory and handle memory
references in the code of the program so that they always point to the right location in memory.

Rellocatable Partition Memory Management technique provides a solution to the


fragmentation problem. In this technique all occupied areas of memory are brought to one end
of the memory so as to get one large block of free memory space.This process is called
compaction or recompaction or burping. User programs are unaffected by the compaction
because there is no visible effect on the address space.

Memory can be compacted under the following conditions:

Dept of CS, GSC,Hassan Page 14


OPERATING SYSTEM

• As soon as a job terminates.


• When a new job cannot be loaded into memory due to fragmentation.
• At fixed time intervals

0
OS 100 OS 100 OS 100
J1 J1 J1
300 300 300
J2 J2 J2
400 400 400
Computation J3 J3
500 500

J3
700 J4 of size 400 KB
J4
800
900
1000 1000 1000

Advantages :
• This technique eliminates fragmentation problem .
• Increase the degree of multi-programming.
• Increase the utility of processor and memory

Disadvantages:
• This technique requires a special hardware called rellocatable hardware.
• The rellocatable hardware increases the cost of the computer
• Compaction time is more

Paged Memory Management:

In this technique the logical address space of the job is divided into equal pieces called
pages. In the same way the physical memory is divided into pieces of equal sizes called frames
or blocks.By providing a suitable hardware mapping facility any page(logical address space )
can be placed onto any frame (physical memory).In page map table(PMT) the pages remain
contiguous but the corresponding frames not necessarily contiguous.

Page 1 4 3
Dept of CS, GSC,Hassan Page 15
Page 2 5 4
7
Page 3 1 1
OPERATING SYSTEM

2
3

5
6
\
7

Logical Address BMT Physical Memory


Space Of the Job

Advantages:
• This technique eliminates compaction problem.

Disadvantages:
• It requires a special page map hardware (address map hardware) that increases the
cost of the computer.
• Certain amount of memory will be wasted to store page map tables.

VIRTUAL MEMORY:

In all the previous memory management schemes the size of the physical memory must
be large enough to store entire job’s address space.This restrict the size of the job to the size of
the available physical memory .This problem can be resolved by the operating system that
produce an impression of having extremely large memory .Existance of this large memory is
mearly an illusion and hence it is referred to as virtual memory.

Thus virtual memory technique gives an application program the impression that it has
contiguous working memory (an address space), while in fact it may be physically fragmented
and may even overflow on to disk storage. Systems that use this technique make programming
of large applications easier and use real physical memory (e.g. RAM) more efficiently than
those without virtual memory. All modern general-purpose computer operating systems use
virtual memory techniques for ordinary applications, such as word processors, spreadsheets,
multimedia players, accounting, etc

The two major virtual memory techniques are

• Demand Paging Technique


• Segmentation with Paging

Demand Paging Technique :

Dept of CS, GSC,Hassan Page 16


OPERATING SYSTEM

Demand paging is an application of virtual memory. When the job is scheduled for
execution initially first page of the job is loaded into main memory, keeping the remaining
pages in the secondary memory. It follows that pages should only be brought into main memory
if the executing process demands them. Hence, the name demands paging technique. This is
often referred to as lazy evaluation because only those pages demanded by the process are
swapped from secondary storage to main memory.( This is in contrast to pure swapping, where
all memory for a process is swapped from secondary storage to main memory during the
process startup.)

To achieve this process, a page table implementation is used. The page table maps
logical memory to physical memory. A valid page is one that currently resides in main memory.
An invalid page is one that currently resides in secondary memory. When a process tries to
access a page, the following steps are generally followed:

• Attempt to access page.


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

Advantages
• Only loads pages that are demanded by the executing process.
• Job size is no longer restricted to the size of physical memory.
• As there is more space in main memory, more processes can be loaded reducing
context switching time which utilizes large amounts of resources.
• Does not need extra hardware support than what paging needs.

Disadvantages
• Programs running on low-cost, low-power embedded systems may not have a memory
management unit that supports page replacement.
• It requires a special hardware that increases the cost .
• Memory management with page replacement algorithms becomes slightly more
complex.

Segment :

A segment is a logical grouping of instructions such as main, stack, subroutine, work


area, data area etc.Thus jobs address space consist of collection of segments.

Segmentation with Paging

Segmentation and paging technique are combined to improve memroy


management.The combined scheme was “page the segment”.In this scheme each segment is
divided into number of pages and each segment maintains a separate table. Thus, job’s address
space consists of 3 parts, segment number S, page no. P and displacement T.Segment number
S and displacement T are used by the processor to calculate the absolute address of the
segment.This scheme is schematically represented as below.

Dept of CS, GSC,Hassan Page 17


OPERATING SYSTEM

Advantage:
• A segment can grow dynamically
• A segment can be shared by many jobs.

Disadvantages:
• It is difficult to maintain varying sized segments.
• This technique may induce compaction overhead.

UNIT – 2

Dept of CS, GSC,Hassan Page 18


OPERATING SYSTEM

Definition of Process:

The term "process" was first used by the designers of the MULTICS in 1960's. Since
then, the term process used somewhat interchangeably with 'task' or 'job'.
The process is “a program in Execution.” Process is not the same as program. It is more than
a program code. A process is an 'active' entity that compete for the resources .A program on
the other hand is an algorithm expressed in suitable notation. It is a passive entity and
constitutes only a part of process. Process, on the other hand, includes:

• Current value of Program Counter (PC)


• Contents of the processors registers
• Value of the variables
• The process stack (SP)
• A data section that contains global variables.

A process is the unit of work in a system. Each process has its own virtual CPU. In reality,
the CPU switches back and forth among processes.

Program verses Process

Program Process

Consists of instructions in machine code. Consists of instructions in any programming language.

Resides in secondary memory. Resides in primary memory

Unlimited span of time. Limited span of time.

Static (no changes in state)and Passive entity. Dynamic(changes in state )and an active entity.

Process State: A process being an active entity that changes its state during its life cycle. The
state of a process can be defined by its current activity. A process may be in one of the following
states: (As shown in Fig.1)

• New State: The process being created but not yet submitted to the pool of executable
processes.
• Ready State: A process is waiting for the processor assignment.
• Running State: A process is said to be running if it has the CPU and starts executing
the instructions.
• Blocked (or waiting) State: A process is said to be blocked if it is waiting for some
event to happen (may be an I/O operation or other interrupt operation) before it can
proceed.

• Terminated state: The process that has finished its execution.

OS moves a process from one state to another during its life cycle.

Dept of CS, GSC,Hassan Page 19


OPERATING SYSTEM

➢ New to ready: When a process is created and prepared to be executed then the O.S.
move the process from new to ready state.
➢ Ready to running: O.S. selects one of the jobs from the ready queue and allocate the cpu
and make it move from ready to running state for execution.
➢ Running to waiting state: A process in running state issued an interrupt or required an i/o
then the O.S. moves it from running state to wait state (blocked state) till the event
completes.
➢ Waiting to ready: When the requested i/o operation is completed then the process is shifted
from wait state to ready state.
➢ Running to terminate state: When a process finishes its operation it gets terminated.

Process Control Block (PCB):

In operating system each process is represented by a data structure known as Process Control
Block (PCB) or Task Control Block or process descriptor. The PCB contains many pieces of
information associated with a specific process like the

• The current state of the process.


• Unique identification number of the process .
• A pointer to parent process.
• Similarly, a pointer to child process (if it exists).
• The priority of process (a part of CPU scheduling information).
• Pointers to locate memory of processes.
• A register save area.
• The program counter.
• Accounting information, etc

Pointer to
parent process
Process State
Pointer to child
process
Process Identification number

Process Priority

Program Counter (PC)

CPU Registers

Pointer to process memory

Memory Limit

Pointer: Contains a pointer to a parent process or child process.


Process State: Indicate the current state of the process.
Process ID : It is a unique no: that identifies each process.
Program Counter: It contains the address of the next instruction to be executed.
CPU Registers: Whenever the processor switches from one process to another, information
about the current status of the old process is saved in cpu registers along with PC, so that the
process be allowed to continue correctly from the point of stop.
Memory limit: Indicates the starting memory address and the size.

Dept of CS, GSC,Hassan Page 20


OPERATING SYSTEM

Accounting information: indicates the actual cpu time used in executing a process.
I/O status information: includes the name of i/o devices used by a process
Thus the PCB serves as a central store of information that allows the operating system to
locate all the key information about a process. The os use this information to perform various
operations like naming a process, change the process priority, dispatch process, etc

CPU Scheduling:
Scheduling refers to the set of polices and mechanisms that an operating system supports
for determining the order of execution of the processes. The main objective of Scheduling is
to increase CPU utilization and to increase the throughput. Scheduling streamlines the
operation of processes and optimizes the utilization of system resources. The behavior of a
process plays a key role in CPU Scheduling. This is depicted by CPU-I/ O burst cycle.

CPU I/O Burst Cycle:

The execution of a process consists of an alternate CPU and I/O bursts. Actually, a
process runs for a while (cpu burst) perform some i/o (i/o burst) again a CPU burst followed
by an I/O burst . The last CPU burst does not ends with an I/O burst but it ends with system
request to terminate the execution. This cycle of operation is called CPU-I/O burst cycle.
If the CPU bursts are relatively short compared to the I/O bursts, then the process is said
to be I/O bound.
For example, a typical data processing task involves reading a record, some minimal
computation and writing a record.
If CPU bursts are long compared to I/O bursts, a process is said to be CPU bound.
For example: A number crunching task involves an I/O burst to read parameters. A very
long CPU burst and another I/O burst to write the results.

Schedulers: A scheduler is an OS program (module) that selects the next job to be admitted
for execution. Schedulers are of 3 distinct types: long-term scheduler (also known as job
scheduler or high-level scheduler), mid-term or medium-term scheduler and short-term
scheduler (also known as processor scheduler or a dispatcher).

Long-Term Scheduler ( job scheduler or high-level scheduler)


The long-term scheduler (LTS) is responsible for selecting the processes from
secondary storage device like a disk and loads them into main memory for execution. The
primary objective of long term scheduler is to provide a balanced mix of jobs i e the
combination of cpu and i/o bound processes. When processor utilization is low then the
Dept of CS, GSC,Hassan Page 21
OPERATING SYSTEM

scheduler admits more jobs. Similarly when CPU utilization is high, it may reduce the rate
of admission.
It is called long-term scheduler because the time for which the scheduling is valid is
long. The long-term scheduler executes less frequently as it takes from minutes to hours
for the creation of new processes in the system. Its job is to ensure fixed number of
processes in the ready queue.

LTS STS EXIT


Batch queue Ready queue CPU
Batch jobs Main memory

Suspended queue

Long-term scheduling is also important in large-scale systems such as batch processing


systems, computer clusters, supercomputers and render farms. In these cases, special purpose
job scheduler software is typically used to assist these functions.

Mid-term Scheduler:

The mid-term scheduler, present in all systems with virtual memory, temporarily removes
processes from main memory and places them on secondary memory (such as a disk drive)
commonly referred as "swapping out" . The mid-term scheduler may decide to swap out
a process which has not been active for some time, or a process which has a low priority,
or a process which is page faulting frequently, or a process which is taking up a large
amount of memory. Later on when more memory is available or when the process has
been unblocked and is no longer waiting for a resource then it is “swapped in” for
continuing execution by the medium term scheduler.

Short-term Scheduler (Dispatcher) :

The short-term scheduler is responsible for assigning the cpu to a process in the ready
queue for execution. Its main objective is to maximize CPU utilization. It must select new
processes for execution more frequently because cpu executes a process only for a few
milliseconds. Thus the short-term scheduler makes scheduling decisions much more
frequently (at least once in every 10 ms) than the long-term or mid-term schedulers.
The process of assigning cpu to a process is also known as process dispatching. A
program responsible for assigning the cpu to the process that has been selected by the STS
is known as a dispatcher. STS decides which of the ready processes are to be executed
next following a clock interrupt, or an IO interrupt or a system call. This scheduler can be
preemptive s (implying that it is capable of forcibly removing processes from a CPU when
it decides to allocate CPU to another process )or non-preemptive (also known as
"voluntary" or "co-operative" in which case the scheduler is unable to "force" processes
off the CPU).

Scheduling Queues

Dept of CS, GSC,Hassan Page 22


OPERATING SYSTEM

Ready queue: Processes that are selected from secondary memory and loaded into
primary memory for execution by long term scheduler form a ready queue. In a ready queue
processes are ready to run and waiting for processor assignment. These processes are ordered
by priority. The process of highest priority is placed at the front end of the queue, because
queue works in FIFO(First In First Out) principle, so CPU always selects a process of highest
priority. Processes are listed together means their PCB’s are linked together .the header of the
queue contains two pointers, the first pointer points to the first PCB while the second pointer
points to the last PCB in the list. Each PCB has a pointer to the next process in the ready queue.

Pointer1 PCB1 PCB2 PCB3 PCBn


Pointer2

Device queue: There are some processes which are blocked due to unavailability of an
i/o device. A list of processes waiting for a particular I/O device called a device queue. There
is a separate queue for each i/o device.
Process scheduling is commonly represented by a queuing diagram. In this diagram a
rectangular box represents a queue. The circle represents the resources that serve the queues
and the arrow indicates the flow of processes in the system.
A new process which is initially placed in the ready queue has to wait until it is selected for
execution. Once the CPU is allocated and is executing, one of the following events could occur.
1. The process could issue an I/O request and then be placed in an I/O queue.
2.The process could be removed forcibly from the CPU and placed back in the ready
queues.
3.The process may create new sub-process and wait for its termination.
A process continues this cycle until it terminates and is removed from all queues, its PCB and
resources de-allocated.

CP
Ready queue

I/ I/O queue I/O request


O
Time slice
expired

Child Create a
execute child process

Interrup Wait for an


t occurs interrupt

Queuing diagram representation of process scheduling

Dept of CS, GSC,Hassan Page 23


OPERATING SYSTEM

Goals of Scheduling (or objectives) or Scheduling criteria:

Many objectives must be considered in the design of a scheduling discipline. In particular, a


scheduler should consider efficiency, response time, turnaround time, throughput, etc.

CPU utilization –is defined as the average fraction of time during which CPU is busy
executing either user programs or system modules. The key idea is that ,if cpu is busy at all
time,the utilization of all other components of the system will also be high. So , higher the
cpu utilization, better will be the performance. CPU utilization may range from 0 to 100
percent. In a real system, it should range from 40 percent (for a lightly loaded system) to
90 percent (for a heavily loaded system). A scheduler should maximize the cpu utilization
or minimize the cpu idle time.

Throughput – is defined as the number of processes that complete their execution per unit
time. A scheduler should maximize the throughput.

Turn around time(TAT)- is defined as the total time elapsed between submission of a
process and completion of it. It is the sum of the time periods spent waiting to get into
memory, waiting in the ready queue, executing on the CPU, and doing I/O.
TAT=(process finish time)-(process arrival time)
Lower the turn around time better is the performance.

Waiting time - Waiting time is defined as the total time spent by a process while waiting
in suspended state or ready state in a multiprogramming environment.
Waiting time=(turn around time - processing time).
Lower the waiting time better is the performance.

Response time – is defined as the time interval between the submission of a request by a
process and the first response or service to the request.
A scheduler should minimize the response time for interactive user.

Fairness - 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 to all
processes is not fair. Think of safety control and payroll at a nuclear plant.

CPU / Process Scheduling algorithms :

CPU Scheduling deals with the problem of deciding to which of the processes in the
ready queue the CPU is to allocate ?. When more than one process is run able, the operating
system must decide which one first. The part of the operating system concerned with this
decision is called the scheduler, and the algorithm it uses is called the scheduling algorithm.
.
The Scheduling algorithms can be divided into two categories with respect to how they deal
with clock interrupts?. They are preemptive and non preemptive scheduling.
In a preemptive discipline, the CPU may be deallocated forcibly from a process before
completion and allocate CPU to another process.( The strategy of allowing processes that are
logically runable to be temporarily suspended is called Preemptive Scheduling.)

Dept of CS, GSC,Hassan Page 24


OPERATING SYSTEM

In non preemptive scheduling, once the CPU is assigned to a process, the CPU cannot be
taken away from that process till completion.

Scheduling Algorithms:
Some of the common scheduling algorithms are
• FCFS (First Come First Served) Scheduling algorithm.
• SJF (Shortest job First)Scheduling algorithm.
• Round Robin Scheduling algorithm.
• Priority Scheduling algorithm.

First-Come-First-Served (FCFS) Scheduling:

Other names of this algorithm are: First-In-First-Out (FIFO) ,Run-to-Completion

First-Come-First-Served algorithm is the simplest of all the scheduling algorithms.


Processes are dispatched according to their arrival time on the ready queue. Being a non
preemptive discipline, once a process has a CPU, it runs to completion. The FCFS scheduling
is fair in the human sense of fairness but it is unfair in the sense that long jobs make short jobs
wait and unimportant jobs make important jobs wait.

Consider a set of four processes with cpu burst time given in milliseconds. If these
processes were served in FCFS order, average waiting time calculated using GANTT chart as
shown.

P1 10ms
P2 5ms
P3 8ms
P4 4ms

P1 P2 P3 P4
0 10 15 23 27
AWT = wait time of all processes / No: of processes
AWT= wait time(p1+p2+p3+p4) / 4
= (0+10+15+23)/ 4
=48/4
= 12 ms
Average turn around time = turn around time(p1+p2+p3+p4) / 4.
= (10+15+23+27 ) / 4
= 75 / 4
= 18.7 ms

Advantages:
• The algorithm is simple and easy to implement.

Disadvantages:
• FCFS scheme is not useful in scheduling interactive users because it cannot guarantee
good response time.
• One of the major drawbacks of this scheme is that the average time is quite long.

Dept of CS, GSC,Hassan Page 25


OPERATING SYSTEM

• The First-Come-First-Served algorithm is rarely used as a master scheme in modern


operating systems but it is often embedded within other schemes.
• Less throughput, poor performance and poor utilization of resources
• Shorter jobs may suffer considerable turn around time delays and waiting time when
cpu allocated to longer jobs.

Shortest-Job-First (SJF) Scheduling:

The basic principle of Shortest-Job-First (SJF) scheduling is to allocate the cpu to a


process with least cpu burst time. If two or more processes having the same cpu burst time,
then FCFS is used to break the tie. It is a non-preemptive discipline in which once the CPU is
assigned to a process, the CPU cannot be taken away from that process till completion.

Consider a set of four processes with cpu burst time given in milliseconds. If these
processes were served in SJF order they would be scheduled in the order p4,p2,p3 and p1.
Average waiting time and turn around time could be calculated using Gantt chart as shown.

P1 10ms
P2 5ms
P3 8ms
P4 4ms

P4 P2 P3 P1
0 4 9 17 27

AWT = waiting time of all processes / No: of processes


AWT = wait time(p4+p2+p3+p1) / 4
= (0+4+9+17)/ 4 =30/4 = 7.5 ms
Average turn around time = turn around time(p1+p2+p3+p4) / 4.
= (4+9+17+27) /4
= 57 / 4
= 14.2 ms
NOTE: here AWT (7.5) and ATAT (14.2) is considerably small when compared to FCFS
scheme(awt= 12 ms and atat= 18.7)
Advantages:
1. It gives minimum average waiting time for a given set of processes. So it is an optimal
algorithm
2.The SJF scheduling is especially appropriate for batch processes for which the run times
are known in advance.
Disadvantages:
• The SJF algorithm favors short jobs (or processors) at the expense of longer ones.
• The obvious problem with SJF scheme is that it requires precise knowledge of how long a
job or process will run, and this information is not usually available.
• Like FCFS, SJF is a non preemptive algorithm , therefore, it is not used in timesharing
environment where in reasonable response time must be guaranteed.

Round Robin Scheduling :

Dept of CS, GSC,Hassan Page 26


OPERATING SYSTEM

This is one of the simplest and widely used algorithm ,here the processes are dispatched
in FIFO manner but are given a limited amount of CPU time called a time-slice or time
quantum( usually 10 to 100 ms). If a process does not complete before the expiry of its CPU-
time, the CPU is preempted and given to the next process waiting in a ready queue. The
preempted process is then placed at the back of the ready list to wait for the next allocation..
Consider a set of five processes P1, P2,P3,P4 and P5(with cpu burst time given in
milliseconds). Let the time slice be 4 ms. When these processes were served in round robin
method, average waiting time and turn around time could be calculated using Gantt chart as
shown.

P1 10ms
P2 5ms
P3 8ms
P4 4ms
P5 2ms

P1 P2 P3 P4 P5 P1 P2 P3 P1
0 4 8 12 16 18 22 23 27 29

AWT = waiting time of all processes / No: of processes


Wait Time of p1= 19
Wait Time of p2= 18
Wait Time of p3= 19
Wait Time of p4= 12
Wait Time of p5= 16
AWT = (19+18+19+12+16) /5 = 84/5=16.8ms

Average turn around time = turn around time(p1+p2+p3+p4+p5) / 5


= (29+23+27+16+18) /5
= 113 / 5
= 22.6 ms

Response time of p1 = 0
Response time of p2 = 4
Response time of p3 = 8
Response time of p4 = 12
Response time of p5 = 16

ART =(0+4+8+12+16)/5 = 8ms

Round Robin Scheduling is a preemptive scheduling (at the end of time-slice) therefore
it is effective in time-sharing environments where reasonable response time is required and
share the system fairly among users.

The interesting issue with round robin scheme is the length of the quantum. Setting the
quantum too short, causes too many context switches and lower the CPU efficiency. On the
other hand, setting the quantum too long, may cause poor response time and approximates
FCFS.

Dept of CS, GSC,Hassan Page 27


OPERATING SYSTEM

Advantages:
1. Utilize the resources uniformly.
2. Small processes may be completely executed in a single time slice giving good
response time
Disadvantages;
In any event, the average waiting time under round robin scheduling is often quite long.

Priority Scheduling:

In this discipline, each process in the ready queue is given a priority and process of
highest priority is allowed to run first. Equal-Priority processes are scheduled in FCFS order.
Priority scheduling may be either preemptive or non preemptive.
Priority is generally some fixed range of numbers. The level of priority may be determined on
the basis of resource requirements, process characteristics and its run time behavior.

Consider a set of processes P1, P2 ,P3, P4 and P5(with cpu burst time given in milliseconds
) associated with the following priorities. Therefore the order of scheduling is p2,p4,p1,p3 and
p5.

P1 10ms 3 (priority)
P2 5ms 1
P3 4ms 3
P4 8ms 2
P5 10ms 4

P2 P4 P1 P3 P5
0 5 13 23 27 37

AWT = waiting time of all processes / No: of processes


= (0+5+13+23+27) /5
= 68/5=13.6 ms
ATAT = (5+13+23+27+37)/5 =105/5 = 21 ms

1. In preemptive scheduling cpu will be preempted from a low priority process, when
a high priority process arrives. A major problem with priority based scheduling is indefinite
blocking of low priority processes by high priority processes. This is known as starvation.
Thus , completion of a process within a finite time cannot be guaranteed with this scheduling.
Aging: Aging is a solution to the problem of starvation. It is a technique of gradually
increasing the priority of a process that is waiting in a system for a long time. Thus the older
processes attain high priority and are ensured of completion in a finite time.
2. In non preemptive scheduling ,when a new process of high priority arrives, it issimply
put at the head of the queue and cpu cannot be preempted from a low priority process till
completion.

Synchronization

Synchronization refers to a kind of cooperation extended by two or more things to


perform a specific task. The two distinct but related synchronization concepts in computer
system are synchronization of processes and synchronization of data.

Dept of CS, GSC,Hassan Page 28


OPERATING SYSTEM

Process synchronization refers to the coordination between multiple processes to get


the correct run time order and to avoid race condition.
Data synchronization refers to the idea of keeping multiple copies of a dataset in coherence
with one another, or to maintain data integrity. The basic technique used to implement
synchronization is to block a process until an appropriate condition is fulfilled.

Critical section(CS): A section of code or a set of operations in which the process may be
changing shared variables, updating a common file or table, etc is known as critical section of
that process. The important feature of the system is no two processes are executing in their
critical section at the same time. This should be ensured by the synchronization mechanism.
The other section of code is called remainder section.

Race condition: A situation where two or more processes are reading or writing some shared
data and the result depends on who runs precisely is called a race condition. A race condition
on a data item arises when many processes concurrently update its value.

Requirements for critical section problem solutions: A solution to the critical section
problem must satisfy the following requirements.
Mutual exclusion: No two contending processes are executing in their CS simultaneously.
Bounded wait: No process should have to wait indefinitely to enter its CS.
Progress: When a CS is not in use, one of the processes wishing to enter, it will be granted
entry to the CS.

Semaphores
Semaphore is a solution to the critical section problem where two or more
processes waiting to access the same resource. Semaphores are used to restrict the access of
resources in a multiprogramming environment.

Definition: A semaphore(S) is a shared integer variable with non negative values which can
only be subjected to the following operations 1. Initialization. 2.Two atomic, indivisible wait
(p) and signal(V) operations.
Atomic means that either an operation in the CS should happen completely, ie it should not
be interrupted in the middle or not at all.
Indivisibility of P and V implies that these operations cannot be executed concurrently.
The value of a semaphore is the number of units of the resource that are free. If there exist only
one resource then the semaphore is called a binary semaphore.

Binary semaphore : A binary semaphore S can assume only one of the two values ie S=0 or
S=1.
A wait operation on semaphore S denoted by wait(S)or P(S) wait until a resource is available.
It operates as follows
P(S): if S >0
Then S=S-1;
Else (block the process executing the P operation).
A signal operation on semaphore S denoted by signal(S)or V(S) makes a resource available .
It operates as follows
V(S): if (one or more processes waiting on S)
Then (let one of these processes to proceed)
Else S=S+1;

Dept of CS, GSC,Hassan Page 29


OPERATING SYSTEM

Thus, the wait operation decrements the value of S while the signal operation increments the
value of S. Mutual exclusion on the semaphore, S, is enforced within P(S) and V(S).

If several processes attempt a P(S) simultaneously, only one process will be allowed to
proceed. The other processes will be kept waiting, but the implementation of P and V
guarantees that processes will not suffer indefinite postponement.

Advantages of binary semaphore:


Implementation of binary semaphore is extremely simple.
Disadvantages
It does not meet the requirement of bounded waiting because a waiting process is selected
arbitrarily to enter its critical section.

Spooling
We know that i/o devices are much slower devices than the processor. So it is
necessary to reduce the speed mismatch in order to increase resource utilization and
throughput. Spooling is one such technique commonly used to reduce the speed mismatch.

The term "spool" is an acronym of "Simultaneous Peripheral Operation On Line”.


Spooling refers to keeping data or putting jobs in a buffer( buffer is a special area in memory
or on a disk ). The devices can access the data from the buffer at their own speed. Thus the
buffer provides a waiting station where data can rest until the devices accessing the data.

Disk

I/P CPU O/P

The most common spooling application is print spooling. In print spooling, documents
are loaded into a buffer from which the printer pulls them off at its own rate. Because the
documents are in a buffer where they can be accessed by the printer, we can perform other
operations on the computer while the printing takes place in the background. Thus spooling
keeps both cpu and i/o devices busy at much higher rate and optimizes the utility of resources.

Deadlocks :

A set of processes is in a deadlock state if each process in the set is waiting for an event
that can be caused by another process in the same set. In other words, each process in the set
of deadlock processes is waiting for a resource that can be released only by a deadlock process.
None of them can release any resources, none of the processes can run. Such an undesirable
state is known as a deadlock state.
Example: The simplest example of deadlock is where process p1 has been allocated a non-
sharable resource A(say a tape drive). Process p2 has been allocated a non-sharable resource
B(say printer). Now if p1 needs B and p2 needs A to proceed and these are the only two
processes in the system, each is blocked the other and all useful work stops. This situation is
termed as deadlock.

Dept of CS, GSC,Hassan Page 30


OPERATING SYSTEM

A P B P2
1

The resources may be either physical or logical.


Examples of physical resources are Printers, Tape Drivers, Memory Space, and CPU Cycles.
Examples of logical resources are Files, Semaphores, and Monitors.

Necessary conditions for Deadlock to occur( or Deadlock characterization):

There are four conditions that must be held simultaneously for a deadlock to occur.
They are
1.Mutual Exclusion :
A resource(non-sharable) can be used by only one process at a time . if another process
requests for the same resource at the same time, it must be delayed until the resource has been
released.

2.Hold and Wait :


Process holding some resources and requesting for additional resources ,which are
currently being held by other processes in a non-sharable mode.

P R1 P R2
1 2

3.No Preemption :

Resources allocated to a process cannot be preempted or released back to the system


before completion of the task. A Process can release the resource voluntarily only after
completion of its task.

4 .Circular Wait

A set of processes in the system form a circular list or chain where each process in the
list is waiting for a resource held by the next process in the list.
For eg: A set of processes P1 to P3 exists in a chain or cyclic form such that P1 is waiting for
R1, R1 is held by P2, P2 is waiting for R2 and R2 is held by P3 , P3 is waiting for R3, R3 is
held by P1.

Dept of CS, GSC,Hassan Page 31


OPERATING SYSTEM

P R1 P R2 P
1 2 3

R3

Methods for handling deadlocks:

Deadlock can be handled by any of the following methods.


Deadlock prevention: Prevent deadlock by resource scheduling so as to negate at least
one of the four conditions.
Deadlock avoidance: Avoid deadlock by careful resource scheduling.
Deadlock detection and recovery

Deadlock prevention: Occurrence of deadlock can be prevented by ensuring that atleast one
of the four necessary conditions cannot hold in the system. Let us apply this technique to each
of the conditions.
In this method we design a system such that the probability of deadlock is excluded. It can be
done by two methods
a> Direct method; Here we need to prevent the occurrence of a circular wait .
b> Indirect method: Here we need to prevent the occurrence of one of the three
conditions namely mutual exclusion, hold and wait and no preemption.

Elimination of Mutual Exclusion :

Mutual exclusion occurs when all the resources are in non-sharable mode .We can deny
this condition by using a protocol. The rule used here is “ convert all non-sharable resources
into sharable resources “. When many processes would like to access a read only file, allow
them to access. Here, mutual exclusion is not necessary. However, some non-sharable
resources cannot be changed into sharable.
For ex: multiple processes cannot be allowed to update a common file. Similarly, for
intrinsically non-sharable resources like printer we cannot apply this condition. In general
mutual exclusion condition cannot be disallowed.

Elimination of Hold and Wait :

Occurrence of hold and wait condition can be prevented by using a protocol that 1. A
process can request the resource only when it has none of the other resources ie it must release
all the resources currently being held before requesting for additional resources.
Alternatively, 2. Each process has to declare all its requirements at the beginning and all the
required resources are allocated once.
In either case the problems are
1. A process may need many resources to start its execution so it must be waiting for
one or the other resources indefinitely. This leads to starvation.
2. Many resources like printer allocated at the beginning but used only at the end of
the process. This leads to low resource utilization.

Dept of CS, GSC,Hassan Page 32


OPERATING SYSTEM

Elimination of No Preemption :

No preemption can be denied by allowing the preemption of resources. The protocol


used here is “if processes holding some resources and request for additional resources, if the
additional resources cannot be immediately allocated, then all resources currently being held
are preempted”. The preempted resources may be allocated to other waiting processes. The
process(old) must request the resources again together with additional resources.

Elimination of Circular Wait :

Occurrence of circular wait condition can be prevented by imposing total hierarchical


ordering of all resources i.e., by assigning an integer value to each of the resources. Each
process can request the resources in a numerical order of enumeration, with this, the resource
allocation graph can never have a cycle.
Let R={R1,R2,........Rm} be a set of resource types. We assign a unique integer number to each
using a function. F(R) N
F is a one to one function that maps a resource R to an integer value N
Ex : F ( card reader ) = 1
F ( printer ) =2
F ( plotter ) =3
F ( tape drive ) = 4
F( disk drive ) = 5

A process may request printer first and then a tape drive(order 2,4) and it may not request a
plotter first and then a printer(order 3,2). By using these rules we can easily break the cyclic
wait.

Deadlock Avoidance :

Deadlocks can be avoided by maintaining the system in safe state.


In this method each process has to declare the total number of resource types and number of
instances of each type that it may need in its life time. If this information is available in advance,
it is possible to construct an algorithm that ensures the system will never leads to deadlock.
A deadlock avoidance algorithm dynamically examines the resource allocation state to ensures
that a circular wait condition can never exist. The resource allocation state is defined by the
number of available and allocated resources and the maximum demands of the processes.
Safe state: A system is said to be in safe state “ if all the waiting processes are executed
successfully in some sequence by satisfying all their resource requirements. The sequence is
said to be a safe sequence. If no such sequence exists, then the system state is said to be unsafe.

deadlock

Dept of CS, GSC,Hassan Page 33


OPERATING SYSTEM

A safe state is not a deadlocked state, conversely a deadlock state is an unsafe state. Not all
unsafe states are deadlock, however an unsafe state may lead to a deadlock.
Illustration: consider a system with 12 tape drives and with a set of 3 processes. The maximum
need and number of tape drives currently allocated to each process is given as below

Max Allocated
P1 10 5
P2 4 2
P3 9 2
At time t1,the system is in safe state with 3 free tape drives. The sequence P2,P1,P3 satisfy the
safety condition and is a safe sequence.
A system can go from safe to unsafe state. Suppose at time t1, process P3 request one more
tape drive and is allocated, the system is no longer in a safe state. At this point only P2 can be
allocated all its tape drives. When it returns, only 4 tape drives are available. Neither P1 nor P3
can be satisfied and resulting in a deadlock.

Banker’s Algorithm :

Banker’s algorithm is the most common deadlock avoidance algorithm proposed by


Dijkstra in 1965. It is so named because, the procedure used here is analogous to the operations
of a banking system.
[ A banker never allocates all the cash of his bank to his customers. He cannot
grant the request of all customers. Before granting a loan he used to examine the status of the
bank whether the allocation leaves the bank in safe state , if so the loan is granted else the
customer need to wait ]
According to this algorithm when a new process enters the system, it must declare the
maximum number of instances of each resource type that it may need. This number may not
exceed the total number of resources in the system. When a process requests a set of resources,
the system must determine whether the allocation of requested resources will leave the system
in safe state, if so the request is granted. Otherwise the process must wait until enough number
of resources are released. Implementation of banker’s algorithm need to maintain several data
structures and need to write safety algorithm.

Let m and n be the number of processes and the number of resource types respectively.
Available: Available is a vector of length n that defines the number of instances of each
resource type that are available(free) in the system.
If Available [j]=k means k instances of resource type Rj are available in the system.
Max; Max is an m x n matrix that defines the maximum demand of each process.
If Max[i,j]=k means that process Pi may need at the most k instances of resource type
Rj to finish its task.

Allocation: Allocation is an m x n matrix that defines the number of instances of each resource
type which are currently allocated to a process.
If Allocation[i,j]=k means that process Pi is currently allocated k instances of resource type Rj.
Need : Need is a m x n matrix that defines number of instances of each resource type which
are additionally requested by a process.
If Need[i,j]=k means that process Pi may need k more instances of resource type
Rj to finish its task.
Need[i,j]= Max[i,j]- Allocation[i,j]

Dept of CS, GSC,Hassan Page 34


OPERATING SYSTEM

Safety algorithm:

Step1. Let work and finish be the two vectors of length m and n respectively.
Initialize work=available
Finish[i]=false for i=1,2,3......n.

Step2. Find i such that both


a)Finish[i]=false for i=1,2,3......n.
b) Needi <= work
Step3. Work = work + Allocationi i
Finish[i]=true
goto Step2;
Step4. If Finish[i]=true for all i, then the system is in safe state else it is in unsafe state.

Banker’s algorithm: Let Requesti be the request vector for process Pi. If Request i[j] =k, then
process Pi wants k instances of resource type Rj. When request is made by Pi, the following
actions are taken:

Step1: if Request i <= Needi, goto Step2 . else raise an exception.


Step2: if Requesti <=Available, goto Step3, else Pi must wait.
Step3: Have the system pretend to have allocated the requested resources to process Pi by
modifying the state as follows.
Available = Available – Requesti
Allocationi = Allocationi + Requesti
Needi = Needi – Requesti

If the resulting resource allocation is safe, then the transaction is completed and process Pi is
allocated the requested resources. However ,if the new state is unsafe then Pi must wait and the
old resource allocation state is restored.
Illustration: Let there be a set of 5 processes and 3 resource types A,B,C. A has 10 instances,
B has 5 instances, and C has 7 instances. Suppose at time t0, the following snapshot of the
system has been taken
Allocation Max Available
A B C A B C A B C
P1 0 1 0 7 5 3 3 3 2
P2 2 0 0 3 2 2
P3 3 0 2 9 0 2
P4 2 1 1 2 2 2
P5 0 0 2 4 3 3

SOLUTION: find the Need vector, Need = Max – Allocation


So we get,

Allocation Max Available Need Sequence

A B C A B C A B C A B C
P1 0 1 0 7 5 3 3 3 2 7 4 3 4
P2 2 0 0 3 2 2 1 2 2 1
P3 3 0 2 9 0 2 6 0 0 5
P4 2 1 1 2 2 2 0 1 1 2

Dept of CS, GSC,Hassan Page 35


OPERATING SYSTEM

P5 0 0 2 4 3 3 4 3 1 3

The sequence < P2, P4, P5, P1 , P3 >is a safe sequence. So the system is in safe state.

Deadlock Detection and Recovery : Detect deadlock and, when it occurs, take steps to
recover.
Deadlock Detection
Deadlock detection is the process of determining the existence of deadlock and identifying
the processes and resources involved in the deadlock.
Once a deadlock is detected, there needs to be a way to recover from deadlock.several
alternative ways exist are

• Temporarily prevent resources from deadlocked processes.


• Back off a process to some check point allowing preemption of a needed resource and
restarting the process at the checkpoint later.
• Successively kill processes until the system is deadlock free.

These methods are expensive in the sense that each iteration calls the detection
algorithm until the system proves to be deadlock free. The complexity of algorithm is O(N2)
where N is the number of proceeds. Another potential problem is starvation; same process
killed repeatedly.
Detection mechanism for systems with single instance and multiple instances of resource type
is different. In case of single instance of resource type deadlock can be detected using wait for
graph while for multiple instances detection can be done by detection algorithm.

Deadlock detection in systems with single instance of resource type:


In this case deadlock is detected by wait for graph. Wait for graph is a graph derived
from ‘resource allocation graph’ consists of only processes as vertices or nodes.

R1
R1 R1 R2
P1 P2 P3

R3 R4
Resource Allocation Graph

P1 P2 P3

Wait for graph

An edge from Pi to Pj in a wait for graph implies that process Pi is waiting for process
Pj to release a resource. An edge from Pi to Pj in a wait for graph exists if and only if the
corresponding Resource Allocation Graph contains two edges namely a waiting edge Pi to
Rq and an allocation edge Rq to Pj.

Dept of CS, GSC,Hassan Page 36


OPERATING SYSTEM

A system is in deadlock state, if and only if the wait for graph contains a cycle. Thus we
can easily detect the deadlock in terms of cycle in the wait for graph.
In the above example, there exists two cycles
P1 to P2 to P1
P2 to P3 to P2 so the system is in deadlock state.

Deadlock detection in systems with multiple instances of resource type:


The wait for graph method is not applicable to detect deadlock in systems with multiple
instances of resource type. So we need another method for this type, that is ‘deadlock detection
algorithm. This algorithm looks like banker’s algorithm and employ several data structures
that are similar to those used in the banker’s algorithm.

Data structures

Available: Available is a vector of length n indicates the number of available resources of each
type.
Allocation: Allocation is a m x n matrix that defines the number of resources of each type
currently allocated to each process.
Request: Request is an m x n matrix that indicates the current request of each process.
If Request [i,j] = k then process Pi is requesting k more instances of resource type Rj.

Detection algorithm

Step1: Available i, for i=1,2,3,…..


Allocation i!= 0, then finish[i] = false
Otherwise finish[i]=true.

Step2: find an index i such that both


1 finish [i]=false
2 Request[i]< Available

Step3: Available= Available +Allocation[i]


Finish[i]=true
Goto step2

Step4: if finish[i]=false, then the system is in a deadlock state, moreover process Pi is deadlocked.

Recovery from deadlock:


There are two methods for breaking a deadlock. One method is simply to abort one by
one process to break the circular wait.
The second method is to preempt some resources from one or more of the deadlocked
process/es
Process termination : Process termination is one of the methods to recover from deadlock. It
can be done by the following ways
a)abort all deadlocked processes : release all processes in the deadlocked state and start
allocation from the beginning but it is a very expensive method.
b)Abort one by one process until the deadlock cycle is eliminated ,here first abort one of the
processes and allocate the resources(obtained from abort process) to other process in the
deadlock state, then check whether deadlock is break or not. If not, abort another process until
the system is recovered from deadlock.

Dept of CS, GSC,Hassan Page 37


OPERATING SYSTEM

Disk scheduling:

In multiprogramming systems several different processes may want to use the system's
resources simultaneously. For example, processes will contend to access an auxiliary storage
device such as a disk. The disk drive needs some mechanism to resolve this contention, sharing
the resource between the processes fairly and efficiently.

Disks are commonly used for bulk storage because disks are accessed randomly and
access time is very small.A magnetic disk consists of a collection of platters which rotate on
about a central spindle. These platters are metal disks covered with magnetic recording material
on both sides. Each disk surface is divided into concentric circles called tracks. Each track is
divided into sectors where information is stored. The reading and writing device, called the
head moves over the surface of the platters until it finds the track and sector it requires.. There
is one head for each surface on which information is stored each on its own arm. In most
systems the arms are connected together so that the heads move in unison, so that each head is
over the same track on each surface. The term cylinder refers to the collection of all tracks
which are under the heads at any time.

Disk Perfromance :

Performance of a disk can be evaluated using many parameters like seek time, latency
delay and transfer rate.
Seek time :
It is the time required by the read/write head to reach the desired track on a disk.It
depends on two factors internal start of time and time taken to traverse the cylinders .It is given
by the relation TS = m * n + S .
Where TS = estimated seek time
m=constant that depends on the disk drive
n=number of tracks traversed
S=startup time
Dept of CS, GSC,Hassan Page 38
OPERATING SYSTEM

Latency (Rotational)Delay :
It is the time required to reach desired sector on a track by the read/write head.
Rotational delay generally varies from 100 to 200ms.
Access time: Access time is the sum of seek time and rotational delay.
Transfer Time :
Transfer time depends upon the rotational speed of the disk and it is given by
T = B/R * N
T=Transfer rate
B=number of bytes to be transferred
R=rotational speed in revolutions per sec
N=number of bytes on track

Disk Scheduling Algorithms :


The most popular disk scheduling algorithms are
FCFS Disk Scheduling
SSTF Disk Scheduling
SCAN Disk Scheduling
C-SCAN Disk Scheduling

FCFS (First Come First Serve )Disk Scheduling :

It is the simplest disk scheduling algorithm. In this scheme the disk queue for I/O request
were served in first-come first-served basis.
For instance, consider a disk queue with I/O request for the following cylinders :
80, 150,40,120,30,70,60 & 10.Let the read/write head be initially at 50th cylinder .If these
requests served in FCFS basis, it can be depicted as follows :

.0 40 60 80 100 120 140 160 180 200


20

. . .
. .
.
. ..
Total head movement = ( 50 – 80 ) + ( 80 – 150 ) + ( 150 – 40 ) + (40 – 120 ) + ( 120 – 30 ) +
( 30 - 70) + ( 70 – 60 ) + ( 60 – 10 )

Dept of CS, GSC,Hassan Page 39


OPERATING SYSTEM

= 30 + 70 + 110 + 80 + 90 + 40 + 10 + 50
= 480 cylinders
Average head movement = Total head movement / no. of requests
= 480 / 8
= 60 cylinders
Remarks :

FCFS algorithm is simple to implement, but the performance is not satisfactory because
the average head movement is very high for a given set of requests..

SSTF(Shortest Seek Time First ) Disk Scheduling :

This algorithm select the request with minimum seek time from the current head
position that means the I/O request with the shortest seek time from the current position is
served first.
For instance, consider a disk queue with I/O requests for the following cylinders :
80,150,40,120,30,70,60 & 10. Let the read/write head be initially at 50th cylinder .If these
requests were served in SSTF basis, it can be depicted as follows :

.0 20 40 60 80 100 120 140 160 180 200

. ..
.
.. . . .
Total head movement = ( 50 – 40) + ( 40 – 30 ) + ( 30 – 10 ) + (10 – 60 ) + ( 60 – 70 ) +
( 70 - 80) + ( 80 – 120 ) + ( 120 – 150 )
= 10 + 10 + 20 + 50 + 10 + 10 + 40 + 30
= 180 cylinders
Average head movement = Total head movement / no. of requests
= 180 / 8
= 22.5 cylinders

Remarks :
1. SSTF algorithm shows substantial improvement of performance over FCFS
algorithm. The average head movement is considerably less for a given set of
requests .
2. Waiting time is high for requests having higher seek time.

SCAN Disk Scheduling :

Dept of CS, GSC,Hassan Page 40


OPERATING SYSTEM

In this scheme the disk arm starts at one end of the disk and move towards the other
end by serving the requests along the way . At the other end the direction of movement is
reversed and servicing continues. Hence, this algorithm is also called as elevator algorithm.

For instance, consider a disk queue with I/O requests for the following cylinders :
80, 150,40,120,30,70,60 & 10.Let the read/write head be initially at 50th cylinder .The above
request served by scan algorithm as shown below :

.0 20 40

... .
60 80 100
.
120 140
. 160 180 200

. ..
Remarks :

• Elevator algorithm has better performance over FCFS.


• Waiting time is not uniform because some requests were serviced immediately while
some are waiting much time.

C-SCAN Disk Scheduling :

In this algorithm, the disc head moves from one end to the other end of the disc by
servicing the request along the way. When the head reaches the other end, it immediately
returns to the begin of the disc without servicing the requests on the return trip. In other words,
this algorithm treats the requests as a circular list.

For instance, consider a disk queue with I/O requests for the following cylinders :
80, 150, 40, 120, 30, 70, 60 & 10. Let the read/write head be initially at 50th cylinder .The
above request served by scan algorithm as shown below :

.0 20 40

. .. .
60 80 100
.
120 140
. 160 180 200

. ..
Remarks :

Dept of CS, GSC,Hassan Page 41


OPERATING SYSTEM

• Average head movement is more than the scan algorithm but it provides more
uniform wait time.

There are two methods for breaking a deadlock. One method is simply to abort one by one process to
break the circular wait.
The second method is to preempt some resources from one or more of the deadlocked process/es
Process termination : Process termination is one of the methods to recover from deadlock. It can be done
by the following ways
a)abort all deadlocked processes : release all processes in the deadlocked state and start allocation from
the beginning but it is a very expensive method.
b)Abort one by one process until the deadlock cycle is eliminated ,here first abort one of the processes
and allocate the resources(obtained from abort process) to other process in the deadlock state, then check whether
deadlock breaked or not. If not abort another until the system is recovered from deadlock.

Input – Output Management :

Input-output management deals with the management and control of input-output


operations and input-output devices.Now-a-days a variety of input-output devices are
available.They are grouped into three categories.

Human Readable Input-Output Devices :


These devices are used to establish communication between the computers and user.
Eg., keyboard , mouse,printer,video display terminal

Machine Readable Device :


These devices are used to communicate with electronic equipments.
Eg., disk drives, tape drives[magnetic drive]

Communication Devices :
These devices are used to communicate with remote devices.
Eg., network cards, modem

Dept of CS, GSC,Hassan Page 42


OPERATING SYSTEM

Disk scheduling:

In multiprogramming systems several different processes may want to use the system's
resources simultaneously. For example, processes will contend to access an auxiliary storage
device such as a disk. The disk drive needs some mechanism to resolve this contention, sharing
the resource between the processes fairly and efficiently.

Disks are commonly used for bulk storage because disks are accessed randomly and
access time is very small.A magnetic disk consists of a collection of platters which rotate on
about a central spindle. These platters are metal disks covered with magnetic recording material
on both sides. Each disk surface is divided into concentric circles called tracks. Each track is
divided into sectors where information is stored. The reading and writing device, called the
head moves over the surface of the platters until it finds the track and sector it requires.. There
is one head for each surface on which information is stored each on its own arm. In most
systems the arms are connected together so that the heads move in unison, so that each head is
over the same track on each surface. The term cylinder refers to the collection of all tracks
which are under the heads at any time.

Disk Perfromance :

Performance of a disk can be evaluated using many parameters like seek time, latency
delay and transfer rate.

Seek time :
It is the time required by the read/write head to reach the desired track on a disk.It
depends on two factors internal start of time and time taken to traverse the cylinders .It is given
by the relation TS = m * n + S .
Where TS = estimated seek time
m=constant that depends on the disk drive
n=number of tracks traversed
s=startup time

Dept of CS, GSC,Hassan Page 43


OPERATING SYSTEM

Latency (Rotational)Delay :
It is the time required to reach desired sector on a track by the read/write head.
Rotational delay generally varies from 100 to 200ms.
Access time: Access time is the sum of seek time and rotational delay.
Transfer Time :
Transfer time depends upon the rotational speed of the disk and it is given by
T = B/R * N
T=Transfer rate
B=number of bytes to be transferred
R=rotational speed in revolutions per sec
N=number of bytes on track

Disk Scheduling Algorithms :


The most popular disk scheduling algorithms are
FCFS Disk Scheduling
SSTF Disk Scheduling
SCAN Disk Scheduling
C-SCAN Disk Scheduling

FCFS (First Come First Serve )Disk Scheduling :

It is the simplest disk scheduling algorithm. In this scheme the disk queue for I/O request
were served in first-come first-served basis.
For instance, consider a disk queue with I/O request for the following cylinders :
80, 150,40,120,30,70,60 & 10.Let the read/write head be initially at 50th cylinder .If these
requests served in FCFS basis, it can be depicted as follows :

Dept of CS, GSC,Hassan Page 44


OPERATING SYSTEM

.0 40 60 80 100 120 140 160 180 200


20

. . .
. .
.
. ..
Total head movement = ( 50 – 80 ) + ( 80 – 150 ) + ( 150 – 40 ) + (40 – 120 ) + ( 120 – 30 ) +
( 30 - 70) + ( 70 – 60 ) + ( 60 – 10 )
= 30 + 70 + 110 + 80 + 90 + 40 + 10 + 50
= 480 cylinders
Average head movement = Total head movement / no. of requests
= 480 / 8
= 60 cylinders
Remarks :

FCFS algorithm is simple to implement, but the performance is not satisfactory because
the average head movement is very high.

SSTF(Shortest Seek Time First ) Disk Scheduling :

This algorithm select the request with minimum seek time from the current head
position that means the I/O request with the shortest seek time from the current position is
served first.
For instance, consider a disk queue with I/O requests for the following cylinders :

Dept of CS, GSC,Hassan Page 45


OPERATING SYSTEM

80,150,40,120,30,70,60 & 10. Let the read/write head be initially at 50th cylinder .If these
requests were served in SSTF basis, it can be depicted as follows :

.0 20 40 60 80 100 120 140 160 180 200

. ..
.
.. . . .
Total head movement = ( 50 – 40) + ( 40 – 30 ) + ( 30 – 10 ) + (10 – 60 ) + ( 60 – 70 ) +
( 70 - 80) + ( 80 – 120 ) + ( 120 – 150 )
= 10 + 10 + 20 + 50 + 10 + 10 + 40 + 30
= 180 cylinders
Average head movement = Total head movement / no. of requests
= 180 / 8
= 22.5 cylinders

Remarks :
3. SSTF algorithm shows substantial improvement of performance over FCFS
algorithm. The average head movement is considerably less for a given set of
requests .
4. Waiting time is high for requests having higher seek time.

SCAN Disk Scheduling :

In this scheme the disk arm starts at one end of the disk and move towards the other
end by serving the requests along the way . At the other end the direction of movement is
reversed and servicing continues. Hence, this algorithm is also called as elevator algorithm.

For instance, consider a disk queue with I/O requests for the following cylinders :
80, 150,40,120,30,70,60 & 10.Let the read/write head be initially at 50th cylinder .The above
request served by scan algorithm as shown below :

Dept of CS, GSC,Hassan Page 46


OPERATING SYSTEM

.0 20 40

. .. .
60 80 100
.
120 140
. 160 180 200

. ..
Remarks :

• Elevator algorithm has better performance over FCFS.


• Waiting time is not uniform because some requests were serviced immediately while
some are waiting much time.

C-SCAN Disk Scheduling :

In this algorithm, the disc head moves from one end to the other end of the disc by
servicing the request along the way. When the head reaches the other end, it immediately
returns to the begin of the disc without servicing the requests on the return trip. In other words,
this algorithm treats the requests as a circular list.

For instance, consider a disk queue with I/O requests for the following cylinders :
80, 150, 40, 120, 30, 70, 60 & 10. Let the read/write head be initially at 50th cylinder .The
above request served by scan algorithm as shown below :

.0 20 40

. .. .
60 80 100
.
120 140
. 160 180 200

. ..
Remarks :

• Average head movement is more than the scan algorithm but it provides more
uniform wait time.

Dept of CS, GSC,Hassan Page 47


OPERATING SYSTEM

The simplest example of deadlock is where process 1 has been allocated a non-shareable
resources A, say, a tape drive, and process 2 has been allocated non-sharable resource B, say,
a printer. Now, if process 1 needs resource B (printer) and process 2 needs resource A (the tape
drive) to proceed and these are the only two processes in the system, each is blocked the other
and all useful work in the system stops. This situation is termed as deadlock. The system is in
deadlock state because each process holds a resource being requested by the other process
neither process is willing to release the resource it holds.

Dept of CS, GSC,Hassan Page 48


OPERATING SYSTEM

whose value can be accessed and altered only by the operations P and V and initialization
operation called 'Semaphoiinitislize'.
The value of a semaphore is the number of units of the resource which are free. If there is only
one resource , then the semaphore is called a binary semaphore, it can assume only the value
0 or the value 1. Counting semaphores also called general semaphores can assume only
nonnegative values.

The P operations wait until a resource is available. V is the inverse operation that simply
makes a resource available again after the process has finished its task.
The P (or wait or sleep or down) operation on semaphores S, written as P(S) or wait (S),
operates as follows:

P(S): IF S > 0
THEN S:= S-1
ELSE (wait on S)

The V (or signal or wakeup or up) operation on semaphore S, written as V(S) or signal (S),
operates as follows:

V(S): IF (one or more process are waiting on S)


THEN(let one of these processes proceed)
ELSE S := S +1

Operations P and V are done as single, indivisible, atomic action. It is guaranteed that
once a semaphore operations has started, no other process can access the semaphore until
operation has completed. Mutual exclusion on the semaphore, S, is enforced within P(S) and
V(S).

If several processes attempt a P(S) simultaneously, only one process will be allowed to
proceed. The other processes will be kept waiting, but the implementation of P and V
guarantees that processes will not suffer indefinite postponement.
Semaphores solve the lost-wakeup problem.

Spooling

The term "spool" is an acronym of "Simultaneous Peripheral Operation On Line”.


Spooling refers to putting jobs in a buffer( a special area in memory or on a disk )where a
device can access them when it is ready. Spooling is useful because devices access data at
different rates. The buffer provides a waiting station where data can rest while the slower device
catches up.

Dept of CS, GSC,Hassan Page 49


OPERATING SYSTEM

Disk

I/P CPU O/P

The most common spooling application is print spooling. In print spooling, documents
are loaded into a buffer (usually an area on a disk), and then the printer pulls them off the buffer
at its own rate. Because the documents are in a buffer where they can be accessed by the printer,
you can perform other operations on the computer while the printing takes place in the
background. Thus spooling keep both cpu and i/o devices busy at much higher rate and
optimizes the utility of resources.
Priority can be defined either internally or externally. Internally defined priorities use some
measurable quantities or qualities to compute priority of a process.
Examples of Internal priorities are

• Time limits.
• Memory requirements.
• File requirements,
for example, number of open files.
• CPU Vs I/O requirements.

ternally 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.

Priority scheduling can be either preemptive or non preemptive

• A preemptive priority algorithm will preempt the CPU if the priority of the newly
arrival process is higher than the priority of the currently running process.
• A non-preemptive priority algorithm will simply put the new process at the head of the
ready queue.
• A major problem with priority scheduling is indefinite blocking or starvation. A
solution to the problem of indefinite blockage of the low-priority process is aging.
Aging is a technique of gradually increasing the priority of processes that wait in the
system for a long period of time within the defined time interval.

Input – Output Management :

Dept of CS, GSC,Hassan Page 50


OPERATING SYSTEM

Input-output management deals with the management and control of input-output


operations and input-output devices.Now-a-days a variety of input-output devices are
available.They are grouped into three categories.

Human Readable Input-Output Devices :


These devices are used to establish communication between the computers and user.
Eg., keyboard , mouse,printer,video display terminal

Machine Readable Device :


These devices are used to communicate with electronic equipments.
Eg., disk drives, tape drives[magnetic drive]

Communication Devices :
These devices are used to communicate with remote devices.
Eg., network cards, modem

Dead Locks :

A set of processes is in a deadlock state if each process in the set is waiting for an event
that can be caused by only another process in the set. In other words, each process in the set of
deadlock processes is waiting for a resource that can be released only by a deadlock process.
None of the processes can run, none of them can release any resources, and none of them can
be awakened.

A P B P2
1

The resources may be either physical or logical. Examples of physical resources are Printers,
Tape Drivers, Memory Space, and CPU Cycles. Examples of logical resources are Files,
Semaphores, and Monitors.

The simplest example of deadlock is where process 1 has been allocated a non-
shareable resources A, say, a tape drive, and process 2 has been allocated non-sharable resource
B, say, a printer. Now, if process 1 needs resource B (printer) and process 2 needs resource A
(the tape drive) to proceed and these are the only two processes in the system, each is blocked
the other and all useful work in the system stops. This situation is termed as deadlock. The
system is in deadlock state because each process holds a resource being requested by the other
process neither process is willing to release the resource it holds.

Resource Allocation Graph :

It is a directed graph used to represent deadlock.It consists of two types of nodes.

• Process is indicated by a circle and resource indicated by a square.

Dept of CS, GSC,Hassan Page 51


OPERATING SYSTEM

• An edge from process P to resource R (P--> R)called request edge and an edge from
resource R to process P (R-→P)called assignment edge.

P R1 P R2
1 2

Request edge Assignment edge

Necessaery Conditions for Deadlock :

Mutual Exclusion :

This condition holds in the system when the resources are in non-sharable mode.
that is, only one process at a time claims exclusive control of the resource. If another process
requests that resource, the requesting process must be delayed until the resource has been
released.
Ex. Printer is a non-sharable resource, it cannot be shared by two or more processes at a time.If
such a resource is requested by two or more processes, they have to wait.

Hold and Wait :

This condition occur when each and every process in the system holding one or the
other resource and requesting for additional resources that are currently being held by other
processes.

P R1 P R2
1 2

No Preemption :

Resources already allocated to a process cannot be preempted that is


resources are not released before completion of the task. A Process voluntarily releases the
resource only after completion of its task.

Circular Wait

The processes in the system form a circular list or chain where each process in the list
is waiting for a resource held by the next process in the list.
For eg : a set of processes P1 to Pn exists in a chain such that P1 is wait for R1, R1 is held by
P2, P2 is waiting for R2 and R2 is held by P3 …..Rn is allocated to P1.

Dept of CS, GSC,Hassan Page 52


OPERATING SYSTEM

P R1 P R2 P
1 2 3

R3

Deadlock Prevention :

Deadlock prevention deals with preventive methods before attacking dead lock. A
system enters into deadlock state when all the four necessary condition holds simultaneously
in the system.By ensuring that atleast one of these necessary conditions cannot hold in the
system we can prevent the occurance of deadlock.Let us apply this technique to each of the
conditions.

Mutual Exclusion :

Mutual exclusion occurs when all resources are in non-sharable mode .We can deny
this condition by using a protocol that “ convert all non-sharable resources into sharable
resources “ but for intrisically non-sharable resources like printer we cannot apply this
condition

Hold and Wait :

Hold and wait condition arises when each and every process in the system by holding
one or the other resources and requesting for additional resources.We can deny this condition
using a protocol that “ A process can request the resource only when it has none of the other
resources”

No Preemption :

Resources are not released in the middle of the task , the process can voluntarily release
its resources after the completion of its task.We can deny this condition by using a
protocol”pre-empt the resource from waiting process and allocated to some other process “

Circular Wait :

Occurance of circular wait condition can be prevented by imposing of total ordering of


all resources i.e., by assigning an integer value to each of the resources.

F:R N
F is a one to one function that maps a resource R to an integer value N
Ex : F ( card reader ) = 1
F( disk drive ) = 5
F( printer ) = 12

Dept of CS, GSC,Hassan Page 53


OPERATING SYSTEM

Dead Lock Avoidance :

Dead lock avoidance is one of the method of dynamically escaping from the dead lock
.In this scheme when a process request for the resource the algorithm checks for the status of
the system whether the alloction of the resource leaves the system in safe state or not.
If the system remains in safe state even after the allocation of the resource the resource are
allocated.Thus a safe state is one in which no deadlock occurs.A deadlock state is an unsafe
state however all unsafe states are not dead lock.

Banker’s Algorithm :

Banker’s algorithm is a deadlock avoidance algorithm .According to this algorithm


when a process enters the system it must declare the maximum number of instances of each
resources type that it may need. This number maynot exceed the total number of resources
available in the resource .This algorithm

Preemptable and Nonpreemptable Resources:

Resources come in two flavors: preemptable and nonpreemptable. A preemptable


resource is one that can be taken away from the process with no ill effects. Memory is an
example of a preemptable resource. On the other hand, a nonpreemptable resource is one that
cannot be taken away from process (without causing ill effect). For example, CD resources are
not preemptable at an arbitrary moment. Reallocating resources can resolve deadlocks that
involve preemptable resources. Deadlocks that involve nonpreemptable resources are difficult
to deal with.

Necessary and Sufficient Deadlock Conditions

Coffman (1971) identified four (4) conditions that must hold simultaneously for there to be a
deadlock.

Consider each section of the street as a resource.

• Mutual exclusion condition applies, since only one vehicle can be on a section of the
street at a time.
• Hold-and-wait condition applies, since each vehicle is occupying a section of the street,
and waiting to move on to the next section of the street.
• No-preemptive condition applies, since a section of the street that is a section of the
street that is occupied by a vehicle cannot be taken away from it.
• Circular wait condition applies, since each vehicle is waiting on the next vehicle to
move. That is, each vehicle in the traffic is waiting for a section of street held by the
next vehicle in the traffic.

Dept of CS, GSC,Hassan Page 54


OPERATING SYSTEM

The simple rule to avoid traffic deadlock is that a vehicle should only enter an
intersection if it is assured that it will not have to stop inside the intersection.
It is not possible to have a deadlock involving only one single process. The deadlock involves
a circular “hold-and-wait” condition between two or more processes, so “one” process cannot
hold a resource, yet be waiting for another resource that it is holding. In addition, deadlock is
not possible between two threads in a process, because it is the process that holds resources,
not the thread that is, each thread has access to the resources held by the process.

Dealing with Deadlock Problem:

In general, there are four strategies of dealing with deadlock problem:

Ostrich Approach : Just ignore the deadlock problem altogether.

Deadlock Detection and Recovery : Detect deadlock and, when it occurs, take steps to
recover.

Deadlock Avoidance : Avoid deadlock by careful resource scheduling.

Deadlock Prevention : Prevent deadlock by resource scheduling so as to negate at least one


of the four conditions.

Deadlock Detection

Deadlock detection is the process of determining the existence of deadlock and


identifying the processes and resources involved in the deadlock.
The basic idea is to check allocation against resource availability for all possible allocation
sequences to determine if the system is in deadlocked state a. Of course, the deadlock detection
algorithm is only half of this strategy. Once a deadlock is detected, there needs to be a way to
recover several alternatives exists:

• Temporarily prevent resources from deadlocked processes.


• Back off a process to some check point allowing preemption of a needed resource and
restarting the process at the checkpoint later.
• Successively kill processes until the system is deadlock free.

These methods are expensive in the sense that each iteration calls the detection
algorithm until the system proves to be deadlock free. The complexity of algorithm is O(N2)
where N is the number of proceeds. Another potential problem is starvation; same process
killed repeatedly.

Disk scheduling:

In multiprogramming systems several different processes may want to use the system's
resources simultaneously. For example, processes will contend to access an auxiliary storage
device such as a disk. The disk drive needs some mechanism to resolve this contention, sharing
the resource between the processes fairly and efficiently.

Disks are commonly used for bulk storage because disks are accessed randomly and
access time is very small.A magnetic disk consists of a collection of platters which rotate on

Dept of CS, GSC,Hassan Page 55


OPERATING SYSTEM

about a central spindle. These platters are metal disks covered with magnetic recording material
on both sides. Each disk surface is divided into concentric circles called tracks. Each track is
divided into sectors where information is stored. The reading and writing device, called the
head moves over the surface of the platters until it finds the track and sector it requires.. There
is one head for each surface on which information is stored each on its own arm. In most
systems the arms are connected together so that the heads move in unison, so that each head is
over the same track on each surface. The term cylinder refers to the collection of all tracks
which are under the heads at any time.

Disk Perfromance :

Performance of a disk can be evaluated using many parameters like seek time, latency
delay and transfer rate.

Seek time :
It is the time required by the read/write head to reach the desired track on a disk.It
depends on two factors internal start of time and time taken to traverse the cylinders .It is given
by the relation TS = m * n + S .
Where TS = estimated seek time
m=constant that depends on the disk drive
n=number of tracks traversed
s=startup time

Latency (Rotational)Delay :
It is the time required to reach desired sector on a track by the read/write head.
Rotational delay generally varies from 100 to 200ms.
Access time: Access time is the sum of seek time and rotational delay.
Transfer Time :
Transfer time depends upon the rotational speed of the disk and it is given by
T = B/R * N
T=Transfer rate

Dept of CS, GSC,Hassan Page 56


OPERATING SYSTEM

B=number of bytes to be transferred


R=rotational speed in revolutions per sec
N=number of bytes on track

Disk Scheduling Algorithms :


The most popular disk scheduling algorithms are
FCFS Disk Scheduling
SSTF Disk Scheduling
SCAN Disk Scheduling
C-SCAN Disk Scheduling

FCFS (First Come First Serve )Disk Scheduling :

It is the simplest disk scheduling algorithm. In this scheme the disk queue for I/O request
were served in first-come first-served basis.
For instance, consider a disk queue with I/O request for the following cylinders :
80, 150,40,120,30,70,60 & 10.Let the read/write head be initially at 50th cylinder .If these
requests served in FCFS basis, it can be depicted as follows :

Dept of CS, GSC,Hassan Page 57


OPERATING SYSTEM

.0 40 60 80 100 120 140 160 180 200


20

. . .
. .
.
. ..
Total head movement = ( 50 – 80 ) + ( 80 – 150 ) + ( 150 – 40 ) + (40 – 120 ) + ( 120 – 30 ) +
( 30 - 70) + ( 70 – 60 ) + ( 60 – 10 )
= 30 + 70 + 110 + 80 + 90 + 40 + 10 + 50
= 480 cylinders
Average head movement = Total head movement / no. of requests
= 480 / 8
= 60 cylinders
Remarks :

FCFS algorithm is simple to implement, but the performance is not satisfactory because
the average head movement is very high.

SSTF(Shortest Seek Time First ) Disk Scheduling :

This algorithm select the request with minimum seek time from the current head
position that means the I/O request with the shortest seek time from the current position is
served first.
For instance, consider a disk queue with I/O requests for the following cylinders :

Dept of CS, GSC,Hassan Page 58


OPERATING SYSTEM

80,150,40,120,30,70,60 & 10. Let the read/write head be initially at 50th cylinder .If these
requests were served in SSTF basis, it can be depicted as follows :

.0 20 40 60 80 100 120 140 160 180 200

. ..
.
.. . . .
Total head movement = ( 50 – 40) + ( 40 – 30 ) + ( 30 – 10 ) + (10 – 60 ) + ( 60 – 70 ) +
( 70 - 80) + ( 80 – 120 ) + ( 120 – 150 )
= 10 + 10 + 20 + 50 + 10 + 10 + 40 + 30
= 180 cylinders
Average head movement = Total head movement / no. of requests
= 180 / 8
= 22.5 cylinders

Remarks :
1. SSTF algorithm shows substantial improvement of performance over FCFS
algorithm. The average head movement is considerably less for a given set of
requests .
2. Waiting time is high for requests having higher seek time.

SCAN Disk Scheduling :

In this scheme the disk arm starts at one end of the disk and move towards the other
end by serving the requests along the way . At the other end the direction of movement is
reversed and servicing continues. Hence, this algorithm is also called as elevator algorithm.

For instance, consider a disk queue with I/O requests for the following cylinders :
80, 150,40,120,30,70,60 & 10.Let the read/write head be initially at 50th cylinder .The above
request served by scan algorithm as shown below :

Dept of CS, GSC,Hassan Page 59


OPERATING SYSTEM

.0 20 40

. .. .
60 80 100
.
120 140
. 160 180 200

. ..
Remarks :

• Elevator algorithm has better performance over FCFS.


• Waiting time is not uniform because some requests were serviced immediately while
some are waiting much time.

C-SCAN Disk Scheduling :

In this algorithm, the disc head moves from one end to the other end of the disc by
servicing the request along the way. When the head reaches the other end, it immediately
returns to the begin of the disc without servicing the requests on the return trip. In other words,
this algorithm treats the requests as a circular list.

For instance, consider a disk queue with I/O requests for the following cylinders :
80, 150, 40, 120, 30, 70, 60 & 10. Let the read/write head be initially at 50th cylinder .The
above request served by scan algorithm as shown below :

.0 20 40

. .. .
60 80 100
.
120 140
. 160 180 200

. ..
Remarks :

• Average head movement is more than the scan algorithm but it provides more
uniform wait time.

Dept of CS, GSC,Hassan Page 60


OPERATING SYSTEM

UNIT – 3

SYSTEM PROGRAMMING
System programming can be viewed as establishing a programming environment
which makes more productive use of the programmer's time than that provided by the hardware
alone.

System programs were developed to make computers better adapted to the needs of
their users. There are many computers in use now in virtually every application. The
productivity of each computer is heavily dependent upon the effectiveness, efficiency, and
sophistication of the system programs.
System programming can be classified into two types:

System software:

It is general purpose software. It is the code that defines what the computer is and how it
controls and uses the hardware connected to it.
Eg: Operating Systems, Assemblers, Loaders, Linkers, Compilers, and Macros.

Application software:

Is written to perform a specific task (or a group of related tasks). The removal of
application software does not affect the functioning of computer. It requires system software
for its functioning.
Eg: "Microsoft Word"- the application being a word processor, Microsoft Exel, DBMS, Games.

The components of System Software are broadly classified into three main categories:

• Assemblers.
• Loaders and Linkers.
• Compilers.

ASSEMBLERS

An assembler is a program that accepts as input an assembly language program and


produces its machine language equivalent along with information for the loader.

Assembly Assembler Machine language and


Language Program other information to
Program the loader

Data Bases

Dept of CS, GSC,Hassan Page 61


OPERATING SYSTEM

Functions of an Assembler:

1. Generate instructions:

• Evaluate the mneumonic in the operation field to produce its machine code.
• Evaluate the subfields – find the value of each symbol, process literals, and
assign addresses.

2. Process pseudo ops.

We can group these tasks into two passes or sequential scans over the input associated
with each task are one or more assembler modules.

Pass 1:

Purpose - define symbols and literals.


• Determine length of machine instructions.
• Keep track of Location Counter.
• Remember values of symbols until pass 2.
• Process some pseudo ops, e.g., EQU, DS.
• Remember literals.

Pass 2:

Purpose - generate object program


• Look up value of symbols.
• Generate instructions.
• Generate data.
• Process pseudo ops.

PASS 1:

Data Bases:

• Input source program.


• A Location Counter (LC), used to keep track of each instruction’s location.
• A table, the Machine – Operation Table (MOT), that indicates the symbolic mneumonic
for each instruction and its length.
• A table, the Pseudo – Operation Table (POT), that indicates the symbolic mneumonic
and action to be taken for each pseudo – op.
• A table, the Symbol Table (ST), used to store each label and its corresponding value.
• A table, the Literal Table (LT), used to store each literal encountered and its
corresponding assigned location.
• A copy of the input to be used later by passes 2. This may be stored in a secondary
storage device.

Initialize
Dept of CS, GSC,Hassan Page 62
OPERATING SYSTEM

Read

Yes Store label in


Label ST with LC
on value
card
No

Found
End
Search Pseudo-op
Table (POT) Go to
DS
Type? Pass 2
DS

Determine
Search Machine- length of data
op Table (MOT) space required

Get length of Process literals Update Location


instruction Counter (LC)

Pass 1 overview: define symbols

The purpose of the first pass is to assign a location to each instruction and data –
defining pseudo – instruction, and thus to define values for symbols appearing in the label
fields of the source program. Initially the Location Counter (LC) is set to the first location in
the program (relative address 0). Then a source statement is read. The operation-code field is
examined to determine if it is a pseudo-op; if it is not, the table of machine op-codes is searched
to find a match for the source statement’s op-code field. The matched MOT entry specifies the
length of the instruction. The operand field is scanned for the presence of a literal. If a new
literal is found, it is entered into the Literal Table for later processing. The level field of the
source statement is then examined for the presence of a symbol. If there is a label, the symbol
is saved in the Symbol Table along with the current value of the location counter. Finally, the
current value of the location counter is incremented by the length of the instruction and a copy
of the source card is saved for use by pass 2. The above sequence is then repeated for the next
instruction.

Pass 1 is only concerned with pseudo-ops that define symbols (labels) or affect the
location counter; USING and DROP cards are saved for Pass 2. The DS and DC pseudo-ops
can affect both the location counter and the definition of symbols in Pass 1. The operand field
must be examined to determine the number of bytes of storage required. Due to requirements

Dept of CS, GSC,Hassan Page 63


OPERATING SYSTEM

for certain alignment conditions, it may be necessary to adjust the location counter before
defining the symbol.

When the END pseudo-op is encountered, Pass 1 is terminated. Before transferring


control t Pass 2, there are various “housekeeping” operations that must be performed. These
include assigning locations to literals that have been collected during Pass 1, finally, conditions
are reinitialized for processing by Pass 2.

PASS 2:

Data Bases:

• Copy of source program input to Pass 1.


• Location Counter (LC).
• A table, the Machine Operation Table (MOT), that indicates for each instruction: (a)
symbolic mneumonic; (b) length; (c) binary machine op-code, and (d) format.
• A table, the Pseudo-Operation Table (POT), indicates for each pseudo-op the symbolic
mneumonic and the action to be taken in Pass 2.
• The Symbol Table (ST), prepared by Pass 1, containing each label and its corresponding
value.
• A table, the Base Table (BT), that indicates which registers are currently specified as
base registers by USING pseudo-op and what are the specified contents of these
registers.
• A work-space, INST, that is used to hold each instruction as its various parts are being
assembled together.
• A work-space, PRINT LINE, used to produce a printed listing.
• A work-space, PUNCH CARD, used prior to actual outputting for converting the
assembled instructions into the format needed by the loader.
• An output deck of assembled instructions in the format needed by the loader.

Dept of CS, GSC,Hassan Page 64


OPERATING SYSTEM

Initialize

Read

Search Pseudo-op
Table (POT) Found
End Clean
Type?
up and
D exit
C D U D
Search Machine- S S R
op Table (MOT) Convert I O
and output G P
constants
Get length of
instruction, type &
binary code Determine
length of
data space
Evaluate operand
expressions by
searching for
values of symbols Indicate
available
base register
(BT)
Assemble together
the parts of the
instruction Indicate
unavailable
base register
(BT)
Update Location
Counter (LC)

Pass 2 overview: evaluate fields and generate code

After all the symbols have been defined by Pass 1, it is possible to finish the assembly
by processing each card and determining values for its operation cod and its operand field. In
addition, Pass2 must structure the generated code into the appropriate format for later
processing by the loader and print an assembly listing containing the original source and the
hexadecimal equivalent of the bytes generated. The Location Counter is initialized as in Pass
1, and the processing continues as follows.

Dept of CS, GSC,Hassan Page 65


OPERATING SYSTEM

A card is read from the source file left by Pass 1. The operation code field is examined
to determine if it is a pseudo-op; if it is not, the table of machine op-codes (MOT) is searched
to find a match for the card’s op-code field. The matching MOT entry specifies the length,
binary op-code, and the format-type of instruction. The operand fields of the different
instruction format types require different processing.

After the instruction has been assembled, it is put into the necessary format for later
processing by the loader. Typically, several instructions are placed on a single card. A listing
line containing a copy of the source card, its assigned storage location, and its hexadecimal
representation is then printed. Finally, the location counter is incremented and processing is
continued with the next card.

Each of the pseudo-ops calls for special processing. The EQU pseudo-op requires very
little processing in Pass 2, because symbol definition was completed in Pass 1. it is necessary
only to print the EQU card as part of the printed listing.

The USING and DROP pseudo-ops require additional processing in Pass 2. the operand
fields of the pseudo-ops are evaluated; then the corresponding Base Table entry either marked
as available, if USING, or unavailable, if DROP. The base table is used to compute the base
and displacement fields for machine instructions with storage operands.

Actual code must be generated for the DC pseudo-op. depending upon the data type
specified; this involves various conversions and symbol evaluations.

The END pseudo-op indicates the end of the source program and terminates the
assembly. Various “housekeeping” tasks must be performed.
Ex: code must be generated for any literals remaining in the Literal Table (LT).

LOADERS
The loader is a program which accepts the object program, prepares these programs for
execution by the computer, and initiates the execution.

In particular, the loader must perform four functions:

• Allocate space in memory for the program (allocation).


• Resolve symbolic references between object programs (linking).
• Adjust all address dependent locations, such as address constants, to correspond to the
allocated space (relocation).
• Physically place the machine instructions and data into memory (loading).

Dept of CS, GSC,Hassan Page 66


OPERATING SYSTEM

Loader
A

B
B

Data bases

Programs loaded in
memory Ready for
execution

General Loading Scheme

Types of Loaders:

Compile – and – Go Loaders:

One method of performing the loader functions is t have the assembler run in one part
of memory and place the assembled machine instructions and data, as they are assembled,
directly into their assigned memory locations. When the assembly is completed, the assembler
causes a transfer to the starting instruction of the program.

Such a loading scheme is commonly called “Compiler–and–Go” or “Assemble–and–


go”. It is relatively easy to implement. The assembler simply places the code into core, and the
“loader” consists of one instruction that transfers to the starting instruction of the newly
assembled program.

Disadvantages:

• A portion of memory is wasted because the core occupied by the assembler is


unavailable to the object program.
• It is necessary to retranslate the user’s program every time it is run.
• It is very difficult to handle multiple segments, especially if the source programs are in
different languages.
• It is very difficult to produce orderly modular programs.

Dept of CS, GSC,Hassan Page 67


OPERATING SYSTEM

Compile-and- Program loaded


Source Go translator in memory
program

Assembler

Memory

“Compile-and-Go” loader scheme

General Loader Scheme:

Outputting the instructions and data as they are assembled circumvents the problem of
wasting core for the assembler. Such an output could be saved and loaded whenever the code
was to be executed. This output form is known as an object program.

The use of an object program as intermediate data to avoid one disadvantage of the
proceeding “Compile-and-Go” scheme requires the addition of a new program to the system,
a loader. The loader accepts the assembled machine instructions, data, and other information
present in the object format, and places machine instructions and data in core in an executable
computer form. The loader is assumed to be smaller than the assembler, so that more memory
is available to the user. Another advantage is that reassembly is no longer necessary to run the
program at a later date.

Object
Source Translator program 1
program

Object programs
Loader ready for execution

Object
Source Translator program 2 Loader
program

General Loader Scheme


Absolute Loaders:

Dept of CS, GSC,Hassan Page 68


OPERATING SYSTEM

The simplest type of loader scheme, which fits the general model, is called an absolute
loader. In this scheme the assembler outputs the machine language translation of the source
program in almost the same form as in the “Assemble-and-Go” scheme, except that the data is
punched on object program instead of being placed directly in memory. The loader in turn
simply accepts the machine language text and places it into core at the location prescribed by
the assembler. The scheme makes more cores available to the user since the assembler is not
in memory at load time.

Absolute loaders are simple to implement.

100
MAIN
MAIN

248
Absolute Loader

SQRT 400
SQRT
Object Program 478

Absolute Loader

Designing of an Absolute Loader:

With an absolute loading scheme the programmer and the assembler perform the tasks
of allocation, relocation and linking. Therefore, it is only necessary for the loader to read cards
of the object program and move the text on the cards into the absolute locations specified by
the assembler.

There are two types of information that the object program must communicate from the
assembler to the loader:

• It must convey the machine instructions that the assembler has created along with the
assigned core locations.
• It must convey the entry point of the program, which is where the loader is to transfer
control when all instructions are loaded.

The instructions are stored on the card is card as one core byte per column and format is as
shown below:

Dept of CS, GSC,Hassan Page 69


OPERATING SYSTEM

Text Card (for Instruction & data)


Card Column Contents
1 Card Type = 0 (for text card identifier)
2 Count of number of bytes of information on card
3–5 Address at which data on card is to be put
6–7 Empty (for validity checking)
8 – 72 Instructions and data to be loaded
73 – 80 Card sequence number

Transfer Cards (to hold entry point to program)


Card Column Contents
1 Card Type = 1 (transfer card identifier)
2 Count = 0
3–5 Address of entry point
6 – 72 Empty
73 – 80 Card sequence number

The object program consists of a series of text cards terminated by a transfer card.
Therefore, the loader should read one card at a time, moving the text to the location specified
on the card, until the transfer card is reached. At this point the assembled instructions are in
core, and it is only necessary to transfer to the entry point specified on the transfer card. The
process is illustrated as follows,

Dept of CS, GSC,Hassan Page 70


OPERATING SYSTEM

Initialize

Read Card

Set CURLOC to location


in character 3-5

Card
Type
?

Set LNG to count in Transfer to location


character 2 CURLOC

Move LNG bytes of text


from character 8-72 to
location CURLOC

Relocating Loaders:

To avoid possible reassembling of all subroutines when a single subroutine is changed,


and to perform the tasks of allocation and linking for the programmer, the general class of
relocating loaders was introduced. The assembler assembles each procedure segment
independently and passes on to the loader the text and information as to relocation and inter-
segment references.

For each source program the assembler outputs a text prefixed by a transfer vector that
consists of addresses containing names of the subroutines referenced by the source program.
The assembler also provides the loader with additional information, such as the length of the
entire program and the length of the transfer vector portion.

The relocating loader then loads the text and the transfer vector into main memory and
changes any memory location if it is necessary. It allows sharing of several procedure segments.

Dept of CS, GSC,Hassan Page 71


OPERATING SYSTEM

It also makes use of transfer vector to transfer program control. Therefore problem of absolute
addressing is resolved.

Disadvantages:

• The use of transfer vector increases the size of object program in memory.
• It does not allow sharing of data segments.

Direct Linking Loaders:

A direct linking loader is a general relocatable loader and has the advantage of allowing
the programmer multiple procedure segments and multiple data segments and of giving
complete freedom in referencing data or instructions contained in other segments. This
provides flexible inter-segment referencing and accessing ability, while at the same time
allowing independent translations of programs.

The assembler must provide the following information to the loader with each procedure
or data segment:

• The length of segment.


• A list of all the symbols in the segment that may be referenced in the segment.
• A list of all symbols not defined in the segment but referenced in the segment.
• Information as to where address constants are located in the segment and a description
of how to revise their values.
• The machine code translation of the source program and the relative addresses assigned.

Dept of CS, GSC,Hassan Page 72


OPERATING SYSTEM

COMPILERS
A compiler is system software which accepts a program written in a higher level
language as input and produces its machine language equivalent as output.

Functions of compilers:

• Recognizes certain strings as basic elements.


• Recognizes combinations of elements as syntactic units and interpret their meaning.
• Allocate storage and assign locations for all variables.
• Generate the appropriate object code.

General Model or Phases of the Compiler:

There are seven distinct logical phases of the Compiler model.

• Lexical Analysis Phase – recognition of basic elements and creation of uniform


symbols.

• Syntax Analysis Phase – recognition of basic syntactic constructs through reductions.

• Interpretation Phase – definition of exact meaning, creation of matrix and tables by


action routines.

• Machine Independent Optimization Phase – creation of more optimal matrix.

• Storage Assignment Phase – modification of identifier and literal tables. It makes


entries in the matrix that allow code generation to create code that allocates dynamic
storage, and that also allow the assembly phase to reserve the proper amounts of static
storage.

• Code Generation Phase – use of macro processor to produce more optimal assembly
code.

• Assembly and Output Phase – resolving symbolic addresses and generating machine
language.

Dept of CS, GSC,Hassan Page 73


OPERATING SYSTEM

Dept of CS, GSC,Hassan Page 74


OPERATING SYSTEM

Lexical Analysis Phase:

The action of parsing the source program into the proper syntactic classes is known as
Lexical Analysis. The source program is can sequentially and it involves the simple string
processing techniques.

Lexical Analysis phase performs 3 important functions:

1) To divide source program into the basic elements (tokens) of the language.
2) To build the Literal Table & Identifier Table.
3) To build the Uniform Symbol Table.

Lexical phase uses source program & Lexical Table as inputs & produces Uniform Symbol
Table (UST) Literal Table (LT) & Identifier Table (IT) as outputs.

In Lexical Analysis, the source program is broken into tokens approximate entries are
made in the table. The UNIFORM SYMBOL TABLE is built which consists of the table
number, index of the symbol in the terminal table. The various identifiers appearing in the
program are stored in the IT. If a token is not an identifier or terminal symbol then it is entered
into Literal Table.

The Lexical process can be done in one continuous pass through the data by creating
an intermediate form of the program consisting of a chain or table of tokens. It discovers a note
Lexical Errors also discards commands since they have no effect on the processing of the
program.

Syntax Analysis Phase:

The function of Syntax Analysis is to recognize the major constructs of the language.
This phase calls the appropriate action routines which will generate intermediate form of matrix
for these constructs. Syntax phase takes Uniform Symbol Table & reductions as input’s &
produces intermediate form or matrix as output.

Syntax Analysis also notes syntactic errors and assures some sort of recovery so that
the compiler can continue to look for other compilation errors.

The reduction database contains the syntax rule of the language. Syntax Analysis phase
is driven by the reductions.

Interpretation Phase:

It consists of a collection of routines which are called up when a construct (statement)


recognized as valid in the syntax analysis. The purpose of interpretation phase is to create an
intermediate form of source program (matrix) & to add information to Identifier Table (IT)
about the attribute of identifier’s these actions are called by appropriate action routines.

Once the syntactic construction is determined, the compiler can generate object code
for each construction and creates an Intermediate form of the source program. The Intermediate
form affords two advantages,

Dept of CS, GSC,Hassan Page 75


OPERATING SYSTEM

• It facilitates optimization of object code.


• It allows a logical separation between the machine-independent phases (Lexical,
Syntax, and Interpretation) and the machine-independent phases (Code Generation and
Assembly).

Separation between syntax phase & interpretation phase is a logical division. Syntax
phase recognizes the constructs (Statements), while interpretation phrase gives precise meaning
of these constructs entering them into matrix or Identifier Table.

Therefore interpretation phase accepts Uniform Symbol Table as input & produces
matrix & updated Identifier Table as output.

Optimization Phase:

There are two types of optimization perform by a compiler,

• Machine Dependent and


• Machine Independent

Machine dependent optimization is related to the instructions that get generated that it
was incorporated into the code generation phase, where as machine independent optimization
was done in a separate optimization phase.

Machine Independent Optimization Phase:


It is used to increase the efficiency of the matrix. Generally four types of optimization
are performed.

1) Elimination of common sub-expression:

In the matrix, if there are sub expressions which are repeated more than once
they can be eliminated making the object program concise (small) & efficient.

2) Compile time Compute:

Doing computations involving constants at compile time serves both space &
execution times for the object program. This is especially helpful if there is such an
arithmetic computation within a loop.

3) Boolean Expression Optimization:

We may use properties of Boolean expressions to shorten their computations.


Ex:- if (A or B or C) then
Here condition A is true then the condition’s B & C need not to be executed.

4) Moving invariant Computations outside of Loops:

If a computation within a loop depends on a variable that does not change within that
loop, the computation may be moved outside the loop.

Dept of CS, GSC,Hassan Page 76


OPERATING SYSTEM

This involves a reordering of a part of the matrix. There are three general problems that
need to be solved in an algorithm of this type.
• Recognition of invariant computations.
• Discovering where to move the invariant computation.
• Moving the invariant computation.

Therefore the optimization phase accepts matrix as input & produces optimized matrix
as output.

Storage Assignment Phase:

Storage assignment phase perform the following functions.


• It assigns storage to all variables which are used in the source program.
• It assigns storage to all the literals which are used in the source program.
• It assigns storage to temporary locations which are required for intermediate results.
• It ensures that the storage is allocated and appropriate locations are initialized (literals
and any variables with the initial attributes).

The storage assignment phase takes Identifier Table & Literal Table as input & makes
entries into the matrix which is used by the future phases.

Code Generation Phase:

The purpose of the code generation phase is to produce the appropriate code (Assembly
level language or Machine level language). The code generation phase uses the code
productions (macro definitions) which define the operators that may appear in the matrix to
produce code. It also references Identifier Tables & Literal Tables in order to generate proper
address and code conversions.

The code Generation phase has the matrix as input.

Assembly & Output Phase:

The task of the Assembly phase depends on how much has been done in code
generation. If a lot of work has been done in code generation, then the Assembly phase must
resolve label references in the object program, format the object program and format the
appropriate information for the loader.

At the other extreme, if code generation has simply generated symbolic machine
instructions and labels, the assemble phase must:

• Resolve Label References


• Calculate Addresses
• Generate Binary Machine Instructions
• Generate Storage
• Convert Literals

The Assembly phase uses Identifier Table and Literal Table as input, object code is the
output of the code generation.

Dept of CS, GSC,Hassan Page 77


OPERATING SYSTEM

Dept of CS, GSC,Hassan Page 78

You might also like