Operating System Complete Note 1
Operating System Complete Note 1
Introduction
An Operating System (OS) is an interface between computer user and computer hardware. An
operating system is software which performs all the basic tasks like file management, memory
management, process management, handling input and output, and controlling peripheral devices such
as disk drives and printers.
Some popular Operating Systems include Linux Operating System, Windows Operating System, VMS,
OS/400, AIX, z/OS, etc.
1
2. The Operating System as a Resource Manager
The main important objective of an operating system is to manage the various resources of the
computer system. This involves performing such tasks as keeping track of who is using which
resources, granting resource requests, accounting for resource usage, and mediating conflicting
requests from different programs and users.
Executing a job on a computer system often requires several of its resource such as CPU time,
memory space, file storage space, I/O devices and so on. The operating system acts as the manager
of the various resources of a computer system and allocates them to specific programs and users
to execute their jobs successfully. When a computer system is used to simultaneously handle
several applications, there may be many, possibly conflicting, requests for resources. In such a
situation, the operating system must decide which requests are allocated resources to operate the
computer system efficiently and fairly (providing due attention to all users). The efficient and fair
sharing of resources among users and/or programs is a key goal of most operating system.
2
The First Generation (1945-55) Vacuum Tubes and Plug boards
After Babbage’s unsuccessful efforts, little progress was made in constructing digital computers
until World War II. Around the mid-1940s, Howard Aiken at Harvard, John von Neumann at the
Institute for Advanced Study in Princeton, J. Presper Eckert and William Mauchley at the
University of Pennsylvania, and Konrad Zuse in Germany, among others, all succeeded in building
calculating engines. The first ones used mechanical relays but were very slow, with cycle times
measured in seconds. Relays were later replaced by vacuum tubes. These machines were
enormous, filling up entire rooms with tens of thousands of vacuum tubes, but they were still
millions of times slower than even the cheapest personal computers available today.
In these early days, a single group of people designed, built, programmed, operated, and maintained
each machine. All programming was done in absolute machine language, often by wiring up plug
boards to control the machine’s basic functions. Programming languages were unknown (even
assembly language was unknown). Operating systems were unheard of. The usual made of
operation was for the programmer to sign up for a block of time on the signup sheet on the wall,
then come down to the machine room, insert his or her plug board into the computer, and spend
the next few hours hoping that none of the 20,000 or so vacuum tubes would burn out during the
run. Virtually all the problems were straightforward numerical calculations, such as grinding out
tables of sines, cosines, and logarithms.
By the early 1950s, the routine had improved somewhat with the introduction of punched cards. It
was now possible to write programs on cards and read them in instead of using plug boards;
otherwise, the procedure was the same.
These machines, now called mainframes, were locked away in specially air conditioned computer
rooms, with staffs of professional operators to run them. Only big corporations or major
government agencies or universities could afford the multimillion dollar price tag. To run a job
(i.e., a program or set of programs), a programmer would first write the program on paper (in
FORTRAN or assembler), then punch it on cards. He would then bring the card deck down to the
input room and hand it to one of the operators and go drink coffee until the output was ready.
When the computer finished whatever job it was currently running, an operator would go over to
the printer and tear off the output and carry it over to the output room, so that the programmer
could collect it later. Then he would take one of the card decks that had been brought from the
input room and read it in. If the FORTRAN compiler was needed, the operator would have to get
it from a file cabinet and read it in. Much computer time was wasted while operators were walking
around the machine room.
3
The solution generally adopted was the batch system. The idea behind it was to collect a tray
full of jobs in the input room and then read them onto a magnetic tape using a small (relatively)
inexpensive computer, such as the IBM 1401, which was very good at reading cards, copying
tapes, and printing output, but not at all good at numerical calculations. Other, much more
expensive machines, such as the IBM 7094, were used for the real computing.
The greatest strength of the “one family” idea was simultaneously its greatest weakness. The
intention was that all software, including the operating system, OS/360 had to work on all
models. It had to run on small systems, which often just replaced 1401s for copying cards to
tape, and on very large systems, which often replaced 7094s for doing weather forecasting and
other heavy computing. It had to be good on systems with few peripherals and on systems with
many peripherals. It had to work in commercial environments and in scientific environments.
Above all, it had to be efficient for all of these different uses.
Despite its enormous size and problems, OS/360 and the similar third-generation operating systems
produced by other computer manufacturers actually satisfied most of their customers reasonably
well. They also popularized several key techniques absent in second-
4
generation operating systems. Probably the most important of these was multiprogramming. On
the 7094, when the current job paused to wait for a tape or other I/O operation to complete, the
CPU simply sat idle until the I/O finished. With heavily CPU-bound scientific calculations, I/O is
infrequent, so this wasted time is not significant. With commercial data processing, the I/O wait
time can often be 80 or 90 percent of the total time, so something had to be done to avoid having
the (expensive) CPU be idle so much.
5
CP/M, MS-DOS, and other operating systems for early microcomputers were all based on users
typing in commands from the keyboard. That eventually changed due to research done by Doug
Engelbart at Stanford Research Institute in the 1960s. Engelbart invented the GUI (Graphical
User Interface), pronounced “gooey,” complete with windows, icons, menus, and mouse. These
ideas were adopted by researchers at Xerox PARC and incorporated into machines they built.
One day, Steve Jobs, who co-invented the Apple computer in his garage, visited PARC, saw a
GUI, and instantly realized its potential value; something Xerox management famously did not
(Smith and Alexander, 1988). Jobs then embarked on building an Apple with a GUI. This project
led to the Lisa, which was too expensive and failed commercially. Jobs’ second attempt, the Apple
Macintosh, was a huge success, not only because it was much cheaper than the Lisa, but also
because it was user friendly, meaning that it was intended for users who not only knew nothing
about computers but furthermore had absolutely no intention whatsoever of learning.
Another Microsoft operating system is Windows NT (NT stands for New Technology), which is
compatible with Windows 95 at a certain level, but a complete rewrite from scratch internally. It
is a full 32-bit system.
An interesting development that began taking place during the mid-1980s is the growth of
networks of personal computers running network operating systems and distributed operating
systems. In a network operating system, 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. Each machine runs
its own local operating system and has its own local user (or users).
The Fifth Generation (1990–Present): Mobile Computers
Ever since detective Dick Tracy started talking to his ‘‘two-way radio wrist watch’’ in the 1940s
comic strip, people have craved a communication device they could carry around wherever they
went. The first real mobile phone appeared in 1946 and weighed some 40 kilos. You could take it
wherever you went as long as you had a car in which to carry it.
The first true handheld phone appeared in the 1970s and, at roughly one kilogram, was positively
featherweight. It was affectionately known as ‘‘the brick.’’ Pretty soon everybody wanted one.
Today, mobile phone penetration is close to 90% of the global population. We can make calls not
just with our portable phones and wrist watches, but soon with eyeglasses and other wearable
items. Moreover, the phone part is no longer that interesting. We receive email, surf the Web, text
our friends, play games, navigate around heavy traffic—and do not even think twice about it.
While the idea of combining telephony and computing in a phone-like device has been around
since the 1970s also, the first real smartphone did not appear until the mid-1990s when Nokia
released the N9000, which literally combined two, mostly separate devices: a phone and a PDA
(Personal Digital Assistant). In 1997, Ericsson coined the term smartphone for its GS88
‘‘Penelope.’’
Now that smartphones have become ubiquitous, the competition between the various operating
systems is fierce and the outcome is even less clear than in the PC world. At the time of writing,
Google’s Android is the dominant operating system with Apple’s iOS a clear second, but this was
not always the case and all may be different again in just a few years. If anything is clear in the
world of smartphones, it is that it is not easy to stay king of the mountain for long.
After all, most smartphones in the first decade after their inception were running Symbian OS. It
was the operating system of choice for popular brands like Samsung,
6
Sony Ericsson, Motorola, and especially Nokia. However, other operating systems like RIM’s
Blackberry OS (introduced for smartphones in 2002) and Apple’s iOS (released for the first iPhone
in 2007) started eating into Symbian’s market share. Many expected that RIM would dominate the
business market, while iOS would be the king of the consumer devices. Symbian’s market share
plummeted. In 2011, Nokia ditched Symbian and announced it would focus on Windows Phone
as its primary platform.
For some time, Apple and RIM were the toast of the town (although not nearly as dominant as
Symbian had been), but it did not take very long for Android, a Linux-based operating system
released by Google in 2008, to overtake all its rivals.
For phone manufacturers, Android had the advantage that it was open source and available under
a permissive license. As a result, they could tinker with it and adapt it to their own hardware with
ease. Also, it has a huge community of developers writing apps, mostly in the familiar Java
programming language. Even so, the past years have shown that the dominance may not last, and
Android’s competitors are eager to claw back some of its market share.
At the high end are the operating systems for mainframes, those room-sized computers still found
in major corporate data centers. These computers differ from personal computers in terms of their
I/O capacity. A mainframe with 1000 disks and millions of gigabytes of data is not unusual; a
personal computer with these specifications would be the envy of its friends. Mainframes are also
making something of a comeback as high-end Web servers, servers for large-scale electronic
commerce sites, and servers for business-to-business transactions.
The operating systems for mainframes are heavily oriented toward processing many jobs at once,
most of which need prodigious amounts of I/O. They typically offer three kinds of services: batch,
transaction processing, and timesharing. A batch system is one that processes routine jobs without
any interactive user present. Claims processing in an insurance company or sales reporting for a
chain of stores is typically done in batch mode. Transaction-processing systems handle large
numbers of small requests, for example, check processing at a bank or airline reservations. Each
unit of work is small, but the system must handle hundreds or thousands per second. Timesharing
systems allow multiple remote users to run jobs on the computer at once, such as querying a big
database. These functions are closely related; mainframe operating systems often perform all of
them. An example mainframe operating system is OS/390, a descendant of OS/360. However,
mainframe operating systems are gradually being replaced by UNIX variants such as Linux.
One level down are the server operating systems. They run on servers, which are either very large
personal computers, workstations, or even mainframes. They serve multiple users at once over a
network and allow the users to share hardware and software resources. Servers can provide print
service, file service, or Web service. Internet providers run many server machines to support their
customers and Websites use servers to store the Web pages and handle the incoming requests.
Typical server operating systems are Solaris, FreeBSD, Linux and Windows Server 201x.
7
3. Multiprocessor Operating Systems
An increasingly common way to get major-league computing power is to connect multiple CPUs
into a single system. Depending on precisely how they are connected and what is shared, these
systems are called parallel computers, multicomputers, or multiprocessors. They need special
operating systems, but often these are variations on the server operating systems, with special
features for communication, connectivity, and consistency.
With the recent advent of multicore chips for personal computers, even conventional desktop and
notebook operating systems are starting to deal with at least small-scale multiprocessors and the
number of cores is likely to grow over time. Luckily, quite a bit is known about multiprocessor
operating systems from years of previous research, so using this knowledge in multicore systems
should not be hard. The hard part will be having applications make use of all this computing power.
Many popular operating systems, including Windows and Linux, run on multiprocessors.
The next category is the personal computer operating system. Modern ones all support
multiprogramming, often with dozens of programs started up at boot time. Their job is to provide
good support to a single user. They are widely used for word processing, spreadsheets, games, and
Internet access. Common examples are Linux, FreeBSD, Windows 7, Windows 8, and Apple’s OS
X. Personal computer operating systems are so widely known that probably little introduction is
needed. In fact, many people are not even aware that other kinds exist.
Continuing on down to smaller and smaller systems, we come to tablets, smartphones and other
handheld computers. A handheld computer, originally known as a PDA (Personal Digital
Assistant), is a small computer that can be held in your hand during operation. Smartphones and
tablets are the best-known examples. As we have already seen, this market is currently dominated
by Google’s Android and Apple’s iOS, but they have many competitors. Most of these devices
boast multicore CPUs, GPS, cameras and other sensors, copious amounts of memory, and
sophisticated operating systems. Moreover, all of them have more third-party applications
(‘‘apps’’) than you can shake a (USB) stick at.
6. Embedded Operating Systems
Embedded systems run on the computers that control devices that are not generally thought of as
computers and which do not accept user-installed software. Typical examples are microwave
ovens, TV sets, cars, DVD recorders, traditional phones, and MP3 players. The main property
which distinguishes embedded systems from handhelds is the certainty that no untrusted software
will ever run on it. You cannot download new applications to your microwave oven—all the
software is in ROM. This means that there is no need for protection between applications, leading
to design simplification. Systems such as Embedded Linux, QNX and VxWorks are popular in this
domain.
Networks of tiny sensor nodes are being deployed for numerous purposes. These nodes are tiny
computers that communicate with each other and with a base station
8
using wireless communication. Sensor networks are used to protect the perimeters of buildings,
guard national borders, detect fires in forests, measure temperature and precipitation for weather
forecasting, glean information about enemy movements on battlefields, and much more.
The sensors are small battery-powered computers with built-in radios. They have limited power
and must work for long periods of time unattended outdoors, frequently in environmentally harsh
conditions. The network must be robust enough to tolerate failures of individual nodes, which
happen with ever-increasing frequency as the batteries begin to run down.
Each sensor node is a real computer, with a CPU, RAM, ROM, and one or more environmental
sensors. It runs a small, but real operating system, usually one that is event driven, responding to
external events or making measurements periodically based on an internal clock. The operating
system has to be small and simple because the nodes have little RAM and battery lifetime is a
major issue. Also, as with embedded systems, all the programs are loaded in advance; users do not
suddenly start programs they downloaded from the Internet, which makes the design much simpler.
TinyOS is a well-known operating system for a sensor node.
Another type of operating system is the real-time system. These systems are characterized by
having time as a key parameter. For example, in industrial process-control systems, real-time
computers have to collect data about the production process and use it to control machines in the
factory. Often there are hard deadlines that must be met. For example, if a car is moving down an
assembly line, certain actions must take place at certain instants of time. If, for example, a welding
robot welds too early or too late, the car will be ruined. If the action absolutely must occur at a
certain moment (or within a certain range), we have a hard real-time system. Many of these are
found in industrial process control, avionics, military, and similar application areas. These systems
must provide absolute guarantees that a certain action will occur by a certain time.
A soft real-time system, is one where missing an occasional deadline, while not desirable, is
acceptable and does not cause any permanent damage. Digital audio or multimedia systems fall in
this category. Smartphones are also soft realtime systems.
Since meeting deadlines is crucial in (hard) real-time systems, sometimes the operating system is
simply a library linked in with the application programs, with ev erything tightly coupled and no
protection between parts of the system. An example of this type of real-time system is eCos.
The categories of handhelds, embedded systems, and real-time systems overlap considerably.
Nearly all of them have at least some soft real-time aspects. The embedded and real-time systems
run only software put in by the system designers; users cannot add their own software, which
makes protection easier. The handhelds and embedded systems are intended for consumers,
whereas realtime systems are more for industrial usage. Nevertheless, they hav e a certain amount
in common.
The smallest operating systems run on smart cards, which are credit-card-sized devices containing
a CPU chip. They hav e very severe processing power and memory constraints. Some are powered
by contacts in the reader into which they are inserted, but contactless smart cards are inductively
powered, which greatly limits what they can do. Some of them can
9
handle only a single function, such as electronic payments, but others can handle multiple
functions. Often these are proprietary systems.
Some smart cards are Java oriented. This means that the ROM on the smart card holds an
interpreter for the Java Virtual Machine (JVM). Java applets (small programs) are downloaded to
the card and are interpreted by the JVM interpreter. Some of these cards can handle multiple Java
applets at the same time, leading to multiprogramming and the need to schedule them. Resource
management and protection also become an issue when two or more applets are present at the same
time. These issues must be handled by the (usually extremely primitive) operating system present
on the card.
Functions of Operating System
Following are some of important functions of an operating System.
1. Memory Management
2. Processor Management
3. Device Management
4. File Management
5. Security
6. Control over system performance
7. Job accounting
8. Error detecting aids
9. Coordination between other software and users
1. Memory Management
Memory management refers to management of Primary Memory or Main Memory. Main memory
is a large array of words or bytes where each word or byte has its own address.
Main memory provides a fast storage that can be accessed directly by the CPU. For a program to
be executed, it must in the main memory. An Operating System does the following activities for
memory management −
• Keeps tracks of primary memory, i.e., what part of it are in use by whom, what part are not
in use.
• In multiprogramming, the OS decides which process will get memory when and how much.
• Allocates the memory when a process requests it to do so.
• De-allocates the memory when a process no longer needs it or has been terminated.
2. Processor Management
In multiprogramming environment, the OS decides which process gets the processor when and for
how much time. This function is called process scheduling. An Operating System does the
following activities for processor management −
• Keeps tracks of processor and status of process. The program responsible for this task is
known as traffic controller.
1
0
• Allocates the processor (CPU) to a process.
• De-allocates processor when a process is no longer required.
3. Device Management
An Operating System manages device communication via their respective drivers. It does the
following activities for device management −
• Keeps tracks of all devices. Program responsible for this task is known as the I/O
controller.
• Decides which process gets the device when and for how much time.
• Allocates the device in the efficient way.
• De-allocates devices.
4. File Management
A file system is normally organized into directories for easy navigation and usage. These
directories may contain files and other directions.
An Operating System does the following activities for file management −
• Keeps track of information, location, uses, status etc. The collective facilities are often
known as file system.
• Decides who gets the resources.
• Allocates the resources.
• De-allocates the resources.
5. Security
The operating system uses password protection to protect user data and similar other techniques.
it also prevents unauthorized access to programs and user data.
7. Job accounting
Operating system Keeps track of time and resources used by various tasks and users, this
information can be used to track resource usage for a particular user or group of user.
1
1
Operating systems also coordinate and assign interpreters, compilers, assemblers and other
software to the various users of the computer systems.
Service
procedures
Utility
procedures
It provides minimal services of process and memory management. The communication between
client program/application and services running in user address space is established through
message passing, reducing the speed of execution microkernel. The Operating System remains
unaffected as user services and kernel services are isolated so if any user service fails it does not
affect kernel service. Thus, it adds to one of the advantages in a microkernel. It is easily extendable
i.e. if any new services are to be added they are added to user address space and hence requires no
modification in kernel space. It is also portable, secure and reliable.
Microkernel is solely responsible for the most important services of operating system they are
named as follows:
• Inter process-Communication
• Memory Management
• CPU-Scheduling Advantages:
1
4
•Since we are using User Space and Kernel Space separately, so the communication between
these can reduce the overall execution time. Exokernel
Rather than cloning the actual machine, as is done with virtual machines, another strategy is
partitioning it, in other words, giving each user a subset of the resources. Thus one virtual machine
might get disk blocks 0 to 1023, the next one might get blocks 1024 to 2047, and so on.
At the bottom layer, running in kernel mode, is a program called the exokernel (Engler et al., 1995).
Its job is to allocate resources to virtual machines and then check attempts to use them to make
sure no machine is trying to use somebody else’s resources. Each user-level virtual machine can
run its own operating system, as on VM/370 and the Pentium virtual 8086s, except that each one
is restricted to using only the resources it has asked for and been allocated.
The advantage of the exokernel scheme is that it saves a layer of mapping. In the other designs,
each virtual machine thinks it has its own disk, with blocks running from 0 to some maximum, so
the virtual machine monitor must maintain tables to remap disk addresses (and all other resources).
With the exokernel, this remapping is not needed. The exokernel need only keep track of which
virtual machine has been assigned which resource. This method still has the advantage of
separating the multiprogramming (in the exokernel) from the user operating system code (in user
space), but with less overhead, since all the exokernel has to do is keep the virtual machines out of
each other’s hair.
Nanokernel
In a Nanokrnel, as the name suggests, the whole code of the kernel is very small i.e. the code
executing in the privileged mode of the hardware is very small. The term nanokernel is used to
describe a kernel that supports a nanosecond clock resolution.
Advantages:
Layered System
The system had six layers, as shown in Figure. Layer 0 dealt with allocation of the processor,
switching between processes when interrupts occurred or timers expired. Above layer 0, the system
consisted of sequential processes, each of which could be programmed without having to worry
about the fact that multiple processes were running on a single processor. In other words, layer 0
provided the basic multiprogramming of the CPU.
Layer Function
5 The operator
4 User programs
3 Input/output management
2 Operator-process communication
1
5
1 Memory and drum management
Client-Server Model
A slight variation of the microkernel idea is to distinguish two classes of processes, the servers,
each of which provides some service, and the clients, which use these services. This model is
known as the client-server model. Often the lowest layer is a microkernel, but that is not required.
The essence is the presence of client processes and server processes.
Communication between clients and servers is often by message passing. To obtain a service, a
client process constructs a message saying what it wants and sends it to the appropriate service.
The service then does the work and sends back the answer. If the client and server happen to run
on the same machine, certain optimizations are possible, but conceptually, we are still talking about
message passing here.
An obvious generalization of this idea is to have the clients and servers run on different computers,
connected by a local or wide-area network, as depicted in Figure. Since clients communicate with
servers by sending messages, the clients need not know whether the messages are handled locally
on their own machines, or whether they are sent across a network to servers on a remote machine.
As far as the client is concerned, the same thing happens in both cases: requests are sent and replies
come back. Thus the client-server model is an abstraction that can be used for a single machine or
for a network of machines.
Machine 1 Machine 2 Machine 3 Machine 4
Network
Message from
client to server
Virtual Machines
The initial releases of OS/360 were strictly batch systems. Nevertheless, many 360 users wanted
to be able to work interactively at a terminal, so various groups, both inside and outside IBM,
decided to write timesharing systems for it. The official IBM timesharing system, TSS/360, was
delivered late, and when it finally arrived it was so big and slow that few sites converted to it.
VM/370 was based on a smart observation: a timesharing system provides (1) multiprogramming
and (2) an extended machine with a more convenient interface than the bare hardware. The essence
of VM/370 is to completely separate these two functions.
The heart of the system, known as the virtual machine monitor, runs on the bare hardware and
does the multiprogramming, providing not one, but several virtual machines to the next layer up,
as shown in Fig. 1-28. However, unlike all other operating systems, these virtual machines are not
extended machines, with files and other nice features. Instead, they are exact copies of the bare
hardware, including kernel/user mode, I/O, interrupts, and everything else the real machine has.
The Shell
The operating system is the code that carries out the system calls. Editors, compilers, assemblers,
linkers, utility programs, and command interpreters definitely are
1
7
not part of the operating system, even though they are important and useful. At the risk of confusing
things somewhat, in this section we will look briefly at the UNIX command interpreter, the shell.
Although it is not part of the operating system, it makes heavy use of many operating system
features and thus serves as a good example of how the system calls are used. It is also the main
interface between a user sitting at his terminal and the operating system, unless the user is using a
graphical user interface. Many shells exist, including sh, csh, ksh, and bash. All of them support
the functionality described below, which derives from the original shell (sh).
When any user logs in, a shell is started up. The shell has the terminal as standard input and
standard output. It starts out by typing the prompt, a character such as a dollar sign, which tells
the user that the shell is waiting to accept a command. If the user now types date for example, the
shell creates a child process and runs the date program as the child. While the child process is
running, the shell waits for it to terminate. When the child finishes, the shell types the prompt again
and tries to read the next input line.
The user can specify that standard output be redirected to a file, for example, date >file
Similarly, standard input can be redirected, as in sor t <file1 >file2 which invokes the
sort program with input taken from file1 and output sent to file2.
The output of one program can be used as the input for another program by connecting them with
a pipe. Thus cat file1 file2 file3 | sort >/dev/lp invokes the cat program to concatenate three files and
send the output to sort to arrange all the lines in alphabetical order. The output of sort is redirected
to the file /dev/lp, typically the printer.
If a user puts an ampersand after a command, the shell does not wait for it to complete. Instead it
just gives a prompt immediately. Consequently, cat file1 file2 file3 | sort >/dev/lp & starts up the sort
as a background job, allowing the user to continue working normally while the sort is going on.
The shell has a number of other interesting features, which we do not have space to discuss here.
Most books on UNIX discuss the shell at some length (e.g., Kernighan and Pike, 1984; Quigley,
2004; Robbins, 2005).
Most personal computers these days use a GUI. In fact, the GUI is just a program running on top
of the operating system, like a shell. In Linux systems, this fact is made obvious because the user
has a choice of (at least) two GUIs: Gnome and KDE or none at all (using a terminal window on
X11). In Windows, it is also possible to replace the standard GUI desktop (Windows Explorer)
with a different program by changing some values in the registry, although few people do this.
1
8
Unit-3 Process Management
Process
A process is basically a program in execution. The execution of a process must progress in a
sequential fashion. For example, when we write a program in C or C++ and compile it, the compiler
creates binary code. The original code and binary code are both programs. When we actually run
the binary code, it becomes a process.
When a program is loaded into the memory and it becomes a process, it can be divided into four
sections ─ stack, heap, text and data.
Stack
The process Stack contains the temporary data such as method/function parameters, return address
and local variables.
Heap
This is dynamically allocated memory to a process during its run time.
Text
This includes the current activity represented by the value of Program Counter and the contents of
the processor's registers.
1
9
Data
This section contains the global and static variables.
Fig: (a) Multiprogramming of four programs. (b) Conceptual model of four independent, sequential
processes. (c) Only one program is active at once.
With the CPU switching back and forth among the processes, the rate at which a process performs
its computation will not be uniform and probably not even reproducible if the same processes are
run again. Thus, processes must not be programmed with built-in assumptions about timing. The
difference between a process and a program is subtle, but crucial. An analogy makes help here:
Consider a culinary-minded computer scientist who is baking a birthday cake for his daughter. He has
a birthday cake recipe and a kitchen well stocked with all the input: flour, eggs, sugar, extract of vanilla,
and so on. In this analogy, the recipe is the program (i.e., an algorithm expressed in some suitable
notation), the computer scientist is the processor (CPU), and the cake ingredients are the input data.
The process is the activity consisting of our baker reading the recipe, fetching the ingredients, and
baking the cake.
Now imagine that the computer scientist’s son comes running in crying, saying that he has been stung
by a bee. The computer scientist records where he was in the recipe (the state of the current process is
saved), gets out a first aid book, and begins following the directions in it. Here we see the processor
being switched from one process (baking) to a higher-priority process (administering medical care),
each having a different program (recipe versus first aid book). When the bee sting has been taken care
of, the computer scientist goes back to his cake, continuing at the point where he left off.
2
0
The key idea here is that a process is an activity of some kind. It has a program, input, output, and
a state. A single processor may be shared among several processes, with some scheduling
algorithm being used to determine when to stop work on one process and service a different one.
Logically, the first two states are similar. In both cases the process is willing to run, only in the
second one, there is temporarily no CPU available for it. The third state is fundamentally different
from the first two in that the process cannot run, even if the CPU is idle and has nothing else to do.
Four transitions are possible among these three states, as shown. Transition 1 occurs when the
operating system discovers that a process cannot continue right now. In some systems the process
can execute a system call, such as pause, to get into blocked state. In other systems, including
UNIX, when a process reads from a pipe or special file (e.g., a terminal) and there is no input
available, the process is automatically blocked.
2
1
Transitions 2 and 3 are caused by the process scheduler, a part of the operating system, without the
process even knowing about them. Transition 2 occurs when the scheduler decides that the
running process has run long enough, and it is time to let another process have some CPU time.
Transition 3 occurs when all the other processes have had their fair share and it is time for the
first process to get the CPU to run again. The subject of scheduling, that is, deciding which process
should run when and for how long, is an important one; we will look at it later in this chapter.
Many algorithms have been devised to try to balance the competing demands of efficiency for the
system as a whole and fairness to individual processes.
Transition 4 occurs when the external event for which a process was waiting (such as the arrival
of some input) happens. If no other process is running at that instant, transition 3 will be triggered
and the process will start running. Otherwise it may have to wait in ready state for a little while
until the CPU is available and its turn comes.
2
2
This contains the address of the next instruction that needs to be executed in the process.
Registers
This specifies the registers that are used by the process. They may include accumulators, index
registers, stack pointers, general purpose registers etc.
1. Creation: This the initial step of process execution activity. Process creation means the
construction of a new process for the execution. This might be performed by system, user or
old process itself. There are several events that leads to the process creation. Some of the such
events are following:
3. Blocking: When a process invokes an input-output system call that blocks the process and
operating system put in block mode. Block mode is basically a mode where process waits for
inputoutput. Hence on the demand of process itself, operating system blocks the process and
dispatches another process to the processor. Hence, in process blocking operation, the
operating system puts the process in ‘waiting’ state.
4. Preemption: When a timeout occurs that means the process hadn’t been terminated in the
allotted time interval and next process is ready to execute, then the operating system preempts
the process. This operation is only valid where CPU scheduling supports preemption. Basically
this happens in priority scheduling where on the incoming of high priority process the ongoing
process is preempted. Hence, in process preemption operation, the operating system puts the
process in ‘ready’ state.
5. Termination: Process termination is the activity of ending the process. In other words, process
termination is the relaxation of computer resources taken by the process for the execution. Like
creation, in termination also there may be several events that may lead to the process
termination.
Some of them are:
• Process completes its execution fully and it indicates to the OS that it has finished.
• Operating system itself terminates the process due to service errors.
• There may be problem in hardware that terminates the process. One process can be
terminated by another process.
Process Hierarchies
In some systems, when a process creates another process, the parent process and child process
continue to be associated in certain ways. The child process can itself create more processes,
forming a process hierarchy. Note that unlike plants and animals that use sexual reproduction, a
process has only one parent (but zero, one, two, or more children). So a process is more like a
hydra than like, say, a cow.
In UNIX, a process and all of its children and further descendants together form a process
group. When a user sends a signal from the keyboard, the signal is delivered to all members of the
process group currently associated with the keyboard (usually all active processes that were created
in the current window). Individually, each process can catch the signal, ignore the signal, or take
the default action, which is to be killed by the signal.
As another example of where the process hierarchy plays a key role, let us look at how UNIX
initializes itself when it is started, just after the computer is booted. A special process, called init,
is present in the boot image. When it starts running, it reads a file telling how many terminals there
are. Then it divides off a new process per terminal. These processes wait for someone to log in. If
a login is successful, the login process executes a shell to accept commands. These commands may
2
4
start up more processes, and so forth. Thus, all the processes in the whole system belong to a single
tree, with init at the root.
In contrast, Windows has no concept of a process hierarchy. All processes are equal. The only
hint of a process hierarchy is that when a process is created, the parent is given a special token
(called a handle) that it can use to control the child. However, it is free to pass this token to some
other process, thus invalidating the hierarchy. Processes in UNIX cannot disinherit their children.
Implementation on Process
To implement the process model, the operating system maintains a table (an array of structures),
called the process table, with one entry per process. (Some authors call these entries process
control blocks.) This entry contains important information about the process’ state, including its
program counter, stack pointer, memory allocation, the status of its open files, its accounting and
scheduling information, and everything else about the process that must be saved when the process
is switched from running to ready or blocked state so that it can be restarted later as if it had never
been stopped.
Figure below shows some of the key fields in a typical system. The fields in the first column relate
to process management. The other two relate to memory management and file management,
respectively. It should be noted that precisely which fields the process table has is highly system
dependent, but this figure gives a general idea of the kinds of information needed.
Now that we have looked at the process table, it is possible to explain a little more about how the
illusion of multiple sequential processes is maintained on one (or each) CPU. Associated with
each I/O class is a location (typically at a fixed location near the bottom of memory) called the
interrupt vector. It contains the address of the interrupt service procedure. Suppose that user
process 3 is running when a disk interrupt happens. User process 3’s program counter, program
status word, and sometimes one or more registers are pushed onto the (current) stack by the
interrupt hardware. The computer then jumps to the address specified in the interrupt vector.
That is all the hardware does. From here on, it is up to the software, in particular, the interrupt
service procedure.
All interrupts start by saving the registers, often in the process table entry for the current process.
Then the information pushed onto the stack by the interrupt is removed and the stack pointer is set
to point to a temporary stack used by the process handler. Actions such as saving the registers and
setting the stack pointer cannot even be expressed in high-level languages such as C, so they are
performed by a small assembly-language routine, usually the same one for all interrupts since the
work of saving the registers is identical, no matter what the cause of the interrupt is.
When this routine is finished, it calls a C procedure to do the rest of the work for this specific
interrupt type. (We assume the operating system is written in C, the usual choice for all real
operating systems.) When it has done its job, possibly making some process now ready, the
scheduler is called to see who to run next. After that, control is passed back to the
assemblylanguage code to load up the registers and memory map for the now-current process and
start it running.
2
5
Fig: Fields of Process Table Entry
Cooperating Processes
Cooperating processes are those that can affect or are affected by other processes running on the
system. Cooperating processes may share data with each other.
There may be many reasons for the requirement of cooperating processes. Some of these are given
as follows −
• Modularity
Modularity involves dividing complicated tasks into smaller subtasks. These subtasks can
completed by different cooperating processes. This leads to faster and more efficient
completion of the required tasks.
• Information Sharing
Sharing of information between multiple processes can be accomplished using cooperating
processes. This may include access to the same files. A mechanism is required so that the
processes can access the files in parallel to each other.
• Convenience
There are many tasks that a user needs to do such as compiling, printing, editing etc. It is
convenient if these tasks can be managed by cooperating processes.
• Computation Speedup
Subtasks of a single task can be performed parallely using cooperating processes. This
increases the computation speedup as the task can be executed faster. However, this is only
possible if the system has multiple processing elements.
Methods of Cooperation
2
6
Cooperating processes can coordinate with each other using shared data or messages. Details about
these are given as follows −
• Cooperation by Sharing
The cooperating processes can cooperate with each other using shared data such as
memory, variables, files, databases etc. Critical section is used to provide data integrity and
writing is mutually exclusive to prevent inconsistent data.
In the above diagram, Process P1 and P2 can cooperate with each other using shared data
such as memory, variables, files, databases etc.
• Cooperation by Communication
The cooperating processes can cooperate with each other using messages. This may lead
to deadlock if each process is waiting for a message from the other to perform a operation.
Starvation is also possible if a process never receives a message.
2
7
In the above diagram, Process P1 and P2 can cooperate with each other using messages to
communicate.
System Calls
The interface between a process and an operating system is provided by system calls. In general,
system calls are available as assembly language instructions. They are also included in the manuals
used by the assembly level programmers. System calls are usually made when a process in user
mode requires access to a resource. Then it requests the kernel to provide the resource via a system
call.
As can be seen from this diagram, the processes execute normally in the user mode until a system
call interrupts this. Then the system call is executed on a priority basis in the kernel mode. After
the execution of the system call, the control returns to the user mode and execution of user
processes can be resumed.
2
8
In general, system calls are required in the following situations −
• If a file system requires the creation or deletion of files. Reading and writing from files
also require a system call.
• Creation and management of new processes.
• Network connections also require system calls. This includes sending and receiving
packets.
• Access to a hardware devices such as a printer, scanner etc. requires a system call.
Types of System Calls
There are mainly five types of system calls. These are explained in detail as follows −
Process Control
These system calls deal with processes such as process creation, process termination etc.
File Management
These system calls are responsible for file manipulation such as creating a file, reading a file,
writing into a file etc.
Device Management
These system calls are responsible for device manipulation such as reading from device buffers,
writing into device buffers etc.
Information Maintenance
These system calls handle information and its transfer between the operating system and the user
program.
Communication
These system calls are useful for interprocess communication. They also deal with creating and
deleting a communication connection.
Some of the examples of all the above types of system calls in Windows and Unix are given as
follows −
2
9
Threads
A thread is a flow of execution through the process code, with its own program counter that keeps
track of which instruction to execute next, system registers which hold its current working
variables, and a stack which contains the execution history.
A thread shares with its peer threads few information like code segment, data segment and open
files. When one thread alters a code segment memory item, all other threads see that.
A thread is also called a lightweight process. Threads provide a way to improve application
performance through parallelism.
3
0
Difference between process and thread
Process Thread
1. Process is heavy weight or resource 1. Thread is light weight, taking lesser
intensive. resources than a process.
2. Process switching needs interaction with 2. Thread switching does not need to interact
operating system. with operating system.
3. In multiple processing environments, each 3. All threads can share same set of open files,
process executes the same code but has its child processes.
own memory and file resources.
4. If one process is blocked, then no other 4. While one thread is blocked and waiting, a
process can execute until the first process second thread in the same task can run
is unblocked.
5. Multiple processes without using threads use 5. Multiple threaded processes use fewer
more resources. resources.
6. In multiple processes each process operates 6. One thread can read, write or change another
independently of the others. thread's data.
Advantages of Thread
1. Responsiveness: If the process is divided into multiple threads, if one thread completes its
execution, then its output can be immediately returned.
2. Faster context switch: Context switch time between threads is lower compared to process
context switch. Process context switching requires more overhead from the CPU.
3
1
3. Effective utilization of multiprocessor system: If we have multiple threads in a single
process, then we can schedule multiple threads on multiple processors. This will make
process execution faster.
4. Resource sharing: Resources like code, data, and files can be shared among all threads
within a process. Note: stack and registers can’t be shared among the threads. Each thread
has its own stack and registers.
5. Communication: Communication between multiple threads is easier, as the threads shares
common address space. while in process we have to follow some specific communication
technique for communication between two process.
Types of Thread
The user-level threads are implemented by users and the kernel is not aware of the existence
of these threads. It handles them as if they were single-threaded processes. User-level threads
are small and much faster than kernel level threads. They are represented by a program counter
(PC), stack, registers and a small process control block. Also, there is no kernel involvement
in synchronization for user-level threads. Advantages
• User-level threads are easier and faster to create than kernel-level threads. They can also
be more easily managed.
• User-level threads can be run on any operating system.
• There are no kernel mode privileges required for thread switching in user-level threads.
Disadvantages
3
2
• Kernel threads are generally slower to create and manage than the user threads.
• Transfer of control from one thread to another within the same process requires a mode
switch to the Kernel.
Multithreading Models
Some operating system provide a combined user level thread and Kernel level thread facility.
Solaris is a good example of this combined approach. In a combined system, multiple threads
within the same application can run in parallel on multiple processors and a blocking system call
need not block the entire process. Multithreading leads to maximum utilization of the CPU by
multitasking. Multithreading models are three types
In this model, we have multiple user threads mapped to one kernel thread. In this model
when a user thread makes a blocking system call entire process blocks. As we have only
one kernel thread and only one user thread can access kernel at a time, so multiple threads
are not able access multiprocessor at the same time.
• Semaphore
A semaphore is a variable that controls the access to a common resource by multiple
processes. The two types of semaphores are binary semaphores and counting semaphores.
• Mutual Exclusion
Mutual exclusion requires that only one process thread can enter the critical section at a
time. This is useful for synchronization and also prevents race conditions.
• Barrier
3
4
A barrier does not allow individual processes to proceed until all the processes reach it.
Many parallel languages and collective routines impose barriers.
• Spinlock
This is a type of lock. The processes trying to acquire this lock wait in a loop while checking
if the lock is available or not. This is known as busy waiting because the process is not
doing any useful operation even though it is active.
Race Condition
A race condition occurs when two or more threads can access shared data and they try to change
it at the same time.
To see how interprocess communication works in practice, let us now consider a simple but
common example: a print spooler. When a process wants to print a file, it enters the file name in a
special spooler directory. Another process, the printer daemon, periodically checks to see if
there are any files to be printed, and if there are, it prints them and then removes their names from
the directory.
Imagine that our spooler directory has a very large number of slots, numbered 0 , 1, 2, ..., each one
capable of holding a file name. Also imagine that there are two shared variables, out, which points
to the next file to be printed, and in, which points to the next free slot in the directory. These two
variables might well be kept in a two-word file available to all processes. At a certain instant, slots
0 to 3 are empty (the files have already been printed) and slots 4 to 6 are full (with the names of
files queued for printing). More or less simultaneously, processes A and B decide they want to
queue a file for printing. This situation is shown in Fig.
Fig: Two processes want to access shared memory at the same time
Process A reads in and stores the value, 7, in a local variable called next free slot. Just then a clock
interrupt occurs and the CPU decides that process A has run long enough, so it switches to process
B. Process B also reads in and also gets a 7. It, too, stores it in its local variable next free slot. At
this instant both processes think that the next available slot is 7.
Process B now continues to run. It stores the name of its file in slot 7 and updates in to be an 8.
Then it goes off and does other things. Eventually, process A runs again, starting from the place it
left off. It looks at next free slot, finds a 7 there, and writes its file
3
5
name in slot 7, erasing the name that process B just put there. Then it computes next free slot + 1,
which is 8, and sets in to 8. The spooler directory is now internally consistent, so the printer daemon
will not notice anything wrong, but process B will never receive any output. Situations like this,
where two or more processes are reading or writing some shared data and the final result depends
on who runs precisely when, are called race conditions.
Critical Section
It is part of the program where shared resources are accessed by various processes. When one
process is executing in its critical section no other process is to be allowed to execute in its critical
section. Mutual exclusion is a way of making sure that if one process is using a shared modifiable
data, the other processes will be excluded from doing the same thing. If we could arrange matters
such that no two processes were ever in their critical sections simultaneously, we could avoid race
conditions.
We need four conditions to hold to have a good solution for the critical section problem (mutual
exclusion).
• No two processes may at the same moment inside their critical sections.
• No assumptions are made about relative speeds of processes or number of CPUs.
• No process should outside its critical section should block other processes.
• No process should wait arbitrary long to enter its critical section.
3
6
Look at the figure above, suppose there are two processes P0 and P1. Both share a common variable
A=0. While accessing A both the processes increments the value of A by 1.
First case
The order of execution of the processes is P0, P1 respectively.
Process P0 reads the value of A=0, increments it by 1 (A=1) and writes the incremented value in
A.
Now, process P1 reads the value of A=1, increments its value by 1 (A=2) and writes the
incremented value in A.
So, after both the process P0 and P1 finishes accessing the variable A. The value of A is 2.
Second case
Consider that process P0 has read the variable A=0. Suddenly context switch happens and P1 takes
the charge, and start executing. P1 would increment the value of A (A=1). After execution P1 gives
the charge again to P0.
Now the value of A for P0 is 0 & when it starts executing it would increment the value of A, from
0 to 1.
So here, when both the processes P0 & P1 end up accessing the variable A, the value of A =1
which is different from the value of A=2 in the first case
Now, this type of condition where the sequence of execution of the processes affects the result is
called race condition
1. Interrupt disabling
On a single-processor system, the simplest solution is to have each process disable all interrupts
just after entering its critical region and re-enable them just before leaving it. With interrupts
disabled, no clock interrupts can occur. The CPU is only switched from process to process as a
result of clock or other interrupts, after all, and with interrupts turned off the CPU will not be
switched to another process. Thus, once a process has disabled interrupts, it can examine and
update the shared memory without fear that any other process will intervene.
Advantages
• Mutual exclusion can be achieved by implementing operating system primitives to disable
and enable interrupts.
3
7
Disadvantages
• It is unwise to give user process the power to turn off interrupts. Suppose that if one of
them did it and never turned on again. That could be end of the system.
• If the system is multiprocessor with two or more CPUs, disabling interrupts affects only
the CPU that executed the disable instruction.
2. Lock Variables
When a process wants to enter its critical region, it first tests the lock. If the lock is 0, the process
sets it to 1 and enters the critical region. If the lock is already 1, the process just waits until it
becomes 0. Thus, a 0 means that no process is in its critical region, and a 1 means that some
process is in its critical region.
Advantages
• Mutual exclusion can be achieved.
Disadvantages
• Suppose that one process reads the lock and sees that it is 0. Before it can set the lock to 1,
another process is scheduled, runs, and sets the lock to 1. When the first process runs again,
it will also set the lock to 1, and two processes will be in their critical regions at the same
time.
3. Strict Alternation
Process 0 Process 1
The integer variable turn, initially 0, keeps track of whose turn it is to enter the critical region and
examine or update the shared memory. Initially, process 0 inspects turn, finds it to be 0, and enters
its critical region. Process 1 also finds it to be 0 and therefore sits in a tight loop continually testing
turn to see when it becomes 1. Continuously testing a variable until some value appears is called
busy waiting.
When process 0 leaves the critical region, it sets turn to 1, to allow process 1 to enter its critical
region. Suppose that process 1 finishes its critical region quickly, so that both processes are in their
noncritical regions, with turn set to 0. Now process 0 executes its whole loop quickly, exiting its
critical region and setting turn to 1. At this point turn is 1 and both processes are executing in their
noncritical regions.
Suddenly, process 0 finishes its noncritical region and goes back to the top of its loop.
Unfortunately, it is not permitted to enter its critical region now, because turn is 1 and process 1 is
busy with its noncritical region. It hangs in its while loop until process 1 sets turn to 0.
3
8
4. Peterson Algorithm
Before using the shared variables (i.e., before entering its critical region), each process calls
enter region with its own process number, 0 or 1, as parameter. This call will cause it to wait,
if need be, until it is safe to enter. After it has finished with the shared variables, the process
calls leave region to indicate that it is done and to allow the other process to enter, if it so
desires.
Initially neither process is in its critical region. Now process 0 calls enter region. It indicates
its interest by setting its array element and sets turn to 0. Since process 1 is not interested, enter
region returns immediately. If process 1 now makes a call to enter region, it will hang there
until interested[0] goes to FALSE, an event that happens only when process 0 calls leave region
to exit the critical region.
Advantages
• Preserves all conditions of mutual exclusion. Disadvantages
3
9
• Difficulty to program for n process system.
5. Hardware solution TSL (Test and Set Lock)
Hardware features can make the programming tasks easier and improve system efficiency.
TSL RX,LOCK
It reads the contents of the memory word lock into register RX and then stores a nonzero value
at the memory address lock. The operations of reading the word and storing into it are
guaranteed to be indivisible—no other processor can access the memory word until the
instruction is finished. The CPU executing the TSL instruction locks the memory bus to
prohibit other CPUs from accessing memory until it is done. To use the TSL instruction, we
will use a shared variable, lock, to coordinate access to shared memory.
When lock is 0, any process may set it to 1 using the TSL instruction and then read or write
the shared memory.When it is done, the process sets lock back to 0 using an ordinary move
instruction.
Advantages
• Preserves all condition, easier programming task and improve system efficiency.
Disadvantages
Difficulty in hardware design.
6. Sleep and Wakeup
One of the alternative to the busy waiting is to use sleep and wake up pair. Sleep is a system call
that causes the caller to block, that is be suspended until another process wakes it up.
4
0
Producer-Consumer problem with a fatal race condition
The producer produces the item and inserts it into the buffer. The value of the global variable
count got increased at each insertion. If the buffer is filled completely and no slot is available
then the producer will sleep, otherwise it keep inserting. On the consumer's end, the value of
count got decreased by 1 at each consumption. If the buffer is empty at any point of time then
the consumer will sleep otherwise, it keeps consuming the items and decreasing the value of
count by 1. The consumer will be waked up by the producer if there is at least 1 item available
in the buffer which is to be consumed. The producer will be waked up by the consumer if there
is at least one slot available in the buffer so that the producer can write that.
There are two main types of semaphores i.e. counting semaphores and binary semaphores. Details
about these are given as follows −
4
1
1. Counting Semaphores
These are integer value semaphores and have an unrestricted value domain. These semaphores
are used to coordinate the resource access, where the semaphore count is the number of
available resources. If the resources are added, semaphore count automatically incremented
and if the resources are removed, the count is decremented.
The definitions of wait and signal are as follows –
Wait
The wait operation decrements the value of its argument S. If S is negative, then put process in
suspend list.
wait(S)
{
S--; if(S<0)
put process (PCB) in suspend list, sleep()
else return;
}
Signal
The signal operation increments the value of its argument S.
signal(S)
{
S++;
if(S<=0)
Select a process from suspend list, wakeup()
}
2. Binary Semaphores
The binary semaphores are like counting semaphores but their value is restricted to 0 and 1.
The wait operation only works when the semaphore is 1 and the signal operation succeeds
when semaphore is 0. It is sometimes easier to implement binary semaphores than counting
semaphores.
4
3
Solving Producer Consumer problem using Semaphore
We have a buffer of fixed size. A producer can produce an item and can place in the buffer. A
consumer can pick items and can consume them. We need to ensure that when a producer is placing
an item in the buffer, then at the same time consumer should not consume any item. In this problem,
buffer is the critical section.
To solve this problem, we need two counting semaphores – Full and Empty. “Full” keeps track of
number of items in the buffer at any given time and “Empty” keeps track of number of unoccupied
slots.
When producer produces an item then the value of “empty” is reduced by 1 because one slot will
be filled now. The value of mutex is also reduced to prevent consumer to access the buffer. Now,
the producer has placed the item and thus the value of “full” is increased by 1. The value of mutex
is also increased by 1 because the task of producer has been completed and consumer can access
the buffer.
As the consumer is removing an item from buffer, therefore the value of “full” is reduced by 1 and
the value is mutex is also reduced so that the producer cannot access the buffer at this moment.
4
4
Now, the consumer has consumed the item, thus increasing the value of “empty” by 1. The value
of mutex is also increased so that producer can access the buffer now.
Monitors
A monitor is a collection of procedures, variables and data structures grouped together in a single
module or package. Processes can call the monitor procedures but cannot access the internal data
structures. Only one process can be active in a monitor at any instant. Monitors are a programming
language construct, so the compiler knows they are special and can handle calls to monitor
procedure, the first few instructions of the procedure will check to see if any other process is
currently active within the monitor. If so, the calling process will be suspended until the other
process has left the monitor. If no other process is using the monitor, the calling process may enter.
So, no two processes will be executing their critical regions at the same time.
Monitors are needed because if many programmers are using a semaphore and one programmer
forgets to signal after the program has left the critical section, then the entire synchronization
mechanism will end up in a deadlock.
4
5
while(TRUE){
item=produce_item();
ProducerConsumer.insert(item);
}
}
void consumer(){
while(TRUE){
item=ProducerConsumer.remove();
consume_item();
}
}
Difference between semaphore and monitor
Semaphore Monitor
1. Semaphores is an integer variable S. 1. Monitor is an abstract data type.
2. The value of Semaphore S indicates the 2. The Monitor type contains shared variables
number of shared resources availabe in the and the set of procedures that operate on
system the shared variable.
3. When any process access the shared 3. When any process wants to access the
resources it perform wait() operation on S shared variables in the monitor, it needs to
and when it releases the shared resources it access it through the procedures.
performs signal() operation on S.
4. Semaphore does not have condition 4. Monitor has condition variables.
variables.
Mutex
Mutex (Mutual Exclusion) lock is essentially a variable that is binary nature that provides code
wise functionality for mutual exclusion. At times, there maybe multiple threads that may be trying
to access same resource like memory or I/O etc. To make sure that there is no overriding. Mutex
provides a locking mechanism.
Only one thread at a time can take the ownership of a mutex and apply the lock. Once it done
utilizing the resource and it may release the mutex lock.
4
6
Message Passing
This method of interprocess communication uses two primitives, send and receive, which, like
semaphores and unlike monitors, are system calls rather than language constructs. As such, they
can easily be put into library procedures, such as
send(destination, &message); and
receive(source, &message);
The former call sends a message to a given destination and the latter one receives a message from
a given source (or from ANY, if the receiver does not care). If no message is available, the receiver
can block until one arrives. Alternatively, it can return immediately with an error code.
4
7
Producer-Consumer Problem (Bounded Buffer)
Bounded Buffer problem which is also called producer consumer problem is one of the classic
problems of synchronization.
There is a buffer of n slots and each slot is capable of storing one unit of data. There are two
processes running, namely, producer and consumer, which are operating on the buffer.
A producer tries to insert data into an empty slot of the buffer. A consumer tries to remove data
from a filled slot in the buffer. As you might have guessed by now, those two processes won't
produce the expected output if they are being executed concurrently.
4
8
There needs to be a way to make the producer and consumer work in an independent manner.
Solution
Producer Operation
Consumer Operation
• The consumer waits until there is at least one full slot in the buffer.
• Then it decrements the full semaphore because the number of occupied slots will be
decreased by one, after the consumer completes its operation.
• After that, the consumer acquires lock on the buffer.
• Following that, the consumer completes the removal operation so that the data from one of
the full slots is removed.
• Then, the consumer releases the lock.
• Finally, the empty semaphore is incremented by 1, because the consumer has just removed
data from an occupied slot, thus making it empty.
Serializability
When multiple transactions are running concurrently then there is a possibility that the database
may be left in an inconsistent state. Serializability is a concept that helps us to check which
schedules are serializable. A serializable schedule is the one that always leaves the database in
consistent state.
A serial schedule is always a serializable schedule because in serial schedule, a transaction only
starts when the other transaction finished execution. However a non-serial schedule needs to be
checked for Serializability.
A non-serial schedule of n number of transactions is said to be serializable schedule, if it is
equivalent to the serial schedule of those n transactions. A serial schedule doesn’t allow
concurrency, only one transaction executes at a time and the other starts when the already running
transaction finished.
4
9
There are two types of serializability
Conflict Serializability is one of the type of Serializability, which can be used to check whether
a non-serial schedule is conflict serializable or not. A schedule is called conflict serializable if we
can convert it into a serial schedule after swapping its non-conflicting operations.
View Serializability is a process to find out that a given schedule is view serializable or not. To
check whether a given schedule is view serializable, we need to check whether the given schedule
is View Equivalent to its serial schedule.
If we can prove that the given schedule is View Equivalent to its serial schedule then the given schedule
is called view Serializable.
Locking Protocols
A lock is a data variable which is associated with a data item. This lock signifies that operations
that can be performed on the data item. Locks help synchronize access to the database items by
concurrent transactions.
All lock requests are made to the concurrency-control manager. Transactions proceed only once
the lock request is granted.
Binary Locks: A Binary lock on a data item can either locked or unlocked states.
5
0
Shared/exclusive: This type of locking mechanism separates the locks based on their uses. If a
lock is acquired on a data item to perform a write operation, it is called an exclusive lock.
A shared lock is also called a Read-only lock. With the shared lock, the data item can be shared
between transactions. This is because you will never have permission to update data on the data
item.
For example, consider a case where two transactions are reading the account balance of a person.
The database will let them read by placing a shared lock. However, if another transaction wants to
update that account's balance, shared lock prevent it until the reading process is over.
With the Exclusive Lock, a data item can be read as well as written. This is exclusive and can't be
held concurrently on the same data item. X-lock is requested using lock-x instruction. Transactions
may unlock the data item after finishing the 'write' operation.
For example, when a transaction needs to update the account balance of a person. You can allows
this transaction by placing X lock on it. Therefore, when the second transaction wants to read or
write, exclusive lock prevent this operation.
This type of lock-based protocols allows transactions to obtain a lock on every object before
beginning operation. Transactions may unlock the data item after finishing the 'write' operation.
4. Pre-claiming Locking
Pre-claiming lock protocol helps to evaluate operations and create a list of required data items
which are needed to initiate an execution process. In the situation when all locks are granted, the
transaction executes. After that, all locks release when all of its operations are over.
• In the first phase, when the transaction begins to execute, it requires permission for the
locks it needs.
• The second part is where the transaction obtains all the locks. When a transaction releases
its first lock, the third phase starts.
5
1
• In this third phase, the transaction cannot demand any new locks. Instead, it only releases
the acquired locks.
The Two-Phase Locking protocol allows each transaction to make a lock or unlock request in two
steps:
• Growing Phase: In this phase transaction may obtain locks but may not release any locks.
• Shrinking Phase: In this phase, a transaction may release locks but not obtain any new
lock
It is true that the 2PL protocol offers serializability. However, it does not ensure that deadlocks do
not happen.
Timestamp-based Protocols
The timestamp-based algorithm uses a timestamp to serialize the execution of concurrent
transactions. This protocol ensures that every conflicting read and write operations are executed in
timestamp order. The protocol uses the System Time or Logical Count as a Timestamp.
The older transaction is always given priority in this method. It uses system time to determine the
time stamp of the transaction. This is the most commonly used concurrency protocol.
Lock-based protocols help you to manage the order between the conflicting transactions when they
will execute. Timestamp-based protocols manage conflicts as soon as an operation is created.
Example:
Suppose there are there transactions T1, T2, and T3.
T1 has entered the system at time 0010
T2 has entered the system at 0020
T3 has entered the system at 0030
Priority will be given to transaction T1, then transaction T2 and lastly Transaction T3.
5
2
Classical IPC Problem
Dining philosopher problem
Consider five philosophers who spend their lives thinking and eating. The philosophers share a
circular table surrounded by five chairs, each belonging to one philosopher. In the centre of the
table is a bowl of rice, and the table is laid with five single chopsticks. When a philosopher thinks,
she does not interact with her colleagues. From time to time, a philosopher gets hungry and tries
to pick up the two chopsticks that are closest to her (the chopsticks that are between her and her
left and right neighbours). A philosopher may pick up only one chopstick at a time. Obviously, she
cannot pick up a chopstick that is already in the hand of a neighbour. When a hungry philosopher
has both her chopsticks at the same time, she eats without releasing the chopsticks. When she is
finished eating, she puts down both chopsticks and starts thinking again.
5
3
The diningphilosophers problem is considered a classic synchronization problem neither because
of its practical importance nor because computer scientists dislike philosophers but because it is
an example of a large class of concurrency-control problems. It is a simple representation of the
need to allocate several resources among several processes in a deadlock-free and starvation-free
manner. One simple solution is to represent each chopstick with a semaphore. A philosopher tries
to grab a chopstick by executing a wait() operation on that semaphore. She releases her chopsticks
by executing the signal() operation on the appropriate semaphores.
Although this solution guarantees that no two neighbours are eating simultaneously, it nevertheless
must be rejected because it could create a deadlock. Suppose that all five philosophers become hungry
at the same time and each grabs her left chopstick. All the elements of chopstick will now be equal to
0. When each philosopher tries to grab her right chopstick, she will be delayed forever. Several possible
remedies to the deadlock problem are replaced by:
5
4
The Readers and Writers Problem
5
5
There is a shared resource which should be accessed by multiple processes. There are two types of
processes in this context. They are reader and writer. Any number of readers can read from the
shared resource simultaneously, but only one writer can write to the shared resource. When a
writer is writing data to the resource, no other process can access the resource. A writer cannot
write to the resource if there are non zero number of readers accessing the resource at that time.
Solution
From the above problem statement, it is evident that readers have higher priority than writer. If a
writer wants to write to the resource, it must wait until there are no readers currently accessing that
resource.
Here, we use one mutex m and a semaphore w. An integer variable read_count is used to maintain
the number of readers currently accessing the resource. The variable read_count is initialized to 0.
A value of 1 is given initially to m and w.
Instead of having the process to acquire lock on the shared resource, we use the mutex m to make
the process to acquire and release lock whenever it is updating the read_count variable.
Code for writer process while(TRUE)
{
wait(w); /* perform the write operation */ signal(w);
}
As seen above in the code for the writer, the writer just waits on the w semaphore until it gets a
chance to write to the resource. After performing the write operation, it increments w so that the
next writer can access the resource. On the other hand, in the code for the reader, the lock is
acquired whenever the read_count is updated by a process. When a reader wants to access the
resource, first it increments the read_count value, then accesses the resource and then decrements
the read_count value. The semaphore w is used by the first reader which enters the critical section
and the last reader which exits the critical section. The reason for this is, when the first readers
enters the critical section, the writer is blocked from the resource. Only new readers can access the
resource now. Similarly, when the last reader exits the critical section, it signals the writer using
the w semaphore because there are zero readers now and a writer can have the chance to access
the resource.
The solution to this problem includes three semaphores. First is for the customer which counts
the number of customers present in the waiting room (customer in the barber chair is not
included because he is not waiting). Second, the barber 0 or 1 is used to tell whether the barber
is idle or is working, And the third mutex is used to provide the mutual exclusion which is
required for the process to execute. In the solution, the customer has the record of the number
of customers waiting in the waiting room if the number of customers is equal to the number of
chairs in the waiting room then the upcoming customer leaves the barbershop.
When the barber shows up in the morning, he executes the procedure barber, causing him to
block on the semaphore customers because it is initially 0. Then the barber goes to sleep until
the first customer comes up.
If the chair is available then customer sits in the waiting room and increments the variable
waiting value and also increases the customer’s semaphore this wakes up the barber if he is
sleeping.
At this point, customer and barber are both awake and the barber is ready to give that person a
haircut. When the haircut is over, the customer exits the procedure and if there are no customers
in waiting room barber sleeps.
When a customer arrives, he executes customer procedure the customer acquires the mutex for
entering the critical region, if another customer enters thereafter, the second one will not be
able to anything until the first one has released the mutex. The customer then checks the chairs
in the waiting room if waiting customers are less then the number of chairs then he sits
otherwise he leaves and releases the mutex.
5
7
Algorithm for Sleeping Barber problems
Semaphore Customers = 0;
Semaphore Barber = 0;
Mutex Seats = 1;
int FreeSeats = N;
Barber {
while(true) {
/* waits for a customer (sleeps). */
down(Customers);
/* mutex to protect the number of available seats.*/
down(Seats);
/* a chair gets free.*/
FreeSeats++;
/* sitting down.*/
FreeSeats--; /*
notify the barber. */
up(Customers);
5
8
/* wait in the waiting room if barber is busy. */
down(Barber);
// customer is having hair cut
} else {
/* release the lock */
up(Seats);
// customer leaves
}
}
}
Process Scheduling
The process scheduling is the activity of the process manager that handles the removal of the
running process from the CPU and the selection of another process on the basis of a particular
strategy.
Process scheduling is an essential part of a Multiprogramming operating systems. Such operating
systems allow more than one process to be loaded into the executable memory at a time and the
loaded process shares the CPU using time multiplexing.
Process Scheduling Queues
The OS maintains all PCBs in Process Scheduling Queues. The OS maintains a separate queue for
each of the process states and PCBs of all processes in the same execution state are placed in the
same queue. When the state of a process is changed, its PCB is unlinked from its current queue
and moved to its new state queue.
The Operating System maintains the following important process scheduling queues −
• Job queue − This queue keeps all the processes in the system.
5
9
• Ready queue − This queue keeps a set of all processes residing in main memory, ready
and waiting to execute. A new process is always put in this queue.
• Device queues − The processes which are blocked due to unavailability of an I/O device
constitute this queue.
The OS can use different policies to manage each queue (FIFO, Round Robin, Priority, etc.). The
OS scheduler determines how to move processes between the ready and run queues which can only
have one entry per processor core on the system; in the above diagram, it has been merged with
the CPU.
Schedulers
Schedulers are special system software which handle process scheduling in various ways. Their
main task is to select the jobs to be submitted into the system and to decide which process to run.
Schedulers are of three types −
• Long-Term Scheduler
• Short-Term Scheduler
• Medium-Term Scheduler
Long-Term Scheduler
It is also called a job scheduler. A long-term scheduler determines which programs are admitted
to the system for processing. It selects processes from the queue and loads them into memory for
execution. Process loads into the memory for CPU scheduling.
The primary objective of the job scheduler is to provide a balanced mix of jobs, such as I/O bound
and processor bound. It also controls the degree of multiprogramming. If the degree of
multiprogramming is stable, then the average rate of process creation must be equal to the average
departure rate of processes leaving the system.
6
0
On some systems, the long-term scheduler may not be available or minimal. Time-sharing
operating systems have no long-term scheduler. When a process changes the state from new to
ready, then there is use of long-term scheduler.
Short Term Scheduler
It is also called as CPU scheduler. Its main objective is to increase system performance in
accordance with the chosen set of criteria. It is the change of ready state to running state of the
process. CPU scheduler selects a process among the processes that are ready to execute and
allocates CPU to one of them.
Short-term schedulers, also known as dispatchers, make the decision of which process to execute
next. Short-term schedulers are faster than long-term schedulers.
Medium Term Scheduler
Medium-term scheduling is a part of swapping. It removes the processes from the memory. It
reduces the degree of multiprogramming. The medium-term scheduler is in-charge of handling the
swapped out-processes.
A running process may become suspended if it makes an I/O request. A suspended process cannot
make any progress towards completion. In this condition, to remove the process from memory and
make space for other processes, the suspended process is moved to the secondary storage. This
process is called swapping, and the process is said to be swapped out or rolled out. Swapping may
be necessary to improve the process mix.
Dispatcher
A dispatcher is a special program which comes into play after the scheduler. When the scheduler
completes its job of selecting a process, it is the dispatcher which takes that process to the desired
state/queue. The dispatcher is the module that gives a process control over the CPU after it has
been selected by the short-term scheduler. This function involves the following:
• Switching context
• Switching to user mode
• Jumping to the proper location in the user program to restart that program
Difference between the Scheduler and Dispatcher
Consider a situation, where various processes are residing in the ready queue waiting to be
executed. The CPU cannot execute all of these processes simultaneously, so the operating system
has to choose a particular process on the basis of the scheduling algorithm used. So, this procedure
of selecting a process among various processes is done by the scheduler. Once the scheduler has
selected a process from the queue, the dispatcher comes into the picture, and it is the dispatcher
who takes that process from the ready queue and moves it into the running state. Therefore, the
scheduler gives the dispatcher an ordered list of processes which the dispatcher moves to the CPU
over time. Example
There are 4 processes in the ready queue, P1, P2, P3, P4; Their arrival times are t0, t1, t2, t3
respectively. A First in First out (FIFO) scheduling algorithm is used. Because P1 arrived first, the
scheduler will decide it is the first process that should be executed, and the dispatcher will remove
P1 from the ready queue and give it to the CPU. The scheduler will then determine P2 to be the
next process that should be executed, so when the dispatcher returns to the queue for a new process,
6
1
it will take P2 and give it to the CPU. This continues in the same way for P3, and then P4.
6
2
Context Switch
A context switch is the mechanism to store and restore the state or context of a CPU in Process
Control block so that a process execution can be resumed from the same point at a later time.
Using this technique, a context switcher enables multiple processes to share a single CPU. Context
switching is an essential part of a multitasking operating system features.
When the scheduler switches the CPU from executing one process to execute another, the state
from the current running process is stored into the process control block. After this, the state for
the process to run next is loaded from its own PCB and used to set the PC, registers, etc. At that
point, the second process can start executing.
Context switches are computationally intensive since register and memory state must be saved and
restored. To avoid the amount of context switching time, some hardware systems employ two or
more sets of processor registers. When the process is switched, the following information is stored
for later use.
• Program Counter
• Scheduling information
• Base and limit register value
6
3
• Currently used register
• Changed State
• I/O State information
• Accounting information
Types of Scheduling
CPU scheduling decisions may take place under the following four circumstances:
6
4
1. When a process switches from the running state to the waiting state (for I/O request or
invocation of wait for the termination of one of the child processes).
2. When a process switches from the running state to the ready state (for example, when an
interrupt occurs).
3. When a process switches from the waiting state to the ready state (for example, completion
of I/O).
4. When a process terminates.
In circumstances 1 and 4, there is no choice in terms of scheduling. A new process (if one
exists in the ready queue) must be selected for execution. There is a choice, however in
circumstances 2 and 3.
When Scheduling takes place only under circumstances 1 and 4, we say the scheduling scheme
is non-preemptive; otherwise the scheduling scheme is preemptive.
Preemptive Scheduling
In this type of Scheduling, the tasks are usually assigned with priorities. At times it is necessary to
run a certain task that has a higher priority before another task although it is running. Therefore,
the running task is interrupted for some time and resumed later when the priority task has finished
its execution.
Preemptive scheduling is used when a process switches from running state to ready state or from
waiting state to ready state. The resources (mainly CPU cycles) are allocated to the process for the
limited amount of time and then is taken away, and the process is again placed back in the ready
queue if that process still has CPU burst time remaining. That process stays in ready queue till it
gets next chance to execute.
Advantages
• Preemptive scheduling method is more robust, approach so one process cannot monopolize
the CPU
• Choice of running task reconsidered after each interruption.
• Each event cause interruption of running tasks
• The OS makes sure that CPU usage is the same by all running process.
• In this, the usage of CPU is the same, i.e., all the running processes will make use of CPU
equally.
• This scheduling method also improvises the average response time.
• Preemptive Scheduling is beneficial when we use it for the multi-programming
environment.
Disadvantages
6
6
9. Preemptive algorithm has the overhead of 9. Non-preemptive Scheduling has no such
switching the process from the ready state overhead of switching the process from
to the running state and vice-versa. running into the ready state.
Scheduling Criteria
There are many different criteria to check when considering the "best" scheduling algorithm, they
are:
CPU Utilization
To make out the best use of CPU and not to waste any CPU cycle, CPU would be working most
of the time(Ideally 100% of the time). Considering a real system, CPU usage should range from
40% (lightly loaded) to 90% (heavily loaded.)
Throughput
It is the total number of processes completed per unit time or rather say total amount of work done
in a unit of time. This may range from 10/second to 1/hour depending on the specific processes.
Turnaround Time
It is the amount of time taken to execute a particular process, i.e. The interval from time of
submission of the process to the time of completion of the process (Wall clock time).
Waiting Time
The sum of the periods spent waiting in the ready queue amount of time a process has been waiting
in the ready queue to acquire get control on the CPU.
Load Average
It is the average number of processes residing in the ready queue waiting for their turn to get into
the CPU.
Response Time
Amount of time it takes from when a request was submitted until the first response is produced.
Remember, it is the time till the first response and not the completion of process execution (final
response).
In general CPU utilization and Throughput are maximized and other factors are reduced for proper
optimization.
Scheduling Algorithm
Scheduling algorithms are mainly divided into following three categories.
6
7
a. First Come First Served (non-preemptive, preemptive)
b. Shortest Job First (non-preemptive, preemptive)
c. Shortest Remaining Time Next (preemptive)
2. Interactive system scheduling
a. Round Robin Scheduling (preemptive)
b. Priority Scheduling (non preemptive)
c. Multilevel Queue Scheduling
d. Multilevel Feedback Queue Scheduling
3. Real time scheduling
a. Priority Fair Share Scheduling
b. Guaranteed Scheduling
c. Lottery Scheduling
d. High Response Ratio Next
6
8
• No guarantee of good response time. Large average waiting time.
Example 1
Consider the processes P1, P2, P3, P4 given in the below table, arrives for execution in the same
order, with Arrival Time 0, and given Burst Time, let's find the average waiting time using the
FCFS scheduling algorithm.
Process Burst time
P1 21
P2 3
P3 6
P4 2
Solution
Gannt chart for the above processes
P1 P2 P3 P4
0 21 24 30 32
Process Turnaround time = Waiting time =
Completion Time- Arrival Time Turn Around Time – Burst Time
P1 21-0 =21 ms 21-21 = 0
P2 24-0 =24 ms 24-3 = 21
P3 30-0 = 30 ms 30-6 = 24
P4 32-0 = 32 ms 32-2 = 30
Example 2
Consider the processes P1, P2, P3, P4 given in the below table, arrives for execution in the same
order, with Arrival Time 0, 2, 2 and 3 respectively and given Burst Time, let's find the average
waiting time using the FCFS scheduling algorithm.
Process Arrival time Burst time
P1 0 ms 21
P2 2 ms 3
P3 2 ms 6
6
9
P4 3 ms 2
Solution
Gannt chart for the above processes
P1 P2 P3 P4
0 21 24 30 32
Process Turnaround time = Waiting time =
Completion Time- Arrival Time Turn Around Time – Burst Time
P1 21-0 =21 ms 21-21 = 0
P2 24-2 =22 ms 22-3 = 19
P3 30-2 = 28 ms 28-6 = 22
P4 32-3 = 29 ms 29-2 = 27
Solution
Gannt chart for the above processes
P1 P2 P3 P4
0 2 4 5 8 12
Process Turnaround time = Waiting time =
Completion Time- Arrival Time Turn Around Time – Burst Time
P1 2-0 =2 ms 2-2 = 0
P2 4-1 =3 ms 3-2 = 1
P3 8-5 = 3 ms 3-3 = 0
P4 12-6 = 6 ms 6-4 = 2
7
0
Total waiting time: (0+1+0+2) = 3 ms
Average waiting time: 3/4 = 0.75 ms
Total turnaround time: (2+3+3+6) = 14 ms
Average turnaround time: 14/4 = 3.5 ms
Characteristics
• The processing time are known in advanced.
• SJF selects the process with shortest expected processing time. In case of the tie FCFS
scheduling is used.
• The decision policies are based on the CPU burst time.
Advantages
• Reduces the average waiting time over FCFS. Favors shorts jobs at the cost of long jobs.
Problems
• It may lead to starvation if only short burst time processes are coming in the ready state.
• Estimation of run time to completion.
• Accuracy
• Not applicable in timesharing system.
Example 1
Consider the processes P1, P2, P3, P4 given in the below table, arrives for execution in the same
order, with Arrival Time 0, and given Burst Time, let's find the average waiting time using the SJF
scheduling algorithm.
Process Burst time
P1 21
P2 3
P3 6
P4 2
7
1
Solution
Gantt chart for the above processes
P4 P2 P3 P1
0 2 5 11 32
As in the Gantt chart, the process P4 will be picked up first as it has the shortest burst time, then
P2, followed by P3 and at last P1.
Process Turnaround time = Waiting time =
Completion Time- Arrival Time Turn Around Time – Burst Time
P1 32-0 = 32 ms 32-21 = 11
P2 5-0 =5 ms 5-3 = 2
P3 11-0 = 11 ms 11-6 = 5
P4 2-0 =2 ms 2-2 = 0
Example 2
Consider the processes P1, P2, P3, P4 given in the below table with arrival time 1, 2, 1 and 4
respectively and given Burst Time, let's find the average waiting time using the SJF scheduling
algorithm.
Process Arrival Time Burst time
P1 1 3
P2 2 4
P3 1 2
P4 4 4
Solution
Gantt chart for the above processes
P3 P1 P2 P4
0 1 3 6 10 14
Process Turnaround time = Waiting time =
Completion Time- Arrival Time Turn Around Time – Burst Time
P3 3-1 =2 ms 2 -2 = 0
P1 6-1 =5 ms 5-3 = 2
7
2
P2 10-2 = 8 ms 8-4 = 4
P4 14-4 = 10 ms 10-4 = 6
Solution
Gantt chart for the above processes
7
3
P1 P2 P4 P3 P1
0 1 4 6 12 32
As it is seen in the Gantt chart above, as P1 arrives first, hence it’s execution starts immediately,
but just after 1 ms, process P2 arrives with a burst time of 3 ms which is less than the burst time of
P1, hence the process P1 (1 ms done, 20 ms left) is preempted and process P2 is executed.
As P2 is getting executed, after 1ms, P3 arrives, but it has a burst time greater than that of P2,
hence execution of P2 continues. But after another millisecond, P4 arrives with a burst time of
2ms, as a result P2 (2ms done, 1ms left). Since remaining time of P2 is smaller than P4, P2
continues. After the completion of P2, process P4 is picked up and finishes, then P3 will get
executed and at last P1.
Process Turnaround time = Waiting time =
Completion Time- Arrival Time Turn Around Time – Burst Time
P1 32-0 =32 ms 32 -21 = 11
P2 4-1 =3 ms 3-3 = 0
P3 12-2 = 10 ms 10-6 = 4
P4 6-3 = 3 ms 3-2 = 1
Solution
Gantt chart for the above processes
P1 P4 P1 P3 P2
0 1 3 6 10 17 25
In the above example, at time 1ms, there are two processes i.e. P1 and P2. Process P1 is having
burst time as 6ms and the process P2 is having 8ms. So, P1 will be executed first. Since it is a
preemptive approach, so we have to check at every time quantum. At 2ms, we have three processes
i.e. P1 (5ms remaining), P2 (8ms) and P3 (7ms). Out of these three, P1
7
4
is having the least burst time, so it will continue its execution. After 3ms, we have four processes
i.e. P1 (4ms remaining), P2 (8ms), P3 (7ms) and P4 (3ms). Out of these four, P4 is having the least
burst time, so it will be executed. The process P4 keeps on executing for the next 3ms because it
is having the shortest burst time. After 6ms we have 3 processes i.e P1 (4ms remaining), P2 (8ms),
and P3 (7ms). So P1 will be selected and executed. This process of time comparison will continue
until we have all the processes executed. So waiting and turnaround time of the processes will be:
Process Turnaround time = Waiting time =
Completion Time- Arrival Time Turn Around Time – Burst Time
P1 10-1 =9 ms 9 -6 = 3
P2 25-1 =24 ms 24-8 = 16
P3 17-2 = 15 ms 15-7 = 8
P4 6-3 = 3 ms 3-3 = 0
In this algorithm the process is allocated the CPU for the specific time period called time slice or
quantum, which is normally of 10 to 100 milliseconds. If the process completes its execution within
this time slice, then it is removed from the queue otherwise it has to wait for another time slice.
Preempted process is placed at the back of the ready list.
In this approach of CPU scheduling, we have a fixed time quantum and the CPU will be allocated
to a process for that amount of time only at a time. For example, if we are having three process P1,
P2 and P3 and our time quantum is 2ms, then P1 will be given 2ms for its execution, then P2 will
be given 2ms, then P3 will be given 2ms. After one cycle, again P1 will be given 2ms, and then P2
will be given 2ms and so on until the processes complete its execution. Advantages
Disadvantages
7
5
Quantum size: If the quantum is very large, each process is given as much time as needs for
completion, Round Robin degenerate to First Come First Serve policy. If quantum is very small,
system bus at just switching from one process to another process, the overhead of contextswitching
causes the system efficiency degrading.
Example 1
Consider the processes P1, P2, P3 given in the below table, arrives for execution in the same order,
with Arrival Time 0 and given Burst Time, let's find the average waiting time and average
turnaround time using the Round Robin scheduling algorithm with quantum size = 2.
Solution
Gantt chart for the above processes
Ready queue P1 P2 P3 P1 P2 P3 P1 P2 P3 P1 P3 P1
7
6
P2 1 4
P3 2 2
P4 4 1
Solution
Gantt chart for the above processes
Ready Queue P1 P1P3P2 P2P4P1P3 P2P4P1 P1P2P4 P1P2 P1
Running Queue P1 P2 P3 P1 P4 P2 P1
0 2 4 6 8 9 11 12
Here context switches occur for 6 times.
Process Turnaround time = Waiting time = Response Time=
Completion Time - Turn Around Time – Burst CPU First time – Arrival
Arrival Time Time Time
P1 12-0 =12 ms 12-5 = 7 0-0=0
P2 11-1 =10 ms 10-4 = 6 2-1=1
P3 6-2 = 4 ms 4-2 = 2 4-2=2
P4 9-4 = 5 ms 5-1 = 4 8-4=4
b. Priority scheduling
In this scheduling algorithm the priority is assigned to all the processes and the process with highest
priority executed first. Priority assignment of process is done on the basis of internal factor such
as CPU and memory requirements or external factor such as user’s choice. It is just used to identify
which process is having a higher priority and which process is having a lower priority. For
example, we can denote 0 as the highest priority process and 100 as the lowest priority process.
Also, the reverse can be true i.e we can denote 100 as the highest priority and 0 as the lowest
priority.
7
7
Advantages
Disadvantages
• It can lead to starvation if only higher priority process comes into the ready state. Low
priority processes may never execute.
• If the priorities of more two processes are the same, then we have to use some other
scheduling algorithm.
Example 1
Find average waiting time and average turnaround time of following four processes with their
arrival time, burst time and priority as below.
Process Arrival time Burst time Priority
P1 0 5 1
P2 1 3 2
P3 2 8 1
P4 3 6 3
Take higher priority number as high priority.
Solution
Gantt chart for the above processes
Ready Queue P1 P1P2 P1 P2P3 P1 P2P3P4 P1 P2P3 P1P3 P3
Running Queue P1 P2 P4 P2 P1 P3
0 1 2 3 9 10 14 22
In the above example, at 0ms, we have only one process P1. So P1 will execute for 1ms. After 1ms
P2 arrives in the ready queue among P1and P2, P2 has higher priority so it preempts P1. At 2ms
P3 arrives at ready queue. Among P1, P2 and P3, P2 has higher priority so it continues, at 3ms P4
arrives at the ready queue and it preempts P2 because it has higher priority. At 9ms P4 completes
and P2 continues and at 10ms P2 completes and P1 continues. At 14ms P1 completes and P3
continues and at 22ms P3 completes. The waiting and turnaround time of processes will be:
Process Turnaround time = Waiting time =
Completion Time - Turn Around Time – Burst
Arrival Time Time
P1 14-0 =14 ms 14-5 = 9
P2 10-1 =9 ms 9-3 = 6
7
8
P3 22-2 = 20 ms 20-8 = 12
P4 9-3 = 6 ms 6-6 = 0
Batch Processes
Low Priority Queue 3
Solution:
In the above example, we have two queues, i.e Queue 1 and Queue 2. Queue 1 is having higher
priority and Queue 1 is using the FCFS approach and Queue 2 is using the round-robin approach
(time quantum =2ms). Below is the Gantt chart of the problem.
7
9
P1 P4 P2 P3 P2 P3 P3 P3
0 5 11 13 15 16 18 20 22
Since the priority of Queue 1 is higher, so Queue 1 will be executed first. In the Queue 1, we have
two processes i.e. P1 and P4 and we are using FCFS. So, P1 will be executed followed by P4.
Now, the job of the Queue 1 is finished. After this, the execution of the processes of Queue2 will
be started by using the round-robin approach.
d. Multilevel Feedback Queue Scheduling
It allows the process to move in between queues. The idea is to separate processes according to the
characteristics of their CPU bursts. If a process uses too much CPU time, it is moved to a lower-
priority queue. Similarly, a process that waits too long in a lower-priority queue may be moved to
a higher priority queue. This form of aging prevents starvation.
Example: Consider a system which has CPU Bound process which requires burst time of 40 times
units. Multilevel feedback queue scheduling is used. The time quantum is 2 units and it will be
incremented by 5 units in each level. How many times the process will be interrupted and in which
queue process will complete the execution?
Solution:
Queue 1, Quantum=2 First time, Interrupt,
Burst time remaining =38
Queue 2, Quantum=7
Second time, Interrupt, Burst time remaining =31
Queue 3, Quantum=12
Third time, Interrupt, Burst time remaining =19
Queue 4, Quantum=17
Fourth time, Interrupt, Burst time remaining =2
Queue 5, Quantum=22
Hence from above figure process will be interrupted in 4 times and in 5th queue the process will
complete the execution.
A real-time scheduling system is composed of the scheduler, clock and the processing hardware
elements. In a real-time system, a process or task has schedulability; tasks are accepted by a
realtime system and completed as specified by the task deadline depending on the characteristics
of the scheduling algorithm. Modeling and evaluation of a real-time scheduling system concern is
on the analysis of the algorithm capability to meet a process deadline. A deadline is defined as the
time required for a task to be processed. For example, in a real-
8
0
time scheduling algorithm a deadline could be set to 5 nano-seconds. In a critical operation the
task must be processed in the time specified by the deadline (i.e. five nano-seconds). A task in a
real-time system must be completed neither too early nor too late. A system is said to be
unschedulable when tasks cannot meet the specified deadlines. A task can be classified as either a
periodic or a periodic process.
The criteria of a real-time can be classified as hard, firm or soft. The scheduler set the algorithms
for executing tasks according to a specified order. There are multiple mathematical models to
represent a scheduling System; most implementations of real-time scheduling algorithm are
modeled for the implementation of uniprocessors or multiprocessors configurations. In the
algorithms for a real-time scheduling system, each task is assigned a description, deadline and an
identifier (indicating priority). The selected scheduling algorithm determines how priorities are
assigned to a particular task. A real-time scheduling algorithm can be classified as static or
dynamic. For a static scheduler, task priorities are determined before the system runs. A dynamic
scheduler determines task priorities as it runs. Tasks are accepted by the hardware elements in a
real-time scheduling system from the computing environment and processed in real-time. An
output signal indicates the processing status. A task deadline indicates the time set to complete for
each task.
a. Priority Fair share scheduling
Fair share scheduling is a scheduling algorithm for computer operating systems in which the
CPU usage is equally distributed among system users or groups as opposed to equal distribution
among processes.
One common method of logically implementing the fair-share scheduling strategy is to recursively
apply for the round-robin scheduling strategy at each level of abstraction (processes, users, groups,
etc). The time quantum required by round-robin is arbitrary, as any equal division of time will
produce the same results.
Example
If four users (A, B, C, D) are concurrently executing one process each, the scheduler will logically
divide the available CPU cycles such that each user gets 25% of the whole (100% /4 =25%). If
user B starts a second process, each user will still receive 25% of the total cycles, but each of user
B’s processes will now be attributed 12.5% of the total CPU cycles each, totaling user B’s fair
share of 25%. On the other hand, if a new user starts a process on the system, the scheduler will
reapportion the available CPU cycles such that each user gets 20% of the whole (100% /5=20%).
Another layer of abstraction allows us to partition users into groups, and apply the fair share
algorithm to the groups as well. In this case, the available CPU cycles are divided first among the
groups, then among the users within the groups, and then among the processes for that user. For
example, if there are three groups (1,2,3) containing three, two and four users respectively, the
available CPU cycles will be distributed as follows:
8
1
Group 2: (33.3% /2 users) = 16.7% per user Group
b. Guaranteed Scheduling
It makes real promises to the users about performance. If there are n users logged in while you are
working, you will receive about 1/n of the CPU power. Similarly, on a single-user system with n
processes running, all things being equal, each one should get 1 /n of the CPU cycles. To make
good on this promise, the system must keep track of how much CPU each process has had since
its creation. CPU time consumed to the CPU time entitled. A ratio of 0.5 means that a process has
only had half of what it should have had, and a ration of 2.0 means that a process has had twice as
much as it was entitled to. The algorithm is then to run the process with the lowest ratio until its
ratio has moved above its closest competitor.
c. Lottery Scheduling
Lottery Scheduling is type of process scheduling, somewhat different from other scheduling.
Processes are scheduled in a random manner. Lottery scheduling can be preemptive or
nonpreemptive. It also solves the problem of starvation. Giving each process at least one lottery
ticket guarantees that it has non-zero probability of being selected at each scheduling operation.
In this scheduling every process has some tickets and scheduler picks a random ticket and process
having that ticket is the winner and it is executed for a time slice and then another ticket is picked
by the scheduler. These tickets represent the share of processes. A process having a higher number
of tickets give it more chance to get chosen for execution.
Example
If we have two processes A and B having 60 and 40 tickets respectively out of total 100 tickets.
CPU share of A is 60% and that of B is 40%. These shares are calculated probabilistically and not
deterministically.
Explanation:
1. We have two processes A and B. A has 60 tickets (ticket number 1 to 60) and B have
40 tickets (ticket no 61 to 100).
2. Scheduler picks a random number from 1 to 100. If the picked no. is from 1 to 60 then
A is executed otherwise B is executed.
3. An example of 10 tickets picked by Scheduler may look like this-
Ticket number: {73 82 23 45 32 87 49 39 12 09}
Resulting schedule: {B B A A A B A A A A}
4. A is executed 7 times and B is executed 3 times. As you can see that A takes 70% of
CPU and B takes 30% of CPU which is not the same as what we need as we need A to
have 60% of CPU and B should have 40% of CPU. This
8
2
happens because shares are calculated probabilistically but in a long run (i.e. when no.
of tickets picked is more than 100 or 1000) we can achieve a share percentage of
approx.. 60 and 40 for A and B respectively.
d. Highest Response Ratio Next (HRN)
HRN is non-preemptive scheduling algorithm. In Shortest Job First scheduling, priority is
given to shortest job, which may sometimes indefinite blocking of longer job. HRN Scheduling
is used to correct this disadvantage of SJF. For determining priority, not only the job’s service
time but the waiting time is also considered. In this algorithm, dynamic priorities are used
instead of fixed priorities. Dynamic priorities in HRN are calculated as, Priority
= (waiting time + service time) /service time.
So, shortest job get preference over longer processes because service time appears in the
denominator. Longer jobs that have been waiting for long period are also give favorable
treatment because waiting time is considered in numerator.
Steps of HRN implementation
1. Input the number of processes, their arrival time and burst times.
2. Sort them according to their arrival time.
3. At any given time calculate the response ratios and select the appropriate process to be
scheduled.
4. Calculate the turnaround time as
Turnaround = completion time – arrival time.
5. Calculate the waiting time as
Waiting time = turnaround time – burst time
6. Turnaround time divided by the burst time gives the normalized turnaround time.
7. Sum up the waiting and turnaround times of all processes and divide by the number of
processes to get the average waiting and turnaround time.
Example:
P1 0 ms 4 ms
P2 2 ms 5 ms
P3 4 ms 3 ms
P4 6 ms 6 ms
P5 8 ms 3 ms
Solution
P1 P2 P3 P5 P4
0 4 9 12 15 21
Explanation
8
3
At time t=0, only the process P1 is available in the ready queue. So, process P1 executes
till its completion. At time t=4, only the process P2 and P3 are available in the ready queue.
So, we have to calculate the response ratio. The process which has the highest response
ratio will be executed next.
Process P2 has highest response ratio so it will be selected for execution. After the
completion of execution process P2, there are three processes P3, P4 and P5 are in the
ready queue.
Process P3 has highest response ratio so it will be selected for execution. After the
completion of execution of process P3, there are three processes P4 and P5 are in the ready
queue. So, the Response Ratio for processes P4 and P5 are,
Process P5 has highest response ratio so it will be executed next. After the completion of
the execution of process P5, there are only process P4 in the ready queue. So, it will be
executed next.
Advantages
Disadvantages
advance. Exercise 1
8
4
For the process listed in following table, draw Gantt chart illustrating their execution and calculate
average waiting time and turnaround time using:
8
5
Total waiting time: (10+4+0+18+1) = 33 ms
Average waiting time: 33/5 = 6.6 ms
Total turnaround time: (18+10+1+27+4) = 60 ms
Average turnaround time: 60/5 = 12 ms
Gantt chart for Priority Scheduling
P2 P3 P4 P5 P1
0 6 7 16 19 27
Process Turnaround time = Waiting time =
Completion Time- Arrival Time Turn Around Time – Burst Time
P1 27-0 = 27 27-8 = 19
P2 6-0 = 6 6-6 = 0
P3 7-0 = 7 7-1 = 6
P4 16-0 = 16 16-9 = 7
P5 19-0 = 19 19-3 = 16
Exercise 2
8
6
For the process listed in following table, draw Gantt chart illustrating their execution and calculate
average waiting time and turnaround time using:
Running Queue P1 P4 P2 P3 P5
0 8 17 23 24 27
Process Turnaround time = Waiting time =
Completion Time- Arrival Time Turn Around Time – Burst Time
P1 8-0 = 8 8-8 = 0
P2 23-2 = 21 21-6 = 15
P3 24-2 = 22 22-1 = 21
P4 17-1 = 16 16-9 = 7
P5 27-3 = 24 24-3 = 21
8
7
P3 3-2 = 1 1-1 = 0
P4 27-1 = 26 26-9 = 17
P5 6-3 = 3 3-3 = 0
Unit-4
Deadlocks
Introduction
Deadlock is a situation where a set of processes are blocked because each process is holding a
resource and waiting for another resource acquired by some other process.
Consider an example when two trains are coming toward each other on same track and there is
only one track, none of the trains can move once they are in front of each other. Similar situation
occurs in operating systems when there are two or more processes hold some resources and wait
for resources held by other(s). For example, in the below diagram, Process 1 is holding Resource
1 and waiting for resource 2 which is acquired by process 2, and process 2 is waiting for resource
1.
System Model
A system model or structure consists of a fixed number of resources to be circulated among some
opposing processes. The resources are then partitioned into numerous types, each consisting of
some specific quantity of identical instances. Memory space, CPU
8
9
cycles, directories and files, I/O devices like keyboards, printers and CD-DVD drives are prime
examples of resource types. When a system has 2 CPUs, then the resource type CPU got two
instances.
Under the standard mode of operation, any process may use a resource in only the belowmentioned
sequence:
1. Request: When the request can't be approved immediately (where the case may be when another
process is utilizing the resource), then the requesting job must remain waited until it can obtain
the resource.
2. Use: The process can run on the resource (like when the resource is a printer, its job/process is
to print on the printer).
3. Release: The process releases the resource (like, terminating or exiting any specific process).
System Resources: Preemptable and Nonpreemptable Resources
A preemptable resource is one that can be taken away from the process with no ill effects. Memory
is an example of a preemptable resource. On the other hand, a nonpreemptable resource is one that
cannot be taken away from process (without causing ill effect). For example, CD resources are not
preemptable at an arbitrary moment.Reallocating resources can resolve deadlocks that involve
preemptable resources. Deadlocks that involve nonpreemptable resources are difficult to deal with.
A deadlock state can occur when the following four circumstances hold simultaneously within a
system:
1. Mutual exclusion: At least there should be one resource that has to be held in a non-sharable
manner; i.e., only a single process at a time can utilize the resource. If other process demands
that resource, the requesting process must be postponed until the resource gets released. Printer
is an example of a resource that can be used by only one process at a time.
2. Hold and wait: There must exist a process which holds some resource and waits for another
resource held by some other process.
3. No preemption: Once the resource has been allocated to the process, it can not be preempted.
It means resource can not be snatched forcefully from one process and given to the other process.
4. Circular wait: All the processes must wait for the resource in a cyclic manner where the last
process waits for the resource held by the first process.
9
0
Deadlock Modeling
Holt (1972) showed how these four conditions can be modeled using directed graphs. The graphs
have two kinds of nodes: processes, shown as circles, and resources, shown as squares. A directed
arc from a resource node (square) to a process node (circle) means that the resource has previously
been requested by, granted to, and is currently held by that process. In Fig (a), resource R is
currently assigned to process A.
A directed arc from a process to a resource means that the process is currently blocked waiting for
that resource. In Fig.(b), process B is waiting for resource S. In Fig.(c) we see a deadlock: process
C is waiting for resource T, which is currently held by process D. Process D is not about to release
resource T because it is waiting for resource U, held by C. Both processes will wait forever. A
cycle in the graph means that there is a deadlock involving the processes and resources in the cycle
(assuming that there is one resource of each kind). In this example, the cycle is C T D U
C.
Resource allocation graphs. (a) Holding a resource. (b) Requesting a resource. (c) Deadlock.
The operating system is free to run any unblocked process at any instant, so it could decide to run
A until A finished all its work, then run B to completion, and finally run C.
This ordering does not lead to any deadlocks (because there is no competition for resources) but it
also has no parallelism at all. In addition to requesting and releasing resources, processes compute
and do I/O. When the processes are run sequentially, there is no possibility that while one process
is waiting for I/O, another can use the CPU. Thus, running the processes strictly sequentially may
not be optimal. On the other hand, if none of the processes does any I/O at all, shortest job first is
better than round robin, so under some circumstances running all processes sequentially may be
the best way.
9
1
The resource requests might occur in the order of Fig.(d). If these six requests are carried out in
that order, the six resulting resource graphs are as shown in Fig.(e)–(j). After request 4 has been
made, A blocks waiting for S, as shown in Fig.(h). In the next two steps B and C also block,
ultimately leading to a cycle and the deadlock of Fig.(j).
The operating system is not required to run the processes in any special order. In particular, if
granting a particular request might lead to deadlock, the operating system can simply suspend the
process without granting the request (i.e., just not schedule the process) until it is safe. In above
Fig. if the operating system knew about the impending deadlock, it could suspend B instead of
granting it S. By running only A and C, we would get the requests and
9
2
releases of Fig.(k) instead of Fig.(d). This sequence leads to the resource graphs of Fig.(l)–(q),
which do not lead to deadlock. After step (q), process B can be granted S because A is finished and
C has everything it needs. Even if B blocks when requesting T, no deadlock can occur. B will just
wait until C is finished.
1. Deadlock prevention
2. Deadlock avoidance
• Banker’s algorithm
3. Deadlock detection
• Resource allocation graph
4. Recovery from deadlock
1. Deadlock prevention
Deadlock has following characteristics
1. Mutual Exclusion
2. Hold and Wait
3. No preemption
4. Circular wait
We can prevent Deadlock by eliminating any of the above four conditions.
Mutual section from the resource point of view is the fact that a resource can never be used by
more than one process simultaneously which is fair enough but that is the main reason behind the
deadlock. If a resource could have been used by more than one process at the same time then the
process would have never been waiting for any resource.
Hold and wait condition lies when a process holds a resource and waiting for some other resource
to complete its task. Deadlock occurs because there can be more than one process which are
holding one resource and waiting for other in the cyclic order.
However, we have to find out some mechanism by which a process either doesn't hold any resource
or doesn't wait. That means, a process must be assigned all the necessary resources before the
execution starts. A process must not wait for any resource once the execution has been started.
9
3
Eliminate No-Preemption Condition
One protocol is “if a process that is holding some resources requests another resource and that
resource cannot be allocated to it, then it must release all resources that are currently allocated to
it”.
Another protocol is “when a process requests some resources, if they are available, allocate them.
If a resource it requested is not available, then we check whether it is being used or it is allocated
to some other process waiting for other resources. If that resource is not being used, then the OS
preempts it from the waiting process and allocate it to the requesting process. If that resource is
used, the requesting process must wait”. This protocol can be applied to resources whose states
can easily be saved and restored. It cannot be applied to resources like printers.
Each resource will be assigned with a numerical number. A process can request the resources
increasing/decreasing. order of numbering. For Example, if P1 process is allocated R5 resources,
now next tim’e if P1 ask for R4, R3 lesser than R5 such request will not be granted, only request
for resources more than R5 will be granted.
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. So named because the process is analogous to that used by a
banker in deciding if a loan can be safely made.
A deadlock avoidance algorithm dynamically examines the resources allocation state to ensure that
a circular wait condition case never exists. Where, the resources allocation state is defined by
available and allocated resources and the maximum demand of the process. There are 3 states of
the system:
• Safe state
• Unsafe state
• Deadlock state
A state is safe if the system can allocate resources to each process (up to its maximum) in some
order and still avoid a deadlock. More formally, a system is in a safe state only if there exists a safe
sequence. A sequence of processes <P1, P2, ..., Pn> is a safe sequence for the current allocation
state if, for each Pi, the resource requests that Pi can still make can be satisfied by the currently
available resources plus the resources held by all Pj, with j <i.
If a safe sequence does not exist, then the system is in an unsafe state, which may lead to deadlock.
All safe states are deadlock free, but not all unsafe states lead to deadlocks.
9
4
Fig: Demonstration that the state in (a) is safe.
In Fig.(a) we have a state in which A has three instances of the resource but may need as many as
nine eventually. B currently has two and may need four altogether, later. Similarly, C also has two
but may need an additional five. A total of 10 instances of the resource exist, so with seven
resources already allocated, three there are still free.
The state of Fig. (a) is safe because there exists a sequence of allocations that allows all processes
to complete. Namely, the scheduler can simply run B exclusively, until it asks for and gets two
more instances of the resource, leading to the state of Fig.(b). When B completes, we get the state
of Fig.(c). Then the scheduler can run C, leading eventually to Fig.(d). When C completes, we get
Fig.(e). Now A can get the six instances of the resource it needs and also complete. Thus, the state
of Fig.(a) is safe because the system, by carefully scheduling, can avoid deadlock.
Now suppose we have the initial state shown in Fig.(a), but this time A requests and gets another
resource, giving Fig.(b). Can we find a sequence that is guaranteed to work? Let us try. The
scheduler could run B until it asked for all its resources, as shown in Fig.(c). Eventually, B
completes and we get the state of Fig.(d). At this point we are stuck. We only have four instances
of the resource free, and each of the active processes needs five. There is no sequence that
guarantees completion. Thus, the allocation decision that moved the system from Fig. (a) to Fig.
(b) went from a safe to an unsafe state.
Execute process
9
5
Calculate new available as,
Step2. Otherwise
Example 1
Assume that there are 5 processes, P0 through P4, and 4 types of resources. At T0 we have the
following system state:
9
6
1. What is the content of the matrix need?
2. Is the system in the safe state?
Solution
Need matrix (Max-Allocation)
9
7
= 1,12,7,6 + 1,2,3,1
= 2,14,10,7
Example 2
Assume that there are three resources, A, B, and C. There are 4 processes P0 to P3. At T0
we have the following snapshot of the system:
Solution
9
8
If Needi <= work then work = work + allocation
Example 3
Assume that there are 5 processes, P0 through P4, and 4 types of resources. At T0 we have the
following system state:
P0 0 0 1 2 0 0 1 2 1 5 2 0
P1 1 0 0 0 1 7 5 0
P2 1 3 5 4 2 3 5 6
P3 0 6 3 2 0 6 5 2
P4 0 0 1 4 0 6 5 6
1. What is the content of the matrix need?
2. Is the system in a safe state?
3. If a request from process p1 arrives for (0 4 2 0) can the request be granted immediately?
Solution
9
9
Need Matrix
A B C D
P0 0 0 0 0
P1 0 7 5 0
P2 1 0 0 2
P3 0 0 2 0
P4 0 6 4 2
If Needi <= work then work = work + allocation
= 1,5,2,0 – 0,4,2,0
1
0
= 1,1,0,0
= 1,0,0,0 + 0,4,2,0
= 1,4,2,0
= 0,7,5,0 – 0,4,2,0
= 0,3,3,0
P0 0 0 1 2 0 0 1 2 1 1 0 0
P1 1 4 2 0 1 7 5 0
P2 1 3 5 4 2 3 5 6
P3 0 6 3 2 0 6 5 2
P4 0 0 1 4 0 6 5 6
Need Matrix
A B C D
P0 0 0 0 0
P1 0 3 3 0
P2 1 0 0 2
P3 0 0 2 0
P4 0 6 4 2
If Needi <= work then work = work + allocation
1
0
P2 1,0,0,2 <= 1,1,1,2 true, now work = work + allocation
= 1,1,1,2 + 1,3,5,4
= 2,4,6,6
P3 0,0,2,0 <= 2,4,6,6 true, now work = work + allocation
= 2,4,6,6 + 0,6,3,2
= 2,10,9,8
P4 0,6,4,2 <= 2,10,9,8 true, now work = work + allocation
= 2,10,9,8 + 0,6,4,2
= 2,16,13,10
P1 0,3,3,0 <= 2,16,13,10 true, now work = work + allocation
= 2,16,13,10 + 0,3,3,0
= 2,19,16,10
3. 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 a. Of course, the deadlock detection algorithm is only half of this strategy. Once
a deadlock is detected, there needs to be a way to recover several alternatives exists:
1
0
We use the resource allocation graph for the pictographic representation of the state of a system.
The resource allocation graph contains all the information related to the processes that are holding
some resources and also waiting for some more resources.
Resource allocation graph consists of all the information which is related to all the instances of the
resources means the information about available resources and the resources which the process is
being using.
In the Resource Allocation Graph, we use a circle to represent the process and rectangle to
represent the resource. There are two components of the resource allocation graph vertices and
edges.
In RAG vertices are two type –
1. Process vertex – Every process will be represented as a process vertex. Generally, the
process will be represented with a circle.
2. Resource vertex – Every resource will be represented as a resource vertex. It is also two
type.
• Single instance type resource – It represents as a box, inside the box, there will be one
dot. So the number of dots indicate how many instances are present of each resource
type.
• Multi-resource instance type resource – It also represents as a box, inside the box,
there will be many dots present.
1. Assign Edge – If you already assign a resource to a process then it is called Assign edge. 2.
Request Edge – It means in future the process might want some resource to complete the
execution, that is called request edge.
1
0
So, if a process is using a resource, an arrow is drawn from the resource node to the process node.
If a process is requesting a resource, an arrow is drawn from the process node to the resource node.
Example 1 (Single instances RAG)
If there is a cycle in the Resource Allocation Graph and each resource in the cycle provides only
one instance, then the processes will be in deadlock. For example, if process P1 holds resource R1,
process P2 holds resource R2 and process P1 is waiting for R2 and process P2 is waiting for R1,
then process P1 and process P2 will be in deadlock.
1
0
Here’s another example, that shows Processes P1 and P2 acquiring resources R1 and R2 while
process P3 is waiting to acquire both resources. In this example, there is no deadlock because there
is no circular dependency.So cycle in single-instance resource type is the sufficient condition for
deadlock.
From the above example, it is not possible to say the RAG is in a safe state or in an unsafe state.
So to see the state of this RAG, let’s construct the allocation matrix and request matrix.
1
0
• The total number of processes are three; P1, P2 & P3 and the total number of resources are
two; R1 & R2.
Allocation matrix –
• For constructing the allocation matrix, just go to the resources and see to which process it is
allocated.
• R1 is allocated to P1, therefore write 1 in allocation matrix and similarly, R2 is allocated to
P2 as well as P3 and for the remaining element just write 0.
Request matrix –
• In order to find out the request matrix, you have to go to the process and see the outgoing
edges.
• P1 is requesting resource R2, so write 1 in the matrix and similarly, P2 requesting R1 and for
the remaining element write 0. So now available resource is = (0, 0).
So, there is no deadlock in this RAG. Even though there is a cycle, still there is no deadlock.
Therefore, in multi-instance resource cycle is not sufficient condition for deadlock.
1
0
Above example is the same as the previous example except that, the process P3 requesting for
resource R1. So the table becomes as shown in below.
So, the Available resource is = (0, 0), but requirement are (0, 1), (1, 0) and (1, 0).So you can’t
fulfill any one requirement. Therefore, it is in deadlock.
Therefore, every cycle in a multi-instance resource type graph is not a deadlock, if there has to be
a deadlock, there has to be a cycle. So, in case of RAG with multi-instance resource type, the cycle
is a necessary condition for deadlock, but not sufficient.
1
0
Fig: Resource allocation graph with a deadlock Fig: Resource allocation graph with a
cycle but no deadlock
4. Deadlock Recovery
1. Process Termination:
To eliminate the deadlock, we can simply kill one or more processes. For this, we use two
methods:
a. Abort all the Deadlocked Processes:
Aborting all the processes will certainly break the deadlock, but with a great
expenses. The deadlocked processes may have computed for a long time and the
result of those partial computations must be discarded and there is a probability to
recalculate them later.
b. Abort one process at a time until deadlock is eliminated:
Abort one deadlocked process at a time, untill deadlock cycle is eliminated from the
system. Due to this method, there may be considerable overhead, because after
aborting each process, we have to run deadlock detection algorithm to check
whether any processes are still deadlocked.
2. Resource Preemption:
To eliminate deadlocks using resource preemption, we preempt some resources from processes
and give those resources to other processes. This method will raise three issues – a. Selecting a
victim:
We must determine which resources and which processes are to be preempted and also the
order to minimize the cost.
b. Rollback:
We must determine what should be done with the process from which resources are
preempted. One simple idea is total rollback. That means abort the process and restart it.
1
0
Unit-5 Memory
Management
Introduction
1
0
Memory Management is the process of controlling and coordinating computer memory, assigning
portions known as blocks to various running programs to optimize the overall performance of the
system.
Memory Hierarchy
The memory in a computer can be divided into five hierarchies based on the speed as well as use.
The processor can move from one level to another based on its requirements. The five hierarchies
in the memory are registers, cache, main memory, magnetic discs, and magnetic tapes. The first
three hierarchies are volatile memories which mean when there is no power, and then automatically
they lose their stored data. Whereas the last two hierarchies are not volatile which means they store
the data permanently. A memory element is the set of storage devices which stores the binary data
in the type of bits. In general, the storage of memory can be classified into two categories such as
volatile as well as non- volatile.
The memory hierarchy design in a computer system mainly includes different storage devices.
Most of the computers were inbuilt with extra storage to run more powerfully beyond the main
memory capacity. The following memory hierarchy diagram is a hierarchical pyramid for
computer memory. The designing of the memory hierarchy is divided into two types such as
primary (Internal) memory and secondary (External) memory.
1
1
Usually, the register is a static RAM or SRAM in the processor of the computer which is used for
holding the data word which is typically 64 or 128 bits. The program counter register is the most
important as well as found in all the processors. Most of the processors use a status word register
as well as an accumulator. A status word register is used for decision making, and the
accumulator is used to store the data like mathematical operation. Usually, computers like
complex instruction set computers have so many registers for accepting main memory, and
RISC- reduced instruction set computers have more registers.
Cache Memory
Cache memory can also be found in the processor, however rarely it may be another IC (integrated
circuit) which is separated into levels. The cache holds the chunk of data which are frequently used
from main memory. When the processor has a single core then it will have two (or) more cache
levels rarely. Present multi-core processors will be having three, 2-levels for each one core, and
one level is shared.
Main Memory
The main memory in the computer is nothing but, the memory unit in the CPU that communicates
directly. It is the main storage unit of the computer. This memory is fast as well as large memory
used for storing the data throughout the operations of the computer. This memory is made up of
RAM as well as ROM.
Magnetic Disks
The magnetic disks in the computer are circular plates fabricated of plastic otherwise metal by
magnetized material. Frequently, two faces of the disk are utilized as well as many disks may be
stacked on one spindle by read or write heads obtainable on every plane. All the disks in computer
turn jointly at high speed. The tracks in the computer are nothing but bits which are stored within
the magnetized plane in spots next to concentric circles. These are usually separated into sections
which are named as sectors.
Magnetic Tape
This tape is a normal magnetic recording which is designed with a slender magnetizable covering
on an extended, plastic film of the thin strip. This is mainly used to back up huge data. Whenever
the computer requires to access a strip, first it will mount to access the data. Once the data is
allowed, then it will be unmounted. The access time of memory will be slower within magnetic
strip as well as it will take a few minutes for accessing a strip.
1
1
5. The Logical Address is generated by the 5. Physical Address is Computed by MMU.
CPU
Swap-in is a method of removing a program from a hard disk and putting it back into the main
memory or RAM.
The diagram shows five processes in memory with three (shaded) free holes corresponding to 0
values in the bitmap. Initially, the memory is empty and the bitmap contains all 0 entries. To
allocate a region of memory, the memory manager needs to search the bitmap for a string of 0’s
which represents a region large enough to accommodate the request. If the memory is large and
the allocation units are relatively small, then searching the bitmap string maybe time consuming.
To deallocate a region, the memory manager simply sets the appropriate bits in the bitmap back to
0.
• The OS has to assign some memory for bitmap as well since it stores the details about
allocation units. That much amount of memory cannot be used to load any process therefore
that decreases the degree of multiprogramming as well as throughput.
• To identify any hole in the memory, the OS need to search the string of 0s in the bitmap.
This searching takes a huge amount of time which makes the system inefficient to some
extent.
1
1
When the processes and holes are kept on a list sorted by address, several algorithms can be used
to allocate memory for a newly created process (or an existing process being swapped in from
disk). We assume that the memory manager knows how much memory to allocate.
Advantages
1. It is simple to implement.
2. We will get Excellent read performance.
3. Supports Random Access into files.
Disadvantages
1
1
main memory enables the operating system to have a larger pool of ready-to-execute process.
When a program gets swapped out to a disk memory, then it is not always possible that when it is
swapped back into main memory then it occupies the previous memory location, since the location
may still be occupied by another process. We may need to relocate the process to a different area
of memory. Thus there is a possibility that program may be moved in main memory due to
swapping.
The figure depicts a process image. The process image is occupying a continuous region of main
memory. The operating system will need to know many things including the location of process
control information, the execution stack, and the code entry. Within a program, there are memory
references in various instructions and these are called logical addresses.
After loading of the program into main memory, the processor and the operating system must be
able to translate logical addresses into physical addresses. Branch instructions contain the address
of the next instruction to be executed. Data reference instructions contain the address of byte or
word of data referenced.
Memory Protection
Memory protection is a way to control memory access rights on a computer, and is a part of most
modern instruction set architectures and operating systems. The main purpose of memory
protection is to prevent a process from accessing memory that has not been allocated to it. This
prevents a bug or malware within a process from affecting other processes, or the operating system
itself. Protection may encompass all accesses to a specified area of memory, write accesses, or
attempts to execute the contents of the area. An attempt to access unowned memory results in a
hardware fault, called a segmentation fault or storage violation exception, generally causing
abnormal termination of the offending process. Memory protection for computer security includes
additional techniques such as address space layout randomization and executable space protection.
1
1
Fig: Memory protection
Memory Allocation
Memory allocation is an action of assigning the physical or the virtual memory address space to a
process (its instructions and data). The two fundamental methods of memory allocation are static
and dynamic memory allocation.
Static memory allocation method assigns the memory to a process, before its execution. On the
other hand, the dynamic memory allocation method assigns the memory to a process, during its
execution.
To get a process executed it must be first placed in the memory. Assigning space to a process in
memory is called memory allocation. Memory allocation is a general aspect of the term binding.
Let us understand binding with the help of an example. Suppose, there is an entity in a program,
with the set of attributes. Now, a variable of this entity will have values for these set of attributes.
For storing these values, we must have memory allotted to these attributes.
So, the act of assigning the memory address to the attribute of the variable is called memory
allocation. And the act of specifying/binding the values to the attributes of the variable is called
binding. This action of binding must be performed before the variable is used during the execution
of the program.
Different allocation algorithms are:
a. First fit
b. Next fit
c. Best fit
d. Worst fit
e. Quick fit
a. First fit
1
1
In the first fit, the partition is allocated which is the first sufficient block from the top of Main
Memory. It scans memory from the beginning and chooses the first available block that is large
enough. Thus it allocates the first hole that is large enough.
Advantage
The remaining unused memory areas left after allocation become waste if it is too smaller. Thus
request for larger memory requirement cannot be accomplished.
b. Next fit
Next fit is a modified version of first fit. It begins as first fit to find a free partition. When called
next time it starts searching from where it left off, not from the beginning.
c. Best fit
Allocate the process to the partition which is the first smallest sufficient partition among the free
available partition. It searches the entire list of holes to find the smallest hole whose size is greater
than or equal to the size of the process.
1
1
Advantage
Memory utilization is much better than first fit as it searches the smallest free partition first
available.
Disadvantage
It is slower and may even tend to fill up memory with tiny useless holes. d.
Worst fit
Allocate the process to the partition which is the largest sufficient among the freely available
partitions available in the main memory. It is opposite to the best-fit algorithm. It searches the
entire list of holes to find the largest hole and allocate it to process.
Advantage
1
1
Disadvantage
If a process requiring larger memory arrives at a later stage then it cannot be accommodated as the
largest hole is already split and occupied.
e. Quick fit
The quick fit algorithm suggests maintaining the different lists of frequently used sizes. Although,
it is not practically suggestible because the procedure takes so much time to create the different
lists and then expending the holes to load a process.
Exercise
Given memory partitions of 100k, 500k, 200k, 300k and 600k (in order), how would each of the
First-fit, Best-fit and Worst-fit algorithms place processes of 212k, 417k, 112k and 426k (in order)?
Which algorithm makes the most efficient use of memory?
Solution
First-fit
Primary
100K 500K 200K 300K 600K
memory
Best-fit
Primary
100K 500K 200K 300K 600K
memory
Worst-fit
Primary
100K 500K 200K 300K 600K
memory
Fragmentation
Fragmentation is an unwanted problem where the memory blocks cannot be allocated to the
processes due to their small size and the blocks remain unused. It can also be understood as when
the processes are loaded and removed from the memory they create free space or hole in the
memory and these small blocks cannot be allocated to new upcoming processes and results in
inefficient use of memory. Basically, there are two types of fragmentation:
• Internal Fragmentation
• External Fragmentation
Internal fragmentation
In this fragmentation, the process is allocated a memory block of size more than the size of that
process. Due to this some part of the memory is left unused and this cause internal fragmentation.
1
2
Example: Suppose there is fixed partitioning (i.e. the memory blocks are of fixed sizes) is used
for memory allocation in RAM. These sizes are 2MB, 4MB, 4MB, 8MB. Some part of this RAM
is occupied by the Operating System (OS).
Now, suppose a process P1 of size 3MB comes and it gets memory block of size 4MB. So, the
1MB that is free in this block is wasted and this space can’t be utilized for allocating memory to
some other process. This is called internal fragmentation.
External fragmentation
In this fragmentation, although we have total space available that is needed by a process still we
are not able to put that process in the memory because that space is not contiguous. This is called
external fragmentation.
Example: Suppose in the above example, if three new processes P2, P3, and P4 come of sizes
1MB, 3MB, and 6MB respectively. Now, these processes get memory blocks of size 2MB, 4MB
and 8MB respectively allocated.
So, now if we closely analyze this situation then process P3 (unused 1MB) and P4 (unused
1
2
2MB) are again causing internal fragmentation. So, a total of 4MB (1MB (due to process P1) +
1MB (due to process P3) + 2MB (due to process P4)) is unused due to internal fragmentation.
Now, suppose a new process of 4MB comes. Though we have a total space of 4MB still we can’t
allocate this memory to the process. This is called external fragmentation.
1
2
Difference between Internal fragmentation and External fragmentation
Internal fragmentation External fragmentation
2. Internal fragmentation happens when the 2. External fragmentation happens when the
method or process is larger than the method or process is removed.
memory.
5. The difference between memory allocated 5. The unused spaces formed between
and required space or memory is called noncontiguous memory fragments are too
Internal fragmentation. small to serve a new process, is called
External fragmentation .
1
2
As illustrated in above figure, first process is only consuming 1MB out of 4MB in the main
memory. Hence, Internal Fragmentation in first block is (4-1)=3MB. Sum of Internal
Fragmentation in every block = (4-1)+(8-7)+(8-7)+(16-14)= 3+1+1+2 = 7MB.
Suppose process P5 of size 7MB comes. But this process cannot be accommodated inspite of
available free space because of contiguous allocation (as spanning is not allowed). Hence, 7MB
becomes part of External Fragmentation.
There are some advantages and disadvantages of fixed partitioning.
Advantages of Fixed Partitioning – 1.
Easy to implement:
Algorithms needed to implement Fixed Partitioning are easy to implement. It simply requires
putting a process into certain partition without focussing on the emergence of Internal and
External Fragmentation.
2. Little OS overhead:
Processing of Fixed Partitioning require lesser excess and indirect computational power.
Disadvantages of Fixed Partitioning – 1. Internal Fragmentation:
Main memory use is inefficient. Any program, no matter how small, occupies an entire
partition. This can cause internal fragmentation.
2. External Fragmentation:
The total unused space (as stated above) of various partitions cannot be used to load the
processes even though there is space available but not in the contiguous form (as spanning is
not allowed).
3. Limit process size:
Process of size greater than size of partition in Main Memory cannot be accommodated.
Partition size cannot be varied according to the size of incoming process’s size. Hence, process
size of 32MB in above stated example is invalid.
4. Limitation on Degree of Multiprogramming:
Partition in Main Memory are made before execution or during system configure. Main
Memory is divided into fixed number of partition. Suppose if there
1
2
are n1 partitions in RAM and n2 are the number of processes, then n2<=n1 condition must be
fulfilled. Number of processes greater than number of partitions in RAM is invalid in Fixed
Partitioning.
The Non-contiguous memory allocation allows a process to acquire the several memory blocks at
the different location in the memory according to its requirement. The non-contiguous memory
allocation also reduces the memory wastage caused due to internal and external fragmentation. As
it utilizes the memory holes, created during internal and external fragmentation.
1. Paging
2. Segmentation
3. Segmentation with paging
1
2
i) Paging
A non-contiguous policy with a fixed size partition is called paging. A computer can address more
memory than the amount of physically installed on the system. This extra memory is actually called
virtual memory. Paging technique is very important in implementing virtual memory. Secondary
memory is divided into equal size partition (fixed) called pages. Every process will have a separate
page table. The entries in the page table are the number of pages a process. At each entry either we
have an invalid pointer which means the page is not in main memory or we will get the
corresponding frame number. When the frame number is combined with instruction of set D than
we will get the corresponding physical address. Size of a page table is generally very large so
cannot be accommodated inside the PCB, therefore, PCB contains a register value PTBR( page
table base register) which leads to the page table.
Disadvantages:
1. It makes the translation very slow as main memory access two times.
2. A page table is a burden over the system which occupies considerable space.
ii) Segmentation
Segmentation is a programmer view of the memory where instead of dividing a process into equal
size partition we divided according to program into partition called segments. The translation is
the same as paging but paging segmentation is independent of internal fragmentation but suffers
from external fragmentation. Reason of external fragmentation is program can be divided into
segments but segment must be contiguous in nature.
1
2
Difference between contiguous and non-contiguous memory allocation
Contiguous memory allocation Non-contiguous memory allocation
1. Contiguous memory allocation allocates 1. Non-Contiguous memory allocation
consecutive blocks of memory to a allocates separate blocks of memory to a
file/process. file/process.
2. Faster in Execution 2. Slower in Execution
3. It is easier for the OS to control. 3. It is difficult for the OS to control.
4. Overhead is minimum as not much address
4. More Overheads are there as there are more
translations are there while executing a
address translations.
process.
5. Internal fragmentation occurs in 5. External fragmentation occurs in
Contiguous memory allocation method. NonContiguous memory allocation
method.
6. It includes single partition allocation and
6. It includes paging and segmentation.
multi-partition allocation.
7. Wastage of memory is there. 7. No memory wastage is there
8. In contiguous memory allocation, swapped- 8. In non-contiguous memory allocation,
in processes are arranged in the originally swapped-in processes can be arranged in
allocated space any place in the memory.
1
2
The process of merging two adjacent holes to form a single larger hole is called coalescing.
Figure: coalescing
Even when holes are coalesced, no individual hole may be large enough to hold the job, although
the sum of holes is larger than the storage required for a process. It is possible to combine all the
holes into one big one by moving all the processes downward as far as possible; this technique is
called memory compaction. The efficiency of the system is decreased in the case of compaction
due to the fact that all the free spaces will be transferred from several places to a single place. Huge
amount of time is invested for this procedure and the CPU will remain idle for all this time. Despite
of the fact that the compaction avoids external fragmentation, it makes system inefficient.
Fig: Compaction
Virtual Memory
Virtual Memory is a space where large programs can store themselves in form of pages while their
execution and only the required pages or portions of processes are loaded into the main memory.
This technique is useful as large virtual memory is provided for user programs when a very small
physical memory is there. whenever some pages needs to be loaded in the main memory for the
execution and the memory is not available for those many pages, then in that case, instead of
stopping the pages from entering in the main memory, the OS search for the RAM area that are
least used in the recent times or that are not referenced and copy that into the secondary memory
to make the space for the new pages in the main memory.
1
3
Benefits of having Virtual Memory
1. Large programs can be written, as virtual space available is huge compared to physical
memory.
2. Less I/O required, leads to faster and easy swapping of processes.
3. More physical memory available, as programs are stored on virtual memory, so they
occupy very less space on actual physical memory.
Paging
A computer can address more memory than the amount physically installed on the system. This
extra memory is actually called virtual memory and it is a section of a hard that's set up to emulate
the computer's RAM. Paging technique plays an important role in implementing virtual memory.
Paging is a memory management technique in which process address space is broken into blocks
of the same size called pages (size is power of 2, between 512 bytes and 8192 bytes). The size of
the process is measured in the number of pages.
Similarly, main memory is divided into small fixed-sized blocks of (physical) memory called
frames and the size of a frame is kept the same as that of a page to have optimum utilization of
the main memory and to avoid external fragmentation.
1
3
Page address is called logical address and represented by page number and the offset.
Logical Address = Page number + page offset
Frame address is called physical address and represented by a frame number and the offset.
Physical Address = Frame number + page offset
A data structure called page map table is used to keep track of the relation between a page of a
process to a frame in physical memory.
1
3
When the system allocates a frame to any page, it translates this logical address into a physical
address and create entry into the page table to be used throughout execution of the program.
When a process is to be executed, its corresponding pages are loaded into any available memory
frames. Suppose you have a program of 8Kb but your memory can accommodate only 5Kb at a
given point in time, then the paging concept will come into picture. When a computer runs out of
RAM, the operating system (OS) will move idle or unwanted pages of memory to secondary
memory to free up RAM for other processes and brings them back when needed by the program.
This process continues during the whole execution of the program where the OS keeps removing
idle pages from the main memory and write them onto the secondary memory and bring them back
when required by the program.
Advantages of paging
Disadvantages of paging
There are several types of page tables, which are optimized for different requirements. Some of
major page tables are listed below:
1
3
b.
In hashed page tables, the virtual page number in the virtual address is hashed into the hash
table. They are used to handle address spaces higher than 32 bits. Each entry in the hash table
has a linked list of elements hashed to the same location (to avoid collisions – as we can get
the same value of a hash function for different page numbers). The hash value is the virtual
page number. The Virtual Page Number is all the bits that are not a part of the page offset.
For each element in the hash table, there are three fields –
1. Virtual Page Number (which is the hash value).
2. Value of the mapped page frame.
3. A pointer to the next element in the linked list.
It has only one-page table for all processes. This table has an entry for each real page (block of
physical memory). Each element contains the virtual address of the page stored in that physical
location, with information about the process that owns that page.
1
3
d. Shared page table
The primary purpose of sharing page tables is improved performance for large applications
that share big memory areas between multiple processes. It eliminates the redundant page
tables and significantly reduces the number of minor page faults. Here pages may be shared
by multiple processes.
1
3
Fig: Shared page table
Mapping
The transformation of data from main memory to cache memory is referred to as a mapping. There
are three types of mapping
1. Associative mapping: The fastest and most flexible cache organization uses an associative
memory. The associative memory stores both the address and content (data) of the memory
word. This permits any location in cache to store any word from main memory. A CPU address
is placed in the argument register and the associative memory is searched for a matching
address. If the address is found, the corresponding data is read and sent to the CPU. If no match
occurs, the main memory is accessed for the word. The address- data pair is then transferred to
the associative cache memory.
1
3
2. Direct mapping: Associative memory is expensive compared to random-access memories
because of the added logic associated with each cell. The CPU address is divided into two fields
index and tag field. Least significant bits constitute the index field and the remaining form the
tag field. The main memory needs an address that includes both the tag and the index bits. Each
word in cache consists of the data word and its associated tag. When a new word is first brought
into the cache, the tag bits are stored alongside the data bits. When the CPU generates a memory
request, the index field is used for the address to access the cache. The tag field of the CPU
address is compared with the tag in the word read from the cache. If the two tags match, there
is a hit and the desired data word is in cache. If there is no match, there is a miss and the required
word is read from main memory. The disadvantage of direct mapping is that the hit ratio can
drop considerably if two or more words whose address have the same index but different tags
are accessed repeatedly.
3. Set Associative mapping: Disadvantage of direct mapping is that two words with the same index
in their address but with different tag values cannot reside in cache memory at the same time.
In set associative mapping each word of cache can store two or more words of memory under
the same index address. Each data word is stored together with its tag
1
3
and the number of tag- data items in one word of cache is said to form a set. When the CPU
generates a memory request, the index value of the address is used to access the cache. The tag
field of the CPU address is then compared with both tags in the cache to determine if a match
occurs. The hit ratio will improve as the set size increases because more words with the same
index but different tags can reside in cache. However, an increase in the set size increases the
number of bits in words of cache and requires more complex comparison logic.
Demand paging
A demand paging system is quite similar to a paging system with swapping where processes reside
in secondary memory and pages are loaded only on demand, not in advance. When a context switch
occurs, the operating system does not copy any of the old program’s pages out to the disk or any
of the new program’s pages into the main memory Instead, it just begins executing the new
program after loading the first page and fetches that program’s pages as they are referenced.
While executing a program, if the program references a page which is not available in the main
memory because it was swapped out a little ago, the processor treats this invalid memory reference
as a page fault and transfers control from the program to the operating system to demand the page
back into the memory
1
4
Advantages
Number of tables and the amount of processor overhead for handling page interrupts are
greater than in the case of the simple paged management techniques
Page fault
1
4
A page fault occurs when a program attempts to access a block of memory that is not stored in the
physical memory, or RAM. The fault notifies the operating system that it must locate the data in
virtual memory, then transfer it from the storage device, such as an HDD or SSD, to the system
RAM. So when page fault occurs then following sequence of events happens:
• The computer hardware traps to the kernel and program counter (PC) is saved on the stack.
Current instruction state information is saved in CPU registers.
• An assembly program is started to save the general registers and other volatile information to
keep the OS from destroying it.
• Operating system finds that a page fault has occurred and tries to find out which virtual page
is needed. Sometimes hardware register contains this required information. If not, the operating
system must retrieve PC, fetch instruction and find out what it was doing when the fault
occurred.
• Once virtual address caused page fault is known, system checks to see if address is valid and
checks if there is no protection access problem.
• If the virtual address is valid, the system checks to see if a page frame is free. If no frames are
free, the page replacement algorithm is run to remove a page.
• If frame selected is dirty, page is scheduled for transfer to disk, context switch takes place,
fault process is suspended and another process is made to run until disk transfer is completed.
• As soon as page frame is clean, operating system looks up disk address where needed page is,
schedules disk operation to bring it in.
1
4
• When disk interrupt indicates page has arrived, page tables are updated to reflect its position,
and frame marked as being in normal state.
• Faulting instruction is backed up to state it had when it began and PC is reset. Faulting is
scheduled, operating system returns to routine that called it.
• Assembly Routine reloads register and other state information, returns to user space to continue
execution.
For Example:
Consider page reference string 1, 3, 0, 3, 5, 6, 3 with 3 page frames. Find number of page faults
1
4
.
Initially all slots are empty, so when 1, 3, 0 came they are allocated to the empty slots —> 3 Page
Faults. When 3 comes, it is already in memory so —> 0 Page Faults.
Then 5 comes, it is not available in memory so it replaces the oldest page slot i.e 1. —>1 Page
Fault. 6 comes, it is also not available in memory so it replaces the oldest page slot i.e 3 —>1
Page Fault. Finally when 3 come it is not avilable so it replaces 0 1 page fault
Advantages
• Poor performance.
• Doesn’t consider the frequency of use or last used time, simply replaces the oldest page.
• Suffers from Belady’s Anomaly (i.e. more page faults when we increase the number of
page frames).
In LRU, whenever page replacement happens, the page which has not been used for the longest
amount of time is replaced.
For Example:
Consider the page reference string 7, 0, 1, 2, 0, 3, 0, 4, 2, 3, 0, 3, 2, 3 with 4 page frames. Find
number of page faults.
1
4
Initially all slots are empty, so when 7 0 1 2 are allocated to the empty slots —> 4 Page faults 0 is
already their so —> 0 Page fault. When 3 came it will take the place of 7 because it is least recently
used —>1 Page fault 0 is already in memory so —> 0 Page fault. 4 will takes place of 1 —> 1
Page Fault Now for the further page reference string —> 0 Page fault because they are already
available in the memory. Advantages
• Efficient.
• Doesn't suffer from Belady’s Anomaly.
Disadvantages
• Complex Implementation.
• Expensive.
• Requires hardware support.
In this algorithm, pages are replaced which would not be used for the longest duration of time in
the future, i.e., the pages in the memory which are going to be referred farthest in the future are
replaced.
This algorithm was introduced long back and is difficult to implement because it requires future
knowledge of the program behaviour. However, it is possible to implement optimal page
replacement on the second run by using the page reference information collected on the first run.
For Example
Consider the page references 7, 0, 1, 2, 0, 3, 0, 4, 2, 3, 0, 3, 2, 3 with 4 page frame. Find number
of page fault.
1
4
Initially all slots are empty, so when 7 0 1 2 are allocated to the empty slots —> 4 Page faults 0 is
already there so —> 0 Page fault. When 3 came it will take the place of 7 because it is not used
for the longest duration of time in the future.—>1 Page fault. 0 is already there so —> 0 Page
fault. 4 will takes place of 1 —> 1 Page Fault. Now for the further page reference string —> 0
Page fault because they are already available in the memory. Advantages
• Easy to Implement.
• Simple data structures are used.
• Highly efficient.
Disadvantages
1
4
Number of Page Faults in Optimal Page Replacement Algorithm = 5
1
4
Thrashing
Thrashing in computing is an issue caused when virtual memory is in use. It occurs when the virtual
memory of a computer is rapidly exchanging data for data on hard disk, to the exclusion of most
application-level processing. As the main memory gets filled, additional pages need to be swapped
in and out of virtual memory. The swapping causes a very high rate of hard disk access. Thrashing
can continue for a long duration until the underlying issue is addressed. Thrashing can potentially
result in total collapse of the hard drive of the computer. Thrashing is also known as disk thrashing.
Fig: Thrashing
We already seen Local replacement is better than Global replacement to avoid thrashing. But it
also has disadvantages and not suggestable. Some more techniques are
This model is based on locality. What locality is saying, the page used recently can be used again
and also the pages which are nearby this page will also be used. Working set means set of pages
in the most recent D time. The page which completed its D amount of time in working set
automatically dropped from it. So accuracy of working set depends on D we have chosen. This
working set model avoid thrashing while keeping the degree of multiprogramming as high as
possible.
1
4
It is some direct approach than working set model. When thrashing occurring we know that it has
few number of frames. And if it is not thrashing that means it has too many frames. Based on this
property we assign an upper and lower bound for the desired page fault rate. According to page
fault rate we allocate or remove pages. If the page fault rate become less than the lower limit,
frames can be removed from the process. Similarly, if the page fault rate become more than the
upper limit, more number of frames can be allocated to the process. And if no frames available
due to high page fault rate, we will just suspend the processes and will restart them again when
frames available.
Segmentation
Segmentation is a technique of memory management. It is just like the Paging technique except
the fact that in segmentation, the segments are of variable length but, in Paging, the pages are of
fixed size. In segmentation, the memory is split into variable-length parts. Each part is known as
segments. The information which is related to the segment is stored in a table which is called a
segment table.
There is no simple relationship between logical addresses and physical addresses in segmentation.
A table stores the information about all such segments and is called Segment Table.
1
4
Segment Table – It maps two-dimensional Logical address into one-dimensional Physical address.
It’s each table entry has:
• Base Address: It contains the starting physical address where the segments reside in memory.
• Limit: It specifies the length of the segment.
Advantages of Segmentation
1. No internal fragmentation.
2. Average Segment Size is larger than the actual page size.
3. Less overhead.
4. It is easier to relocate segments than entire address space.
5. The segment table is of lesser size as compare to the page table in paging.
Disadvantages of Segmentation
1
5
Fig: Logical view of segmentation
1
5
number and page offset. 6. Here, logical address is split into section
number and section offset.
7. Paging comprises a page table which 7. While segmentation also comprises the encloses the
base address of every page. segment table which encloses segment number and segment
offset.
8. Paging is invisible to the user. 8. Segmentation is visible to the user.
Instead of an actual memory location the segment information includes the address of a page table
for the segment. When a program references a memory location the offset is translated to a memory
address using page table. A segment can be extended simply by allocating another memory page
and adding it to the segment’s page table.
An implementation of virtual memory on a system using segmentation with paging usually only
moves individual pages back and forth between main memory and secondary storage, similar to a
paged non-segmented system. Pages of the segment can be located anywhere in main memory and
need not be contiguous. This usually results in a reduced amount of input/output between primary
and secondary storage and reduced memory fragmentation.
The MULTICS (Multiplexed Information and Computing Service) operating system was one of
the most influential operating systems ever, having had a major influence on topics as disparate as
UNIX, the x86 memory architecture, TLBs, and cloud computing. It was started as research project
at MIT and went live in 1969. The MULTICS system solved problems of external fragmentation
and lengthy search times by paging the segments.
1
5
Unit-6
Input/Output Device Management
Principles of I/O Hardware
Different people look at I/O hardware in different ways. Electrical engineers look at it in terms of
chips, wires, power supplies, motors, and all the other physical components that make up the
hardware. Programmers look at the interface presented to the software the commands the hardware
accepts, the functions it carries out, and the errors that can be reported back. Nevertheless, the
programming of many I/O devices is often intimately connected with their internal operation.
I/O devices
I/O devices can be roughly divided into two categories: block devices and character devices. A
block device is one that stores information in fixed-size blocks, each one with its own address.
Common block sizes range from 512 bytes to 32,768 bytes. The essential property of a block device
is that it is possible to read or write each block independently of all the other ones. Disks are the
most common block devices.
If you look closely, the boundary between devices that are block addressable and those that are not
is not well defined. Everyone agrees that a disk is a block addressable device because no matter
where the arm currently is, it is always possible to seek to another cylinder and then wait for the
required block to rotate under the head. Now consider a tape drive used for making disk backups.
Tapes contain a sequence of blocks. If the tape drive is given a command to read block N, it can
always rewind the tape and go forward until it comes to block N. This operation is analogous to a
disk doing a seek, except that it takes much longer. Also, it may or may not be possible to rewrite
one block in the middle of a tape. Even if it were possible to use tapes as random access block
devices, that is stretching the point somewhat: they are not normally used that way.
The other type of I/O device is the character device. A character device delivers or accepts a stream
of characters, without regard to any block structure. It is not addressable and does not have any
seek operation. Printers, network interfaces, mice (for pointing), rats (for psychology lab
experiments), and most other devices that are not disk-like can be seen as character devices.
This classification scheme is not perfect. Some devices just do not fit in. Clocks, for example, are
not block addressable. Nor do they generate or accept character streams. All they do is cause
interrupts at well-defined intervals. Still, the model of block and character devices is general
enough that it can be used as a basis for making some of the operating system software dealing
with I/O device independent. The file system, for example, deals only with abstract block devices
and leaves the device-dependent part to lower-level software called device drivers.
Device controllers
Device drivers are software modules that can be plugged into an OS to handle a particular device.
Operating System takes help from device drivers to handle all I/O devices.
The Device Controller works like an interface between a device and a device driver. I/O units
(Keyboard, mouse, printer, etc.) typically consist of a mechanical component and an electronic
component where electronic component is called the device controller.
1
5
There is always a device controller and a device driver for each device to communicate with the
Operating Systems. A device controller may be able to handle multiple devices. As an interface its
main task is to convert serial bit stream to block of bytes, perform error correction as necessary.
Any device connected to the computer is connected by a plug and socket, and the socket is
connected to a device controller. Following is a model for connecting the CPU, memory,
controllers, and I/O devices where CPU and device controllers all use a common bus for
communication.
Interrupts
Interrupts are signals sent to the CPU by external devices, normally I/O devices. They tell the CPU
to stop its current activities and execute the appropriate part of the operating system.
1. Hardware Interrupts are generated by hardware devices to signal that they need
some attention from the OS. They may have just received some data (e.g.,
keystrokes on the keyboard or an data on the ethernet card); or they have just
completed a task which the operating system previous requested, such as
transferring data between the hard drive and memory.
2. Software Interrupts are generated by programs when they want to request a
system call to be performed by the operating system.
3. Traps are generated by the CPU itself to indicate that some error or condition
occurred for which assistance from the operating system is needed.
Interrupts are important because they give the user better control over the computer. Without
interrupts, a user may have to wait for a given application to have a higher priority over the CPU
to be ran. This ensures that the CPU will deal with the process immediately.
1
5
interrupts immediately, if another is in progress, then it is ignored for a moment.
To handle the interrupt, the controller puts a number of address line specifying which device wants
attention and asserts a signal that interrupt the CPU. The number of address lines is used as index
to a table called interrupt vector to fetch a new program counter, this program counter points to the
start of corresponding service procedure.
Goals of IO Software
There are many goals of I/O software. Some of major principles of I/O software are listed below:
a. Device independence
A key concept in the design of I/O software is known as device independence. It means
that I/O devices should be accessible to programs without specifying the device in advance.
For example, a program that reads a file as input should be able to read a file on a floppy
disk, on a hard disk, or on a CD-ROM, without having to modify the program for each
different device.
1
5
b. Uniform naming
Uniform Naming, simply be a string or an integer and not depend on the device in any way.
In UNIX, all disks can be integrated in the file-system hierarchy in arbitrary ways so the
user need not be aware of which name corresponds to which device. Here the name of file
or device should be some specific string or number. It must not depend upon device in any
way. All files and devices are addressed the same way: by a path name.
c. Error handling
Generally, errors should be handled as close as possible to the computer hardware. It should
try to correct the error itself if it can in case if the controller discovers a read error. And in
case if it can’t then the device driver should handle it. If the controller discovers a read
error, it should try to correct the error itself if it can. If it cannot, then the device driver
should handle it, perhaps by just trying to read the block again. In many cases, error
recovery can be done transparently at a low level without the upper levels even knowing
about the error.
Programmed IO
Programmable I/O is one of the I/O technique other than the interrupt-driven I/O and direct
memory access (DMA). The programmed I/O was the simplest type of I/O technique for the
exchanges of data or any types of communication between the
1
5
processor and the external devices. With programmed I/O, data are exchanged between the
processor and the I/O module. The processor executes a program that gives it direct control of the
I/O operation, including sensing device status, sending a read or write command, and transferring
the data. When the processor issues a command to the I/O module, it must wait until the I/O
operation is complete. If the processor is faster than the I/O module, this is wasteful of processor
time. The overall operation of the programmed I/O can be summaries as follow:
Advantages
• Simple to implement
• Very little hardware support
Disadvantages
• Busy waiting
• Ties up CPU for long period with no useful work
Interrupt driven IO
This mode uses an interrupt facility and special commands to inform the interface to issue the
interrupt command when data becomes available and interface is ready for the data transfer. In the
meantime, CPU keeps on executing other tasks and need not check for the flag. When the flag is
set, the interface is informed and an interrupt is initiated. This interrupt causes the CPU to deviate
from what it is doing to respond to the I/O transfer. The CPU responds to the signal by storing the
return address from the program counter (PC) into the memory stack and then branches to service
that processes the I/O request. After the transfer is complete, CPU returns to the previous task it
was executing. The branch address of the service can be chosen in two ways known as vectored
and non-vectored interrupt. In vectored interrupt, the source that interrupts, supplies the branch
information to the CPU while in case of non-vectored interrupt the branch address is assigned to a
fixed location in memory.
1
5
1. Data transfer is initiated by the means of 1. The I/O transfer is initiated by the interrupt
instructions stored in the computer command issued to the CPU.
program. Whenever there is a request for
I/O transfer the instructions are executed
from the program.
2. The CPU stays in the loop to know if the 2. There is no need for the CPU to stay in the
device is ready for transfer and has to loop as the interrupt command interrupts
continuously monitor the peripheral the CPU when the device is ready for data
device. transfer.
3. This leads to the wastage of CPU cycles as 3. The CPU cycles are not wasted as CPU and
CPU remains busy needlessly and thus the hence this method is more efficient.
efficiency of system gets reduced.
4. CPU cannot do any work until the transfer 4. CPU can do any other work until it is
is complete as it has to stay in the loop to interrupted by the command indicating the
continuously monitor the peripheral readiness of device for data transfer.
device.
5. Its module is treated as a slow module. 5. Its module is faster than programmed I/O
module.
6. It is quite easy to program and understand. 6. It can be tricky and complicated to
understand if one uses low level language.
DMA can save processing time and is a more efficient way to move data from the computer’s
memory to other devices. In order for devices to use direct memory access, they must be assigned
to a DMA channel. Each type of port on a computer has a set of DMA channels that can be assigned
to each connected device. For example, a PCI controller and a hard drive controller each have their
own set of DMA channels.
1
5
Fig: Block diagram of DMA
Direct Memory Access (DMA) means CPU grants I/O module authority to read from or write to
memory without involvement. DMA module itself controls exchange of data between main
memory and the I/O device. CPU is only involved at the beginning and end of the transfer and
interrupted only after entire block has been transferred. Direct Memory Access needs a special
hardware called DMA controller (DMAC) that manages the data transfers and arbitrates access to
the system bus. The controllers are programmed with source and destination pointers (where to
read/write the data), counters to track the number of transferred bytes, and settings, which includes
I/O and memory types, interrupts and states for the CPU cycles.
• Interrupt handlers
1
5
• Device drivers
• Device-independent input/output software
• User-space input/output software
In every input/output software, each of the above given four layer has a well-defined function to
perform and a well-defined interface to the adjacent layers. The figure given below shows all the
layers along with hardware of the input/output software system.
Here is another figure shows all the layers of the input/output software system along with their
principal functions.
Interrupt Handlers
Whenever the interrupt occurs, then the interrupt procedure does whatever it has to in order to
handle the interrupt.
Device Drivers
1
6
Basically, device driver is a device-specific code just for controlling the input/output device that
are attached to the computer system.
In some of the input/output software is device specific, and other parts of that input/output software
are device-independent.
The exact boundary between the device-independent software and drivers is device dependent, just
because of that some functions that could be done in a device-independent way sometime be done
in the drivers, for efficiency or any other reasons.
Here are the list of some functions that are done in the device-independent software:
• Uniform interfacing for device drivers
• Buffering
• Error reporting
• Allocating and releasing dedicated devices
• Providing a device-independent block size
Generally most of the input/output software is within the operating system (OS), and some small
part of that input/output software consists of libraries that are linked with the user programs and
even whole programs running outside the kernel.
Disk structure
In modern computers, most of the secondary storage is in the form of magnetic disks. Hence,
knowing the structure of a magnetic disk is necessary to understand how the data in the disk is
accessed by the computer.
A magnetic disk contains several platters. Each platter is divided into circular shaped tracks. The
length of the tracks near the centre is less than the length of the tracks farther from the centre. Each
track is further divided into sectors, as shown in the figure.
Tracks of the same distance from centre form a cylinder. A read-write head is used to read data
from a sector of the magnetic disk.
The speed of the disk is measured as two parts:
• Transfer rate: This is the rate at which the data moves from disk to the computer.
• Random access time: It is the sum of the seek time and rotational latency.
1
6
Seek time is the time taken by the arm to move to the required track. Rotational latency is defined
as the time taken by the arm to reach the required sector in the track.
Even though the disk is arranged as sectors and tracks physically, the data is logically arranged and
addressed as an array of blocks of fixed size. The size of a block can be 512 or 1024 bytes. Each
logical block is mapped with a sector on the disk, sequentially. In this way, each sector in the disk
will have a logical address.
• Seek Time: Seek time is the time taken to locate the disk arm to a specified track where the
data is to be read or write. So the disk scheduling algorithm that gives minimum average seek
time is better.
• Rotational Latency: Rotational Latency is the time taken by the desired sector of disk to
rotate into a position so that it can access the read/write heads. So the disk scheduling
algorithm that gives minimum rotational latency is better.
• Transfer Time: Transfer time is the time to transfer the data. It depends on the rotating speed
of the disk and number of bytes to be transferred.
• Disk Access Time: Disk Access Time is:
Disk Access Time = Seek Time + Rotational Latency + Transfer Time
• Disk Response Time: Response Time is the average of time spent by a request waiting to
perform its I/O operation. Average Response time is the response time of the all requests.
Variance Response Time is measure of how individual request are serviced with respect to
1
6
average response time. So the disk scheduling algorithm that gives minimum variance response
time is better.
• Transmission Time: To access a particular record, first the arm assembly must be moved to
the appropriate cylinder, and then rotate the disk until it is immediately under the read write
head. The time taken to access the whole record is called transmission time.
Disk Scheduling
Various disk scheduling algorithms are:
1. First Come-First Serve (FCFS)
3. Elevator (SCAN)
5. LOOK
6. C-LOOK
FCFS is the simplest of all the Disk Scheduling Algorithms. In FCFS, the requests are addressed
in the order they arrive in the disk queue. Let us understand this with the help of an example.
Example:
Suppose the order of request is- (82,170,43,140,24,16,190)
And current position of Read/Write head is : 50
1
6
Advantages:
• Every request gets a fair chance No indefinite postponement Disadvantages:
• Does not try to optimize seek time
• May not provide the best possible service
In SSTF (Shortest Seek Time First), requests having shortest seek time are executed first. So, the
seek time of every request is calculated in advance in the queue and then they are scheduled
according to their calculated seek time. As a result, the request near the disk arm will get executed
first. SSTF is certainly an improvement over FCFS as it decreases the average response time and
increases the throughput of system. Let us understand this with the help of an example.
Example:
Suppose the order of request is- (82,170,43,140,24,16,190)
And current position of Read/Write head is : 50
1
6
Advantages:
• Average Response Time decreases Throughput increases Disadvantages:
• Overhead to calculate seek time in advance
• Can cause Starvation for a request if it has higher seek time as compared to incoming requests
• High variance of response time as SSTF favours only some requests
3. Elevator (SCAN)
In SCAN algorithm the disk arm moves into a particular direction and services the requests coming
in its path and after reaching the end of disk, it reverses its direction and again services the request
arriving in its path. So, this algorithm works as an elevator and hence also known as elevator
algorithm. As a result, the requests at the midrange are serviced more and those arriving behind
the disk arm will have to wait.
Example:
Suppose the requests to be addressed are-82,170,43,140,24,16,190. And the Read/Write arm is at
50, and it is also given that the disk arm should move “towards the larger value”.
1
6
Therefore, the seek time is calculated as:
=(199-50)+(199-16) =332
Advantages:
• High throughput
• Low variance of response time Average response time Disadvantages:
• Long waiting time for requests for locations just visited by disk arm
In SCAN algorithm, the disk arm again scans the path that has been scanned, after reversing its
direction. So, it may be possible that too many requests are waiting at the other end or there may
be zero or few requests pending at the scanned area.
These situations are avoided in CSCAN algorithm in which the disk arm instead of reversing its
direction goes to the other end of the disk and starts servicing the requests from there. So, the disk
arm moves in a circular fashion and this algorithm is also similar to SCAN algorithm and hence it
is known as C-SCAN (Circular SCAN).
Example:
Suppose the requests to be addressed are-82,170,43,140,24,16,190. And the Read/Write arm is at
50, and it is also given that the disk arm should move “towards the larger value”.
1
6
Advantages:
Provides more uniform wait time compared to SCAN
5. LOOK
It is similar to the SCAN disk scheduling algorithm except for the difference that the disk arm in
spite of going to the end of the disk goes only to the last request to be serviced in front of the head
and then reverses its direction from there only. Thus it prevents the extra delay which occurred
due to unnecessary traversal to the end of the disk.
Example:
Suppose the requests to be addressed are-82,170,43,140,24,16,190. And the Read/Write arm is at
50, and it is also given that the disk arm should move “towards the larger value”.
1
6
6. C-LOOK
As LOOK is similar to SCAN algorithm, in similar way, CLOOK is similar to CSCAN disk
scheduling algorithm. In CLOOK, the disk arm in spite of going to the end goes only to the last
request to be serviced in front of the head and then from there goes to the other end’s last request.
Thus, it also prevents the extra delay which occurred due to unnecessary traversal to the end of the
disk.
Example:
Suppose the requests to be addressed are-82,170,43,140,24,16,190. And the Read/Write arm is at
50, and it is also given that the disk arm should move “towards the larger value”
1
6
So, the seek time is calculated as:
=(190-50)+(190-16)+(43-16)
=341
1
6
Unit-7
File System Interface Management
File concept
A file is a collection of correlated information which is recorded on secondary or non-volatile
storage like magnetic disks, optical disks, and tapes. It is a method of data collection that is used
as a medium for giving input and receiving output from that program.
In general, a file is a sequence of bits, bytes, or records whose meaning is defined by the file creator
and user. Every File has a logical location where they are located for storage and retrieval.
File structure
A File Structure needs to be predefined format in such a way that an operating system understands.
It has an exclusively defined structure, which is based on its type.
File type
It refers to the ability of the operating system to differentiate various types of files like text files,
binary, and source files.
The system uses the extension to indicate the type of the file and the type of operations that can be
done on that file. Only a file with a .com, .exe, or .sh extension can be executed, for instance. The
.com and .exe files are two forms of binary executable files, whereas the .sh file is a shell script
containing, in ASCII format, commands to the operating system. Application programs also use
extensions to indicate file types in which they are interested. For example, Java compilers expect
source files to have a .java extension, and the Microsoft Word processor expects its files to end
with a .doc or .docx extension. These extensions are not always required, so a user may specify a
file without the extension (to save typing), and the application will look for a file with the given
name and the extension it expects. Because these extensions are not supported by the operating
system, they can be considered “hints” to the applications that operate on them.
1
7
Fig: Common file types
The information stored in a file must be accessed and read into memory. Though there are many
ways to access a file, some system provides only one method, other systems provide many
methods, out of which you must choose the right one for the application.
In this method, the information in the file is processed in order, one record after another. For
example, compiler and various editors access files in this manner.
The read-next – reads the next portion of a file and updates the file pointer which tracks the I/O
location. Similarly, the write-next will write at the end of a file and advances the pointer to the new
end of the file.
1
7
2. Direct Access Method
The other method for file access is direct access or relative access. For direct access, the file is
viewed as a numbered sequence of blocks or records. This method is based on the disk model of
file. Since disks allow random access to file block.
You can read block 34, then read 45, and write in block 78, there is no restriction on the order of
access to the file. The direct access method is used in database management. A query is satisfied
immediately by accessing large amount of information stored in database files directly. The
database maintains an index of blocks which contains the block number. This block can be
accessed directly and information is retrieved.
3. Indexed sequential access method
1
7
It is the other method of accessing a file which is built on the top of the sequential access method.
These methods construct an index for the file. The index, like an index in the back of a book,
contains the pointer to the various blocks. To find a record in the file, we first search the index and
then by the help of pointer we access the file directly.
The allocation methods define how the files are stored in the disk blocks. There are three main disk
space or file allocation methods.
• Contiguous Allocation
• Linked Allocation
• Indexed Allocation
The main idea behind these methods is to provide:
• Efficient disk space utilization. Fast access to the file blocks.
1. Contiguous Allocation
In this scheme, each file occupies a contiguous set of blocks on the disk. For example, if a file
requires n blocks and is given a block b as the starting location, then the blocks assigned to the file
will be: b, b+1, b+2,……b+n-1. This means that given the starting block address and the length
of the file (in terms of blocks required), we can determine the blocks occupied by the file.
The directory entry for a file with contiguous allocation contains
Address of starting block Length of the allocated
portion.
The file ‘mail’ in the following figure starts from the block 19 with length = 6 blocks. Therefore,
it occupies 19, 20, 21, 22, 23, 24 blocks.
Advantages:
1
7
• Both the Sequential and Direct Accesses are supported by this. For direct access, the address
of the kth block of the file which starts at block b can easily be obtained as (b+k).
• This is extremely fast since the number of seeks are minimal because of contiguous allocation
of file blocks.
Disadvantages:
• This method suffers from both internal and external fragmentation. This makes it inefficient in
terms of memory utilization.
• Increasing file size is difficult because it depends on the availability of contiguous memory at
a particular instance.
Advantages:
• This is very flexible in terms of file size. File size can be increased easily since the system does
not have to look for a contiguous chunk of memory.
• This method does not suffer from external fragmentation. This makes it relatively better in
terms of memory utilization.
Disadvantages:
• Because the file blocks are distributed randomly on the disk, a large number of seeks are needed
to access every block individually. This makes linked allocation slower.
• It does not support random or direct access. We can not directly access the blocks of a file. A
block k of a file can be accessed by traversing k blocks sequentially (sequential access ) from
the starting block of the file via block pointers.
1
7
• Pointers required in the linked allocation incur some extra overhead.
3. Indexed Allocation
In this scheme, a special block known as the Index block contains the pointers to all the blocks
occupied by a file. Each file has its own index block. The ith entry in the index block contains the
disk address of the ith file block. The directory entry contains the address of the index block as
shown in the image:
Advantages:
• This supports direct access to the blocks occupied by the file and therefore provides fast access
to the file blocks.
• It overcomes the problem of external fragmentation.
Disadvantages:
• The pointer overhead for indexed allocation is greater than linked allocation.
• For very small files, say files that expand only 2-3 blocks, the indexed allocation would keep
one entire block (index block) for the pointers which is inefficient in terms of memory
utilization. However, in linked allocation we lose the space of only 1 pointer per block.
File attributes
A file is named, for the convenience of its human users, and is referred to by its name. A name is
usually a string of characters, such as example.c. Some systems differentiate between uppercase
and lowercase characters in names, whereas other systems do not. When a file is named, it becomes
independent of the process, the user, and even the system that created it. For instance, one user
might create the file example.c, and another user might edit that file by specifying its name. The
file’s owner might write the file to a USB disk, send it as an e-mail attachment, or copy it across a
network, and it could still be called example.c on the destination system. A file’s attributes vary
from one operating system to another but typically consist of these:
1
7
• Name:The symbolic file name is the only information kept in human readable form.
• Identifier: This unique tag, usually a number, identifies the file within the file system; it is
the non-human-readable name for the file.
• Type: This information is needed for systems that support different types of files.
• Location: This information is a pointer to a device and to the location of the file on that
device.
• Size: The current size of the file (in bytes, words, or blocks) and possibly the maximum
allowed size are included in this attribute.
• Protection: Access-control information determines who can do reading, writing, executing,
and so on.
• Time, date, and user identification: This information may be kept for creation, last
modification, and last use. These data can be useful for protection, security, and usage
monitoring.
File operations
A file is an abstract data type. To define a file properly, we need to consider the operations that
can be performed on files. The operating system can provide system calls to create, write, read,
reposition, delete, and truncate files. Let’s examine what the operating system must do to perform
each of these six basic file operations. It should then be easy to see how other similar operations,
such as renaming a file, can be implemented.
• Creating a file: Two steps are necessary to create a file. First, space in the file system must
be found for the file. Second, an entry for the new file must be made in the directory.
• Writing a file: To write a file, we make a system call specifying both the name of the file
and the information to be written to the file. Given the name of the file, the system searches
the directory to find the file’s location. The system must keep a write pointer to the location
in the file where the next write is to take place. The write pointer must be updated whenever
a write occurs.
• Reading a file: To read from a file, we use a system call that specifies the name of the file
and where (in memory) the next block of the file should be put. Again, the directory is
searched for the associated entry, and the system needs to keep a read pointer to the location
in the file where the next read is to take place. Once the read has taken place, the read
pointer is updated. Because a process is usually either reading from or writing to a file, the
current operation location can be kept as a per-process current file-position pointer. Both
the read and write operations use this same pointer, saving space and reducing system
complexity.
• Repositioning within a file: The directory is searched for the appropriate entry, and the
current-file-position pointer is repositioned to a given value. Repositioning within a file
need not involve any actual I/O. This file operation is also known as a file seek.
• Deleting a file: To delete a file, we search the directory for the named file. Having found
the associated directory entry, we release all file space, so that it can be reused by other
files, and erase the directory entry
• Truncating a file: The user may want to erase the contents of a file but keep its attributes.
Rather than forcing the user to delete the file and then recreate it, this function allows all
attributes to remain unchanged—except for file length—but lets the file be reset to length
zero and its file space released.
1
7
Directories
A Directory is the collection of the correlated files on the disk. In simple words, a directory is like
a container which contains file and folder. In a directory, we can store the complete file attributes
or some attributes of the file. A directory can be comprised of various files. With the help of the
directory, we can maintain the information related to the files.
There are several logical structures of directory, these are given as below:
1. Single-level directory
The single-level directory is the simplest directory structure. In it, all files are contained in the
same directory which makes it easy to support and understand.
A single level directory has a significant limitation, however, when the number of files increases
or when the system has more than one user. Since all the files are in the same directory, they must
have a unique name. if two users call their dataset test, then the unique name rule violated.
Advantages
Disadvantages:
• There may chance of name collision because two files can not have the same name.
• Searching will become time taking if the directory is large. This cannot group the
same type of files together.
2. Two-level directory
In the two-level directory structure, each user has their own user files directory (UFD). The
UFDs have similar structures, but each lists only the files of a single user. system’s master file
directory (MFD) is searches whenever a new user id=s logged in. The MFD is indexed by
username or account number, and each entry points to the UFD for that user.
1
7
Advantages:
• We can give full path like /User-name/directory-name/.
• Different users can have the same directory as well as the file name.
• Searching of files becomes easier due to pathname and user-grouping.
Disadvantages:
• A user is not allowed to share files with other users.
• Still, it not very scalable, two files of the same type cannot be grouped together in the same
user.
3. Hierarchical directory
In Hierarchical directory structure, the users can create directories under the root directory and can
also create sub-directories under this structure. As the user is free to create many subdirectories, it
can create different sub-directories for different file types.
1
7
Once we have seen a two-level directory as a tree of height 2, the natural generalization is to extend
the directory structure to a tree of arbitrary height. This generalization allows the user to create
their own subdirectories and to organize their files accordingly.
A tree structure is the most common directory structure. The tree has a root directory, and every
file in the system has a unique path.
Advantages:
• Very general, since full pathname can be given.
• Very scalable, the probability of name collision is less.
• Searching becomes very easy, we can use both absolute paths as well as relative.
Disadvantages:
• Every file does not fit into the hierarchical model; files may be saved into multiple directories.
• We cannot share files.
• It is inefficient, because accessing a file may go under multiple directories.
1
7
It is used in the situation like when two programmers are working on a joint project and they need
to access files. The associated files are stored in a subdirectory, separating them from other projects
and files of other programmers since they are working on a joint project so they want the
subdirectories to be into their own directories. The common subdirectories should be shared. So
here we use Acyclic directories.
It is the point to note that the shared file is not the same as the copy file. If any programmer makes
some changes in the subdirectory it will reflect in both subdirectories.
Advantages:
• We can share files.
• Searching is easy due to different-different paths.
Disadvantages:
• We share the files via linking, in case deleting it may create the problem,
• If the link is a soft link then after deleting the file we left with a dangling pointer.
• In the case of a hard link, to delete a file we have to delete all the references associated with it.
Path names
1. Absolute Path name – In this method, each file is given an absolute path name consisting
of the path from the root directory to the file. As an example, the
path /usr/ast/mailbox means that the root directory contains a subdirectory usr, which in
turn contains a subdirectory ast, which contains the file mailbox.
1
8
Absolute path names always start at the root directory and are unique.
In UNIX the components of the path are separated by ‘/’. In Windows, the separator is ‘’.
Windows usrastmailbox
UNIX /usr/ast/mailbox
2. Relative Path name – This is used in conjunction with the concept of the working
directory (also called the current directory). A user can designate one directory as the
current working directory, in which case all path names not beginning at the root directory
are taken relative to the working directory.
For example, if the current working directory is /usr/ast, then the file whose absolute path
is /usr/ast/mailbox can be referenced simply as mailbox.
In other words, the UNIX command :cp/usr/ast/mailbox/usr/ast/mailbox.bak
and the command : cp mailbox mailbox.bak do exactly the same thing if the
working directory is /usr/ast.
Operations on directory
1. Searching
A directory can be searched for a particular file or for another directory. It can also be searched
to list all the files with the same name.
2. Creating
A new file can be created and inserted to the directory or new directory can be created keeping in
mind that its name must be unique under that particular directory.
3. Deleting
If a file is no longer needed by the user, it can be deleted from the directory. The entire directory
can also be deleted if it is not needed. An empty directory can also be deleted. When a directory
is empty it is resembled by dot and dotdot.
4. List a directory
List of all the files in the directory can be retrieved and also the contents of the directory entry, for
each file in a list. To read the list of all the files in the directory, it must be opened and after
reading the directory must be closed to free up the internal tablespace.
5. Renaming
The name of the file or a directory represents the content it holds and its use. The file or directory
can be renamed in case, the content inside or the use of file get changed. Renaming the file or
directory also changes its position inside the directory.
6. Link
1
8
The file can be allowed to appear in more than one directory. Here, the system call creates a link
between the file and the name specified by the path where the file is to appear.
7. Unlink
If the file is unlinked and is only present in one directory its directory entry is removed. If the file
appears in multiple directories, only the link is removed.
Protection
When information is stored in a computer system, we want to keep it safe from physical damage
(the issue of reliability) and improper access (the issue of protection). Reliability is generally
provided by duplicate copies of files. Many computers have systems programs that automatically
(or through computer-operator intervention) copy disk files to tape at regular intervals (once per
day or week or month) to maintain a copy should a file system be accidentally destroyed. File
systems can be damaged by hardware problems (such as errors in reading or writing), power surges
or failures, head crashes, dirt, temperature extremes, and vandalism. Files may be deleted
accidentally.
Protection can be provided in many ways. For a single-user laptop system, we might provide
protection by locking the computer in a desk drawer or file cabinet. In a larger multiuser system,
however, other mechanisms are needed.
Types of Access
The need to protect files is a direct result of the ability to access files. Systems that do not permit
access to the files of other users do not need protection. Thus, we could provide complete
protection by prohibiting access. Alternatively, we could provide free access with no protection.
Both approaches are too extreme for general use. What is needed is controlled access. Protection
mechanisms provide controlled access by limiting the types of file access that can be made. Access
is permitted or denied depending on several factors, one of which is the type of access requested.
Several different types of operations may be controlled:
• Read. Read from the file.
• Write. Write or rewrite the file.
• Execute. Load the file into memory and execute it.
• Append. Write new information at the end of the file.
• Delete. Delete the file and free its space for possible reuse. List. List the name and
attributes of the file.
Other operations, such as renaming, copying, and editing the file, may also be controlled. For many
systems, however, these higher-level functions may be implemented by a system program that
makes lower-level system calls. Protection is provided at only the lower level. For instance,
copying a file may be implemented simply by a sequence of read requests. In this case, a user with
read access can also cause the file to be copied, printed, and so on.
Access control
The most common approach to the protection problem is to make access dependent on the identity
of the user. Different users may need different types of access to a file or
1
8
directory. The most general scheme to implement identity dependent access is to associate with
each file and directory an access-control list (ACL) specifying user names and the types of access
allowed for each user. When a user requests access to a particular file, the operating system checks
the access list associated with that file. If that user is listed for the requested access, the access is
allowed. Otherwise, a protection violation occurs, and the user job is denied access to the file.
This approach has the advantage of enabling complex access methodologies. The main problem
with access lists is their length. If we want to allow everyone to read a file, we must list all users
with read access. This technique has two undesirable consequences:
• Constructing such a list may be a tedious and unrewarding task, especially if we do not
know in advance the list of users in the system.
• The directory entry, previously of fixed size, now must be of variable size, resulting in
more complicated space management.
These problems can be resolved by use of a condensed version of the access list. To condense the
length of the access-control list, many systems recognize three classifications of users in
connection with each file:
Unit-8 Security
Management
Introduction
Security refers to providing a protection system to computer system resources such as CPU,
memory, disk, software programs and most importantly
1
8
data/information stored in the computer system. If a computer program is run by an unauthorized
user, then he/she may cause severe damage to computer or data stored in it. So a computer system
must be protected against unauthorized access, malicious access to system memory, viruses,
worms etc.
OS security encompasses many different techniques and methods which ensure safety from threats
and attacks. OS security allows different applications and programs to perform required tasks and
stop unauthorized interference.
Security problems
Security must consider external environment of the system and protect the system resources.
Crackers attempt to break security. Threat is potential security violation. Attack can be accidental
or malicious. Easier to protect against accidental than malicious misuse. There are various security
problems in operating system out of them major security problems are listed below:
• User authentication
• Program threats
• System threats
User authentication
Authentication refers to identifying each user of the system and associating the executing programs
with those users. It is the responsibility of the Operating System to create a protection system
which ensures that a user who is running a particular program is authentic. a. Passwords
The most common approach to authenticating a user identity is the use of passwords. When the
user identifies herself by user ID or account name, she is asked for a password. If the user-supplied
password matches the password stored in the system, the system assumes that the account is being
accessed by the owner of that account. Passwords are often used to protect objects in the computer
system, in the absence of more complete protection schemes.
They can be considered a special case of either keys or capabilities. For instance, a password could
be associated with each resource (such as a file). Whenever a request is made to use the resource,
the password must be given. If the password is correct, access is granted. Different passwords may
be associated with different access rights.
For example, different passwords may be used for reading files, appending files, and updating
files. In practice, most systems require only one password for a user to gain full rights. Although
more passwords theoretically would be more secure, such systems tend not to be implemented due
to the classic trade-off between security and convenience. If security makes something
1
8
inconvenient, then the security is frequently bypassed or otherwise circumvented. b. Password
Vulnerabilites
Vulnerability is a cyber-security term that refers to a defect in a system that can leave it open to
attack. Vulnerability may also refer to any type of weakness in a computer system itself, in a set
of procedures, or in anything that leaves information security exposed to a threat.
Strong protection starts with strong passwords. Use a variety of lowercase and uppercase letters,
numbers, characters and symbols; the more jumbled the better. And, be sure to change them every
few months. Never use combinations that include personal information or are easy to guess, like
the website name, your name, birthday, or social security number. Don’t use the same password
for more than one account. It seems like the easy choice, but it comes with risks. If a hacker does
get into one of your accounts, then they will be able to access all the other ones with the same
login.
c. Encrypted Password
Encryption is the process of encoding a message or information in such a way that only authorized
parties can access it and those who are not authorized cannot.
Using one-way encryption formats, user passwords may be encrypted and stored the directory,
which prevents clear passwords from being accessed by any users including the system
administrators. Using two-way encryption formats, passwords are encrypted while stored in the
database, and decrypted when returned to an authorized client. Use of two-way encryption protects
the password stored in the database.
1
8
During authorization, a system verifies an authenticated user's access rules and either grants or
refuses resource access.
Program Threats
Operating system's processes and kernel do the designated task as instructed. If a user program
made these process do malicious tasks, then it is known as Program Threats. One of the common
example of program threat is a program installed in a computer which can store and send user
credentials via network to some hacker. Following is the list of some well-known program threats.
a. Trojan Horse
Trojan horse is a program downloaded and installed on a computer that appears harmless, but is,
in fact, malicious. Unexpected changes to computer settings and unusual activity, even when the
computer should be idle, are strong indications that a Trojan is residing on a computer.
Typically, the Trojan horse is hidden in an innocent-looking email attachment or free download.
When the user clicks on the email attachment or downloads the free program, the malware that is
hidden inside is transferred to the user's computing device. Once inside, the malicious code can
execute whatever task the attacker designed it to carry out.
The easiest way to protect a system from a Trojan horse is by never opening or downloading emails
or attachments from unknown sources. Deleting these messages before opening will delete the
Trojan horse threat
b. Trap Door
Trapdoor- is a method of gaining access to some part of a system other than by the normal
procedure (e.g. gaining access without having to supply a password). Hackers who successfully
penetrate a system may insert trapdoors to allow them entry at a later date, even if the vulnerability
that they originally exploited is closed. There have also been instances of system developers
leaving debug trapdoors in software, which are then discovered and exploited by hackers.
A buffer is a temporary area for data storage. When more data (than was originally allocated to be
stored) gets placed by a program or system process, the extra data overflows. It causes some of
that data to leak out into other buffers, which can corrupt or overwrite whatever data they were
holding.
In a buffer-overflow attack, the extra data sometimes holds specific instructions for actions
intended by a hacker or malicious user; for example, the data could trigger a response that damages
1
8
files, changes data or unveils private information. Attacker would use a buffer-overflow exploit to
take advantage of a program that is waiting on a user’s input. There are two types of buffer
overflows: stack based and heap-based. Heap-based, which are difficult to execute and the least
common of the two, attack an application by flooding the memory space reserved for a program.
Stack-based buffer overflows, which are more common among attackers, exploit applications and
programs by using what is known as a stack: memory space used to store user input. d. Logic
Bomb
A logic bomb is a piece of code inserted into an operating system or software application that
implements a malicious function after a certain amount of time, or specific conditions are met. For
example, a programmer may hide a piece of code that starts deleting files, should they ever be
terminated from the company. Logic bombs are often used with viruses, worms, and trojan horses
to time them to do maximum damage before being noticed.
System Threats
System threats refers to misuse of system services and network connections to put user in trouble.
System threats can be used to launch program threats on a complete network called as program
attack. System threats creates such an environment that operating system resources/ user files are
misused. Following is the list of some well-known system threats.
a. Worms
A worm is a malicious, self-replicating program that can spread throughout a network without
human assistance. Worms cause damage similar to viruses, exploiting holes in security software
and potentially stealing sensitive information, corrupting files and installing a back door for remote
access to the system, among other issues. Worms often utilize large amounts of memory and
bandwidth, so affected servers, networks and individual systems are often overloaded and stop
responding. But worms are not viruses. Viruses need a host computer or operating system. The
worm program operates alone. The worm is often transmitted via file-sharing networks,
information-transport features, email attachments or by clicking links to malicious websites. Once
downloaded, the worm takes advantage of a weakness in its target system or tricks a user into
executing it. Some worms have a phishing component that entices users to run the malicious code.
b. Viruses
A computer virus, much like a flu virus, is designed to spread from host to host and has the
ability to replicate itself. Similarly, in the same way that flu viruses cannot reproduce without a
host cell, computer viruses cannot reproduce and spread without programming such as a file or
document.
In more technical terms, a computer virus is a type of malicious code or program written to alter
the way a computer operates and is designed to spread from one computer to another. A virus
operates by inserting or attaching itself to a legitimate program or document that supports macros
in order to execute its code. In the process, a virus has the potential to cause unexpected or
damaging effects, such as harming the system software by corrupting or destroying data.
1
8
Once a virus has successfully attached to a program, file, or document, the virus will lie dormant
until circumstances cause the computer or device to execute its code. In order for a virus to infect
your computer, you have to run the infected program, which in turn causes the virus code to be
executed.
This means that a virus can remain dormant on your computer, without showing major signs or
symptoms. However, once the virus infects your computer, the virus can infect other computers
on the same network. Stealing passwords or data, logging keystrokes, corrupting files, spamming
your email contacts, and even taking over your machine are just some of the devastating and
irritating things a virus can do.
Different types of viruses are:
3. Browser hijacker
This type of virus “hijacks” certain web browser functions, and you may be automatically directed
to an unintended website.
4. Resident virus
This is a general term for any virus that inserts itself in a computer system’s memory. A resident
virus can execute anytime when an operating system loads.
6. Polymorphic virus
A polymorphic virus changes its code each time an infected file is executed. It does this to evade
antivirus programs.
8. Multipartite virus
This kind of virus infects and spreads in multiple ways. It can infect both program files and system
sectors.
1
8
9. Macro virus
Macro viruses are written in the same macro language used for software applications. Such viruses
spread when you open an infected document, often through email attachments.
A computer virus attack can produce a variety of symptoms. Here are some of them:
• Frequent pop-up windows. Pop-ups might encourage you to visit unusual sites. Or they
might prod you to download antivirus or other software programs.
• Changes to your homepage. Your usual homepage may change to another website, for
instance. Plus, you may be unable to reset it.
• Mass emails being sent from your email account. A criminal may take control of your
account or send emails in your name from another infected computer.
• Frequent crashes. A virus can inflict major damage on your hard drive. This may cause
your device to freeze or crash. It may also prevent your device from coming back on.
• Unusually slow computer performance. A sudden change of processing speed could
signal that your computer has a virus.
• Unknown programs that start up when you turn on your computer. You may become
aware of the unfamiliar program when you start your computer. Or you might notice it by
checking your computer’s list of active applications.
• Unusual activities like password changes. This could prevent you from logging into your
computer.
How to protect against computer viruses?
e. Denial of Services
A denial-of-service (DoS) attack is a type of cyber attack in which a malicious actor aims to render
a computer or other device unavailable to its intended users by interrupting the device's normal
functioning. DoS attacks typically function by overwhelming or flooding a targeted machine with
requests until normal traffic is unable to be processed, resulting in denial-of-service to addition
users. A DoS attack is characterized by using a single computer to launch the attack.
A denial-of-service (DoS) attack occurs when legitimate users are unable to access information
systems, devices, or other network resources due to the actions of a malicious cyber threat actor.
Services affected may include email, websites, online accounts (e.g., banking), or other services
that rely on the affected computer or network. A denial-of-service condition is accomplished by
flooding the targeted host or network with traffic until the target cannot respond or simply crashes,
1
8
preventing access for legitimate users. DoS attacks can cost an organization both time and money
while their resources and services are inaccessible.
Unit-9
Distributed Operating System
Introduction
A distributed operating system is an operating system that runs on several machines whose purpose
is to provide a useful set of services, generally to make the collection of machines behave more
like a single machine. The distributed operating system plays the same role in making the collective
resources of the machines more usable that a typical single-machine operating system plays in
making that machine’s resources more usable. Usually, the machines controlled by a distributed
operating system are connected by a relatively high-quality network, such as a high-speed local
area network. Most commonly, the participating nodes of the system are in a relatively small
geographical areas, something between an office and a campus.
The Distributed Os involves a collection of autonomous computer systems, capable of
communicating and cooperating with each other through a LAN / WAN. A Distributed Os provides
a virtual machine abstraction to its users and wide sharing of resources like as computational
capacity, I/O and files etc. A distributed operating system (DOS), are systems which model where
distributed applications are running on multiple computers, linked by communications.
1
9
Fig: Distributed system
Applications of Distributed Operating Systems:
• Telecommunication Networks
• Network Applications
• Real Time Process Control
• Parallel Computation
1. Scalability:
As computing occurs on each node independently, it is simple and inexpensive to add more
nodes and functionality as required.
2. Reliability:
Most distributed systems are made from many nodes that work together which ultimately
make them fault tolerant. The system doesn’t experience any disruptions if a single
machine fails.
3. Performance:
These systems are regarded to be very efficient as the work load can be broken up and sent
to multiple machines, therefore reducing data processing.
4. Data sharing:
Nodes can easily share data with other nodes as they are connected with each other.
5. No domino effect in case of a node failure:
The failure of one node in a DOS does not have a domino effect and enables all other nodes
fail. Other nodes can still communicate with each other despite the failure.
6. Shareable:
Resources, for instance like printers, can be shared with multiple nodes rather than just
being constrained to just one node.
1. Scheduling:
1
9
The system has to decide which jobs need to be executed, when they should be executed,
and where they should be executed. A scheduler will have limitations, this may lead to
under-utilised hardware and unpredictable runtimes.
2. Latency:
The more widely distributed a system is the more latency can be experienced with
communications. This therefore results in teams/developers to make tradeoffs between
availability, consistency and latency.
3. Observability:
It can be a real challenge to gather, process, present, and monitor hardware usage metrics
for large clusters.
4. Security:
It is difficult to place adequate security in DOS, as the nodes and the connections need to
be secured.
5. Data loss:
Some data/messages may be lost in the network while moving from one node to another.
6. Complicated database:
In comparison to a single user system, the database connected to a DOS is relatively
complicated and difficult to handle.
7. Overloading:
If multiple nodes in DOS send data all at once, then the system network may become
overloaded. 8. Expensive:
These systems are not readily available, as they are regarded to be very expensive.
9. Complex software:
Underlying software is highly complex and is not understood very well compared to other
systems.
Network Operating System
A network operating system (NOS) is a computer operating system (OS) that is designed primarily
to support workstations, personal computers and, in some instances, older terminals that are
connected on a local area network (LAN).
Network Operating System runs on a server and gives the server the capability to manage data,
users, groups, security, applications, and other networking functions. The basic purpose of the
network operating system is to allow shared file and printer access among multiple computers in
a network, typically a local area network (LAN), a private network or to other networks.
Some examples of network operating systems include Microsoft Windows Server 2003, Microsoft
Windows Server 2008, UNIX, Linux, Mac OS X, Novell NetWare, and BSD. Advantages
The main difference between these two operating systems is that in network operating system each
node or system can have its own operating system on the other hand in distribute operating each
node or system have same operating system which is opposite to the network operating system.
7. In Network Operating System, All nodes can 7. While in Distributed Operating System, All
have different operating system. nodes have same operating system.
Hardware and Software Concepts
All distributed systems consist of multiple CPUs. There are several different ways the hardware
can be arranged. The important thing related to hardware is that how they are interconnected and
how they communicate with each other. It is important to take a deep look at distributed system
hardware, in particular, how the machines are connected together and how they interact.
Many classification schemes for multiple CPU computer systems have been proposed over the
years, but none of them have really implemented. Still, the most commonly used taxonomy is
Flynn's (1972), but it was in basic stage. In this scheme, Flynn took only two things to consider
1
9
i.e. the number of instruction streams and the number of data streams.
1. Single Instruction, Single Data Stream (SISD)
A computer with a single instruction stream and a single data stream is called SISD. All traditional
uni-processor computers (i.e., those having only one CPU) fall under this category, from personal
computers to large mainframes. SISD flow concept is given in the figure below.
1
9
Fig: MISD Flow structure
4. Multiple Instruction, Multiple Data Stream (MISD)
The next category is MIMD, which has multiple instructions performances on multiple data
units. This means a group of independent computers; each has its own program counter,
program, and data. All distributed systems are MIMD, so this classification system is not more
useful for simple purposes.
1
9
Fig: Distributed shared memory
Advantages
Disadvantages
2. Message Passing
Message passing model allows multiple processes to read and write data to the message queue
without being connected to each other. Messages are stored on the queue until their recipient
retrieves them. Message queues are quite useful for interprocess communication and are used by
most operating systems.
1
9
Message passing is the basis of most interprocess communication in distributed systems. It is at
the lowest level of abstraction and requires the application programmer to be able to identify the
destination process, the message, the source process and the data types expected from these
processes.
Communication in the message passing paradigm, in its simplest form, is performed using the
send() and receive() primitives. The syntax is generally of the form:
send(receiver, message)
receive(sender, message)
The send() primitive requires the name of the destination process and the message data as
parameters. The addition of the name of the sender as a parameter for the send() primitive would
enable the receiver to acknowledge the message. The receive() primitive requires the name of the
anticipated sender and should provide a storage buffer for the message.
Machine A Machine B
Task 0 Task 1
Data Data
send () recv()
Task 2 Task 3
Data Data
recv() send ()
Message passing leaves the programmer with the burden of the explicit control of the movement
of data. Remote procedure calls (RPC) relieve this burden by increasing the level of abstraction
and providing semantics similar to a local procedure call.
1
9
The syntax of a remote procedure call is generally of the form: call
procedure_name(value_arguments; result_arguments)
The client process blocks at the call() until the reply is received. The remote procedure is the server
processes which has already begun executing on a remote machine. It blocks at the receive() until
it receives a message and parameters from the sender. The server then sends a reply() when it has
finished its task. The syntax is as follows:
reply(caller, result_parameters)
Clock Synchronization
Distributed System is a collection of computers connected via the high speed communication
network. In the distributed system, the hardware and software components communicate and
coordinate their actions by message passing. Each node in distributed systems can share their
resources with other nodes. So, there is need of proper allocation of resources to preserve the state
of resources and help coordinate between the several processes. To resolve such conflicts,
synchronization is used. Synchronization in distributed systems is achieved via clocks.
In distributed systems, this is not the case. Unfortunately, each system has its own timer that drives
its clock. These timers are based either on the oscillation of a quartz crytal, or equivalent IC.
Although they are reasonably precise, stable, and accurate, they are not perfect. This means that
the clocks will drift away from the true time. Each timer has different characteristics --
characteristics that might change with time, temperature, &c. This implies that each systems time
will drift away from the true time at a different rate -- and perhaps in a different direction (slow or
fast).
Berkeley algorithm
• The server polls each machine periodically, asking it for the time.
• Based on the answer it computes for the average time and tells all the other machine to
advance their clocks to the new time or slow their clocks down until some specific
reduction has been achieved.
• The time daemon’s time must be set manually by operator periodically.
2
0
Logical Clock
Logical Clocks refer to implementing a protocol on all machines within your distributed
system, so that the machines are able to maintain consistent ordering of events within some
virtual timespan. A logical clock is a mechanism for capturing chronological and causal
relationships in a distributed system. Distributed systems may have no physically synchronous
global clock, so a logical clock allows global ordering on events from different processes in
such systems.
Lamport’s algorithm
# event is known
time = time + 1; #
event happens
send(message, time);
2
0
Correcting the clock
2
0
2
0
Tribhuvan University
Faculty of Humanities and Social Sciences
OFFICE OF THE DEAN
2019
Candidates are required to answer the questions in their own words as far as possible.
Group A
Attempt all the questions.
[10×1=10]
2
0
Tribhuvan University
Faculty of Humanities and Social Sciences
OFFICE OF THE DEAN
2019
Group C
Attempt any TWO questions. [2×10= 20]
9. What is CPU scheduling criterias? For the process listed in following table, draw [1+9] Gantt
chart illustrating their execution and calculate average waiting time and turnaround time using:
a) First Come First Serve
b) Shortest Remaining Time Next
c) Priority
d) Round Robin (quantum = 1)
2
0
Process Arrival Time Burst Time (Sec) Priority
P0 0.00 7 3
P1 2.01 7 1
P2 3.01 2 4
P3 3.02 2 2
10. Define the term seek time and rotational delay in disk scheduling. Suppose that a [2+8] disk has
100 cylinders, numbered 0 to 99. The drive is currently serving a request at cylinder 43 and
previous request was at cylinder 25. The queue of pending request, in FIFO order is: 86, 70, 13,
74, 48, 9, 22, 50, 30
Starting from the current head position, what is total distance (in cylinders) that
the disk arm moves at satisfy all pending request for each of following disk
scheduling algorithms?
a) FCFS b) SSTF c) SCAN d) LOOK
11. What is clock synchronization? Explain how physical clock synchronize by [1+9] Berkeley
algorithm and logical clock synchronize by Lamport’s algorithm with suitable example.
2
0
208