Вы находитесь на странице: 1из 145

Unit -1

Introduction
What is an Operating System?
An operating system (OS) is the interface between the user and the hardware. It
implements a virtual machine that is easier to program than bare hardware. An OS provides
standard services (an interface) which are implemented on the hardware, including: l Processes,
CPU scheduling, memory management, file system, networking. The OS coordinates multiple
applications and users (multiple processes) in a fair and efficient manner. The goal in OS
development is to make the machine convenient to use (a software engineering problem) and
efficient (a system and engineering problem)
(Ref Class Notes)

Definition:
An operating system is a program that manages the computer hardware. It also provides a basis for
application programs and acts as an intermediary between the computer user and the computer
hardware.

Computer System

(To Remember What OS)

A magician --provides each user with the illusion of a dedicated machine with infinite
memory and CPU time
A government--allocates resources efficiently and fairly, protects users from each other,
provides safe and secure communication.
A parent--always there when you need it, never breaks, always succeeds
A fast food restaurant--provides a service everyone needs, always works the same
everywhere (standardization)
A complex system--but keep it as simple as possible so that it will work
Why Study Operating Systems?

Abstraction how do you give the users the illusion of infinite resources (CPU time,
memory, file space)?

System design tradeoffs between:


o performance and convenience of these abstractions
o performance and simplicity of OS
o functionality in hardware or software

Primary intersection point OS is the point where hardware, software, programming


languages, data structures, and algorithms all come together

Operating systems are among the most complex pieces of software yet developed,
William Stallings, 1994

Requirement of Modern OS Functionality


Concurrency: Multiple processes active at once.
Processes can communicate.
Processes may require mutually exclusive.
access to some resource.
CPU scheduling, resource management
Memory management: Allocate memory to processes, move processes between disk and memory
File system: allocate space for storage of programs and data on disk
Networks and distributed computing: allow computers to work together
Security & protection

Objectives of Operating System


Convenience An operating system makes a computer more convenient to use.
Efficiency An operating system allows the computer system resources to be used in an
efficient manner.
Ability to evolve An operating system should permit the effective development, testing,
and introduction of new system function without interfering with the service.

History of Operating System


The first and second generations (1945-1955, 1956-1965)
The only system program in early computers was a test program that had tried to locate trouble in
computer (that appeared the every hour because of law realibility).
Two persons are working to keep the enormously large machines in the working state. Hardwareengineer was responsible for electronical equipment, the main users were mathematicians with
programming skills on machine language. There were not any economical value (price) related with
thesecomputers.
I think there is a world market for about five computers
Thomas J. Watson (1945)
Computes were still very expensive and only big corporations, major government agencies or
universities could pay the multimillion-dollar price.
The number of users-programmers had increased and the problem appeared how to organize the
efficient computers utilization.

Loader
Job
sequencing

Monitor

Control card
interpreter
User program
area
Memory layout for a resident monitor

The new group of personal was appeared: operators.


They were technicians on computer unit with the main task to execute all operations
faster than programmer could. A programmer first wrote the program on paper, then punch it on
cards (for the second generation computers it existed special room and technicians). After this he
brought the cards to the input room and hands it to one of the operators. An operator executed all
technical operations, went over to the printer and teared off the output and carried it to the output
room, so that the programmer could collect it later. In this chain
Programmer

operator

computer

Much computer time was wasted in the interaction between operator and computer. To
speed up processing two points were developed magnetic tapes were used instead of cards, jobs
with similar needs were batched together and were run through the computer as a group. Second
generation computers were used mostly for scientific and engineering calculations, and
programmed in FORTRAN and ASSEMBLER languages.
The very simple batch operating system was always resident in memory, then normally
reads a steam of separate jobs, each with own control cards that predefine what the job does. For
the purpose of automatic job sequencing a small program was designed, called a resident monitor.
The resident monitor is always (resident) in memory.
Control-card interpreter is responsible for reading and carrying out the instructions on the
cards. Loader loads system programs and application programs into memory.
The Third generation (1965 1980)
This period was a real expansion of computers to commercial companies banks,
enterprises, insurance companies and etc. Most typical computer line was the System/360 as a
series of software compatible machines. OS/360 and the similar third generation operating systems
produced by other companies actually satisfied most of their customers.
New and most important feature was multi-programming.
In one-programming systems the CPU simply sat idle until I/O finished. For commercial
problem with the large data input CPU idle time was about 80 percent of the total time.
The multi-programming operating system keeps several jobs in memory at a time.

Job 3

Job 2

Job 1

Memory layout for a multi-programming system

The operating system picks and begins to execute one of the jobs in memory. Eventually, the job
may have to wait for I/O operation to complete; the operating system simply switches to and
executes another job. When that job needs to wait, the CPU is switched to another job, and so on.
As long as there is some job to execute, the CPU will never be idle. In addition, if several
jobs are ready to run at the same time, the system must choose among them. Making decision is
CPU scheduling, which will be covered later. To keep several programs in memory at the same time
requires some form of memory management.
Multi-programming and time-sharing are the central themes of modern operating system.
The fourth generation (1980-1990 and beyond)
With the development of LSI (Large Scale Integration) circuits, hardware costs have
decreased that made it possible for a single individual to have his own personal computer (PC). The
most powerful personal computers are usually called workstations. Two operating systems have
dominated on the personal computers market: Microsoft MS DOS and UNIX.
But really PCs operating systems were neither multi-user nor multi-tasking. The goals of these
operating systems have changed with time: instead of maximize CPU and peripheral utilization; the
systems were designed for maximizing user convenience and responsiveness.
An interesting development that began during the mid- 1980s is the growth of networks of personal
computers running network operating systems and distributed operating system.
In a network operating system each machine runs its own local operating system, and the users are
aware of the existence of multiple computers, and can log in to remote machines and copy files
from one machine to another. These operating systems are not fundamentally different from
single-processor operating systems. They obviously need a network interface controller and some
low-level software to drive it, and some programs to achieve remote file access.

Evolution of Operating Systems


Ease of Evolution of an Operating System
An operating system will evolve over time for a number of reasons:
Hardware upgrades plus new types of hardware Early versions of UNIX did not employ paging
mechanism because they were run on machines without paging hardware. Most recent versions
have been modified to exploit paging capabilities.
New Services In response to user demand or in response to the needs of system managers, the
operating system will expand to offer new services.
Fixes An operating system may contain some faults which may be detected over time and fixes
will need to be made.

1. Serial Processing
No operating system
Machines run from a console with display lights, toggle switches, input device, and printer.
Programs in machine code were loaded with the input device. If an error halts the program, then
the error indication was given by lights.
Main problems faced by early systems:
Schedule time - Most installations used a sign-up sheet to reserve machine time. On the other
hand, the user might run into problems, or not finish in the allotted time, and be forced to stop
before resolving the problem.
Setup time A single program called a job involves loading the compiler, source program, saving
compiled program, and loading and linking together the object program and common functions.

2. Simple Batch System


Early computers were very expensive and therefore it was important to maximize the machine use.
To improve use, the concept of batch processing was developed. Batch processing scheme use a
piece of software known as the Monitor. With this type of operating system the user submits the
job on cards or tape to a computer operator, who batches the jobs together sequentially and places
the entire batch on an input device for use by the monitor.
The monitor controls the sequence of events. To do this much of the monitor always remains in the
main memory referred as Resident monitor. The rest of the monitor consists of Utilities and
common functions that are loaded as subroutines to the user program at the beginning of any job
that require them. The monitor reads in jobs one at a time from the input device. As it read in, the
current job is placed in the user program area, and control is passed to this job. When the job is
completed, it returns control to the monitor, which immediately reads in the next job.
The monitor handles the scheduling problem - a batch of job is queued up, and jobs are executed as
rapidly as possible, with no intervening idle time. The monitor also handles the setup time by
having the instructions included in primitive form of Job Control Language (JCL), which is a special
type of language used to provide instructions to the monitor regarding what compiler to use and
what data to use.
Hardware Features
Memory protection
While the user program is executing, it must not alter the memory area containing the monitor. If
such an attempt is made, the processor hardware should detect an error and transfer the control to
monitor and job should be aborted by the monitor by displaying an error message.
Timer
A timer is used to prevent a single job from monopolizing the system. A timer is set at the beginning
of the job. If the timer expires, an interrupt occurs and control returns to the monitor.

Privileged instructions
Certain instructions are designated as privileged and can be executed only by the monitor. If the
processor executes such an instruction while executing a user program, an error interrupt occurs.
Interrupts
This facility gives the operating system more flexibility in relinquishing control to and regaining
control from user programs.

3. Multi-programming

Multiprogramming needed for efficiency


Single user cannot keep CPU and I/O devices busy at all times
Multiprogramming organizes jobs (code and data) so CPU always has one to Execute
A subset of total jobs in system is kept in memory
One job selected and run via job scheduling
When it has to wait (for I/O for example), OS switches to another job

Even though the job sequencing is done automatically by simple batch processing system, the
processor is often idle. The problem is that I / O devices are slow compared to the processor. More
than 96% of the time is spend waiting for I / O devices and during this time the processor will be
idle. In order to overcome the inefficiency, the concept of multiprogramming evolved which
requires a large memory space. Suppose if there is a room for one operating system and two user
programs, when one job needs to wait for I / O, the processor can switch to the other job, which
likely is not waiting for I / O. It is known as Multiprogramming

4. Time-Sharing Systems

Timesharing (multitasking) is logical extension in which CPU switches jobs so


frequently that users can interact with each job while it is running, creating
interactive computing
Response time should be < 1 second
Each user has at least one program executing in memory [process
If several jobs ready to run at the same time [ CPU scheduling
If processes dont fit in memory, swapping moves them in and out to run

Multiprogramming systems: several programs use the computer system


Time-sharing systems: several (human) users use the computer system interactively.

Characteristics:

Using multiprogramming to handle multiple interactive jobs


Processors time is shared among multiple users
Multiple users simultaneously access the system through terminals

Batch Multiprogramming

Time Sharing

Principal objective

Maximize processor use

Minimize response time

Source of directives
to operating system

Job control language


commands provided with
the job

Commands entered at
the terminal

Time sharing is multiprogramming. The key differences between time-sharing systems and batch
multiprogramming systems are given in the table above.

Multi-processor systems
Multi-processing is a technique that involves operating several processors in parallel to obtain a
higher calculation power than that obtained using a high-end processor or to increase the
availability of the system (in the event of processor breakdown).
The term SMP (Symmetric Multiprocessing or Symmetric Multiprocessor) refers to an architecture
in which all processors access the same shared memory.

A multiprocessor system must be able to manage memory sharing between several processors but
also to distribute the work load.

Distributed Systems
Distributed system deals with hardware and software systems containing more than one
processing or storage element, concurrent processes, or multiple programs, running under a loosely
or tightly controlled regime. In distributed computing a program is split up into parts that run
simultaneously on multiple computers communicating over a network. Distributed programs often
must deal with heterogeneous environments, network links of varying latencies, and unpredictable
failures in the network or the computers.
The OS can support a distributed file system on a distributed system. There are many
different types of distributed computing systems and many challenges to overcome in successfully
designing one. The main goal of a distributed computing system is to connect users and resources in
a transparent, open, and scalable way. Ideally this arrangement is more fault tolerant and more
powerful than using stand-alone computer systems.
Examples of distributed systems include networked _le systems, many web applications such as
Facebook or twitter, or an instant messaging system. Distributed systems can be built using low
level socket communication or higher level abstractions such as remote procedure calls.

Real time systems


Real time systems, used mainly in industry, are systems designed to operate in a time-constrained
environment. A real time system must also operate reliably according to specific time constraints; in
other words, it must be able to properly process information received at clearly-defined intervals
(regular or otherwise).
Here are some examples of real time operating systems:

OS-9;
RTLinux (RealTime Linux);
QNX;
VxWorks.

More Recent Developments in operating system


Parallel operating systems: - Shared memory, shared clock, Large number of tightly-coupled
processors. Appearance of single operating system
Distributed operating systems: - No shared memory, no shared clock, Small number of looselycoupled processors. Appearance of single operating system is ideal goal, but not realized in practice.
Real-time operating systems: - Meet hard / soft real-time constraints on processing of data.

Unit-1
Components of Operating System
Operating system Components
Even though, not all systems have the same structure many modern operating systems
share the same goal of supporting the following types of system components.
1. Process Management
The operating system manages many kinds of activities ranging from user programs to
system programs like printer spooler, name servers, file server etc. Each of these activities is
encapsulated in a process. A process includes the complete execution context (code, data, PC,
registers, OS resources in use etc.).
It is important to note that a process is not a program. A process is only ONE instant of a
program in execution. There are many processes can be running the same program. The five major
activities of an operating system in regard to process management are

2.

Creation and deletion of user and system processes.

Suspension and resumption of processes.

A mechanism for process synchronization.

A mechanism for process communication.

A mechanism for deadlock handling.

Main-Memory Management

Primary-Memory or Main-Memory is a large array of words or bytes. Each word or byte has
its own address. Main-memory provides storage that can be access directly by the CPU. That is to
say for a program to be executed, it must in the main memory.
The major activities of an operating in regard to memory-management are:

3.

Keep track of which part of memory are currently being used and by whom.

Decide which process are loaded into memory when memory space becomes available.

Allocate and deallocate memory space as needed.

File Management

A file is a collected of related information defined by its creator. Computer can store files on the
disk (secondary storage), which provide long term storage. Some examples of storage media are
magnetic tape, magnetic disk and optical disk. Each of these media has its own properties like
speed, capacity, data transfer rate and access methods.
A file system normally organized into directories to ease their use. These directories may
contain files and other directions.
The five main major activities of an operating system in regard to file management are
1. The creation and deletion of files.
2. The creation and deletion of directions.
3. The support of primitives for manipulating files and directions.
4. The mapping of files onto secondary storage.
5. The back up of files on stable storage media.
4. I/O System Management
I/O subsystem hides the peculiarities of specific hardware devices from the user. Only the
device driver knows the peculiarities of the specific device to whom it is assigned.
5. Secondary-Storage Management
Generally speaking, systems have several levels of storage, including primary storage,
secondary storage and cache storage. Instructions and data must be placed in primary storage or
cache to be referenced by a running program. Because main memory is too small to accommodate
all data and programs, and its data are lost when power is lost, the computer system must provide
secondary storage to back up main memory. Secondary storage consists of tapes, disks, and other
media designed to hold information that will eventually be accessed in primary storage (primary,
secondary, cache) is ordinarily divided into bytes or words consisting of a fixed number of bytes.
Each location in storage has an address; the set of all addresses available to a program is called an
address space.
The three major activities of an operating system in regard to secondary storage management are:
1. Managing the free space available on the secondary-storage device.
2. Allocation of storage space when new files have to be written.
3. Scheduling the requests for memory access.
6.

Networking

A distributed system is a collection of processors that do not share memory, peripheral


devices, or a clock. The processors communicate with one another through communication lines

called network. The communication-network design must consider routing and connection
strategies, and the problems of contention and security.
7.

Protection System

If a computer systems has multiple users and allows the concurrent execution of multiple
processes, then the various processes must be protected from one another's activities. Protection
refers to mechanism for controlling the access of programs, processes, or users to the resources
defined by a computer systems.
8. Command Interpreter System
A command interpreter is an interface of the operating system with the user. The user gives
commands with are executed by operating system (usually by turning them into system calls). The
main function of a command interpreter is to get and execute the next user specified command.
Command-Interpreter is usually not part of the kernel, since multiple command interpreters (shell,
in UNIX terminology) may be support by an operating system, and they do not really need to run in
kernel mode. There are two main advantages to separating the command interpreter from the
kernel.
If we want to change the way the command interpreter looks, i.e., I want to change the
interface of command interpreter, I am able to do that if the command interpreter is separate from
the kernel. I cannot change the code of the kernel so I cannot modify the interface.
If the command interpreter is a part of the kernel it is possible for a malicious process to gain access
to certain part of the kernel that it showed not have to avoid this ugly scenario it is advantageous to
have the command interpreter separate from kernel.

Services of Operating System


(Draw the diagram given in class note book)
Operating-system services provide functions that are helpful to the user.
1. User interface: - Almost all operating systems have a user interface (UI). This interface
can take several forms. One is a command-line interface (CLI), which uses text
commands and a method for entering them (say, a program to allow entering and
editing of commands). Most commonly graphical user interface (GUI) is used.
2. Program execution: - The system must be able to load a program into memory and to
run that program. The program must be able to end its execution, either normally or
abnormally (indicating error).
3. I/O operations:- A running program may require I/O, which may involve a file or an I/O
device. For specific devices, special functions may be desired (such as recording to a CD
or DVD drive or blanking a CRT screen). For efficiency and protection, users usually

cannot control I/O devices directly. Therefore, the operating system must provide a
means to do I/O.
4. File-system manipulation:- The file system is of particular interest. Obviously, programs
need to read and write files and directories. They also need to create and delete them
by name, search for a given file, and list file information. Finally, some programs include
permissions management to allow or deny access to files or directories based on file
ownership.
5. Communications: - There are many circumstances in which one process needs to
exchange information with another process. Such communication may occur between
processes that are executing on the same computer or between processes that are
executing on different computer systems tied together by a computer network.
Communications may be implemented via shared memory or through message passing,
in which packets of information are moved between processes by the operating system.
6. Error detection: - The operating system needs to be constantly aware of possible errors.
Errors may occur in the CPU and memory hardware (such as a memory error or a power
failure), in I/O devices (such as a parity error on tape, a connection failure on a network,
or lack of paper in the printer), and in the user program (such as an arithmetic overflow,
an attempt to access an illegal memory location, or a too-great use of CPU time). For
each type of error, the operating system should take the appropriate action to ensure
correct and consistent computing. Debugging facilities can greatly enhance the user's
and programmer's abilities to use the system efficiently.
7. Resource allocation: - When there are multiple users or multiple jobs running at the
same time, resources must be allocated to each of them. Many different types of
resources are managed by the operating system. Some (such as CPU cycles, main
memory, and file storage) may have special allocation code, whereas others (such as I/O
devices) may have much more general request and release code. For instance, in
determining how best to use the CPU, operating systems have CPU-scheduling routines
that take into account the speed of the CPU, the jobs that must be executed, the
number of registers available, and other factors. There may also be routines to allocate
printers, modems, USB storage drives, and other peripheral devices.
8. Accounting: - We want to keep track of which users use how much and what kinds of
computer resources. This record keeping may be used for accounting (so that users can
be billed) or simply for accumulating usage statistics. Usage statistics may be a valuable
tool for researchers who wish to reconfigure the system to improve computing services.
9. Protection and security: - The owners of information stored in a multiuser or networked
computer system may want to control use of that information. When several separate
processes execute concurrently, it should not be possible for one process to interfere
with the others or with the operating system itself. Protection involves ensuring that all
access to system resources is controlled.

System Call

Process traps to OS Interrupt Handler


Supervisor/ Kernel mode is set
Desired function executed
Returns to application

System calls provide an interface to the services made available by an operating system.

Example to illustrate how system calls are used:


Writing a simple program to read data from one file and copy them to another file. The first input
that the program will need is the names of the two files: the input file and the output file. These
names can be specified in many ways, depending on the operating-system design.
One approach is for the program to ask the user for the names of the two files. In an interactive
system, this approach will require a sequence of system calls, first to write a prompting message on
the screen and then to read from the keyboard the characters that define the two files.
On mouse-based and icon-based systems, a menu of file names is usually displayed in a window.
The user can then use the mouse to select the source name, and a window can be opened for the
destination name to be specified. This sequence requires many I/O system calls.

Types of System Calls


System calls can be grouped roughly into five major categories: process control, file manipulation,
device manipulation, information maintenance, and communications.

Process control
End, abort
Load, execute
Create process, terminate process
Get process attributes, set process
attributes
Wait for time
Wait event, signal event
Allocate, free memory
File management
Create / delete file
Open, close
Read, write, reposition/seek
Get / Set file attributes

Device Management
Request / Release device
Read, write, reposition
Get / Set device attributes
Attach / Detach devices
Information maintenance
Get / Set time or date
Get / Set system data
Get / Set process, file or device attributes
Communications
Create / Delete communication
connection
Send, receive messages
Transfer status information
Attach / Detach remote devices

User mode and kernel mode


Dual-mode operation allows OS to protect itself and other system components
Provides ability to distinguish when system is running user code or kernel code

Some instructions designated as privileged, only executable in kernel mode

System call changes mode to kernel, return from call resets it to user

Unit-1
System programs and Operating System Structure
Structure of operating Systems
As modern operating systems are large and complex careful engineering is required. There are four
different structures that have shown in this document in order to get some idea of the spectrum of
possibilities. These are by no mean s exhaustive, but they give an idea of some designs that have
been tried in practice.

Monolithic Systems:
This approach well known as The Big Mess.
The structure is that there is no structure.
A monolithic kernel is a kernel architecture where the entire kernel is run as a single
privileged entity running on the machine. Functionality in a monolithic kernel can be broken up into
modules; however, the code integration for each module is very tight.
Also, since all the modules run in the same address space, a bug in one module can bring
down the whole system. However, when the implementation is complete and trustworthy, the tight
internal integration of components allows the low-level features of the underlying system to be
effectively utilized, making a good monolithic kernel highly efficient.
In a monolithic kernel, all the systems such as the file system management run in an area
called the kernel mode. The main problem with this organization is maintainability. Examples - Unixlike kernels (Unix, Linux, MS-DOS, Mac OS).

Layered Approach

The operating system is divided into a number of layers (levels), each built on top of lower
layers. The bottom layer (layer 0), is the hardware; the highest (layer N) is the user
interface.
With modularity, layers are selected such that each uses functions (operations) and services
of only lower-level layers
An OS layer is an implementation of an abstract object that is the encapsulation of data and
operations that can manipulate those data. These operations (routines) can be invoked by
higher-level layers. The layer itself can invoke operations on lower-level layers.
Each layer is implemented by using only those operations that are provided lower level
layers.
The major difficulty is appropriate definition of various layers.

General OS Layers

Microkernel System Structure


A microkernel is a minimal computer operating system kernel which, in its purest form, provides
no operating system services at all, only the mechanisms needed to implement such services, such
as low-level address space management, thread management, and inter-process communication
(IPC). The actual operating system services are instead provided by \user-mode" servers. These
include device drivers, protocol stacks, file systems and memory management code. A microkernel
has the benefit of allowing for very customizable and modular operating systems. This greatly
simplifies development of the OS since different modules is completely independent. However, not
many systems use microkernel due to performance concerns caused by the overhead of frequent
context switches between user and kernel mode.

A microkernel is a minimal computer operating system kernel which, in its purest form,
provides no operating-system services at all.
Assigns only a few essential functions to the kernel

Address spaces
Interprocess communication (IPC)
Basic scheduling

The microkernel is the only part of the system executing in a kernel mode. The actual
operating-system services are provided by "user-mode" servers.

*****(Please Refer Class Note Book for university Question Paper)*****

Unit-1
Process Management
In this chapter we will study
How a process comes into existence?
How processes are managed?
A process in execution needs resources like processing resource, memory and IO resources.
Current machines allow several processes to share resources. In reality, one processor is shared
amongst many processes. A system like a file server even support processes from multiple users.
And yet the owner of every process gets an illusion that the server (read processor) is available to
their process without any interruption. This requires clever management and allocation of the
processor as a resource. We shall study the basic processor sharing mechanism amongst processes.
We introduce basic CPU-scheduling concepts and present several CPU-scheduling algorithms. We
also consider the problem of selecting an algorithm for a particular system.

WHAT IS A PROCESS?
A process is a program in execution.

To understand the importance of this definition, lets imagine that we have written a
program called my_prog.c in C. On execution, this program may read in some data and output some
data. Note that when a program is written and a file is prepared, it is still a script. It has no dynamics
of its own i.e., it cannot cause any input processing or output to happen. Once we compile, and still
later when we run this program, the intended operations take place. In other words, a program is a
text script with no dynamic behavior. When a program is in execution, the script is acted upon. It
can result in engaging a processor for some processing and it can also engage in I/O operations. It is
for this reason a process is differentiated from program.
>>Define what a process is and in which way a process differs from a program. <<
A process is an executable entity its a program in execution. When we compile a C
language program we get an a. out file which is an executable file. When we seek to run this file we
see the program in execution.
Processor Utilization:
Processor Utilization: A processor is a central and a key element of a computer system. This
is so because all information processing gets done in a processor. So a computer's throughput
depends upon the extent of utilization of its processor. The greater the utilization of the processor,
larger is the amount of information processed.
In a uni-programming system (figure a) we have one program engaging the processor. In
such a situation the processor is idling for very long periods of time. This is so because IO and
communication to devices (including memory) takes so much longer.

In figure above we see that during intervals when prog_A is not engaging the processor we
can utilize the processor to run another ready to run program. The processor now processes two
programs without significantly sacrificing the time required to process prog_A. Note that we may
have a small overhead in switching the context of use of a processor. However, multiprogramming
results in improving the utilization of computer's resources. In this example, with multiple programs
residing in the memory, we enhance the memory utilization also!!.
Response Time:
In time sharing system, The response time of a system is defined as the time interval which
spans the time from the last character input to the first character of output.
It is important that when we design a time sharing system we keep the response time at
some acceptable level. Otherwise the advantage of giving access to, and sharing, the resource
would be lost. A system which we use to access book information in a library is a time-shared
system. Clearly, the response time should be such that it should be acceptable, say a few seconds. A
library system is also an online system .In online system; devices (which can include
instrumentation in a plant) are continuously monitored (observed) by the computer system. If in an
online system the response time is also within some acceptable limits then we say it is a real-time
system.

Process Control Block


Each process is represented in the operating
system by a process control block
(PCB)also called a task control block.
A PCB is shown in Figure. It contains many
pieces of information associated with a specific
process, including these:

1. Process state. The state may be new, ready, running, waiting, halted, and so on.
2. Program counter the counter indicates the address of the next instruction to be executed for
this process.

3. CPU registers. The registers vary in number and type, depending on the computer architecture.
They include accumulators, index registers, stack pointers, and general-purpose registers, plus
any condition-code information. Along with the program counter, this state information must be
saved when an interrupt occurs, to allow the process to be continued correctly afterward.
4. CPU-scheduling information. This information includes a process priority, pointers to scheduling
queues, and any other scheduling parameters.
5. Memory-management information. This information may include such information as the value
of the base and limit registers, the page tables, or the segment tables, depending on the
memory system used by the operating system.
6. Accounting information. This information includes the amount of CPU and real time used, time
limits, account members, job or process numbers, and so on.

7. I/O status information. This information includes the list of I/O devices allocated to the process,
a list of open files, and so on.

Context Switching
A context switch (also sometimes referred to as a process switch or a task switch) is the
switching of the CPU from one process or thread to another.
What Happens When Context Is Switched?
We will continue to assume that we have a uni-processor multi-programming
environment. We have earlier seen that only ready-to-run, main memory resident processes
can be scheduled for execution. For instance, each process in execution uses the program
counter, registers and other resources within the CPU. So, whenever a process is switched, the
OS moves out, and brings in, considerable amount of context switching information.
We have process P1 currently executing (note that the program counter is pointing in
executable code area of P1). Let us now switch the context to the process P2.
The following must happen:
o All the current context information about process P1 must be updated (save) in
its own context area.
o All context information about process P2 must be downloaded (restore) in its
own context area.

The program counter should have an address value to an instruction of process


P2 and process P2 must be now marked as running".
Information stored during context switching is:
1. Program counter
2. Registers (like stack, index etc.) currently in use

3. Changed state (changed from Running to ready-to-run)


4. The base and limit register values
5. IO status (files opened; IO blocked or completed etc.)
6. Accounting
7. Scheduling information
8. Any other relevant information.
***Diagram for context switching is in class note book.***

Process State
The operating systems principal responsibility is in controlling the execution of
processes. This includes determining the interleaving pattern for execution and allocation of
resources to processes. One part of designing an OS is to describe the behavior that we would
like each process to exhibit. The simplest model is based on the fact that a process is either
being executed by a processor or it is not.
Two State Model
Thus, a process may be considered to be in one of two states,
>>RUNNING
>>NOT RUNNING
When the operating system creates a new process, that process is initially labeled as
NOT RUNNING, and is placed into a queue in the system in the NOT RUNNING state. The
process (or some portion of it) then exists in main memory, and it waits in the queue for an
opportunity to be executed. After some period of time, the currently RUNNING process will be
interrupted, and moved from the RUNNING state to the NOT RUNNING state, making the
processor available for a different process. The dispatch portion of the OS will then select, from
the queue of NOT RUNNING processes, one of the waiting processes to transfer to the
processor. The chosen process is then relabeled from a NOT RUNNING state to a RUNNING
state, and its execution is either begun if it is a new process, or is resumed if it is a process
which was interrupted at an earlier time.

A Queuing Model:

A Queuing Model: Data structures play an important role in management of processes.


In general an OS may use more than one data structure in the management of processes. It may
maintain a queue for all ready to run processes. It may maintain separate queues for blocked
processes. It may even have a separate queue for each of the likely events (including
completion of IO). This formulation shown in the figure below is a very flexible model useful in
modeling computer system operations. This type of model helps in the study and analysis of
chosen OS policies.

Drawback of two state model:Although the two-state process management model is a perfectly valid design for an
operating system, the absence of a BLOCKED state means that the processor lies idle when the
active process changes from CPU cycles to I/O cycles. This design does not make efficient use of
the processor.
Five State Model

Five State Model


The state of a process is defined in part by the current activity of that process. Each process
may be in one of the following states:
New: The process is being created.

Running: Instructions are being executed.


Blocked: The process is waiting for some event to occur (such as an I/O completion or
reception of a signal).
Ready: The process is waiting to be assigned to a processor.
Terminated/Exit: The process has finished execution.
List down the possible transition
1.
2.
3.
4.
5.
6.
7.

Null New
New Ready
Ready Running
Running Ready
Running Blocked
Blocked Ready
Ready Exit, Blocked Exit.

Explain each transition for 10 marks.


Queuing Model for Five State Model

Five state model with two suspend state:

Five State Model with Two Suspend State.

Transitions are as follows:

BLOCKED BLOCKED SUSPEND


If a process in the RUNNING state requires more memory, then at least
one BLOCKED process can be swapped out of memory onto disk. The transition
can also be made for the BLOCKED process if there are READY processes
available, and the OS determines that the READY process that it would like to
dispatch requires more main memory to maintain adequate performance.

BLOCKED SUSPEND READY SUSPEND


A process in the BLOCKED SUSPEND state is moved to the READY
SUSPEND state when the event for which it has been waiting occurs. Note that
this requires that the state information concerning suspended processes be
accessible to the OS.

READY SUSPEND READY


When there are no READY processes in main memory, the OS will need to
bring one in to continue execution. In addition, it might be the case that a
process in the READY SUSPEND state has higher priority than any of the
processes in the READY state. In that case, the OS designer may dictate that it is
more important to get at the higher priority process than to minimize swapping.

READY READY SUSPEND


Normally, the OS would be designed so that the preference would be to
suspend a BLOCKED process rather than a READY one. This is because the READY
process can be executed as soon as the CPU becomes available for it, whereas
the BLOCKED process is taking up main memory space and cannot be executed
since it is waiting on some other event to occur. However, it may be necessary to
suspend a READY process if that is the only way to free a sufficiently large block
of main memory. Finally, the OS may choose to suspend a lower-priority READY
process rather than a higher-priority BLOCKED process if it believes that the
BLOCKED process will be ready soon.

Operation on Thread
The processes in most systems can execute concurrently, and they may be created and deleted
dynamically. Thus, these systems must provide a mechanism for process creation and termination.

Process Creation
In general-purpose systems, some way is needed to create processes as needed during operation.
There are four principal events led to processes creation.

System initialization.
Execution of a process Creation System calls by a running process.
A user request to create a new process.
Initialization of a batch job.

Foreground processes interact with users. Background processes that stay in background sleeping
but suddenly springing to life to handle activity such as email, webpage, printing, and so on.
Background processes are called daemons. This call creates an exact clone of the calling process.
A process may create a new process by some create process such as 'fork'.
The creating process is called a parent process.
The new processes are called the children of that process.
Following are some reasons for creation of a process

User logs on.


User starts a program.
Operating systems creates process to provide service, e.g., to manage printer.
Some program starts another process, e.g., Netscape calls xv to display a picture.

Process Termination
A process terminates when it finishes executing its last statement. Its resources are returned to the
system, it is purged from any system lists or tables, and its process control block (PCB) is erased i.e.,
the PCB's memory space is returned to a free memory pool. The new process terminates the
existing process, usually due to following reasons:

Normal Exist Most processes terminates because they have done their job. This call is
exist in UNIX.
Error Exist When process discovers a fatal error. For example, a user tries to compile a
program that does not exist.
Fatal Error An error caused by process due to a bug in program for example, executing an
illegal instruction, referring non-existing memory or dividing by zero.
Killed by another Process A process executes a system call telling the Operating Systems
to terminate some other process. In UNIX, this call is kill. In some systems when a process
kills all processes it created are killed as well (UNIX does not work this way).

Unit-1
Interprocess Communication and light weight process
Threads
A thread is a single sequence stream within in a process.
Because threads have some of the properties of processes, they are sometimes called
lightweight processes. In a process, threads allow multiple executions of streams. In many
respect, threads are popular way to improve application through parallelism.
The CPU switches rapidly back and forth among the threads giving illusion that the threads are
running in parallel.
Like a traditional process i.e., process with one thread, a thread can be in any of several states

Running
Blocked
Ready
Terminated

Each thread has its own stack. Since thread will generally call different procedures and thus a
different execution history. This is why thread needs its own stack.
An operating system that has thread facility, the basic unit of CPU utilization is a thread. A
thread has or consists of a program counter (PC), a register set, and a stack space.
Threads are not independent of one other like processes as a result threads shares with other
threads their code section, data section, OS resources also known as task, such as open files
and signals.
Processes Vs Threads
As we mentioned earlier that in many respect threads operate in the same way as that of
processes. Some of the similarities and differences are:
Similarities

Like processes threads share CPU and only one thread active (running) at a time.
Like processes, threads within processes, threads within a process execute sequentially.
Like processes, thread can create children.
And like process, if one thread is blocked, another thread can run.

Differences

Unlike processes, threads are not independent of one another.


Unlike processes, all threads can access every address in the task.
Unlike processes, threads are design to assist one other. Note that processes might or
might not assist one another because processes may originate from different users.

Why Threads?
Following are some reasons why we use threads in designing operating systems.
1. A process with multiple threads makes a great server for example printer server.
2. Because threads can share common data, they do not need to use interprocess
communication.
3. Because of the very nature, threads can take advantage of multiprocessors.
Threads are cheap in the sense that
1. They only need a stack and storage for registers therefore, threads are cheap to create.
2. Threads use very little resources of an operating system in which they are working. That
is, threads do not need new address space, global data, program code or operating
system resources.
3. Context switching is fast when working with threads. The reason is that we only have to
save and/or restore PC, SP and registers.
But this cheapness does not come free - the biggest drawback is that there is no protection
between threads.
User-Level Threads
User-level threads implement in user-level libraries, rather than via systems calls, so thread
switching does not need to call operating system and to cause interrupt to the kernel. In fact,
the kernel knows nothing about user-level threads and manages them as if they were singlethreaded processes.
Advantages:
The most obvious advantage of this technique is that a user-level threads package can be
implemented on an Operating System that does not support threads. Some other advantages
are

A user-level thread does not require modification to operating systems.


Simple Representation:
Each thread is represented simply by a PC, registers, stack and a small control block, all
stored in the user process address space.

Simple Management:
This simply means that creating a thread, switching between threads and
synchronization between threads can all be done without intervention of the kernel.

Fast and Efficient:


Thread switching is not much more expensive than a procedure call.

Disadvantages:

There is a lack of coordination between threads and operating system kernel. Therefore,
process as whole gets one time slice irrespective of whether process has one thread or 1000
threads within. It is up to each thread to relinquish control to other threads.
User-level threads require non-blocking systems call i.e., a multithreaded kernel. Otherwise,
entire process will blocked in the kernel, even if there are runnable threads left in the
processes. For example, if one thread causes a page fault, the process blocks.

Kernel-Level Threads
In this method, the kernel knows about and manages the threads. No runtime system is needed in
this case. Instead of thread table in each process, the kernel has a thread table that keeps track of
all threads in the system. In addition, the kernel also maintains the traditional process table to keep
track of processes. Operating Systems kernel provides system call to create and manage threads.
The implementation of general structure of kernel-level thread is
Advantages:

Because kernel has full knowledge of all threads, Scheduler may decide to give more time to
a process having large number of threads than process having small number of threads.
Kernel-level threads are especially good for applications that frequently block.

Disadvantages:

The kernel-level threads are slow and inefficient. For instance, threads operations are
hundreds of times slower than that of user-level threads.
Since kernel must manage and schedule threads as well as processes. It require a full thread
control block (TCB) for each thread to maintain information about threads. As a result there
is significant overhead and increased in kernel complexity.

Advantages of Threads over Multiple Processes

Context Switching Threads are very inexpensive to create and destroy, and they are
inexpensive to represent. For example, they require space to store, the PC, the SP, and the
general-purpose registers, but they do not require space to share memory information,
Information about open files of I/O devices in use, etc. With so little context, it is much

faster to switch between threads. In other words, it is relatively easier for a context switch
using threads.
Sharing Treads allow the sharing of a lot resources that cannot be shared in process, for
example, sharing code section, data section, Operating System resources like open file etc.

Disadvantages of Threads over Multiprocesses

Blocking The major disadvantage if that if the kernel is single threaded, a system call of
one thread will block the whole process and CPU may be idle during the blocking period.
Security Since there is, an extensive sharing among threads there is a potential problem of
security. It is quite possible that one thread over writes the stack of another thread (or
damaged shared data) although it is very unlikely since threads are meant to cooperate on a
single task.

Application that Benefits from Threads


A proxy server satisfying the requests for a number of computers on a LAN would be benefited by a
multi-threaded process. In general, any program that has to do more than one task at a time could
benefit from multitasking.
For example, a program that reads input, process it, and outputs could have three threads, one for
each task.
Application that cannot Benefit from Threads
Any sequential process that cannot be divided into parallel task will not benefit from thread, as they
would block until the previous one completes.
For example, a program that displays the time of the day would not benefit from multiple.

Benefits of Multithreading
The benefits of multithreaded programming can be broken down into fourmajor categories:
1. Responsiveness. Multithreading an interactive application may allow a program to continue
running even if part of it is blocked or is performing a lengthy operation, thereby increasing
responsiveness to the user.
2.

Resource sharing. By default, threads share the memory and the resources of the process
to which they belong. The benefit of sharing code and data is that it allows an application to
have several different threads of activity within the same address space.

3. Economy. Allocating memory and resources for process creation is costly. Because threads
share resources of the process to which they belong, it is more economical to create and
context-switch threads.
4. Utilization of multiprocessor architectures. The benefits of multithreading can be greatly
increased in a multiprocessor architecture, where threads may be running in parallel on
different processors. A single threaded process can only run on one CPU, no matter how
many are available. Multithreading on a multi-CPU machine increases concurrency.
Multithreading Models
There must exist a relationship between user threads and kernel threads. We look at three common
ways of establishing this relationship.

Many-to-One Model
The many-to-one model maps many user-level threads to one kernel thread. Thread management is
done by the thread library in user space, so it is efficient; but the entire process will block if a thread
makes a blocking system call. Also, because only one thread can access the kernel at a time,
multiple threads are unable to run in parallel on multiprocessors.

Many-to-One Model
One-to-One Model
The one-to-one model maps each user thread to a kernel thread. It provides more concurrency than
the many-to-one model by allowing another thread to run when a thread makes a blocking system
call; it also allows multiple threads to run in parallel on multiprocessors.
The only drawback to this model is that creating a user thread requires creating the corresponding
kernel thread. Because the overhead of creating kernel threads can burden the performance of an
application, most implementations of this model restrict the number of threads supported by the
system.

One-to-One Model
Many-to-Many Model
The many-to-many model multiplexes many user-level threads to a smaller or equal number of
kernel threads. Whereas the many-to-one model allows the developer to create as many user

threads as she wishes, true concurrency is not gained because the kernel can schedule only one
thread at a time.
The one-to-one model allows for greater concurrency, but the developer has to be careful not to
create too many threads within an application (and in some instances may be limited in the number
of threads she can create).

Many-to-Many Model
The many-to-many model suffers from neither of these shortcomings:
Developers can create as many user threads as necessary, and the corresponding kernel threads
can run in parallel on a multiprocessor.
When a thread performs a blocking system call, the kernel can schedule another thread for
execution.

Threading Issues
Thread Creation
The fork() and exec() System Calls
If one thread in a program calls f ork ( ), does the new process duplicate all threads, or is the
new process single-threaded? Some UNIX systems have chosen to have two versions of fork ( )
one that duplicates all threads and another that duplicates only the thread that invoked the
fork ( ) system call.
If a thread invokes the exec ( ) system call, the program specified in the parameter to exec ( )
will replace the entire processincluding all threads.

Thread cancellation
Thread cancellation is the task of terminating a thread before it has completed. For example, if
multiple threads are concurrently searching through a database 1 and one thread returns the
result, the remaining threads might be canceled.
A thread that is to be canceled is often referred to as the target thread. Cancellation of a target
thread may occur in two different scenarios:
1. Asynchronous cancellation. One thread immediately terminates the target thread.
2. Deferred cancellation. The target thread periodically checks whether it should terminate,
allowing it an opportunity to terminate itself in an orderly fashion.
Signal Handling
A signal is used in UNIX systems to notify a process that a particular event has occurred. A
signal may be received either synchronously or asynchronously, depending on the source of
and the reason for the event being signaled. All signals, whether synchronous or asynchronous,
follow the same pattern:
1. A signal is generated by the occurrence of a particular event.
2. A generated signal is delivered to a process.
3. Once delivered, the signal must be handled.
Thread Pools
Unlimited threads could exhaust system resources, such as CPU time or memory. One solution
to this issue is to use a thread pool.
The general idea behind a thread pool is to create a number of threads at process startup and
place them into a pool, where they sit and wait for work. When a server receives a request, it
awakens a thread from this poolif one is availableand passes it the request to service. Once
the thread completes its service, it returns to the pool and awaits more work. If the pool
contains no available thread, the server waits until one becomes free.
Thread pools offer these benefits:
1. Servicing a request with an existing thread is usually faster than waiting to create
a thread.
2. A thread pool limits the number of threads that exist at any one point.
This is particularly important on systems that cannot support a large number of concurrent
threads.

Unit 1
CPU Scheduling and Process synchronization
CPU and I/O Burst Cycle

The execution of a process consists of a cycle of CPU execution and I/O wait.
A process begins with a CPU burst, followed by an I/O burst, followed by another
CPU burst and so on. The last CPU burst will end will a system request to
terminate the execution.
The CPU burst durations vary from process to process and computer to computer.
An I/O bound program has many very short CPU bursts.
A CPU bound program might have a few very long CPU bursts.

Histogram of CPU-burst Times


Types of Scheduling

The key to the multiprogramming is scheduling. There are four types of scheduling that
an OS has to perform. These are:
o Long Term scheduling
The long term scheduling determines which programs are admitted to
the system for processing. Thus, it controls the level of
multiprogramming.
Once admitted, a job or a user program becomes a process and is added
to the queue for the short term scheduling (in some cases added to a
queue for medium term scheduling).
Long term scheduling is performed when a new process is created.
The criteria used for long-term scheduling may include first-come-first
serve, priority, expected execution time, and I/O requirements.
o Medium-Term Scheduling
The medium-term scheduling is a part of swapping function. This is a
decision to add a process to those that are at least partially in main
memory and therefore available for execution.
The swapping-in decision is made on the need to manage the degree of
multiprogramming and the memory requirements of the swapped-out
process.
o Short-Term Scheduling
A decision of which ready process to execute next is made in short-term
scheduling.
o I/O Scheduling

The decision as to which processs pending I/O requests shall be handled


by the available I/O device is made in I/O scheduling.

CPU Scheduler

Whenever, the CPU becomes idle, the OS must select one of the processes in the readyqueue to be executed.
The selection process is carried out the short-term scheduler or CPU scheduler. The CPU
scheduler selects a process from the ready queue and allocates the CPU to that process.

CPU scheduling decisions may take place when a process:


1. The running process changes from running to waiting state (current CPU burst of
that process is over).
2. The running process terminates
3. A waiting process becomes ready (new CPU burst of that process begins)
4. The current process switches from running to ready stat (e.g. because of timer
interrupt).

Scheduling under 1 and 2 is nonpreemptive.


Once a process is in the running state, it will continue until it terminates or
blocks itself.

Scheduling under 1 and 2 is preemptive.

Currently running process may be interrupted and moved to the Ready state by
OS.
Allows for better service since any one process cannot monopolize the processor
for very long

Dispatcher

Dispatcher module gives control of the CPU to the process selected by the short-term
scheduler; this involves:
Switching context
Switching to user mode
Jumping to the proper location in the user program to restart that program
Dispatch latency time it takes for the dispatcher to stop one process and start another
running.

What is a Good Scheduler? Criteria

User oriented
Turnaround time: time interval from submission of job until its completion. It
includes actual processing time plus time spent waiting for resources, including
the processor. OR the amount of time between moment a process
first enters Ready State and the moment the process exits Running
State for the last time (Completed).
Waiting time: sum of periods spent waiting in ready queue.

Response time: time interval from submission of job to first response. Often a
process can begin producing some output to the user while continuing to
process the request. Thus this is a better measure than turnaround time from
the users point of view.
Normalized turnaround time: ratio of turnaround time to service time.
Service Time: The amount of time process needs to be in running state (Acquired
CPU) before it is completed.
System oriented
CPU utilization: percentage of time CPU is busy. CPU utilization may range from
0 to 100%. In a real system, it should range from 40% to 90%.
Throughput: number of jobs completed per time unit. This depends on the
average length of the processes.
Service time, wait time, turn around time, and throughput are some of the metrics used
to compare scheduling algorithms.
Any good scheduler should:
Maximize CPU utilization and throughput
Minimize turnaround time, waiting time, response time

Goals of Scheduling Algorithm for Different Systems

All systems
Fairness Giving each process a fair share of the CPU
Policy enforcement seeing that stated policy is carried out.
Balance Keeping all parts of the system busy
Batch systems
Maximize throughput (job/hour)
Minimize turnaround time
Maximize CPU utilization
Interactive systems
Minimize response time (respond to request quickly)
Proportionality Meet users expectations
Real-time systems
Meeting deadlines
Predictability avoid quality degradation in multimedia systems.

Scheduling Algorithms

CPU scheduling deals with the problem of deciding which of the processes in the ready
queue is to be allocated the CPU. There are many different CPU scheduling algorithms,
which we will discuss now.

First-Come First-Served (FCFS) Scheduling

The process that requests the CPU first is allocated the CPU first. It is nonpreemptive
algorithm.
Can easily be implemented with a FIFO queue.
When a process enters the ready queue, its PCB is linked onto the tail of the
queue.
When CPU is free, it is allocated to the process at the head of the queue.

Advantages
Very simple
Disadvantages
Long average and worst-case waiting times
Poor dynamic behavior (convoy effect - short process behind long process)

Example 1:

Process
P1
P2
P3

Burst Time
24
3
3

Suppose that the processes arrive in the order: P1, P2,P3.The Gantt Chart for the
schedule is:

P
0

Waiting time for P1 = 0; P2 = 24; P3 = 27


Average waiting time: (0 + 24 + 27)/3 = 17

P
24

P
27

30

Suppose that the processes arrive in the order P2, P3, P1. The Gantt chart for the
schedule is:

P2
0

P3
3

P1
6

30

Waiting time for P1 = 6; P2 = 0; P3 = 3


Average waiting time: (6 + 0 + 3)/3 = 3

Example 2:

Consider the following set of processes:

Process

Arrival Time

Service Time

P1
P2
P3
P4
P5

Calculate waiting time, average waiting time, and turnaround time.


(Solve as a practice example)

Shortest-Job First Scheduling (SJF)

This algorithm associates with each process the length of its next CPU burst.
When the CPU is available, it is assigned the process that has the smallest next CPU
burst.
It is a non-preemptive policy.

Preemptive SJF Shortest Remaining Time First

Preemptive version of SJF.


If a new process arrives with CPU burst length less than remaining time of current
executing process, preempt the currently executing process and allocate the CPU to the
new process.
Advantages:
Minimizes average waiting times.
Problems:
How to determine length of next CPU burst?
Problem: starvation of jobs with long CPU bursts.

Example:

Process

Arrival Time

Burst Time

P1

0.0

P2

2.0

P3

4.0

P4

5.0

SJF (non-preemptive)

P1
0

P3
7

P2
8

Average waiting time = (0 + 6 + 3 + 7)/4 = 4

P4
12

16

SRT (preemptive SJB)

P1
0

P2
2

P3
4

P2
5

P4
7

P1
11

Average waiting time = (9 + 1 + 0 +2)/4 = 3

Determining Length of the Next CPU burst in SJF

Can only estimate the length.


Can be done by using the length of previous CPU bursts, using exponential averaging.

1. tn actual lenght of nth CPU burst


2. n1 predicted value for the next CPU burst
3. , 0 1
4. Define :

n1 tn 1 n .

16

Priority Scheduling

In priority scheduling, a priority (an integer) is associated with each process.


Priorities can be assigned either externally or internally.
The CPU is allocated to the process with the highest priority (smallest integer means
highest priority).
Priority scheduling can be:
Preemptive
Nonpreemptive
Problem
Starvation or indefinite blocking low priority processes may never execute.
Solution
Aging as time progresses increase the priority of the process.

Conceptual and Implementation view of Priority Scheduling

Example:
Process
P1

CPU Burst

Priority

10

P2
P3
P4
P5

(Solve as a example)
Round-Robin Scheduling

Each process gets a small unit of CPU time (called time quantum), usually 10-100
milliseconds.
After this time has elapsed, the process is preempted and added to the end of the ready
queue.
Ready queue is treated as a circular queue.
CPU scheduler goes around the ready queue, allocating the CPU to each process for a
time interval of 1 time quantum.
Ready queue is a FIFO queue of processes.
New processes are added to the tail of the ready queue and the CPU scheduler
picks the first process from the ready queue, sets a timer to interrupt after 1
time quantum and dispatches the process.
If the process has a CPU burst of less than 1 time quantum, it releases the CPU
voluntarily. Otherwise, the timer will go off and will cause an interrupt to the OS. A
context switch will be executed and the process will be put at the tail of the ready
queue. The CPU scheduler then picks up the next process in the ready queue.
If there are n processes in the ready queue and the time quantum is q,
Each process gets 1/n of the CPU time in chunks of at most q time units at once.
No process waits more than (n-1)q time units until its next time quantum.
Example: If there are 5 processes, with a time quantum of 20 ms, then
each process will get up to 20 ms every 100 ms.
Typically, RR has higher average turnaround than SJF, but better response.

Example:
Process

Burst Time

P1

53

P2

17

P3

68

P4

24
Time Quantum: 20

The Gantt chart is:

P1
0

P2
20

37

P3

P4
57

P1
77

P3
97 117

P4

P3

P3

121 134 154 162

RR With Context Switching Overhead

P1

In RR, context switching overhead should be considered.

RR and Time Quantum

The performance of the RR depends heavily on the size of the time quantum.
If the time quantum is very large, then RR policy is the same as FCFS policy.
If the time quantum is very small then most of the CPU time will be spent on context
switching.
Turnaround time also depends on the time quantum
A rule of thumb is that 80% of the CPU bursts should be shorter than the time quantum.

Turnaround time varies with the time quantum

Multilevel Queue Scheduling

Ready queue is partitioned into separate queues.


Each queue may have its own scheduling algorithm.

A multilevel queue scheduling with absolute priory

Scheduling must be done between the queues.


Fixed priority scheduling; (i.e., serve all from foreground then from background).
Possibility of starvation.
Time slice each queue gets a certain amount of CPU time which it can schedule
amongst its processes; i.e., 80% to foreground in RR and 20% to background in
FCFS.

Multilevel Feedback Queue Scheduling

A process can move between the various queues; aging can be implemented this way.

Multilevel-feedback-queue scheduler defined by the following parameters:


Number of queues
Scheduling algorithms for each queue
Method used to determine when to upgrade a process
Method used to determine when to demote a process
Method used to determine which queue a process will enter when that process
needs service
Example

Three queues:
Q0 time quantum 8 milliseconds
Q1 time quantum 16 milliseconds
Q2 FCFS
Scheduling
A new job enters queue Q0 which is served FCFS. When it gains CPU, job receives 8
milliseconds. If it does not finish in 8 milliseconds, job is moved to queue Q1.
At Q1 job is again served FCFS and receives 16 additional milliseconds. If it still does
not complete, it is preempted and moved to queue Q2.

Algorithm Evaluation

Deterministic modeling takes a particular predetermined workload and defines the


performance of each algorithm for that workload.
Queueing models
Implementation

Process Synchronization

Currency arises in three different contexts:


Multiple applications Multiple programs are allowed to dynamically share
processing time.
Structured applications Some applications can be effectively programmed as a
set of concurrent processes.
Operating system structure The OS themselves are implemented as set of
processes.

Concurrent processes (or threads) often need access to shared data and shared
resources.
Processes use and update shared data such as shared variables, files, and data
bases.

Writing must be mutually exclusive to prevent a condition leading to inconsistent data


views.

Maintaining data consistency requires mechanisms to ensure the orderly execution of


cooperating processes.

Example 1

Shared-memory solution to bounded-butter problem allows at most n 1 items in


buffer at the same time. A solution, where all N buffers are used is not simple.

Suppose that we modify the producer-consumer code by dding a variable counter,


initialized to 0 and incremented each time a new item is added to the buffer.

Shared data
#define BUFFER_SIZE 10
typedef struct {
...
} item;
item buffer[BUFFER_SIZE];
int in = 0;
int out = 0;
int counter = 0;

Producer process
Consumer process
item nextProduced; while (1)
{
while (counter == BUFFER_SIZE)
; /* do nothing */
buffer[in] = nextProduced;
in = (in + 1) % BUFFER_SIZE;
counter++;
}

item nextConsumed; while (1)


{
while (counter == 0)
; /* do nothing */
nextConsumed = buffer[out];
out = (out + 1) % BUFFER_SIZE;
counter--;
}

The statements
counter++;
counter--;
must be performed atomically.

Atomic operation means an operation that completes in its entirety without


interruption.

The statement count++ may be implemented in machine language as:


register1 = counter

register1 = register1 + 1
counter = register1

The statement count may be implemented as:


register2 = counter
register2 = register2 1
counter = register2

If both the producer and consumer attempt to update the buffer concurrently, the
assembly language statements may get interleaved.

Interleaving depends upon how the producer and consumer processes are scheduled.

Assume counter is initially 5. One interleaving of statements is:


producer: register1=counter (register1=5)
producer: register1=register1+1 (register1=6)
consumer: register2=counter (register2 = 5)
consumer: register2=register21 (register2 = 4)
producer: counter=register1 (counter = 6)
consumer: counter=register2 (counter = 4)

The value of count may be either 4 or 6, where the correct result should be 5.

Example 2: Spooler Directory

Race Condition

The race condition is a situation where several processes access (read/write) shared
data concurrently and the final value of the shared data depends upon which process
finishes last
The actions performed by concurrent processes
will then depend on the order in which their
execution is interleaved.
To prevent race conditions, concurrent processes must
be coordinated or synchronized.
It means that neither process will proceed
beyond a certain point in the computation until
both have reached their respective synchronization point.

Critical Section/Region
1. Consider a system consisting of n processes all competing to use some shared data.
2. Each process has a code segment, called critical section, in which the shared data is
accessed.
Example: Race condition updating a variable

Critical Section and Race Condition

Multiprogramming allows logical parallelism, uses devices efficiently - but we lose


correctness when there is a race condition.
So we forbid logical parallelism inside critical section
We lose some parallelism but we regain correctness.

Wherein Lies the Problem

The problem stems from interruption of software-based process while executing critical
section (low-level).

The Critical-Section Problem


1. The critical-section problem is to design a protocol that the processes can cooperate.
The protocol must ensure that when one process is executing in its critical section, no
other process is allowed to execute in its critical section.
2. The critical section problem is to design a protocol that the processes can use so that
their action will not depend on the order in which their execution is interleaved
(possibly on many processors).

Solution to Critical Section Problem Requirements (

A solution to the critical-section problem must satisfy the following three requirements:
1. Mutual Exclusion. If process Pi is executing in its critical section, then no other
processes can be executing in their critical sections.
Implications:
Critical sections better be focused and short.
Better not get into an infinite loop in there.
If a process somehow halts/waits in its critical section, it must not
interfere with other processes.
2. Progress. If no process is executing in its critical section and there exist some
processes that wish to enter their critical section, then the selection of the
processes that will enter the critical section next cannot be postponed
indefinitely.
If only one process wants to enter, it should be able to.
If two or more want to enter, one of them should succeed.
3. Bounded Waiting. A bound must exist on the number of times that other
processes are allowed to enter their critical sections after a process has made a
request to enter its critical section and before that request is granted.
Assume that each process executes at a nonzero speed
No assumption concerning relative speed of the n processes.

Types of Solutions

Software solutions
Algorithms whose correctness does not rely on any other assumptions.
Hardware solutions
Rely on some special machine instructions.
Operating System solutions
Provide some functions and data structures to the programmer through
system/library calls.
Programming Language solutions
Linguistic constructs provided as part of a language.

Mutual Exclusion
Four Requirements of Mutual Exclusion

Solution for Mutual Exclusion


Software approaches
Whether in a uniprocessor system or a multiprocessor one with shared main memory, it usually
assumed that only one access to a memory location can be made at a time, so that the simplest
operation, an assignment to a location, is always performed without interference.

Dekker Algorithm

Peterson Algorithm

Unit 1
Tools for process synchronization
Semaphores

Synchronization tool (provided by the OS) that does not require busy waiting.

Logically, a semaphore S is an integer variable that, apart from initialization, can only be
accessed through 2 atomic and mutually exclusive operations:

wait(S)
signal(S)

Wait and Signal Operations

wait(S)

signal(S)

{
while (S 0 do) ;
S--;

S++;
}

Modification to the value of semaphore (S) in the wait and signal is executed
individually.

In wait, the testing and possible modification of S must also be executed without
interruption.

Usage of Semaphore

The fundamental principle of semaphore is this: Two or more processes can cooperate
by means of simple signals such that a process can be forced to stop at a specified place
until it has received a specified signal.

Usage 1: Critical Section of n Processes Problem


Shared data:
semaphore mutex; //initially mutex=1
Process Pi:
do {
wait(mutex);

/*critical section*/
signal(mutex);
remainder section
} while (1);

Usage 2: synchronization of 2 processes

Suppose that we have two processes, P1 with statement s1 and P2 with statement s2. We
require that s2 be executed only after s1 has completed. This can be done with a shared
semaphore variable, mutex, initialized to 0, and by inserting the following statements in
P1
s1 ;
signal(mutex);

And the following statements in P2

waite(mutex)
s2 ;

Semaphore Implementation

The semaphore discussed so far requires a busy waiting. That is if a process is in criticalsection, the other process that tries to enter its critical-section must loop continuously
in the entry code.
To overcome the busy waiting problem, the definition of the semaphore operations wait
and signal should be modified.
When a process executes the wait operation and finds that the semaphore value
is not positive, the process can block itself. The block operation places the
process into a waiting queue associated with the semaphore.
A process that is blocked waiting on a semaphore should be restarted when
some other process executes a signal operation. The blocked process should be
restarted by a wakeup operation which put that process into ready queue.
To implemented the semaphore, we define a semaphore as a record as:
typedef struct {
int value;
struct process *L;
} semaphore;

Assume two simple operations:


block suspends the process that invokes it.
wakeup(P) resumes the execution of a blocked process P.

Semaphore operations now defined as


wait(S):
S.value--;
if (S.value < 0)
{
add this process to S.L;
block;
}

signal(S):
S.value++;
if (S.value <= 0)
{
remove a process P from S.L;
wakeup(P);
}

Types of Semaphores

Counting semaphore integer value can range over an unrestricted domain.


Binary semaphore integer value can range only between 0 and 1; can be simpler
to implement.
Can implement a counting semaphore S as a binary semaphore.

Implementing S as a Binary Semaphore

Data structures:
binary-semaphore S1, S2;
int C:

Initialization:
S1 = 1
S2 = 0
C = initial value of semaphore S
Implementing S
wait operation

signal operation

wait(S1);

wait(S1);

C--;

C ++;

if (C < 0) {

if (C <= 0)

signal(S1);
wait(S2);
}
signal(S1);

signal(S2);
else
signal(S1);

A programmer who needs to write a tailor-made synchronization scheme can define one or more
variables of type condition:
condition x, y;
The only operations that can be invoked on a condition variable are wait () and signal(). The operation
x.waitO ;
means that the process invoking this operation is suspended until another process invokes
x.signal();
The x. signal () operation resumes exactly one suspended process. If no process is suspended, then the
signal () operation has no effect; that is, the state of x is the same as if the operation had never been
executed. Contrast this operation with the signal () operation associated with semaphores, which
always affects the state of the semaphore.

Unit 2
Classical Problems of Process synchronization
Classical Problems of Synchronization
Bounded-Buffer Problem
Readers and Writers Problem
Dining-Philosophers Problem
\*** Solution for all problems are given in the Class Note Book ****/

Unit 2
Concurrency and Deadlock
Resources

Examples of computer resources


Printers
Tape drives
Tables

Preemptable resources
Can be taken away from a process with no ill effects

Nonpreemptable resources
Will cause the process to fail if taken away

Reusable resources
Used by one process at a time and not depleted by that use
Examples: Processors, I/O channels, main and secondary memory, files, databases, and
semaphores

Shared and exclusive resources


Example of shared resource: FILE
Example of exclusive resource: PRINTER

Consumable resources
Created (produced) and destroyed (consumed) by a process
Examples: Interrupts, signals, messages, and information in I/O buffers

System Model

A system consists of a number of resources to be distributed among a number of competing


processes.
There are different types of resources R1, R2,..., Rm.
CPU cycles, memory space, I/O devices
Each resource type Ri has Wi instances. For example, if two CPUs then resource type CPU has
two instances.

Sequence of Events Required to Use a Resource

Each process utilizes a resource as follows:


Request a resource:
Request is made through a system call
Process must wait if request is denied
Requesting process may be blocked
may fail with error code
Use the resource:
The process can operate on the resource.
Release the resource:
The process releases the resource. A resource is released through a system call.

Deadlock

Formal Definition
A set of processes is deadlocked if each process in the set is waiting for an event that only
another process in the set can cause

Usually the event is release of a currently held resource

None of the processes can


Run
Release resources
Be awakened
Involve conflicting needs for resources by two or more processes

Examples of Deadlock
Example 1
System has 2 tape drives. P1 and P2 each hold one tape drive and each needs another
one.
Example 2
Semaphores A and B, initialized to 1
P0
P1

wait (A);
wait (B);

wait(B)
wait(A)

Example 3
Space is available for allocation of 200K bytes, and the following sequence of events
occur
P0

P1

Request 80KB;

Request 70KB;

Request 60KB;

Request 80KB;

Deadlock occurs if both processes progress to their second request

Four Conditions for Deadlock

Deadlock can arise if four conditions hold simultaneously


Mutual exclusion condition:
Only one process at a time can use a resource (non-shareable resource).
Each resource is assigned to a process or is available
Hold and wait condition:
A process holding at least one resource can request for additional resources
No preemption condition:
A resource can be released only voluntarily by the process holding it. That is
previously granted resources cannot be forcibly taken away.
Circular wait condition:
there exists a set {P0,P1,,P0} of waiting processes such that P0 is waiting for a
resource that is held by P1, P1 is waiting for a resource that is held by P2,,Pn1
is waiting for a resource that is held by Pn, and P0 is waiting for a resource that is
held by P0.

Resource-Allocation Graph

Deadlocks can be described more precisely in terms of a directed graph, called a system
resource-allocation graph.
This graph consists of a set of vertices V and a set of edges E.
V is partitioned into two types:
P = {P1,P2,,Pn}, the set consisting of all the processes in the system.
R = {R1, R2, , Rm}, the set consisting of all resource types in the system.
E is partitioned into two types as well:
Request edge directed edge P1 Rj
Assignment edgedirected edge Rj Pi
Different symbols are used to represent processes and resources as given below:
Process:

Resource type of 4 instances:

Pi requests instance of Rj:

Pi
Rj

Pi is holding an instance of Rj:

Pi
Rj

Method of Handling Deadlocks

Just ignore the problem altogether


Prevention
Ensure that the system will never enter a deadlock state
Requires negating one of the four necessary conditions
Dynamic avoidance
Require careful resource allocation
Detection and recovery
Allow the system to enter a deadlock state and then recover
We need some methods to determine whether or not the system has entered into
deadlock.
We also need algorithms to recover from the deadlock.
The Ostrich Algorithm

Pretend there is no problem


The system will eventually stop functioning
Reasonable if
Deadlocks occur very rarely
Cost of prevention is high
UNIX and Windows takes this approach
It is a trade off between
Convenience
Correctness

Deadlock Prevention

Prevent/deny Mutual Exclusion condition


Use shareable resource.
Impossible for practical system.
Prevent/Deny Hold and Wait condition
(a)
Pre-allocation - Require processes to request resources before starting
A process never has to wait for what it needs
(b)
Process must give up all resources and then request all immediately needed
Problems
May not know required resources at start of run
Low resource utilization many resources may be allocated but not used for long
time
Starvation possible a process may have to wait indefinitely for popular
resources.
Prevent/deny No Preemption condition
(a)
If a process that is holding some resources requests another resource that cannot be
immediately allocated to it, then all resources currently being held are released.

Preempted resources are added to the list of resources for which the process
is waiting.
Process will be restarted only when it can regain its old resources, as well as
the new ones that it is requesting.
(b)
The required resource(s) is/are taken back from the process(s) holding it/them and
given to the requesting process
Problems
Some resources (e.g. printer, tap drives) cannot be preempted without
detrimental implications.
May require the job to restart

Prevent/Deny Circular Wait


Order resources (each resource type is assigned a unique integer) and allow process to
request for them only in increasing order
If a process needs several instances of the same resource, it should issue a single request
for all of them.
Alternatively, we can require that whenever a process requests an instance of a resource
type it has released all the resources which are assigned a smaller inter value.
Problem:
Adding a new resource that upsets ordering requires all code ever written to be
modified
Resource numbering affects efficiency
A process may have to request a resource well before it needs it, just
because of the requirement that it must request resources in ascending
order
An example:

Deadlock Avoidance
OS never allocates resources in a way that could lead to a deadlock
Processes must tell OS in advance how many resources they will request
Some Definitions

State of a system
An enumeration of which processes hold, are waiting for or might request which
resource
Safe state

1. No process is deadlocked, and there exits no possible sequence of future request in


which deadlock could occur

2. No process is deadlocked and the current state will not lead to a dead lock state
3. Safe state is where there is at least one sequence that does not result in deadlock
Unsafe state
Is a state that is not safe

Basic Facts
If a system is in safe state no deadlocks.
If a system is in unsafe state possibility of deadlock.
Avoidance ensure that a system will never enter an unsafe state
Deadlock Avoidance with Resource-Allocation Graph

This algorithm can be used if we have only one instance of each resource type.
In addition to the request and assignment edges, a claim edge is also introduced.
Claim edge Pi Rj indicated that process Pj may request resource Rj in future; represented by a
dashed line.
Claim edge converts to request edge when a process requests a resource.
When a resource is released by a process, assignment edge reconverts to a claim edge.
Resources must be claimed a priori in the system. That is, before a process starts executing, all
of its claim edges must already appear in the resource-allocation graph.
Suppose that process Pi requests resource Rj. The request can be granted only if converting the
request edge if converting the request edge PiRj to an assignment edge does not result in a
cycle in the resource-allocation graph. That is we use a cycle detection algorithm is used. If no
cycle exits, the process Pi will have to wait.

Resource-allocation graph for deadlock avoidance

An unsafe state in the resource-allocation graph

Bankers Algorithm

Applicable to system with multiple instances of resource types.


Each process must a priori claim maximum use.
When a process requests a resource it may have to wait.
When a process gets all its resources it must return them in a finite amount of time.
Bankers algorithm runs each time:
A process requests resource Is it sage?
A process terminates Can I allocate released resources to a suspended process waiting
for them?
A new state is safe if and only if every process can complete after allocation is made
Make allocation and then check system state and deallocate if unsafe

Data Structures for Bankers algorithm


Let n = number of processes, and m = number of resources types.
Available: Vector of length m. If available [j] = k, there are k instances of resource type Rj
available.
Max: n x m matrix. Max [i,j] = k mean that process Pi may request at most k instances of Rj.
Allocation: n x m matrix. If Allocation[i,j] = k then Pi is currently allocated k instances of Rj.
Need: n x m matrix. If Need[i,j] = k, then Pi may need k more instances of Rj to complete its task.
Need [i,j] = Max[i,j] Allocation [i,j].

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

Resource-Request algorithm for Process Pi

Request = request vector for process Pi. If Requesti [j] = k then process Pi wants k instances of
resource type Rj.
1. If Requesti Needi go to step 2. Otherwise, raise error condition, since process has exceeded
its maximum claim.
2. If Requesti Available, go to step 3. Otherwise Pi must wait, since resources are not available.
3. Pretend to allocate requested resources to Pi by modifying the state as follows:
Available = Available = Requesti;
Allocationi = Allocationi + Requesti;
Needi = Needi Requesti

If safe the resources are allocated to Pi.


If unsafe Pi must wait, and the old resource-allocation state is restored
Example of Bankers Algorithm
5 processes P0 through P4; 3 resource types
A (10 instances),
B (5 instances), and
C (7 instances).

Snapshot at time T0:


Max

Available

ABC

ABC

ABC

P0

010

753

332

P1

200

322

P2

302

902

P3

211

222

P4

002

433

Allocation

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

Process

Need
ABC

P0

743

P1

122

P2

600

P3

011

P4

431

The system is in a safe state since the sequence <P1,P3,P4,P2,P0> satisfies safety criteria.
Example P1 Request (1,0,2)

Check that Request Available


that is, (1,0,2) (3,3,2) true.

Process

Allocation

Need

Available

ABC

ABC

ABC

P0

010

743

230

P1

302

020

P2

301

600

P3

211

011

P4

002

431

Executing safety algorithm shows that sequence <P1, P3, P4, P0, P2> satisfies safety
requirement.
Can request for (3,3,0) by P4 be granted?
Can request for (0,2,0) by P0 be granted?

Deadlock Detection Recovery

Allow system to enter deadlock state


Need a detection algorithm
Need a recovery algorithm

How to Detect a Deadlock Using a Resource-Graph?

If each resource type has exactly one instance and the graph has a cycle then a deadlock has
occurred. Or if the cycle involves only a set of resource types, each of which has only a single
instance, then the deadlock has occurred.
Therefore, a cycle in the graph is both a necessary and sufficient condition for the
existence of a deadlock.

Examples:

Resource-allocation graph with a deadlock

Recovery from Deadlocks Process Termination

Abort all deadlocked processes.


Abort one process at a time until the deadlock cycle is eliminated.
In which order should we choose to abort?
Priority of the process.
How long process has computed, and how much longer to completion.
Resources the process has used.
Resources process needs to complete.
How many processes will need to be terminated?
Is process interactive or batch?

Recovery from Deadlocks Resource Preemption

Selecting a victim minimize cost.


Rollback return to some safe state, restart process for that state.
Starvation same process may always be picked as victim, include number of rollback in cost
factor.

Combined Approach to Deadlock Handling

Combine the three basic approaches


prevention
avoidance
detection

allowing the use of the optimal approach for each of resources in the system.

Partition resources into hierarchically ordered classes.


Use most appropriate technique for handling deadlocks within each class.

Unit-2
Memory management
Mono-programming

In uni-processor systems, main memory is divided into two parts as shown below:

Multi-programming

In multiprogramming systems, user part of memory must be subdivided to accommodate


multiple processes.

Memory Management

Main memory is a resource that must be allocated and deallocated

Memory Management Techniques determine:


How the memory is to be (logically) subdivided?
Where and how a process resides in memory?
How addressing is performed?
How process can be relocated?
How memory is to be protection?
How memory can be shared by processes?
How to logical and physically organize memory

Addressing Requirements of a Process

The Basics

Program must be brought into memory and placed within a process for it to be run.
Input queue collection of processes on the disk that are waiting to be brought into memory to
run the program.

Normally, a process is selected from the input queue and is brought into the memory for
execution. During execution of a process, it accesses instruction and data from the memory.

Most systems allow a user process to be loaded in any part of the memory. This affects the
addresses that a user program can access.

User programs go through several steps before being run.

Addresses may be represented during these steps.


Addresses in the source program are generally symbolic (variable name).
A compiler typically binds these addresses to relocatable addresses (in terms of offsets).
The linkage editor or loader binds relocatable addresses to absolute addresses (physical
addresses).

Loading and Loader

A loader is responsible to place a load module in main memory at some starting address.
There are three approaches, which can be used for loading.
Absolute loading A given module is always loaded into the same memory location. All
references in the load module must be absolute memory addresses. The address binding
can be done at programming time, compile time or assembly time.
Relocatable loading The loader places a module in any desired location of the main
memory. To make it possible, the compiler or assembler must generate relative
addresses.
Dynamic loading - Routine is not loaded until it is called resulting in better memoryspace utilization (unused routine is never loaded). It is useful when large amounts of
code are needed to handle infrequently occurring cases. No special support from the
operating system is required implemented through program design.

Linking and Linker

The function of a linker is to take as input a collection of object modules and produce a load
module consisting of an integrated set of programs and data modules to be passed to the
loader.
In each object module, there may be symbolic reference to location in other modules.
The linking can be done either statically or dynamically.
Static linking A linker generally creates a single load module that is the contiguous
joining of all the object modules with references properly changed. This is called static
Dynamic linking Linking is postponed until execution time. All external references are
not resolved until the CPU executes the external call.
Small piece of code, stub, used to locate the appropriate memory-resident library
routine.
Stub replaces itself with the address of the routine, and executes the routine.
Operating system needed to check if routine is in processes memory address.
Dynamic linking is particularly useful for libraries.

Binding of Instructions and Data to Memory

Address binding of instructions and data to memory addresses can happen at three different
stages.
Compile time: If memory location known a priori, absolute code can be generated; must
recompile code if starting location changes.
Load time: The compiler must generate relocatable code if memory location is not
known at compile time. The final binding is delayed until load time.
Execution time: Binding delayed until run time if the process can be moved during its
execution from one memory segment to another. Need hardware support for address
maps (e.g., base and limit registers).

Logical vs. Physical Address Space

The concept of a logical address space that is bound to a separate physical address space is
central to proper memory management.
Logical address generated by the CPU; also referred to as virtual address.
Physical address address seen by the memory unit.
Logical and physical addresses are the same in compile-time and load-time address-binding
schemes; logical (virtual) and physical addresses differ in execution-time address-binding
scheme.

Memory Management Unit - (MMU)

MMU is a hardware device that maps virtual to physical address at run-time.


In a simple MMU scheme, the value in the relocation register is added to every address
generated by a user process at the time it is sent to memory.
The user program deals with logical addresses; it never sees the real physical addresses.

Dynamic relocation using a relocation register

Overlays

Overlay is one of the techniques to run a program that is bigger than the size of the physical
memory.
The idea of overlays is to keep in memory only those instructions and data that are needed at
any given time.
The basic idea is to divide the program into modules in such a way that not all modules need to
be in the memory at the same time.
Programmer specifies which modules can overlay each other
The linker inserts commands to invoke the loader when modules are referenced
Used in DOS as an alternative to Expanded memory

Overlays for a two-pass assembler

Advantages
Reduced memory requirements
Disadvantages
Overlap map must be specified by programmer
Programmer must know memory requirements
Overlapped modules must be completely disjoint

Swapping

A process needs to be in the memory to be executed. However, a process can be swapped


temporarily out of memory to a backing store, and then brought back into memory for
continued execution.
Swapping needs a backing store fast disk large enough to accommodate copies of all memory
images for all users; must provide direct access to these memory images.
Roll out, roll in swapping variant used for priority-based scheduling algorithms; lower-priority
process is swapped out so higher-priority process can be loaded and executed.
Major part of swap time is transfer time; total transfer time is directly proportional to the
amount of memory swapped. Note that context switching time in this scheme is quite high.
If we want to swap out a process, we must be sure that it is completely idle.
Modified versions of swapping are found on many systems, i.e., UNIX, Linux, and Windows.

Swapping of two processes using a disk as a backing store

Contiguous Allocation Techniques

Main memory usually divided into two partitions:


Resident operating system, usually held in low memory with interrupt vector.
User processes then held in high memory.
Every process is loaded into a single contiguous partition of the memory.
Single-partition Allocation

A single processes in loaded into the memory at a time.

Advantages:
Simplicity
No special hardware required
Disadvantages:
CPU wasted
Main memory not fully used
Limited job size

Multiple-Partition Allocation Fixed Partitions

Desirable in multiprogramming environment.


A simple multi-partition scheme is to divide the memory into fixed size partitions.
Each process is loaded into a single partition.
In this scheme, the degree of multiprogramming is bounded by the number of partitions.
This scheme was originally used in IBM OS/360. No longer in use now.

There might be a separate process queue (need absolute addressing) for each partition or a
single queue for all partitions (need dynamic addressing).

Multiple-partition Allocation Dynamic Partitions

Partitions are of variable length and number


Process is allocated exactly as much memory as required
MVT, a dynamic partition scheme, works as follow:
Operating system maintains information about:
Allocated partitions
Free partitions (hole). Initially, all the memory is available and is considered as a
single block (one big hole).
When a process arrives, we search for a hole large enough for the process. If a hole is
found, it is allocated to the process. If we find a hole, which is too large, it is split into
two: one part is allocated to the arriving process, the other is returned to the set of
holes.
When a process completes, the memory is freed and is placed in to the set of holes. If the
new hole is adjacent to other holes, they are packed in the set of holes. At this point, it is
checked whether there is a process waiting for memory and whether this newly freed
hole and recombined memory could satisfy the demands of any of the waiting processes.

Merging of holes

Pros and Cons of Dynamic Allocation

Advantages
Efficient use of memory
Disadvantages
Partition management
Compaction or external fragmentation
Internal fragmentation

Dynamic Allocation Placement Algorithms

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


There are many solutions to this problem, but most common are as follows:
First-fit: Allocate the first hole that is big enough. The search can start either at the
beginning of the set of holes or where the previous first-fit search was ended.
Best-fit: Allocate the smallest hole that is big enough; must search entire list, unless
ordered by size. Produces the smallest leftover hole.
Worst-fit: Allocate the largest hole; must also search entire list. Produces the largest
leftover hole.
First-fit and best-fit better than worst-fit in terms of speed and storage utilization.
Example

Fragmentation

xternal Fragmentation total memory space exists to satisfy a request, but it is not contiguous.
Internal Fragmentation allocated memory may be slightly larger than requested memory; this
size difference is memory internal to a partition, but not being used.
Reduce external fragmentation by compaction.
Shuffle memory contents to place all free memory together in one large block.
Compaction is possible only if relocation is dynamic, and is done at execution time.
I/O problem
Latch job in memory while it is involved in I/O.
Do I/O only into OS buffers.
High cost of compaction.

Paging

Another possible solution to external fragmentation is paging.


In paging, logical address space of a process can be noncontiguous; process is allocated physical
memory whenever the latter is available.
Physical memory is divided into fixed-sized blocks called frames (size is power of 2, between 512
bytes and 8192 bytes).
Logical memory is also divided into blocks of same size called pages. The page size is defined by
the hardware and is typically a power of 2 varying between 512 bytes and 16 megabytes per
page. The selection of a power of 2 as a page size makes the translation of logical address into a
page number and offset easy.
To run a program of size n pages, need to find n free frames and load program.
Set up a page table (for each process) to translate logical to physical addresses.
Internal fragmentation is possible.

Memory Allocation in Paging

When a new process arrives, its size, in pages, is determined.


If the process has n pages then n frames must be available in the physical memory.
The first page of the process is then loaded into the first available frame, the next into next
available frame and so on.
Since OS is managing the physical memory, it must be aware of the allocation details of physical
memory (allocated and free frames). This information is generally kept in a data structure known
as frame table which has one entry for each frame and indicates whether it is free or not and if
allocated to which page of which process.

Free frames (a) before allocation (b) after allocation

Address Translation

Address generated by CPU is divided into:


Page number (p) used as an index into a page table which contains base address of
each page in physical memory.
Page offset (d) Byte within the page

Address translation architecture

Implementation of Page Table

Page table for each process is kept in main memory.


Page-table base register (PTBR) points to the page table.
Page-table length register (PTLR) indicates size of the page table.
In this scheme every data/instruction access requires two memory accesses. One for the page
table and one for the data/instruction.
The two memory access problem can be solved by the use of a special fast-lookup hardware
cache called associative memory or translation look-aside buffers (TLBs).

Associative memory

Associative memory allows parallel search


Contains page table entries that have been most recently used
Functions same way as a memory cache
Given a virtual address, processor examines the TLB
If page table entry is present (a hit), the frame number is retrieved and the real address is
formed
If page table entry is not found in the TLB (a miss), the page number is used to index the process
page table
First checks if page is already in main memory
if not in main memory a page fault is issued
The TLB is updated to include the new page entry

Note that TLB must be flushed every time a new page table is selected (during context switching,
for example).

Effective Access Time

Associative Lookup = time unit


Assume memory cycle time is 1 microsecond
Hit ratio percentage of times that a page number is found in the associative registers; ration
related to number of associative registers.
Hit ratio =
Effective Access Time (EAT)
EAT = (1 +) + (2 +)(1 )
=2+

Memory Protection

Memory protection implemented by associating protection bit with each frame. Normally these
bits are kept in the page table. One bit can define a page to be read and write or read-only. Since
every reference to a memory location is through the page table, and protection bits can be
checked at the address translation time. An illegal operation causes a trap.
One more bit, Valid-invalid bit, is also attached to each entry in the page table:
valid indicates that the associated page is in the process logical address space, and is
thus a legal page.
invalid indicates that the page is not in the process logical address space.

Valid and invalid bit in a page table

Page Table Structure

Hierarchical Paging
Hashed Page Tables
Inverted Page Tables

Hierarchical Page Tables

Break up the logical address space into multiple page tables.


A simple technique is a two-level page table.

Two Level Paging Example

A logical address (on 32-bit machine with 4K page size) is divided into:
a page number consisting of 20 bits.
a page offset consisting of 12 bits.
Since the page table is paged, the page number is further divided into:
a 10-bit page number.
a 10-bit page offset.
Thus, a logical address is as follows:
Page offset

Page number

P1
10

P2
10

12

where pi is an index into the outer page table, and p2 is the displacement within the page of the outer
page table.

Address Translation Scheme

Address-translation scheme for a two-level 32-bit paging architecture

Hashed Page Tables

Common in address spaces > 32 bits.


The virtual page number is hashed into a page table. This page table contains a chain of
elements hashing to the same location.

Virtual page numbers are compared in this chain searching for a match. If a match is found, the
corresponding physical frame is extracted.

Hash page table


Inverted Page Table

One entry for each real page of memory.


Entry consists of the virtual address of the page stored in that real memory location with
information about the process that owns that page.
Decreases memory needed to store each page table, but increases time needed to search the
table when a page reference occurs.
Use hash table to limit the search to one or at most a few page-table entries.

Inverted page table architecture

Shared Pages

Shared code
One copy of read-only (reentrant) code shared among processes (i.e., text editors,
compilers, window systems).
Shared code must appear in same location in the logical address space of all processes.

Private code and data


Each process keeps a separate copy of the code and data.
The pages for the private code and data can appear anywhere in the logical address
space.

Pros and Cons of Paging

Advantages:
Efficient memory use
Simple partition management due to discontinuous loading and fixed partition size
No compaction is necessary
Easy to share pages
Disadvantages
Job size <= memory size
Internal fragmentation
Need special hardware for address translation
Some main memory is used for page table
Address translation lengthens memory cycle times

Segmentation

Memory-management scheme that supports user view of memory.


A program is a collection of segments. A segment is a logical unit such as:
main program,
procedure,
function,
method,
object,
local variables, global variables,
common block,
stack,
symbol table, arrays

1
4

1
2
3
4

2
3

user
space

physical memory
space

Address Mapping in Segmentation

The user specifies each address by two quantities: segment name/number and offset.

<segment-number, offset>,

Mapping from logical address to physical address is done with the help of a segment table.
Segment table maps two-dimensional physical addresses; each table entry has:
base contains the starting physical address where the segments reside in memory.
limit specifies the length of the segment.
Segment-table base register (STBR) points to the segment tables location in memory.
Segment-table length register (STLR) indicates number of segments used by a program;
Segment number s is legal if s < STLR.

Segmentation Example

Comparison of Paging and Segmentation

Virtual Memory
Background
Virtual memory is a technique that allows execution of processes that may not be completely in
the physical memory.
Virtual Memory gives the illusion of more physical memory than there really is (via demand
paging)
Virtual memory provides many benefits:
Only part of the program needs to be in memory for execution.
Logical address space can therefore be much larger than physical address space.
Allows address spaces to be shared by several processes.
Allows for more efficient process creation.
Use of virtual memory is also justified for many reasons such as:
There may be some code (e.g. error code) that may never be executed.
Arrays, list and tables are often allocated more memory than they actually use.
Certain options and features of a program may be used rarely.
Virtual memory can be implemented via:
Demand paging
Demand segmentation
Program Execution in Virtual memory

Operating system brings into main memory a few pieces of the program
Resident set - portion of process that is in main memory
An interrupt is generated when an address is needed that is not in main memory
Operating system places the process in a blocking state
Piece of process that contains the logical address is brought into main memory
Operating system issues a disk I/O Read request
Another process is dispatched to run while the disk I/O takes place
An interrupt is issued when disk I/O complete which causes the operating system to
place the affected process in the Ready state

Demand Paging

When we want to execute a process, it is swapped into the memory. However, a pager
(swapper) does not bring the whole process into the memory. Only those pages, which are
needed, are brought into the memory. That is, bring a page into memory only when it is needed.
Often page 0 is loaded initially when a job is scheduled
In Demand Paging a programs working set is kept in memory, reference outside WS causes
corresponding code to be retrieved from disk (page fault)
Provides the illusion of virtual memory
Demand paging has the many benefits such as
Less I/O needed
Less memory needed
Faster response
More users

Transfer of a Paged Memory to Contiguous Disk Space


Valid-Invalid Bit

For the above-mentioned scheme, we need some hardware support.


With each page table entry a validinvalid bit is associated (1 in-memory, 0 not-inmemory)
Initially validinvalid but is set to 0 on all entries.
During address translation, if validinvalid bit in page table entry is 0 page fault.
Page Fault - Interrupt that arises upon a reference to a page that is not in main memory.

Page Fault Handling

If there is ever a reference to a page, first reference will trap to


OS page fault
OS looks at an internal table, usually kept in processs PCB, to decide:
Invalid reference abort.
Just not in memory.
If the page is not in the memory, then
Get empty frame.
Swap page into frame.
Reset tables, validation bit = 1.
Restart instruction
Note that when a page fault happens, the hardware traps to the OS. The OS reads the desire
page and restart the process from the point where it was interrupted.

Steps in Handling a Page Fault

What Happens If There is no Free Frame?

Page replacement find some page in memory, but not really in use, swap it out.
Algorithm
Performance want an algorithm which will result in minimum number of page faults
Same page may be brought into memory several times

Performance of Demand Paging

Page Fault Rate 0 p 1.0


if p = 0 no page faults
if p = 1, every reference is a fault
Effective Access Time (EAT)
EAT = (1 p) x memory access
+ p ( page fault overhead
+ [swap page out ]

+ swap page in

+ restart overhead )
Example

Memory access time = 1 microsecond


50% of the time the page that is being replaced has been modified and therefore needs to be
swapped out.
Swap Page Time = 10 msec = 10,000 msec
EAT = (1 p) x 1 + p (15000)
1 + 15000P (in msec)

Page Replacement

Prevent over-allocation of memory by modifying page-fault service routine to include page


replacement.
Use modify (dirty) bit to reduce overhead of page transfers only modified pages are written to
disk.
Page replacement completes separation between logical memory and physical memory large
virtual memory can be provided on a smaller physical memory.

Basic Page Replacement Algorithm

Find the location of the desired page on disk.


Find a free frame:
- If there is a free frame, use it.
- If there is no free frame, use a page replacement algorithm to select a victim frame.

Read the desired page into the (newly) free frame. Update the page and frame tables.
Restart the process.

Page Replacement Algorithms

Want lowest page-fault rate.


Evaluate algorithm by running it on a particular string of memory references (reference string)
and computing the number of page faults on that string.

FIFO Algorithm

When a page must be replaced, the oldest page is chosen.


FIFO page replacement algorithm associates with each page the time when the page was
brought into the memory.
A FIFO queue (instead of time) may also be used to implement this algorithm.

Example of FIFO Algorithm

Beladys Anomaly

An interesting observation if FIFO algorithm is that increasing the page frames may not decrease
the number of page faults.

Optimal Algorithm

According to optimal (OPT or MIN), the page that is to be replaced is the one that will not be
used for the longest period of time.
Difficult to implement because it requires future knowledge of the reference string.

Example of OPT Algorithm

Least Recently Used (LRU) Algorithm

LRU chooses a page for replacement that has not been used for the longest period of time.
Uses the recent past as an approximation of the near future

Two possible implantations of LRU:


o Counter implementation
Every page entry has a counter; every time page is referenced through this entry,
copy the clock into the counter.
When a page needs to be changed, look at the counters to determine which are
to change.
o Stack implementation
keep a stack of page numbers in a double link form:
Page referenced:
move it to the top
requires 6 pointers to be changed
No search for replacement

LRU Approximation Algorithms

Use a reference bit


With each page associate a bit, initially = 0
When page is referenced bit set to 1.
Replace the one which is 0 (if one exists). We do not know the order, however.
Addition reference-bits algorithm
Reference bits are recorded in regular intervals. For example, we can keep an 8-bit for
each page in a table in the memory.
After regular intervals, a timer interrupt transfers the control to the OS which shifts the
reference bit for each page into the high-order bit of 8-bit shift registers containing the
history of page.
The page with the lowest number is the LRU page.

Counting Algorithms

Keep a counter of the number of references that have been made to each page. The following
two schemes can be used:
LFU Algorithm: Replaces page with smallest count is to be replaced.
MFU Algorithm: replace page with the largest value of count. It is based on the argument
that the page with the smallest count was probably just brought in and has yet to be
used.
Not very commonly used algorithm.

Allocation of Frames

Each process needs minimum number of pages. Minimum number of frames is defined by the
computer architecture.
Two major allocation schemes:
Fixed allocation
Priority allocation

Fixed Allocation

Equal allocation e.g., if 100 frames and 5 processes, give each 20 pages.
Proportional allocation Allocate according to the size of process.

Priority Allocation

Use a proportional allocation scheme using priorities rather than size.


If process Pi generates a page fault,
Select for replacement one of its frames.
Select for replacement a frame from a process with lower priority number.

Global vs. Local Allocation

Global replacement process selects a replacement frame from the set of all frames; one
process can take a frame from another.
Local replacement each process selects from only its own set of allocated frames.

Process Creation

Virtual memory allows other benefits during process creation:


Copy-on-Write
Memory-Mapped Files

Copy on Write

Copy-on-Write (COW) allows both parent and child processes to initially share the same pages in
memory.
If either process modifies a shared page, only then is the page copied.
COW allows more efficient process creation as only modified pages are copied.

Memory Mapped Files

Memory-mapped file I/O allows file I/O to be treated as routine memory access by mapping a
disk block to a page in memory.
A file is initially read using demand paging. A page-sized portion of the file is read from the file
system into a physical page. Subsequent reads/writes to/from the file are treated as ordinary
memory accesses.
Simplifies file access by treating file I/O through memory rather than read() write() system calls.
Also allows several processes to map the same file allowing the pages in memory to be shared

Pros/Cons of Demand Paging

Advantages:
Can run program larger than physical memory
Allows higher multiprogramming level than pure paging
Efficient memory usage
No compaction is required
Portions of process that are never called are never loaded
Simple partition management due to discontinuous loading and fixed partition size
Easy to share pages
Disadvantages:
Internal fragmentation
Program turnaround time increases each time a page is replaced, then reloaded
Need special address translation hardware

Unit-2

File
System

Unit-2
I/O Management and Disk Scheduling
Categories of I/O Devices

Human readable
Used to communicate with the user
Printers
Video display terminals
Display
Keyboard
Mouse
Machine readable
Used to communicate with electronic equipment
Disk and tap drives
Sensors
Controllers
Communication
Used to communicate with remote devices
Digital line drivers
Modems

Differences in I/O Devices

Data rate
May be differences of several orders of magnitude between the data transfer rates
Application
Disk used to store files requires file-management software
Disk used to store virtual memory pages needs special hardware and software to support
it
Terminal used by system administrator may have a higher priority
Complexity of control
Unit of transfer
Data may be transferred as a stream of bytes for a terminal or in larger blocks for a disk
Data representation
Encoding schemes
Error conditions
Devices respond to errors differently
Programmed I/O
Process is busy-waiting for the operation to complete
Interrupt-driven I/O
I/O command is issued
Processor continues executing instructions
I/O module sends an interrupt when done

I/O Hardware

Incredible variety of I/O devices


Common concepts
Port (a connection point)
Bus (daisy chain or shared direct access)
Controller (host adapter)
I/O instructions control devices
Devices have addresses, used by
Direct I/O instructions
Memory-mapped I/O

Life Cycle of an I/O Request

Disk Structure

Disks provide the bulk of secondary storage.


Disk drives are addressed as large 1-dimensional arrays of logical blocks, where the logical block
is the smallest unit of transfer. The size of logical block is generally 512 bytes.
The 1-dimensional array of logical blocks is mapped into the sectors of the disk sequentially.
Sector 0 is the first sector of the first track on the outermost cylinder.
Mapping proceeds in order through that track, then the rest of the tracks in that cylinder,
and then through the rest of the cylinders from outermost to innermost.
The number of sectors per tack is not a constant. Therefore, modern disks are organized in zones
of cylinders. The number of sectors per track is constant within a zone.

Disk I/O

Whenever a process needs I/O to or from a disk, it issues a system call to the operating system.
If the desired disk drive and controller is available, the request can be serviced
immediately other wise the request is placed in a queue.
Once an I/O completes, the OS can choose a pending request to serve next.

Disk performance Parameters

The operating system is responsible for using hardware efficiently - for the disk drives, this
means having a fast access time and disk bandwidth.
Disk bandwidth is the total number of bytes transferred, divided by the total time between the
first request for service and the completion of the last transfer.

Access time has two major components


Seek time is the time for the disk are to move the heads to the cylinder containing the
desired sector.
Rotational latency is the additional time waiting for the disk to rotate the desired sector
to the disk head.

Seek time is the reason for differences in performance


Minimize seek time
Seek time seek distance

Disk Scheduling

For a single disk there will be a number of I/O requests


If requests are selected randomly, we will get the worst possible performance
Several algorithms exist to schedule the servicing of disk I/O requests.
We illustrate them with a request queue (0-199).
98, 183, 37, 122, 14, 124, 65, 67
Head pointer 53

First Come First Serve (FCFS)

The I/O requests are served in the order in which they reach. See below (total head
movement=640 cylinders)
FCFS is a fair scheduling algorithm but not an optimal one.

Shortest-Seek-Time-First (SSTF)

Selects the request with the minimum seek time from the current head position.
SSTF scheduling is a form of SJF CPU scheduling
May cause starvation of some requests
Is not optimal.
Illustration shows total head movement of 236 cylinders.

SCAN Scheduling

The disk arm starts at one end of the disk, and moves toward the other end, servicing requests
until it gets to the other end of the disk, where the head movement is reversed and servicing
continues.
Sometimes called the elevator algorithm.
Illustration shows total head movement of 208 cylinders (head is moving towards cylinder 0).

C-SCAN Scheduling

Provides a more uniform wait time than SCAN.


The head moves from one end of the disk to the other, servicing requests as it goes. When it
reaches the other end, however, it immediately returns to the beginning of the disk, without
servicing any requests on the return trip.
Treats the cylinders as a circular list that wraps around from the last cylinder to the first one.

C-Look Scheduling

Version of C-SCAN
Arm only goes as far as the last request in each direction, then reverses direction immediately,
without first going all the way to the end of the disk.

Selecting a Disk Scheduling Algorithm

SSTF is common and has a natural appeal


SCAN and C-SCAN perform better for systems that place a heavy load on the disk.
Performance depends on the number and types of requests.
Requests for disk service can be influenced by the file-allocation method.
The disk-scheduling algorithm should be written as a separate module of the operating system,
allowing it to be replaced with a different algorithm if necessary.
Either SSTF or C-LOOK is a reasonable choice for the default algorithm

Disk Management

Low-level formatting, or physical formatting - Dividing a disk into sectors that the disk controller
can read and write.
To use a disk to hold files, the operating system still needs to record its own data structures on
the disk.
Partition the disk into one or more groups of cylinders.
Logical formatting or making a file system.
Boot block initializes system.
The bootstrap is stored in ROM.
Bootstrap loader program.
Bad sectors may be managed manually. For example MS-DOS format command does a logical
format and if it finds any bad sector, it writes a special value into FAT.
Sector sparing method may also used to handle bad blocks (as used in SCSI disks). The controller
maintains a list of bad sectors which is updated regularly. Low level formatting also sets aside
some spare sectors. The controller can be asked to replace each bad sector logically with one of
the spare sectors.

Contacts
Saurabh Patil
Saurabh.terna@gmail.com
09773445201

Вам также может понравиться