Ics 2270 Os
Ics 2270 Os
Ics 2270 Os
COURSE OUTLINE
Objectives
Enable the student understand the operating systems in the computer system.
Enable student identify various functions and roles of the operating systems in the computer system.
Assessment:
Continuous Assessment Tests 20%
Assignments 10%
End of Semester Exam 70%
Total 100%
References:
1 ChaudhuryP (2006);Operating Systems: Principles and Design(5thEdition); New York, USA
2. Tanenbaum A. S.(2006);Operating Systems; Design and Implementation. (2nd Ed); McGraw Hill
3.Galvin B & Gagne G.(2005);Operating System Concepts Peter . (2nd Ed); McGraw Hill
4. The Internet
1.0 Operating System:- Introduction
2. An operating system (OS) is software that controls a computer. It manages hardware, runs
applications, provides an interface for users, and stores, retrieves, and manipulates files. In
general, you can think of an operating system as the middleman between applications and
hardware, between the user and hardware, and between the user and applications
3. An operating system is the low-level software that enables a user and higher-level application
software to interact with a computer’s hardware and the data and other programs stored on the
computer. An OS performs basic tasks, such as recognizing input from the keyboard, sending
output to the display screen, keeping track of files and directories on the disk, and controlling
peripheral devices such as printer
4. An operating system is the software that controls the hardware by making the hardware useable by
providing an interface (or translator) between the application software (e.g. word processor) and
the hardware (i.e. tells the hardware what to do and how to communicate between the parts).
After being initially loaded into the computer by a boot program, it manages all the other
programs in the computer.
User
Operating system
Hardware
1
The operating system is the most important type of system software in a computer system. Without an
operating system, application programs will not run on the computer hardware.
Controlling the computer involves software at several levels. Differentiate kernel services, library
services, and application-level services, all of which are part of the operating system. Processes run
Applications, which are linked together with libraries that perform standard services. The kernel supports
the processes by providing a path to the peripheral devices. The kernel responds to service calls from the
processes and interrupts from the devices. The core of the operating system is the kernel, a control
program that functions in privileged state (an execution context that allows all hardware instructions to
be executed), reacting to interrupts from external devices and to service requests and traps from processes.
Generally, the kernel is a permanent resident of the computer. It creates and terminates processes and
responds to their request for service.
Operating Systems are resource managers. The main resource is computer hardware in the form of
processors, storage, input/output devices, communication devices, and data. Some of the operating system
functions are: implementing the user interface, sharing hardware among users, allowing users to share
data among themselves, preventing users from interfering with one another, scheduling resources among
users, facilitating input/output, recovering from errors, accounting for resource usage, facilitating parallel
operations, organizing data for secure and rapid access, and handling network communications.
Finally, the resources in a computer (CPU, memory, disk space) are limited. The operating system
must act as a resource manager, optimising the use of the resources, and protecting them against misuse
and abuse. When a system provides multiuser or multitasking capabilities, resources must be allocated
fairly and equitably amongst a number of competing requests.
2
Main Objectives of Operating Systems
Modern Operating systems generally have following three major goals. Operating systems generally
accomplish these goals by running processes in low privilege and providing service calls that invoke the
operating system kernel in high-privilege state.
One can therefore view operating systems from two points of view: Resource manager and Extended
machine. From Resource manager point of view, operating systems manage the different parts of the
system efficiently and from extended machines point of view, operating systems provide a virtual
machine to users that is more convenient to use. The structurally operating systems can be designed as a
monolithic system, a hierarchy of layers, a virtual machine system, or using the client-server model. The
basic concepts of operating systems are processes, memory management, I/O management, the file
systems, and security.
3
Categories of Operating Systems
Every operating system is different, and each is designed to meet a set of goals. However, operating
systems can generally be classified into the following categories.
4
Other Categorization of Operating Systems
Within the broad family of operating systems, there are several types of operating systems, categorized
based on the types of computers they control and the sort of applications they support. The categories are:
(a) Single-user, single task - As the name implies, this operating system is designed to manage the
computer so that one user can effectively do one thing at a time. The Palm OS for Palm handheld computers
(PDA) is a good example of a modern single-user, single-task operating system.
(b) Single-user, multi-tasking - This is the type of operating system most people use on their desktop
and laptop computers today. Microsoft's Windows and Apple's MacOS platforms are both examples of
operating systems that will let a single user have several programs in operation at the same time. For
example, it's entirely possible for a Windows user to be writing a note in a word processor while
downloading a file from the Internet while printing the text of an e-mail message.
(c) Multi-user, multi-tasking - A multi-user operating system allows many different users to take
advantage of the computer's resources simultaneously. The operating system must make sure that the
requirements of the various users are balanced, and that each of the programs they are using has sufficient
and separate resources so that a problem with one user doesn't affect the entire community of users. Unix,
VMS and mainframe operating systems, such as MVS, are examples of multi-user operating systems.
(d) Real-time operating system (RTOS) - Real-time operating systems are used to control
machinery, scientific instruments and industrial systems. An RTOS typically has very little user-interface
capability, and no end-user utilities, since the system will be a "sealed box" when delivered for use. A
very important part of an RTOS is managing the resources of the computer so that a particular operation
executes in precisely the same amount of time, every time it occurs. In a complex machine, having a part
move more quickly just because system resources are available may be just as catastrophic as having it
not move at all because the system is busy.
(e) Process Control: - Generally implies the control by computer of an industrial process such as the
refining of oil, the manufacture of machine tools or even the monitoring of a patient’s condition in a
hospital. The common feature of all these applications is feedback i.e. the computer receives input from
the controlled process, computes a response which will maintain its stability, and initiates the mechanism
for giving it. For example, if the input specifies a dangerous rise in temp, then the response may be to
open a valve in order to raise the flow of coolant. The main function of the OS in process control is to
provide maximum reliability (i.e. avoid faults, tolerate faults and detect and recover from faults) with the
minimum of operator intervention, and to “fail safe” in the event of any h/w malfunctions.
(f) Timesharing
This strategy supports multiple interactive users. Rather than preparing a job for execution ahead of time,
users establish an interactive session with the computer and then provide commands, programs and data
as they are needed during the session.
5
2.0 FUNCTIONS OF OPERATING SYSTEM.
An operating system has a complex mixture of diversified functions. Operating systems is the resource
manager. At the simplest level, an operating system does two things:
One, it manages the hardware and software resources of the system. In a desktop computer, these
resources include such things as the processor, memory, disk space and more (On a cell phone, they
include the keypad, the screen, the address book, the phone dialer, the battery and the network
connection).
It provides a stable, consistent way for applications to deal with the hardware without having to know
all the details of the hardware
The main resource is computer hardware in the form of processors, storage, input/output devices,
communication devices, and data. Some of the functions are: implementing the user interface, sharing
hardware among users, allowing users to share data among themselves, preventing users from interfering
with one another, scheduling resources among users, facilitating input/output, recovering from errors,
accounting for resource usage, facilitating parallel operations, organizing data for secure and rapid access,
and handling network communications.
The operating systems generally accomplish these goals by running processes in low privilege and
providing service calls that invoke the operating system kernel in high-privilege state.
Since the operating system manages many kinds of activities ranging from user programs to
system programs like printer spooler, 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 that can be running in the same program. The five major activities of
an operating system in regard to process management are
Note that primary (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 accessed directly by the CPU. That is to say for a
program to be executed (run), it must in the main memory.
Keep track of which part of memory are currently being used and by whom.
Decide which processes are loaded into memory when memory space becomes available.
Allocate and de-allocate memory space as needed.
There are all kinds of I/O devices in the computer system. The management of these devices is one of the
most complex aspect of operating system design. This is because I/O devices may differ from one
another on all kinds of aspects. Some are human readable, suitable for communicating with the
computer users, such as printers, monitors, speakers, keyboards, and mice. Some are machine readable,
suitable for communicating with electronic equipment such as disks and tapes. Some others are otherwise
used for communicating with remote devices, such as modems and network cards.
The above categories are obtained based on the functions of I/O devices. What’s more, even those
that fall into one category may still be different from one another in various aspects. They may have
different data transferring speed, different complexity of control, printing density, control mechanism, and
other variables etc.
The operating system is also responsible for keeping track of the files and directories that reside on hard
or floppy disks.
A file is a collection 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 is normally organized into directories to ease their use. These directories may
contain files and other directories.
The operating system manages communication among computers connected on a network. Since data
exchanged on the network may be received intermittently instead of continuously, they must be combined
by the operating system. The received data is then converted into a form that computers can process. The
operating system also keeps track of the status of the network, disconnecting faulty portions, reporting
computer usage accounts, and so on.
As local-area networks (LANs) continue to become more widely used, management of distributed
processing--i.e., execution of computational tasks by a LAN with many computers and peripheral
equipment--is becoming increasingly important. Many users (clients) on a LAN need to share data, files,
or printers, and they send their requests to servers or central computers. When a network contains many
LANs, it is a challenging task to keep track of which of the many LANs on the network use which servers
because of the network's constantly changing nature.
The communication-network design must consider routing and connection strategies, and the
problems of contention and security.
2.7 Security.
The operating system protects computers from access by illegal users and from data corruption introduced
by unintentional mistakes made by legitimate users. Security is particularly important for computers that
are connected to a communications network, because many users can freely access any computer.
Authorized users are authenticated by entering an individual password, and then a computer usage fee is
charged (if need be) to the account of each user.
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.
9
2.8 Abstraction: Hide Details of Hardware
An abstraction is software that hides lower level details and provides a set of higher-level functions. An
operating system transforms the physical world of devices, instructions, memory, and time into virtual
world that is the result of abstractions built by the operating system. There are several reasons for
abstraction.
First, the code needed to control peripheral devices is not standardized. Operating systems provide
subroutines called device drivers that perform operations on behalf of programs for example, input/output
operations.
Second, the operating system introduces new functions as it abstracts the hardware. For instance,
operating system introduces the file abstraction so that programs do not have to deal with disks.
Third, the operating system transforms the computer hardware into multiple virtual computers, each
belonging to a different program. Each program that is running is called a process. Each process views the
hardware through the lens of abstraction.
Fourth, the operating system can enforce security through abstraction.
In a computer, a file system is the way in which files are named and where they are placed logically
for storage and retrieval. The DOS, Windows, OS/2, Macintosh, and UNIX-based operating systems all
have file systems in which files are placed somewhere in a hierarchical (tree) structure. A file is placed in
a directory (folder in Windows) or subdirectory at the desired place in the tree structure.
File systems specify conventions for naming files. These conventions include the maximum
number of characters in a name, which characters can be used, and, in some systems, how long the file
name suffix can be. A file system also includes a format for specifying the path to a file through the
structure of directories.
Files
Files are storage areas for programs, source code, data, documents etc. They can be accessed by
processes, but don't disappear when processes die, or when the machine is turned off. They are thus
persistent objects. Operating systems provide mechanisms for file manipulation, such as open, close,
create, read and write. As part of the job of hiding the hardware and providing abstract services, the
operating system must map files onto areas on disks and tapes (or any other storage). The operating
system must also deal with files that grow or shrink in size.
Some operating systems don't enforce any structure to files, or enforce particular file types. Others
distinguish between file types and structures, e.g Java source files, text documents, executable files, data
files etc. Most operating systems allow files to have permissions, allowing certain types of file access to
authorized users only.
Directories may exist to allow related files to be collected. The main reason for the existence of
directories is to make file organization easier and more flexible for the user.
10
File System Manipulation
The output of a program may need to be written into new files or input taken from some files. The
operating system provides this service. The user does not have to worry about secondary storage
management. User gives a command for reading or writing to a file and sees his/her task accomplished.
Thus operating system makes it easier for user programs to accomplish their task.
This service involves secondary storage management. The speed of I/O that depends on secondary
storage management is critical to the speed of many programs and hence it is best relegated to the
operating systems to manage it than giving individual users the control of it. It is not difficult for the user-
level programs to provide these services but for the above mentioned reasons it is best if this service is left
with operating system.
For most users, the file system is the most visible aspect of an operating system, apart from the
user interface. Files store programs and data.
The operating system implements the abstract `file' concept by managing I/O devices, for
example, hard disks. Files are usually organised into directories to make them easier to use. Files also
usually have some form of protections.
The file system provides: (1) An interface to the file objects for both programmers and users and
(2) The policies and mechanisms to store files and their attributes on permanent storage devices.
System Files
System files are files that the OS requires in order to function properly. Each system file has a
function that alters, manages, or runs part of the OS or other system files. Contrast this with a data or
application file, such as a saved game, document, or image file. The existence of an application file will
not alter the behavior of the OS. You can change, create, or delete a non-system file without affecting the
performance of the computer.
In most cases, a deleted, missing, or corrupt system file will result in an improperly functioning
OS feature or an OS that will not function at all. A thorough knowledge of the OS system files and their
functions will allow you to pinpoint and resolve OS problems based on the behavior of the computer or
on the error messages you receive.
Many of the OS system files are location-dependent. That is, they will not be loaded and properly
executed if they are not in a specific location. For this reason, and to keep them from being accidentally
deleted, most system files are hidden. This means that they will not be displayed in the My Computer or
Windows Explorer navigation windows.
File control blocks (FCB), sometimes referred to as file descriptors, are data structures that hold
information about a file. When an operating system needs to access a file, it creates an associated file
control block to manage the file.
The structure of the file control block differs between operating systems, but most file control
blocks include the following parts
Filename
Location of file on secondary storage
Length of file
Date and time or creation or last access
11
Keeping track of files
The hard disk is comprised of a large number of sequentially numbered sectors. As files are created, free
sectors are allocated to hold the file contents and marked as allocated.
To keep track of the sectors and whether they are allocated or free, and to which file they belong, the
operating system maintains a number of tables.
When the operating system is first installed, it creates a root file system on the disk that specifies
how many sectors are available and how they will be allocated.
The root file system is a table of entries like a directory. In general, this is a fixed size, and once
full, no more entries can be added.
Each entry can be either a file or another directory table. The following table depicts this structure.
The root file system is highly operating system specific, but an entry might look like,
Name of file
Beginning cluster number
Length of file in bytes
Type of file
Creation date and last modified right
File permissions (an access control list)
12
4.0 PROCESS COORDINATION & SYNCHRONIZATION
Introduction
Processes in a computer system execute programs to manipulate data. One process may or may not
interact with another process to accomplish its task. Such interactions take place only through shared
data. If two processes use two distinct sets of data items, then they are not considered interacting
processes and their activities do not affect each other. Their behaviors are independent and do not
influence each other. If a process A modifies a data item that another process B reads, then the behaviour
of B may depend on activities of A.
Here we say the two processes are interacting with one another through shared data. The
behaviour of these interacting processes then depends on two factors: the relative speeds of the processes
(what they do with shared data) and how they access the shared data. If the activities of interacting
processes are not controlled suitably, their behaviour may not be as “consistent” as expected from their
specifications.
The theme of process synchronization is the coordination of interacting processes, that is, the
orderly execution of their accesses to shared data. Unlike interprocess communication (discussed later),
process interaction is some what indirect. In many situations, a process may complete its own execution
even if other processes are absent. Processes in general are independent, but they synchronize their
relative speeds and accesses to shared data to ensure consistency of the shared data and/or system states.
Note
1. The operating system or any application execution may be considered to be a collection of
processes in which some interact and some others do not.
2. Synchronization is the single most important topic in highly concurrent systems such as operating
systems, databases, and networks.
Process
A process or task is a portion of a program in some stage of execution. A program can consist of
several tasks, each working on their own or as a unit (periodically communicating with each other).
Each process that runs in an operating system is assigned a process control block that holds
information about the process, such as a unique process ID (a number used to identify the process), the
saved state of the process, the process priority and where it is located in memory.
The process priority is used to determine how often the process receives processor time. The
operating system may run all processes with the same priority, or it may run some processes more often
than others. Processes that have been waiting a long time for execution by the processor may have their
priority increased so that they will be more likely to be executed in the future.
A process goes through a series of discrete process states. A process in a computer system may be in one
of a number of different possible states, such as
Logically, the 'Running' and 'Ready' states are similar. In both cases the process is willing to run, only in
the case of 'Ready' state, there is temporarily no CPU available for it. The 'Blocked' state is different from
the 'Running' and 'Ready' states in that the process cannot run, even if the CPU is available.
When a running process is interrupted by the processor after completing its allotted time, its state is saved
in its process control block, its process state changed to ready and its priority adjusted.
When a running process accesses an input or output device, or for some reason cannot continue, it is
interrupted by the processor, the process state and associated data is saved in the associated process
control block. The process state is changed to blocked and the priority adjusted.
When the scheduler decides the next task to run, it changes the process state of the selected process to
running and loads the saved data associated with that process back into the processor.
14
Process Operations
The two process states are process creation and process termination
Process Creation
In general-purpose systems, some way is needed to create processes as needed during operation. There are
four principal events that lead to processes creation.
System initialization.
Execution of a process Create 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 parameter. If it chooses to do so, the creating
process is called parent process and the created one is called the child process. Only one parent is needed
to create a child process. Note that unlike plants and animals that use sexual representation, a process has
only one parent. This creation of process (processes) yields a hierarchical structure of processes.
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 Exit Most processes terminates because they have done their job.
Error Exit 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.
15
Process synchronization
Modern operating systems are multi-process systems. They run many processes concurrently.
Concurrency among processes is exploited to achieve better utilization of system resources. Two
processes are said to be concurrent if their program executions overlap in real time. That is, the first
operation execution of each process starts before the last operation execution of the other process is
complete.
Process synchronization deals with various mechanisms to ensure orderly execution of cooperating
processes that share a logical address space. The basic purpose of process synchronization is to maintain
data consistency. A cooperating process is one that can affect or be affected by other processes executing
in the system.
Concurrently running processes without any sort of data protection scheme can manipulate data
and produce an incorrect result. Moreover, if the outcome of an execution depends on the particular order
in which the data access takes place, a race condition can occur. So we need to implement some sort of
synchronization of the processes.
Process 1
Process 2
Process 3
Time
(a) Multi processor case
Process 1
Process 2
Process 3
Time
(b) Uni processor case
The figure above shows two typical execution scenarios of three concurrent processes. In figure (a), three
processes are being executed in parallel (i.e., simultaneously) by three different processors, and in figure
(b), they are executed in an interleaved fashion by a single processor. The solid line fragments indicate
executions of instructions by a processor. The broken lines represent process durations. The gap between
two solid line fragments in a process indicates that the process is waiting for some condition
(multiprocessor case) and/or is preempted (uni-processor case).
Depending on the hardware platform configuration, execution of individual instructions from
processes may overlap in real time (simultaneity) or interleave. In multiprocessor systems, instruction
16
executions of many processes can proceed simultaneously in real time. In uniprocessor systems, the only
way concurrency is achieved is through interleaving of instruction executions. In either case, an
instruction execution of one process may affect those of other processes if the instruction executions
reference the same piece of shared data.
In multiprocessor systems, different processes are at different states of execution. They normally
execute programs and access data from their respective private address spaces. From time to time, they
execute system call service routines from the kernel space. The system calls occur at unpredictable times,
and they may access the same kernel data. They run at different speeds, and one process has no control
over the speed of others. Consequently, the behaviour of one process may depend on what other processes
do with the shared data, how they manipulate the data, and their relative speeds. The operating system
must ensure that processes see consistent values of shared data. The data values must satisfy prior
specified integrity constraints.
If executions of concurrent interacting processes are not controlled properly, they may store
inconsistent values in shared data and their behaviour may be difficult to predict. Process coordination
(alias, synchronization)is a fundamental problem in multiprocessor operating system design and
development.
In general, a synchronization problem is to achieve a specified coordination among a set of
concurrent processes. Processes themselves must coordinate their activities to maintain a coherent
behaviour. They, in general, are independent, and may not be aware of one another. One process becomes
aware of another by waiting on a condition that is set by the other process. That is, processes need to
communicate (directly or indirectly) information among themselves to coordinate their own activities.
(This is different from inter-process communication.) Like interprocess communications, these
communications are also done through reading- and writing shared variables.
In a nutshell, chaos ensues in the system when more than one agent(thread, process, kernel path,
processor, etc.) accesses a piece of shared data concurrently, unless such accesses are properly controlled.
Coordination or synchronization of the activities of agents is vital to maintain data consistency.
In modern high performance computers, a large number of activities (of kernel paths) proceed
concurrently in the kernel space. Synchronization problems are almost everywhere in operating systems.
Designers and developers of operating systems must understand the intricacies and complexities of these
problems before building highly concurrent operating systems.
When a number of processes are running at the same time in a system, it is essential that they be able to
talk to each other in a mannered way. Many times, a process cannot continue until another process does
some action. The structured form of communication between these processes is what's known as Inter
Process Communication (IPC).
As Tanenbaum puts it, there are three points of importance in IPC:
1. Processes must be able to pass information to one another.
2. Two or more processes must not get into each other's way when engaging in critical activities.
3. When one process depends on another, there must be proper sequencing.
As an example to the third point, if process A produces data and process B prints them, then B must wait
until A has produced data before it starts printing. Processes are programs in execution.
17
Why Do Processes Intercommunicate?
If they never did, then all input to a process would have to come from the user! There are several
reasons why IPC must be provided. Often a problem is broken into several stages, each handled by a
process (e.g compilation), so each process passes information to the next stage.
Sometimes a package is broken up into several parts (e.g. for an accounting package: inventory,
credits,debits, invoicing, payroll, etc). Each part will need to pass/obtain information to/from another part
(e.g sales affect inventory, also the case of a web server and a web browser, etc.).There are many methods
of intercommunicating information between processes.
Introduction
If you look at any process, you'll notice that it spends some time executing instructions (computing) and
then makes some I/O request, for example to read or write data to a file or to get input from a user. After
that, it executes more instructions and then, again, waits on I/O. The period of computation between I/O
requests is called the CPU burst.
18
Interactive processes spend more time performing and waiting for I/O and generally experience short
CPU bursts:
Compute-intensive processes, conversely, spend more time running instructions and less time on I/O.
They exhibit long CPU bursts:
Most interactive processes, in fact, spend the vast bulk of their existence doing nothing but waiting on
data. As I write these notes on my laptop, I have over 30 processes running just under my user account. These includes a few
browser windows, a word processor, spreadsheet, several shell windows, media player (since I’m listening to music while
preparing these notes), and various monitors and utilities. Most of the time, these processes collectively are using
less than 3% of the CPU. This is not surprising since most of these programs are waiting for user input, a
network message, or sleeping and waking up periodically to check some state.
Consider a quad-core 3.4 GHz Intel Core i7 processor. It can execute a peak rate of 13.6 billion
instructions per second (four hyper threaded cores). It can run 136 billion instructions in the ten seconds it
might take you to skim a web page before you click on a link — or 3.4 billion instructions in the quarter
second it might take you to hit the next key as you're typing. The big idea in increasing overall throughput
was the realization that we could switch the processor to running another process when a process has to
do I/O. This is multiprogramming. The next big idea was realizing that you could preempt a process and
let another process run and do this quickly enough to give the illusion that many processes are running at
the same time. This is multitasking / time slicing.
The assignment of physical processors to processes allows processors to accomplish work. The
problem of determining when processors should be assigned and to which processes is called processor
scheduling or CPU scheduling
When more than one process is runable, the operating system must decide which one first. The
part of the operating system concerned with this decision is called the scheduler, and the algorithm it uses
is called the scheduling algorithm.
Process Scheduling
Most systems have a large number of processes with short CPU bursts interspersed between I/O requests
and a small number of processes with long CPU bursts. To provide good time-sharing performance, we
may preempt a running process to let another one run. The ready list, also known as a run queue, in the
operating system keeps a list of all processes that are ready to run and not blocked on some I/O or other
system request, such as a semaphore (a variable or abstract data type that provides a simple but useful
abstraction for controlling access by multiple processes to a common resource in a parallel programming
or multi user environment.). The entries in this list are pointers to the process control block, which stores
all information and state about a process. When an I/O request for a process is complete, the process
moves from the waiting state to the ready state and gets placed on the ready list.
19
The process scheduler is the component of the operating system that is responsible for deciding whether
the currently running process should continue running and, if not, which process should run next. There
are four events that may occur where the scheduler needs to step in and make this decision:
1. The current process goes from the running to the waiting state because it issues an I/O request or
some operating system request that cannot be satisfied immediately.
2. The current process terminates.
3. A timer interrupt causes the scheduler to run and decide that a process has run for its allotted
interval of time and it is time to move it from the running to the ready state.
4. An I/O operation is complete for a process that requested it and the process now moves from the
waiting to the ready state. The scheduler may then decide to move this ready process into the
running state.
A scheduler is a preemptive scheduler if it has the ability to get invoked by an interrupt and move a
process out of a running state and let another process run. The last two events above may cause this to
happen. If a scheduler cannot take the CPU away from a process then it is a cooperative or non-
preemptive scheduler. Old operating systems such as Windows 3.1 or MacOS before OS X are examples
of such schedulers. Even older batch processing systems had run-to-completion schedulers where a
process ran to termination before any other process would be allowed to run.
The decisions that the scheduler makes concerning the sequence and length of time that processes may
run is called the scheduling algorithm (or scheduling policy). These decisions are not easy ones, as the
scheduler has only a limited amount of information about the processes that are ready to run. A good
scheduling algorithm should:
Be fair – give each process a fair share of the CPU, allow each process to run in a reasonable
amount of time.
Be efficient – keep the CPU busy all the time.
Maximize throughput – service the largest possible number of jobs in a given amount of time;
minimize the amount of time users must wait for their results.
Minimize response time – interactive users should see good performance.
Be predictable – a given job should take about the same amount of time to run when run multiple
times. This keeps users sane.
Minimize overhead – don't waste too many resources. Keep scheduling time and context switch
time at a minimum.
Maximize resource use – favor processes that will use underutilized resources. There are two
motives for this. Most devices are slow compared to CPU operations. We'll achieve better system
throughput by keeping devices busy as often as possible. Secondly, a process may be holding a
key resource and other, possibly more important, processes cannot use it until it is released
(deadlocks discussed next). Giving the process more CPU time may free up the resource quicker.
Avoid indefinite postponement – every process should get a chance to run eventually.
Enforce priorities – if the scheduler allows a process to be assigned a priority, it should be
meaningful and enforced.
Degrade gracefully – as the system becomes more heavily loaded, performance should deteriorate
gradually, not abruptly.
20
It is clear that some of these goals are contradictory. For example, minimizing overhead means that
jobs should run longer, thus hurting interactive performance. Enforcing priorities means that high-priority
processes will always be favored over low-priority ones, causing indefinite postponement. These factors
make scheduling a task for which there can be no perfect algorithm.
To make matters even more complex, the scheduler does not know much about the behavior of
each process. As indicated earlier, some processes perform a lot of input/output operations but use little
CPU time (e.g. web browsers, etc.). They spend much of their time in the blocked state in between little
bursts of computation. The overall performance of these I/O bound processes is constrained by the speed
of the I/O devices. CPU-bound processes spend most of their time computing (e.g. circuit simulators).
Their execution time is determined by the speed of the CPU and the amount of CPU time they can get.
To help the scheduler monitor processes and the amount of CPU time that they use, a
programmable interval timer interrupts the processor periodically (typically 50 or 60 times per second).
This timer is programmed when the operating system initializes itself. At each interrupt, the operating
system's scheduler gets to run and decide whether the currently running process should be allowed to
continue running or whether it should be suspended and another ready process allowed to run. This is the
mechanism used for preemptive scheduling.
Preemptive scheduling allows the scheduler to control response times by taking the CPU away
from a process that it decided has been running too long. It has more overhead than non-preemptive
scheduling since it has to deal with the overhead of context switching (computing process of storing and
restoring the state (context) of a CPU so that execution can be resumed from the same point at a later
time) processes instead of allowing a process to run to completion or until the next I/O operation or other
system call. However, it allows for higher degrees of concurrency and better interactive performance.
Scheduling Algorithms
The scheduling algorithm has the task of figuring out whether a process should be switched out for
another process and which process should get to run next. The dispatcher is the component of the
scheduler that handles the mechanism of actually getting that process to run on the processor. This
requires loading the saved context of the selected process, which is stored in the process control block and
comprises the set of registers, stack pointer, flags (status word), and a pointer to the memory mapping
(typically a pointer to the page table). Once this context is loaded, the dispatcher switches to user mode
via a return from interrupt operation that causes the process to execute from the location that was saved
on the stack at the time that the program stopped running — either via an interrupt or a system call.
Possibly the most straightforward approach to scheduling processes is to maintain a FIFO (first-in,
first-out) ready queue. New processes go to the end of the queue. When the scheduler needs to run a
process, it picks the process that is at the head of the queue. The scheduler is non-preemptive. If the
process has to block on I/O, it enters the waiting state and the scheduler picks the process from the head
of the queue. When I/O is complete and the process is ready to run again, it gets put at the end of the
queue.
21
With first-come, first-served scheduling, a process with a long CPU burst will hold up other
processes. Moreover, it can hurt overall throughput
since I/O on processes in the waiting state may
complete while the CPU bound process is still
running. Now devices are not being used
effectively. For increasing throughput, it would
have been great if the scheduler instead could have
briefly run some I/O bound process that could
request some I/O and wait. Because CPU bound
processes don't get preempted, they hurt interactive
performance because the interactive process won't
get scheduled until the CPU bound one has
completed.
Disadvantage: The greatest drawback of first-come, first-served scheduling is that it is not preemptive.
Because of this, it is not suitable for interactive jobs. Another drawback is that a long-running process will
delay all jobs behind it.
Advantage: Round robin scheduling is fair in that every process gets an equal share of the CPU. It is easy
to implement and, if we know the number of processes on the ready list, we can know the worst-case
response time for a process.
Disadvantage: Giving every process an equal share of the CPU is not always a good idea. For instance,
highly interactive processes will get scheduled no more frequently than CPU-bound processes.
22
Setting the quantum size
What should the length of a quantum be to get "good" performance? A short quantum is good because it
allows many processes to circulate through the processor quickly, each getting a brief chance to run.
Thus, highly interactive jobs that usually do not use up their quantum will not have to wait as long before
they get the CPU again, hence improving interactive performance. On the other hand, a short quantum is
bad because the operating system must perform a context switch whenever a process gets preempted. This
is overhead: anything that the CPU does other than executing user code is essentially overhead. A short
quantum implies many such context switches per unit time, taking the CPU away from performing useful
work (i.e., work on behalf of a process).
Where Q is the length of the time-slice and C is the context switch time.
An increase in Q increases efficiency but reduces average response time.
23
because their quantum expired. The algorithm sorts the ready list by the process' anticipated CPU burst
time, picking the shortest burst time. Doing so will optimize the average response time of processes.
Here's an example of five processes in the ready list. If we process them in a FIFO manner, we see
that all the CPU bursts add up to 25 (pick your favorite time unit; this is just an example). The mean run
time for a process, however, is the mean of all the run times, where the run time is the time spent waiting
to run + the CPU burst time of the process. In this example, our mean run time is (8 + 11 + 21 + 23 +
25)/5, or 17.6.
If we reorder the processes in the queue by the estimated CPU burst time, we still have the same overall
total (the processes take the same amount of time to run) but the mean run time changes. It is now (2 + 4
+ 7 + 15 + 25), or 10.6. We reduced the average run time for our processes by 40%!
7.0 DEADLOCKS
Introduction
Some I/O media such as disks are easily sharable. However, some I/O media such as tape drives or
printers cannot. So they have to be allocated for one process exclusively.
Computer processing is full of resources that can only be used by one process at a time.
Unpredictable results can occur if two or more processes use the same resource at the same time. The OS
control this problem by arbitrating resource requests by granting exclusive access and managing access to
system resources. By allowing processes to have exclusive access to resources, problems may occur: –
Deadlock.
24
Whenever two or more processes try to access a resource that is in use by some other process then
it is said to be a deadlock situation. A deadlock is a situation in which two or more competing actions are
each waiting for the other to finish, and thus neither ever does. Deadlock causes all processes involved in
the deadlock to become permanently blocked. Each process is waiting on resources owned by other
processes that are also deadlocked. Deadlocks occur when processes have been granted exclusive access
to resources.
A set of process is in a deadlock state if each process in the set is waiting for an event that can be
caused by only another process in the set (including itself). In other words, each member of the set of
deadlock processes is waiting for a resource that can be released only by a deadlock process. None of the
processes can run, none of them can release any resources, and none of them can be awakened. It is
important to note that the number of processes and the number and kind of resources possessed and
requested are unimportant.
The resources may be either physical or logical. Examples of physical resources are Printers, Tape
Drives, Memory Space, and CPU Cycles. Examples of logical resources are Files, processes, etc.
Deadlock defined
Assume two processes p1 and p2 running simultaneously. Half way through, p1 requests the
operating system for a file in the tape drive, and p2 requests the operating system for the printer. Assume
also that both requests are granted. After a while, p1 requests for the printer without giving the tape drive.
Simultaneously p2 requests the same tape drive without giving up control of the printer. In this situation,
both processes cannot proceed. P1 will wait until p2 releases the printer. But that can happen only if p2
can proceed further and finish off its processing with the printer. And this can happen only if p2 gets the
same tape drive that p1 is holding. P2 can get the tape drive only if p1 can proceed further and completes
its work with the tape drive. That cannot happen too, unless p1 gets the printer which p2 is holding. This
situation is “deadlock”.
25
Necessary and Sufficient Deadlock Conditions
Coffman identified four (4) conditions that must hold simultaneously for there to be a deadlock.
3. No-Preemptive Condition
Resources already allocated to a process cannot be preempted. i.e. Resources cannot be removed
from the processes are used to completion or released voluntarily by the process holding it.
1. Mutual exclusion condition applies, since only one vehicle can be on a section of the street at a
time.
2. Hold-and-wait condition applies, since each vehicle is occupying a section of the street, and
waiting to move on to the next section of the street.
3. No-preemptive condition applies, since a section of the street that is occupied by a vehicle cannot
be taken away from it.
4. Circular wait condition applies, since each vehicle is waiting on the next vehicle to move. That
is, each vehicle in the traffic is waiting for a section of street held by the next vehicle in the traffic.
26
The simple rule to avoid traffic deadlock is that a vehicle should only enter an intersection if it is assured
that it will not have to stop inside the intersection.
It is not possible to have a deadlock involving only one single process. The deadlock involves a circular
“hold-and-wait” condition between two or more processes, so “one” process cannot hold a resource, yet
be waiting for another resource that it is holding.
Deadlock Prevention
Havender in his pioneering work showed that since all four of the conditions are necessary for deadlock to
occur, it follows that deadlock might be prevented by denying any one of the conditions.
27
Elimination of “Circular Wait” Condition
The last condition, the circular wait, can be denied by imposing a total ordering on all of the resource
types and then forcing, all processes to request the resources in (increasing or decreasing)order. This
strategy impose a total ordering of all resources types, and to require that each process requests resources
in a numerical order (increasing or decreasing) of enumeration. With this rule, the resource allocation
graph can never have a cycle.
For example, provide a global numbering of all the resources, as shown
1 ≡ Card reader
2 ≡ Printer
3 ≡ Plotter
4 ≡ Tape drive
5 ≡ Card punch
Now the rule is this: processes can request resources whenever they want to, but all requests must be
made in numerical order. A process may request first printer and then a tape drive (order: 2, 4), but it may
not request first a plotter and then a printer (order: 3, 2).
Deadlock Avoidance
This approach to the deadlock problem anticipates deadlock before it actually occurs. This approach
employs an algorithm to access the possibility that deadlock could occur and acting accordingly. This
method differs from deadlock prevention, which guarantees that deadlock cannot occur by denying one of
the necessary conditions of deadlock.
If the necessary conditions for a deadlock are in place, it is still possible to avoid deadlock by
being careful when resources are allocated. Perhaps the most famous deadlock avoidance algorithm, is the
Banker’s algorithm. So named because the process is analogous to that used by a banker in deciding if a
loan can be safely made.
Deadlock Detection
Deadlock detection is the process of actually determining that a deadlock exists and identifying the
processes and resources involved in the deadlock. The basic idea is to check allocation against resource
availability for all possible allocation sequences to determine if the system is in deadlocked state. Of
course, the deadlock detection algorithm is only half of this strategy. Once a deadlock is detected, there
needs to be a way to recover and several alternatives exists:
Temporarily prevent resources from deadlocked processes.
Back off a process to some check point allowing preemption of a needed resource and restarting
the process at the checkpoint later.
Successively kill processes until the system is deadlock free.
These methods are expensive in the sense that each iteration calls the detection algorithm until the system
proves to be deadlock free.
28