Operating System
Operating System
SEMESTER : 5
PAPER 6 : Operating Systems And System Software
THEORY : 80 (EXAM) + 10 (IA) PRACTICALS :20 (EXAM)+10 (IA)
UNIT – 1
UNIT – 2
UNIT - 3
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).
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.
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
place to another. One need not write programs for these tasks. Such standard programs are
known as utility programs.
Operating System:
Operating system
Computer hardware
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.
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 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 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 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.
Fourth Generation
With the development of LSI (Large Scale Integration) circuits, chips, operating system
entered in the personal computer and the workstation age.
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
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.
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.
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’.
OS
Job 1
Job2
Job n
IO Completion
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.
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
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.
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 :
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’
• 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
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
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
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.
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.
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.
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.
O.S
J1
J3
J2
J4
1000
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.
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.
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.
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.
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.
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
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
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
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:
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 :
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
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:
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 Process
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.
OS moves a process from one state to another during its life cycle.
➢ 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.
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
Pointer to
parent process
Process State
Pointer to child
process
Process Identification number
Process Priority
CPU Registers
Memory Limit
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.
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).
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.
Suspended queue
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.
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
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.
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
Child Create a
execute child process
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 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.)
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.
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.
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
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
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
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.
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
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
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;
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.
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.
Disk
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.
A P B P2
1
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.
P R1 P R2
1 2
3.No Preemption :
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.
P R1 P R2 P
1 2 3
R3
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.
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.
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.
Elimination of No Preemption :
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 :
deadlock
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 :
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]
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.
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:
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
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
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
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.
R1
R1 R1 R2
P1 P2 P3
R3 R4
Resource Allocation Graph
P1 P2 P3
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.
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.
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
Step4: if finish[i]=false, then the system is in a deadlock state, moreover process Pi is deadlocked.
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
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 :
. . .
. .
.
. ..
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 for a given set of requests..
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 :
. ..
.
.. . . .
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.
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 :
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.
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.
Communication Devices :
These devices are used to communicate with remote devices.
Eg., network cards, modem
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
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
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 :
. . .
. .
.
. ..
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.
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 :
. ..
.
.. . . .
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.
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 :
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.
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.
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:
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
Disk
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.
• 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.
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.
• 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
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.
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 :
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.
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 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 :
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
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 :
Coffman (1971) identified four (4) conditions that must hold simultaneously for there to be a
deadlock.
• 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.
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.
Deadlock Detection and Recovery : Detect deadlock and, when it occurs, take steps to
recover.
Deadlock Detection
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
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
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 :
. . .
. .
.
. ..
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.
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 :
. ..
.
.. . . .
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.
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 :
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.
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
Data Bases
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.
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:
Pass 2:
PASS 1:
Data Bases:
Initialize
Dept of CS, GSC,Hassan Page 62
OPERATING SYSTEM
Read
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
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
for certain alignment conditions, it may be necessary to adjust the location counter before
defining the symbol.
PASS 2:
Data Bases:
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)
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.
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.
Loader
A
B
B
Data bases
Programs loaded in
memory Ready for
execution
Types of 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.
Disadvantages:
Assembler
Memory
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
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.
100
MAIN
MAIN
248
Absolute Loader
SQRT 400
SQRT
Object Program 478
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:
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,
Initialize
Read Card
Card
Type
?
Relocating Loaders:
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.
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.
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:
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:
• Code Generation Phase – use of macro processor to produce more optimal assembly
code.
• Assembly and Output Phase – resolving symbolic addresses and generating machine
language.
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.
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.
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:
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,
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:
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.
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.
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.
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.
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.
The storage assignment phase takes Identifier Table & Literal Table as input & makes
entries into the matrix which is used by the future phases.
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 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:
The Assembly phase uses Identifier Table and Literal Table as input, object code is the
output of the code generation.