0% found this document useful (0 votes)
36 views112 pages

Chapter-1 Introduction To Operating Systems

An operating system (OS) is a program that manages computer hardware and provides a platform for application programs, acting as an intermediary between users and hardware. It aims to enhance user convenience, optimize resource usage, and facilitate problem-solving. The document discusses the structure, functions, and types of operating systems, including single-processor, multiprocessor, and clustered systems, as well as concepts like process management and scheduling.

Uploaded by

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

Chapter-1 Introduction To Operating Systems

An operating system (OS) is a program that manages computer hardware and provides a platform for application programs, acting as an intermediary between users and hardware. It aims to enhance user convenience, optimize resource usage, and facilitate problem-solving. The document discusses the structure, functions, and types of operating systems, including single-processor, multiprocessor, and clustered systems, as well as concepts like process management and scheduling.

Uploaded by

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

CS4TH3: Operating System Chapter 1

CHAPTER-1

Introduction to operating systems

Definition: - An operating system is a program that manages the computer hardware.


It also provides a basis for application programs and acts as an intermediary between the
computer user and the computer hardware.
Operating system goals: -

• An operating system makes a computer system more convenient to use.


• An operating system allows the computer system resources to be used in an
efficient manner.
• Execute user programs and make solving user problems easier.

1.1 What Operating Systems Do:

A computer system can be divided roughly into four components: -


1. Hardware
2. Operating system
3. Application programs
4. Users

Fig 1.1 Abstract view of the components of a computer system

Prepared by: Nirmala G, Asst. Prof, Dept. of CSE, S.S.I.T, Tumkur 1


CS4TH3: Operating System Chapter 1

The hardware consists of memory, central processing unit (CPU), and the input/output
(I/O) devices. It provides the basic computing resources for the system.

System programs consists of compilers, assemblers and editors.

Application programs such as word processors, spreadsheets, etc


The operating system controls the hardware and coordinates its use among the various
application programs for the various users.
Users- People, machines, other computers

Operating systems can be explored from two viewpoints: that of the user and that of the
system

1.1.1 User View


The user's view of the computer varies according to the interface being used.
• Most of the users sit in front of a personal computer, consisting of a monitor,
keyboard, mouse and system unit. Such a system is designed for one use to
monopolize its resources. In this case, the operating system is designed mostly for
ease of use.
• Mainframe or minicomputers is other type of terminal. Many users are accessing
the same computer through other terminals. These users share resources and may
exchange information. The operating system in such cases is designed to
maximize resource utilization.
• Users sit at workstations connected to networks of other workstations and servers.
These users have dedicated resources at their disposal but they also share
resources such as networking and servers.
• Some computers have little or no user view. Ex: Embedded computers

1.1.2 System View

1. We can view an operating system as a resource allocator.


• A computer system has many resources that may be required to solve a problem:
CPU time, memory space, file storage space, I/O devices and so on.

Prepared by: Nirmala G, Asst. Prof, Dept. of CSE, S.S.I.T, Tumkur 2


CS4TH3: Operating System Chapter 1

• The operating system acts as the manager of these resources.


• Decides between conflicting request for efficient and fair resource use.

2. Operating system is a control program.


A control program manages the execution of user programs to prevent errors and
improper use of the computer.

• The interrupt must transfer control to the appropriate interrupt service routine.
• The routine would call the interrupt-specific handler. The interrupt routine is called
indirectly through the table, with no intermediate routine needed. Generally, the table
of pointers is stored in low memory (the first 100 or so locations).
• These locations hold the addresses of the interrupt service routines for the various
devices.
• This array, or interrupt vector, of addresses is then indexed by a unique device
number, given with the interrupt request, to provide the address of the interrupt
service routine for the interrupting device.

1.3 Computer-System Architecture

1.3.1 Single-Processor Systems

• On a single-processor system, there is one main CPU capable of executing a general-


purpose instruction set, including instructions from user processes.
• Almost all systems have other special-purpose processors as well. They may come in
the form of device-specific processors, such as disk, keyboard, and graphics
controllers; or, on mainframes, they may come in the form of more general-purpose
processors, such as I/O processors that move data rapidly among the components of
the system.
• All of these special-purpose processors run a limited instruction set and do not run
user processes.
• For example, a disk-controller microprocessor receives a sequence of requests from
the main CPU and implements its own disk queue and scheduling algorithm.
• PCs contain a microprocessor in the keyboard to convert the keystrokes into codes to
be sent to the CPU.

Prepared by: Nirmala G, Asst. Prof, Dept. of CSE, S.S.I.T, Tumkur 3


CS4TH3: Operating System Chapter 1

1.3.2 Multiprocessor Systems

Multiprocessor systems (also known as parallel systems or tightly coupled systems)


are growing in importance. Suchsystems have two or more processors in close
communication, sharing the computer bus and sometimes the clock, memory, and
peripheral devices.

Multiprocessor systems have three main advantages:

1. Increased throughput: By increasing the number of processors, we expect to get


more work done in less time.
2. Economy of scale: Multiprocessor systems can cost less than equivalent multiple
single-processor systems, because they can share peripherals, mass storage, and power
supplies.
3. Increased reliability: If functions can be distributed properly among several
processors, then the failure of one processor will not halt the system, only slow it down.

Increased reliability of a computer system is crucial in many applications. The ability to


continue providing service proportional to the level of surviving hardware is called
graceful degradation.

Some systems go beyond graceful degradation and are called fault tolerant, because they
can suffer a failure of any single component and still continue operation.

The multiple-processor systems in use today are of two types:

Asymmetric multiprocessing:

• Each processor is assigned a specific task.


• A master processor controls the system; the other processors either look to the master
for instruction or have predefined tasks.
• This scheme defines a master-slave relationship.
• The master processor schedules and allocates work to the slave processors.

Symmetric multiprocessing:

• Each processor performs all tasks within the operating system.


• SMP means that all processors are peers; no master-slave relationship exists between
processors.

Prepared by: Nirmala G, Asst. Prof, Dept. of CSE, S.S.I.T, Tumkur 4


CS4TH3: Operating System Chapter 1

Features of multiprocessor systems:

1. If one processor fails, then another processors should retrieve the interrupted
process state so that execution of the process can continue.
2. The processors should support efficient context switching operation.
3. Multiprocessor system supports large physical address space and large virtual
address space.
4. The IPC mechanism should be provided and implemented in hardware as it
becomes efficient and easy.

1.3.3 Clustered Systems

• Clustered systems gather together multiple CPUs to accomplish computational work.


• Clustered systems differ from multiprocessor systems, however, in that they are
composed of two or more individual systems coupled together.
• Clustered computers share storage and are closely linked via a local-area network
(LAN).
• Clustering is usually used to provide high-availability service; that is, service will
continue even if one or more systems in the cluster fail.
• High availability is generally obtained by adding a level of redundancy in the system.
• Clustering can be structured asymmetrically or symmetrically.
• Asymmetric clustering:

➢ One machine is in hot-standby mode while the other is running the


applications. The hot-standby host machine does nothing but monitor the active
server. If that server fails, the hot-standby host becomes the active server.

• Symmetric clustering:

➢ Two or more hosts are running applications, and are monitoring each other.
This mode is obviously more efficient, as it uses all of the available hardware. It
does require that more than one application be available to run.
Prepared by: Nirmala G, Asst. Prof, Dept. of CSE, S.S.I.T, Tumkur 5
CS4TH3: Operating System Chapter 1

➢ Clusters include parallel clusters and clustering over a wide-area network


(WAN) (as described in Section 1.10). Parallel clusters allow multiple hosts to
access the same data on the shared storage.
➢ To provide shared access to data, the system must also supply access control
and locking to ensure that no conflicting operations occur. This function,
commonly known as a distributed lock manager (DLM). Storage-area
networks (SANs), which allow many systems to attach to a pool of storage.

1.4 Operating-System Structure:

• An operating system provides the environment within which programs are executed.
• One of the most important aspects of operating systems is the ability to multi-
program.
• When two or more programs are in memory at the same time, sharing the processor is
referred to the multiprogramming operating system.

The idea is as follows:

➢ The operating system keeps several jobs in memory simultaneously (Figure 1.7).
➢ This set of jobs can be a subset of the jobs kept in the job pool which contains all
jobs that enter the system.
➢ Since the number of jobs that can be kept simultaneously in memory is usually
smaller than the number of jobs that can be kept in the job pool.
➢ The operating system picks and begins to execute one of the jobs in memory.
➢ The job may have to wait for some task, such as an I/O operation, to complete.

➢ In a non-multiprogrammed system, the CPU would sit idle. In a


multiprogrammed system, the operating system simply switches to, and

Prepared by: Nirmala G, Asst. Prof, Dept. of CSE, S.S.I.T, Tumkur 6


CS4TH3: Operating System Chapter 1

executes, another job. When that job needs to wait, the CPU is switched to
another job, and so on.
➢ Multiprogrammed systems provide an environment in which the various
system resources are utilized effectively.

Advantage:-Multiprogramming increases CPU utilization by organizing jobs


(code and data) so that the CPU always has one to execute.

Disadvantage:-
1. Multiprogrammed systems do not provide for user interaction with the
computer system.
2. To accommodate many jobs in memory, memory management is required.

Time sharing (or multitasking) is a logical extension of multiprogramming.


• In time-sharing systems, the CPU executes multiple jobs by switching
among them, but the switches occur so frequently that the users can
interact with each program while it is running.
• Time sharing requires an interactive (or hands-on) computer system,
which provides direct communication between the user and the
system.
• User gives instructions to the operating system or to a program
directly, using an input device such as a keyboard or a mouse, and
waits for immediate results on an output device.
• Time sharing and multiprogrammed require that several jobs be kept
simultaneously in memory. If several jobs are ready to be brought into
memory, and if there is not enough room for all of them, then the
system must choose among them. Making this decision is job
scheduling.
• When the operating system selects a job from the job pool, it loads that
job into memory for execution.
• If several jobs are ready to run at the same time, the system must
choose among them. Making this decision is CPU scheduling.
• Swapping: Processes are swapped in and out of main memory to the
disk.
• A more common method for achieving this goal is virtual memory, a
technique that allows the execution of a process that is not completely
in memory.

Prepared by: Nirmala G, Asst. Prof, Dept. of CSE, S.S.I.T, Tumkur 7


CS4TH3: Operating System Chapter 3

CHAPTER 3
PROCESS MANAGEMENT

3.1 Process Concept

3.1.1 The Process


Process is a program under execution. It is an active entity and a unit of work. It has a
limited time span.
• Process consists of program code (text section). It also includes the current
activity as represented by the value of the program counter and the contents of the
processor’s registers.
• A process also includes the process stack which contains temporary data(such as
functions parameters, return addresses and local variables).
• It also includes data section which contains global variables.
• A process has heap which is memory i.e dynamically allocated during process
run time.
The structure of a process in memory is shown in the following fig.

Fig: 3.1 Process in memory

Prepared by: Nirmala G, Asst. Prof, Dept. of CSE, S.S.I.T, Tumkur 1


CS4TH3: Operating System Chapter 3

A program is a passive entity such as the contents of a file stored on disk where as
process is an active entity with a program counter specifying the next instruction to
execute and a set of associated resources.

3.1.2 Process State


When process executes, it changes state. Process state is defined as the current activity
of the process.
Process state contains 5 states. Each process is in one of the states. The states are listed
below:
1. New: The process is being created.
2. Running: Instructions are being executed.
3. Waiting: A process that can not execute until some event occurs such as the
completion of an I/O operation.
4. Ready: The process is waiting to be assigned to a processor.
5. Terminated: The process has finished execution.

The state diagram corresponding to these states is shown below:

Fig: 3.2 Diagram of process state

Prepared by: Nirmala G, Asst. Prof, Dept. of CSE, S.S.I.T, Tumkur 2


CS4TH3: Operating System Chapter 3

3.1.3 Process Control Block

Whenever a process is created, a process control block (PCB) is created for the process
by operating system. PCB also called as task control block. This is a data structure that is
used by an OS to keep track of all information concerning a process.
A PCB is shown in the following fig:

Fig 3.3 Process control block

1. Process state: The state may be new, ready, running, waiting, halted and so on.
2. Process number: This is the unique number allocated by the OS to the process on
creation.
3. Program counter: The counter indicates the address of the next instruction to be
executed for this process.
4. CPU registers:
• Number of registers and type of registers totally depends upon the
computer architecture.
• It includes general-purpose registers, stack pointers, index registers and
accumulators, etc.
5. CPU scheduling information: This information includes a process priority,
pointers to scheduling queues and any other scheduling parameters.

Prepared by: Nirmala G, Asst. Prof, Dept. of CSE, S.S.I.T, Tumkur 3


CS4TH3: Operating System Chapter 3

6. Memory-management information: This information may include such


information as the value of the base and limit registers, the page tables or the
segment tables depending on the memory system used by the OS.
7. Accounting information: This information includes the amount of CPU and real
time used, time limits, account numbers, job or process numbers and so on.
8. I/O status information: This information includes the list of I/O devices
allocated to the process, a list of open files and so on.

3.2 Process Scheduling

Process scheduling:- Process scheduling refers to a set of policies and mechanisms built
into the OS that governs the order in which processes should be submitted to the CPU for
execution.

3.2.1 Scheduling Queues

When the process enters into the system, they are put into a job queue. This queue
consists of all processes in the system.
• Ready queue: The processes that are residing in main memory and are ready and
waiting to execute are kept on a list called the ready queue. This queue is stored
as a linked list.
A ready queue header contains pointers to the first and final PCBs in the list. Each
PCB includes a pointer field that points to the next PCB in the ready queue. This
is shown in the following fig.(Ready queue).
• Device queue: The list of processes waiting for a particular I/O device is called a
device queue. Each device has its own device queue as shown below in fig. Ex:
The process makes an I/O request to a shared device such as a disk.

Prepared by: Nirmala G, Asst. Prof, Dept. of CSE, S.S.I.T, Tumkur 4


CS4TH3: Operating System Chapter 3

Fig 3.4 The ready queue and various I/O device queues.

Queuing diagram:
Process scheduling can be represented by queuing diagram as shown in the following
diagram.

Fig 3.5 Queueing diagram representation of process scheduling

• Queue is represented by rectangular box.


• The circles represent the resources that serve the queues.

Prepared by: Nirmala G, Asst. Prof, Dept. of CSE, S.S.I.T, Tumkur 5


CS4TH3: Operating System Chapter 3

• The arrows indicate the flow of processes in the system.

Queues are of 2 types:


Ready queue and a set of device queues.
A newly arrived process is put in the ready queue. It waits in the ready queue until it is
selected for execution. Once the CPU is assigned to the process, then process will
execute.
While executing the process, one of the several events could occur.
1. The process could issue an I/O request and then be placed in an I/O queue.
2. The process could create a new subprocess and wait for the subprocess’s
termination.
3. The process could be removed forcibly from the CPU, as a result of an interrupt
and be put back in the ready queue.
A process continues this cycle until it terminates at that time it is removed from all
queues and has its PCB and resources deallocated.

3.2.2 Schedulers
The scheduler is responsible for deciding which program or process should be executed
on CPU.
Schedulers are of 3 types:
1. Long term scheduler
2. Short term scheduler
3. Medium term scheduler

Long-term scheduler:
• It is also called as job scheduler. Job scheduler selects the processes from this job
pool and loads them into memory for execution.
• The long-term scheduler must make a careful selection. Processes can be
described either I/O bound or CPU bound.

Prepared by: Nirmala G, Asst. Prof, Dept. of CSE, S.S.I.T, Tumkur 6


CS4TH3: Operating System Chapter 3

• I/O bound process: An I/O bound process is one that spends more of its time
doing I/O than it spends doing computations.
• CPU bound process: It generates I/O requests infrequently using more of its time
doing computations.
• Long-term scheduler should select a good process mix of I/O and CPU bound
processes. If all are I/O bound, the ready queue will always be empty.
• If all are CPU bound, I/O device queue will be empty and system will be
imbalanced.
• 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.
• On some systems, the long-term scheduler may be absent or minimal.
• Ex: Time sharing operating systems such as UNIX and Microsoft windows
systems have no long-term scheduler.
• When process changes the state from new to ready, then there is a long-term
scheduler.
Short-term scheduler:
• It is also called CPU scheduler. It selects from among the processes that are ready
to execute and allocates the CPU to one of them.
• Short-term scheduler executes atleast once every 100 milliseconds.

Medium-term scheduler:
Some os such as time-sharing systems may introduce an additional, intermediate level of
scheduling i.e medium-term scheduler.

This medium-term scheduler is shown in the following fig:


Advantage: The medium-term scheduler removes processes from memory and thus
reduce the degree of multiprogramming.

Prepared by: Nirmala G, Asst. Prof, Dept. of CSE, S.S.I.T, Tumkur 7


CS4TH3: Operating System Chapter 3

Later, the process can be reintroduced into memory and its execution can be continued
where it left off. This scheme is called swapping.
The process is swapped out and swapped in by the medium-term scheduler.

Fig 3.6 Addition of medium-term scheduling to the queuing diagram

Comparison between schedulers

Long-term scheduler Short-term scheduler Medium-term scheduler

Also known as job Also known as CPU -


Scheduler. Scheduler.

Speed is less Speed is fast Speed is in between

It controls the degree Less control over Reduces degree of


of multiprogramming. multiprogramming. multiprogramming.

Absent or minimal in Minimal in Time sharing system


Time sharing system. Time sharing system use MTS.

It select processes from It select from among Process can be


pool and load them into the processes that are reintroduced into

Prepared by: Nirmala G, Asst. Prof, Dept. of CSE, S.S.I.T, Tumkur 8


CS4TH3: Operating System Chapter 3

memory for execution. ready to execute. memory and its


execution can be
continued.
Process state is changed Process state is changed This scheduler is
from new to ready. from ready to running. comes into picture
when process is
swapped out.
Therefore, state from
running to wait or
running to reading
and back to running.
3.2.3 Context Switch
• Switching the CPU from one process to another process requires saving the state
of the old process and loading the saved state for the new process. This task is
known as context switch.
• The context of a process is represented in the PCB of a process, it includes the
value of the CPU registers, the process state and memory management
information.
• Context switch is pure overhead because system does not perform any useful
work( or CPU work) while switching.
• The switching speed varies from machine to machine depending on the memory
speed, the no of registers that must be copied, etc.

3.3 Operations on Processes:


• Several operations are possible on the process.
• Process must be created and deleted dynamically.
• OS must provide the environment for the process operation.
Two main operations on processes:
1. Process creation
2. Process termination

Prepared by: Nirmala G, Asst. Prof, Dept. of CSE, S.S.I.T, Tumkur 9


CS4TH3: Operating System Chapter 3

3.3.1 Process creation


A process may create several new processes via a create-process system call during the
course of execution.
The creating process is called a parent process and the new processes are called the
children of that process.
Each of the new processes may in turn create other processes, forming a tree of
processes.
The OS identify processes according to a unique process identifier(pid) which is integer
number.

The C program shown in fig, illustrates the UNIX system calls.


Fig , Creating a separate process using the UNIX fork() system call.

#include <sys/types.h>
#include <stdio.h>
#include <unistd.h>
int main()
{
pid_t pid;
/* fork another process */
pid = fork();
if (pid < 0) { /* error occurred */
fprintf(stderr, "Fork Failed");
return 1;
}
else if (pid == 0) { /* child process */
execlp("/bin/ls", "ls", NULL);
}
else { /* parent process */
/* parent will wait for the child to complete */

Prepared by: Nirmala G, Asst. Prof, Dept. of CSE, S.S.I.T, Tumkur 10


CS4TH3: Operating System Chapter 3

wait (NULL);
printf ("Child Complete");
}
return 0;
}

Fig 3.7 Process creation using fork() system call

3.3.2 Process Termination

A process terminates when it finishes executing its final statement and asks the OS to
delete it by using the exit() system call. At that point process return a status value to its
parent process(via the wait() system call).

All the resources of the process are deallocated by the OS.

For ex:- Terminate process() in Win32 system call is used to terminate the process. The
system call can be invoked only by the parent of the process that is to be terminated.

A parent may terminate the execution of one of its child for a variety of reasons such as
1. The child has exceeded its usage of some of the resources that it has been
allocated.
2. The task assigned to the child is no longer required.

Prepared by: Nirmala G, Asst. Prof, Dept. of CSE, S.S.I.T, Tumkur 11


CS4TH3: Operating System Chapter 3

3. OS does not allow a child to continue if its parent terminates.

Some systems including VMS, do not allow a child to exist if its parent has terminated. In
such systems, if a process terminates then all its children must also be terminated. This
phenomenon is referred to as cascading termination.

Prepared by: Nirmala G, Asst. Prof, Dept. of CSE, S.S.I.T, Tumkur 12


CS4TH3: Operating System Chapter 3

3.4 Interprocess Communication


Process executing concurrently in the operating system may be either independent
processes or co-operating processes.
Independent processes: - The processes which are not affected by each other and they
do not share any data are called independent processes.
Cooperating processes: - The processes which are affected by each other or they share
any data are called cooperating processes.
Process co-operation is needed due to following reasons: -

1. Information sharing: - Many processes need to share the same piece of


information such as files, data, etc. We must provide an environment to allow
concurrent access to such information.
2. Computation speedup: - A task should be broken into smaller subtasks for faster
execution, each will be executing in parallel with each other.
Speedup can be achieved only if the computer has multiple processing elements.
3. Modularity: - System may be developed in modular structure to give high
extensibility and flexibility.
4. Convenience: - An individual user may work on many tasks at the same time. For
instance, a user may be editing, printing and compiling in parallel.

Co-operating processes require an Interprocess communication mechanism that will


allow them to exchange data and information.
There are 2 fundamental models of IPC are:-
1. Shared memory
2. Message passing

In the shared-memory model, a region of memory that is shared by cooperating


processes is established. Processes can then exchange information by reading and writing
data to the shared region.

Prepared by: Nirmala G, Asst. Prof, Dept. of CSE, S.S.I.T, Tumkur 13


CS4TH3: Operating System Chapter 3

In the message-passing model, communication takes place by means of messages


exchanged between the cooperating processes.
The two communication models are shown in the following fig 3.13

3.4.1 Shared-Memory Systems


• IPC using shared memory requires communicating processes to establish a region
of shared memory. Normally OS tries to prevent one process from accessing
another process memory.
• Shared memory requires that two or more processes agree to remove this
restriction. They exchange information by reading and writing data in the shared
areas. The form of the data and the location are determined by these processes and
are not under the OS control. They are also responsible for ensuring that they are
not writing to the same location simultaneously.

To explain the concept of co-operating processes, let’s consider the producer-consumer


problem.
A producer process produces information that is consumed by a consumer process.
Ex: A compiler may produce assembly code which is consumed by an assembler. The
assembler in turn, may produce object modules, which are consumed by the loader.
One solution to the producer-consumer problem uses shared memory.
Two types of buffers can be used.
The unbounded buffer places no practical limit on the size of the buffer. The consumer
may have to wait for new items but the producer can always produce new items.

Prepared by: Nirmala G, Asst. Prof, Dept. of CSE, S.S.I.T, Tumkur 14


CS4TH3: Operating System Chapter 3

1. The bounded buffer assumes a fixed buffer size. In this case, the consumer must
wait if the buffer is empty, and the producer must wait if the buffer is full.

The following variables reside in a region of memory shared by the producer and
consumer processes:
#define BUFFER_SIZE 10
typedef struct {
...
} item;
item buffer[BUFFER_SIZE];
int in = 0;
int out = 0;
The shared buffer is implemented as a circular array with 2 logical pointers in and out.
The variable in points to the next free position in the buffer, out points to the first full
position in the buffer. The buffer is empty when in= =out; the buffer is full when
((in+1)%buffer_size)= = out.
The code for the producer and consumer processes is shown in fig 3.14 and 3.15
respectively
Fig 3.14 The producer process
item nextProduced
while(true)
{
/* Produce an item in nextProduced*/
while ((((in + 1) % buffer_size)= = out)
; /* do nothing*/
buffer[in] = nextProduced;
in = (in + 1) % buffer_size;
}
The producer process has a local variable nextProduced in which the new item to be
produced is stored.

Prepared by: Nirmala G, Asst. Prof, Dept. of CSE, S.S.I.T, Tumkur 15


CS4TH3: Operating System Chapter 3

Fig 3.15 The Consumer process


item nextConsumed;
while(true)
{
while(in= =out)
;// do nothing
nextConsumed=buffer[out];
out=(out+1)%buffer_size;
/* consume the item in next consumed */
}
The consumer process has a local variable nextConsumed in which the item to be
consumed is stored.

3.4.2 Message-Passing Systems

In this model, communication takes place by means of messages exchanged between the
cooperating processes.
Message passing provides a mechanism to allow processes to communicate and to
synchronize their actions without sharing the same address space and is useful in a
distributed environment, where the communicating processes may reside on different
computer connected by a network.
For ex the chat program used on the www
A message passing facility provides at least two operations send(message) and
receive(message). Messages can be of fixed or variable size.

If processes P and Q want to communicate, they must send and receive messages from
each other, a communication link must exist between them.
The link can be implemented in a variety of ways.
1. Direct or Indirect communication
2. Synchronous or Asynchronous communication

Prepared by: Nirmala G, Asst. Prof, Dept. of CSE, S.S.I.T, Tumkur 16


CS4TH3: Operating System Chapter 3

3. Automatic or Explicit buffering.


The issues related to each of these features are:-

3.4.2.1 Naming
The processes that want to communicate, can be either direct or indirect
communication.
In direct communication each process that wants to communicate must explicitly name
the recipient or sender of the communication.
In this scheme send() and receive() primitives are defined as:-
1. Send(P, message) – Send a message to process P
2. Receive(Q, message) – Receive a message from process Q

Properties of communication link:-


1. A link is associated with exactly two processes.
2. Exactly one link exists between each pair of processes.
3. A communication link is established automatically between each pair of processes
that want to communicate.
The above explained scheme is symmetry addressing. Both the sender and receiver
processes use the name for communication.
Asymmetry addressing is also possible in the direct communication. Here only the
sender names the recipient, the recipient is not required to name the sender.
In this scheme, the send() and receive() primitives are defined as follows:-
Send(P, message) – Send a message to process P
Receive( id, message) – Receive a message from any process, the variable id is set to the
name of the process with which communication has taken place.

In indirect communication the messages are sent to and received from mailboxes or
ports. In this scheme a process can communicate with some other process via a number of
different mailboxes. Each mailbox has a unique identification.
Send() and receive() primitives are defined as follows:

Prepared by: Nirmala G, Asst. Prof, Dept. of CSE, S.S.I.T, Tumkur 17


CS4TH3: Operating System Chapter 3

Send(A, mge) - Send a message to mailbox A.


Receive(A, mge) – Receive a message from mailbox A.

Properties of communication link:-


1. A link is established between a pair of processes only if both members of the pair
have a shared mailbox.
2. A link may be associated with more than 2 processes.
3. Between each pair of communicating processes, there may be a number of
different links, with each link corresponding to one mailbox.

Owner of the mailbox is either by process or by the OS. Each mailbox has a unique
owner so that no confusion about receiving message sent to this mailbox.
3.4.2.2 Synchronization

The communication of a message between 2 processes implies some level of


synchronization between the 2 processes.
Message passing may be either blocking or nonblocking also known as synchronous
and asynchronous.
Blocking send: The sending process is blocked until the message is received by the
receiving process or by the mailbox.
Nonblocking send: The sending process sends the message and resumes operation.
Blocking receive: The receiver blocks until a message is available.
Nonblocking receive: The receiver retrieves either a valid message or a null
Both the sender and receiver are blocked until the message is delivered. This is called
Rendezvous.
3.4.2.3 Buffering

Buffering is used in direct and indirect communication. Messages exchanged by


communicating processes reside in a temporary queue.
Buffering is implemented in 3 ways.

Prepared by: Nirmala G, Asst. Prof, Dept. of CSE, S.S.I.T, Tumkur 18


CS4TH3: Operating System Chapter 3

1. Zero capacity: Maximum length of the queue is zero. Communication link can
not have any messages waiting in it. In this case, the sender must block until the
recipient receives message.
2. Bounded capacity: The queue has finite length n thus at most n messages can
reside in it.
3. Unbounded capacity: The queue has potentially infinite length. Any number of
messages can wait in it. The sender never blocks.

Questions:

1. What is a process? With a state diagram, explain states of a process. Also write
the structure of PCB.
2. Explain the process state, with the suitable diagram.
3. What is PCB? Enumerate and explain various fields in PCB.
4. Differentiate between long-term scheduler, short-term scheduler and medium-
term scheduler.
5. Discuss the operations of process creation and process termination in UNIX.
6. Describe the implementation of IPC using shared memory and message passing.
7. Define IPC. What are the different methods used for logical implementation of a
message-passing system? Explain any one.

Prepared by: Nirmala G, Asst. Prof, Dept. of CSE, S.S.I.T, Tumkur 19


CS4TH3 OPERATING SYSTEMS

BACKGROUND

1
CS4TH3 OPERATING SYSTEMS

2
CS4TH3 OPERATING SYSTEMS

THE CRITICAL-SECTION PROBLEM

Consider a system consisting of n processes {P0, P1,..., Pn-1}. Each process has a
segment of code, called a critical section, in which the process may be changing common
variables, updating a table, writing a file, and so on. The important feature of the system is
that, when one process is executing in its critical section, no other process is to be allowed
to execute in its critical section.

3
CS4TH3 OPERATING SYSTEMS

That is, no two processes are executing in their critical sections at the same time.
The critical-section problem is to design a protocol that the processes can use to cooperate.
Each process must request permission to enter its critical section. The section of code
implementing this request is the entry section. The critical section may be followed by an
exit section. The remaining code is the remainder section. The general structure of a
typical process Pi, is shown in the following figure:

A solution to the critical-section problem must satisfy the following three requirements:

1. Mutual exclusion. If process P, is executing in its critical section, then no other


processes can be executing in their critical sections.
2. Progress. If no process is executing in its critical section and some processes wish to
enter their critical sections, then only those processes that are not executing in their
remainder sections can participate in the decision on which will enter its critical section
next, and this selection cannot be postponed indefinitely.
3. Bounded waiting. There exists a bound, or limit, on the number of times that other
processes are allowed to enter their critical sections after a process has made a request to
enter its critical section and before that request is granted.
We assume that each process is executing at a nonzero speed. However, we can make
no assumption concerning the relative speed of the n processes.
Two general approaches are used to handle critical sections in operating systems:
(1) preemptive kernels and (2) nonpreemptive kernels.
A preemptive kernel allows a process to be preempted while it is running in kernel
mode.
A nonpreemptive kernel does not allow a process running in kernel mode to be
preempted; a kernel-mode process will run until it exits kernel mode, blocks, or voluntarily

4
CS4TH3 OPERATING SYSTEMS

yields control of the CPU. Obviously, a nonpreemptive kernel is essentially free from race
conditions on kernel data structures, as only one process is active in the kernel at a time.
We cannot say the same about nonpreemptive kernels, so they must be carefully
designed to ensure that shared kernel data are free from race conditions. Preemptive kernels
are especially difficult to design for SMP architectures, since in these environments it is
possible for two kernel-mode processes to run simultaneously on different processors.
A preemptive kernel is more suitable for real-time programming, as it will allow a
real-time process to preempt a process currently running in the kernel. Furthermore, a
preemptive kernel may be more responsive, since there is less risk that a kernel-mode
process will run for an arbitrarily long period before relinquishing the processor to waiting
processes.

5
CS4TH3 OPERATING SYSTEMS

To prove that this solution is correct, we need to show that:


1. Mutual exclusion is preserved.
2. The progress requirement is satisfied.
3. The bounded-waiting requirement is met.

6
CS4TH3 OPERATING SYSTEMS

SYNCHRONIZATION HARDWARE

7
CS4TH3 OPERATING SYSTEMS

8
CS4TH3 OPERATING SYSTEMS

9
CS4TH3 OPERATING SYSTEMS

10
CS4TH3 OPERATING SYSTEMS

SEMAPHORES

A semaphore S is an integer variable that, apart from initialization, is accessed only


through two standard atomic operations: wait () and signal (). The wait () operation was
originally termed P; signal() was originally called V. The definition of wait () is as follows:

11
CS4TH3 OPERATING SYSTEMS

wait(S) {
while S <= 0
; // no-op S--;
}

The definition of signal () is as follows:

signal(S) {
S++;
}

All the modifications to the integer value of the semaphore in the wait () and signal
() operations must be executed indivisibly. That is, when one process modifies the
semaphore value, no other process can simultaneously modify that same semaphore value.
In addition, in the case of wait(S), the testing of the integer value of S (S < 0), and its
possible modification (S—), must also be executed without interruption.

Operating systems often distinguish between counting and binary semaphores. The
value of a counting semaphore can range over an unrestricted domain. The value of a binary
semaphore can range only between 0 and 1. On some systems, binary semaphores are
known as mutex locks, as they are locks that provide mutual exclusion.

We can use binary semaphores to deal with the critical-section problem for multiple
processes. The n processes share a semaphore, mutex, initialized to 1. Each process Pi is
organized as shown:
do {
waiting(mutex);
// critical section signal
(mutex) ,-
// remainder section
}while (TRUE);

12
CS4TH3 OPERATING SYSTEMS

We can also use semaphores to solve various synchronization problems. For example,
consider two concurrently running processes: P1 with a statement S1 and P2 with a
statement S2. Suppose we require that P2 be executed only after S1 has completed. We can
implement this scheme readily by letting P1 and P2 share a common semaphore synch,
initialized to 0, and by inserting the statements.

S1;
signal(synch);

in process P1, and the statements

wait(synch);
S2;

in process P2. Because synch is initialized to 0, P2 will execute S2 only after P1 has
invoked signal (synch), which is after statement S1 has been executed.
Semaphore Implementation:

The main disadvantage of the mutual-exclusion solutions and of the semaphore definition given
here, isthat they all require busy waiting. While a process is in its critical section, any other process
that tries to enter its critical section must loop continuously in the entry code. This continual
looping is clearly a problem in a real multiprogramming system, where a single CPU is shared
among many processes. Busy waiting wastes CPU cycles that some other process might be able to
use productively. This type of semaphore is also called a spinlock (because the process "spins"
while waiting for the lock). Spinlocks are useful in multiprocessor systems. The advantage of a
spinlock is that no context switch is required when a process must wait on a lock, and a context
switch may take considerable time. Thus, when locks are expected to be held for short times,
spinlocks are useful.
To overcome the need for busy waiting, we can modify the definition of the wait and signal
semaphore operations. When a process executes the wait operation and finds that the semaphore
value is not positive, it must wait. However, rather than busy waiting, the process can block itself.
The block operation places a process into a waiting queue associated with the semaphore, and the
state of the process is switched to the waiting state. Then, control is transferred to the CPU
scheduler, which selects another process to execute.
A process that is blocked, waiting on a semaphore S, should be restarted when some other
process executes a signal operation. The process is restarted by a wakeup operation, which changes
the process from the waiting state to the ready state. The process is then placed in the ready queue.
(The CPU may or may not be switched from the running process to the newly ready process,
depending on the CPU-scheduling algorithm.)
To implement semaphores under this definition, we define a semaphore

13
CS4TH3 OPERATING SYSTEMS

as a "C" struct:

typedef struct {
int value ;
struct process *L;
) semaphore;

Each semaphore has an integer value and a list of processes. When a process must wait on a
semaphore, it is added to the list of processes. A signal operation removes one process from the
list of waiting processes and awakens that process.
The wait semaphore operation can now be defined as

void
wait(semaphore
S) {S.value--;
if (S.value < 0) {
add this process
to S . L; block()
;
}
The signal semaphore operation can now be
defined as void signal(semaphore S) {
S.value++;
if (S.value <= 0) {
remove a process P
from S . L ; wakeup
(PI) ;
}

The block operation suspends the process that invokes it. The wakeup(P1) operation resumes the
execution of ablocked process P. These two operations are provided by the operating system as
basic system calls.

Binary Semaphores
The semaphore construct described in the previous sections is commonly known as a counting
semaphore, since its integer value can range over an unrestricted domain. A binary semaphore is
a semaphore with an integer value that can range only between 0 and 1. A binary semaphore can
be simpler to implement than a counting semaphore, depending on the underlying hardware
architecture. We will now show how a counting semaphore can be implemented using binary
semaphores. Let S be a counting semaphore.
To implement it in terms of binary semaphores we need the following data structures:
binary-semaphore S1, S2;
int C;

14
CS4TH3 OPERATING SYSTEMS

Initially S1 = 1, S2 = 0, and the value of integer C is set to the initial value of the counting
semaphore S.The wait operation on the counting semaphore S can be implemented as
follows:
wait (S1) ;
C--;
i f (C < 0) {
signal(S
1) ; wait
(S2) ;
}
signal(S1);
The signal operation on the counting semaphore S can be
implemented as follows: w a i t (S1) ;
C++ ;
i f (C <= 0)
signal (S2)
;e l s e
signal (S1)
;
Classic Problems of Synchronization
We present a number of different synchronization problems as examples for a large class of
concurrency-control problems. These problems are used for testing nearly every newly proposed
synchronization scheme. Semaphores are used for synchronization in our solutions.

i. THE BOUNDED-BUFFER PROBLEM

The bounded-buffer problem is commonly used to illustrate the power of


synchronization primitives.
We assume that the pool consists of n buffers, each capable of holding one item.
The mutex semaphore provides mutual exclusion for accesses to the buffer pool and is
initialized to the value 1. The empty and full semaphores count the number of empty and
full buffers. The semaphore empty is initialized to the value n; the semaphore full is
initialized to the value 0.
The code for the producer process is shown:
do{
// produce an item in nextp
...
wait(empty); wait ( mutex)
// add nextp to buffer
...
signal(mutex);
15
CS4TH3 OPERATING SYSTEMS

signal (full);
}while (TRUE);

The code for the consumer process is shown:

do {
wait (full);
wait(mutex);
...
//remove an item from buffer to nextc
...
signal(mutex);
signal(empty);
//consume the item in nextc
...
}while (TRUE);

ii. THE READERS-WRITERS PROBLEM

A database is to be shared among several concurrent processes. Some of these


processes may want only to read the database, whereas others may want to update (that is,
to read and write) the database. We distinguish between these two types of processes by
referring to the former as readers and to the latter as writers. Obviously, if two readers
access the shared data simultaneously, no adverse affects will result. However, if a writer
and some other thread (either a reader or a writer) access the database simultaneously,
chaos may ensue.
To ensure that these difficulties do not arise, we require that the writers have
exclusive access to the shared database. This synchronization problem is referred to as the
readers-writers problem.
In the solution to the first readers-writers problem, the reader processes share the
following data structures:
semaphore mutex, wrt;
int readcount;

The semaphores mutex and wrt are initialized to 1; readcount is initialized to 0.


The semaphore wrt is common to both reader and writer processes. The mutex semaphore
is used to ensure mutual exclusion when the variable readcount is updated. The readcount

16
CS4TH3 OPERATING SYSTEMS

variable keeps track of how many processes are currently reading the object. The
semaphore wrt functions as a mutual-exclusion semaphore for the writers.
The code for a writer process is shown:

The code for a reader process is shown:

iii. THE DINING-PHILOSOPHERS 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 center 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 neighbors).

17
CS4TH3 OPERATING SYSTEMS

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 neighbor. When a hungry philosopher
has both her chopsticks at the same time, she eats without releasing her chopsticks. When
she is finished eating, she puts down both of her chopsticks and starts thinking again.

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. Thus, the shared data are
semaphore chopstick[5];

where all the elements of chopstick are initialized to 1. The structure of philosopher i is
shown

18
CS4TH3 OPERATING SYSTEMS

MONITORS

Incorrect use of semaphore operations:


• suppose that a process interchanges the order in which the wait() and signal() operations
on the semaphore mutex are executed, resulting in the following execution:
signal(mutex);
...
critical section
...
wait(mutex);
• suppose that a process replaces signal(mutex) with wait(mutex). that is, it executes

wait(mutex);
...
critical section
...
wait(mutex);
in this case, a deadlock will occur.

• suppose that a process omits the wait(mutex), or the signal(mutex), or both. in this case,
either mutual exclusion is violated or a deadlock will occur.
A type, or abstract data type, encapsulates private data with public methods to
operate on that data. A monitor type presents a set of programmer-defined operations that
are provided mutual exclusion within the monitor.
Monitor type also contains the declaration of variables whose values define the state
of an instance of that type, along with the bodies of procedures or functions that operate on
those variables. The syntax of a monitor is shown:

19
CS4TH3 OPERATING SYSTEMS

20
CS4TH3 OPERATING SYSTEMS

i. DINING-PHILOSOPHERS SOLUTION USING MONITORS

This solution imposes the restriction that a philosopher may pick up her chopsticks
only if both of them are available. To code this solution, we need to distinguish among
three states in which we may find a philosopher. For this purpose, we introduce the
following data structure:

enum {thinking, hungry, eating}state [5];

21
CS4TH3 OPERATING SYSTEMS

Philosopher i can set the variable state [i] = eating only if her two neighbors are
not eating: (state [(i+4) % 5] != eating) and (state [(i+1) % 5] != eating). We also need
to declare

condition self [5];

where philosopher i can delay herself when she is hungry but is unable to obtain
the chopsticks she needs.
The distribution of the chopsticks is controlled by the monitor dp, whose definition
is shown below.(next page)
Each philosopher, before starting to eat, must invoke the operation pickup (). This
may result in the suspension of the philosopher process. After the successful completion of
the operation, the philosopher may eat. Following this, the philosopher invokes the
putdown() operation. Thus, philosopher i must invoke the operations pickup() and
putdown() in the following sequence:
dp.pickup(i);
...
eat
...
dp.putdown(i);

22
CS4TH3 OPERATING SYSTEMS

ii. IMPLEMENTING A MONITOR USING SEMAPHORES

We now consider a possible implementation of the monitor mechanism using


semaphores. For each monitor, a semaphore mut ex (initialized to 1) is provided. A process
must execute wait(mutex) before entering the monitor and must execute signal (mutex)
after leaving the monitor. Since a signaling process must wait until the resumed process
either leaves or waits, an additional semaphore, next, is introduced, initialized to 0, on
which the signaling processes may suspend themselves.
An integer variable next_count is also provided to count the number of processes
suspended on next. Thus, each external procedure F is replaced by

23
CS4TH3 OPERATING SYSTEMS

wait(mutex) ;
...
body of F
...
if (next_count > 0)
signal(next);
else
signal(mutex);

Mutual exclusion within a monitor is ensured.


We can now describe how condition variables are implemented. For each condition
x, we introduce a semaphore x_sem and an integer variable x_count, both initialized to 0.
The operation x. wait () can now be implemented as
x_count++;
if (next_count > 0) signal(next);
else
signal(mutex);
wait(x_sem);
x_count—;

The operation x. signal () can be implemented as

if (x_count > 0) {
next_count++;
signal(x_sem);
wait(next) ;
next_count—;
}

24
Deadlocks

DEADLOCKS

Deadlock:- If a process has to get executed it needs some resources for completion of the task.

A process requests resources and if the resources are not available at that time, the process enters
a waiting state. Sometimes a waiting process is never again able to change state, because the
resource it has requested are held by other waiting processes. The situation is called a deadlock.

Example: Two processes P1 and P2 are running simultaneously P1 requests for resource R1 and
P2 requests for resource R2. Both requests are granted. Now P1 requests for R2 and P2 requests
for R1. Both processes cannot finish the execution because P1 is asking for R2 which is held by
P2 and P2 is asking for R1 which is held by P1.

R1

P1 P2

R2

System Model:

A system consists of a finite number of different types of resources (physical (for eg RAM,
CPU, printer,etc) or logical(for eg file)) for the competing processes. Eg: memory space, CPU
cycle files, I/O devices.

Each resource may have some number of identical instances. If a system has 2 CPUs, then the
resource type CPU has 2 instances.

Identical instances are those instances when allocation of any (resource type) instance will
satisfy the request.

For non-identical instances, separate resource classes should be defined.

Nirmala G, Dept of CSE, SSIT 1


Deadlocks

A process must request a resource before using it and must release the resources after using it

Under the normal mode of operation, a process may utilize a resource in only the following
sequence.

1. Request: The process requests the resource. If the request can’t be granted immediately
(for example, if the resource is being used by another process) then the requesting
process must wait until it can acquire the resource.
2. Use: The process uses the resource if resource is available. Ex: if the resource is a printer
the process can print on the printer.
3. Release: The process releases the resource when work is over.

A process may request for any number of resources but it should not exceed the total number of
resources.

The request and release of resources are carried out through system calls. Eg: request/release
device, open\close files, allocate\free memory.

Deadlock Characterization

In a deadlock, processes never finish executing and system resources are tied up, preventing
other jobs from starting.

Necessary Conditions

A deadlock situation can arise if the following four conditions hold simultaneously in a system.

1. Mutual Exclusion: At least one resource must be held in a non-sharable mode i.e only
one process at a time can use the resource. If another process requests that resource, the
requesting process must be delayed until the resource has been released.
2. Hold and Wait: A process is holding a resource and waits for another resource that are
currently being held by other processes.
3. No preemption: Once the resource is allocated to a process it can’t be preempted i.e a
resource can be released only voluntarily by the process holding it.

Nirmala G, Dept of CSE, SSIT 2


Deadlocks

4. Circular wait: A set {P0, P1, . . . Pn} of waiting processes must exist such that P0 is
waiting for a resource held by P1, P1 is waiting for a resource held by P2 and Pn is
waiting for a resource held by P0.

Note: All the above 4 conditions have to be satisfied if deadlock has to occur. If any one of the
conditions is not satisfied then deadlock will not occur.

Resource – Allocation Graph

Deadlocks can be described more precisely in terms of a directed graph called a system
resource allocation graph.

Resource allocation graph is a graph that consists of set of vertices ‘V’ and set of edges ‘E’.

The set of vertices V is partitioned into 2 different types of Nodes.

1. P={P1, P2, … Pn} the set consisting of all the active processes in the system.
2. R={R1, R2…Rm} the set consisting of all resource types in the system.

The process vertex is pictorially represented as a circle and resource vertex is represented as a
rectangular box. The instances of the resource is represented as dots within the rectangle.

For ex: R1

P1 :

The two types of edges are

1. Request edge: The directed edge from a process to a resource is known as request
edge(shown in below fig a). Requesting of resource by process is denoted by request
egde.

For ex: P1 R1
2. Assignment edge: The directed edge from instance of a resource to process is known as
assignment edge(shown in below fig b). Holding a resource by process is denoted by
assignment edge.

For ex: R1 P1

Nirmala G, Dept of CSE, SSIT 3


Deadlocks

Fig(a & b)

P1 P1

R1 : R1 :

The following fig shows the resource-allocation graph. System consists of 3 processes i.e P1, P2
and P3 and 4 resources i.e R1, R2, R3, and R4. Resources R1 and R3 have one instance, R2 has
two instances and R4 has 3 instances.

1. The sets P, R, and E consists

• P = {P1, P2, P3}


• R = {R1, R2, R3, R4}
• E = { P1→R1, P2→R3, R1→P2, R2→P2, R2→P1, R3→P3 }
2. Resource instances

• Resource R1 has 1 instance


• Resource R2 has 2 instances
• Resource R3 has 1 instance
• Resource R4 has 3 instances
3. Process states

• Process P1 is holding an instance of resource type R2 and is waiting for an


instance of resource type R1.
• Process P2 is holding an instance of R1 and R2 and waiting for an instance of
resource type R3.
• Process P3 is holding an instance of R3.

Nirmala G, Dept of CSE, SSIT 4


Deadlocks

Resource – Allocation Graph

If the graph contains no cycles, then no process in the system is deadlock. If the graph does
contain a cycle, then a deadlock may exist.

Suppose that process P3 requests an instance of resource type R2. Since no resource instance is
currently available, a request edge P3→ R2 is added to the graph. This is shown in the below fig.
Resource allocation graph with deadlock. At this point, two minimal cycles exist in the system.

P1→ R1→P2→R3→P3→R2→P1

P2→R3→P3→R2→P2

Process P1, P2 and P3 are deadlocked.

Fig: Resource allocation graph with a deadlock

Process P2 is waiting for the resource R3, which is held by process P3.

Process P3 is waiting for either process P1 or P2 to release resource R2.


Nirmala G, Dept of CSE, SSIT 5
Deadlocks

Process P1 is waiting for process P2 to release resource R1.

Resource allocation graph with a cycle but no deadlock as shown in the below fig:

Let us consider,

P1→ R1→P3→R2→P1

There is cycle but no deadlock. Because the process P4 may release its instance of resource type
R2. That resource can then be allocated to P3, breaking the cycle.

Methods for handling Deadlocks:


There are 3 solutions to handle deadlocks.

1. Protocol: Using protocol, we can prevent or avoid deadlocks. To take care that, system
will never enter a deadlock state.
2. Detect and recover: Allow the system to enter a deadlock state, detect it and recover
from deadlock.
3. Ignore the deadlock: To ensure the deadlock never occurs in the system.

The third solution is the one used by most operating systems including UNIX and Windows.

To ensure that deadlocks never occur, the system can use either a deadlock prevention or a
deadlock avoidance scheme.

Deadlock prevention is a set of methods for ensuring that at least one of the necessary
conditions cannot hold. These methods prevent deadlocks by constraining how requests for
resources can be made.

Deadlock avoidance requires that the OS be given in advance, additional information


concerning which resources a process will request and use during its lifetime.

Nirmala G, Dept of CSE, SSIT 6


Deadlocks

If a system does not employ either a deadlock prevention or a deadlock avoidance algorithm,
then a deadlock situation may occur.

If a system does not ensure that a deadlock will never occur and also does not provide a
mechanism for deadlock detection and recovery, then the system is in a deadlock state.

Deadlock Prevention
Deadlock prevention provides a set of methods for ensuring that at least one of the necessary
conditions cannot hold, that leads to deadlock.

Mutual Exclusion
The mutual exclusion means a non-sharable resource cannot be shared by multiple processes
simultaneously. A non-sharable resource like printer cannot be simultaneously shared by several
processes. Therefore, mutual exclusion cannot be prevented. Read-only-files are a good example
of a sharable resource. Several process can read the file at the same time.

Hold and Wait


To ensure that hold and wait condition never occur in the system, we must guarantee that
whenever a process request a resource, it does not hold any other resources.

To prevent hold and wait two algorithms will be used.

In first algorithm, the execution of a process can begins only if it gets all the resource.

In second algorithm, the process can request a resource only if it is not holding anything.

For ex: There is a process that copies data from a DVD drive to a file on disk, sorts the file and
then prints the results to a printer. Therefore, it needs three resources for completion of task i.e
DVD drive, file and printer.

The first algorithm is used when OS has to allocate all 3 resources before execution begins. But
the disadvantage is resources will not be utilized to the maximum extent i.e the process would
need the printer only during the end. As printer is already held by P1 it will be unavailable for
other processes.

In second algorithm the process will initially request for DVD drive and file. Once copying and
sorting is complete it will release both these resource and then request for the printer and disk
file. After copying the disk file to the printer, it releases these two resources and terminates.

Nirmala G, Dept of CSE, SSIT 7


Deadlocks

No preemption
Resources allocated to a process should be taken away forcibly from it whenever required. Two
implementations are possible.

1. If a process is holding some resources and requests another resource that can not be
immediately allocated to it, then all the resources currently being held are preempted.
2. When a process requests some resources
a. Check whether resources are available or not
b. If resources are available, allocate them
c. If not, check whether resources are allocated to other processes which are waiting for
additional resources.
d. If so, preempt the desired resources from waiting process and allocate them to the
requesting process.

This method is applicable only when state of resources can be saved and restored when
necessary. Ex CPU registers. But with printers and tape drives it is not possible.

Circular wait
One way to prevent circular wait condition is by linear ordering of different types of system
resources and to require that each process requests resources in an increasing order of
enumeration.

Therefore, a function will be defined on the resources.

Eg: F(Tape drive) = 1

F(disk drive) = 5

F(printer) = 12

The request and grant of a resource works like this initially if a process is not having any
resource and request for a resource then OS will grant it.

Eg: P1 request for R1, R1 will be granted. Now if P2 requests R2, the resource will be allocated
only if F(R2) > F(R1) or F(R1) < F(R2). This can be proved by contradiction.

Assume there are 3 processes involved in circular wait.

The request for P1 will be granted if F(R1) < F(R2)→ 1

The request for P2 can be granted if F(R2) < F(R3)→2

The request for P3 can be granted if F(R3) < F(R1)→3


Nirmala G, Dept of CSE, SSIT 8
Deadlocks

Combining equations 1, 2, 3 we get

F(R1) < F(R2) < F(R3) < F(R1)

Analyzing the above equation, the condition F(R1) < F(R2) cannot be satisfied. Therefore there
can be no circular wait.

Deadlock Avoidance
In deadlock avoidance algorithm, the OS will be provided with extra information a priori to the
execution of the process.

For ex: How many processes will be there in the system? What are the different resources that
these processes might use? The order of request of these resources etc.

If this additional information is provided to the OS in advance, then deadlock can be avoided.

There are two types of deadlock avoidance algorithm

1. Deadlock avoidance of a single resource instance. [Resource-allocation-graph algorithm]


2. Deadlock avoidance of a multiple instance of the resource. [Banker’s Algorithm]
7.5.1 Safe state
A state is safe if the system can allocate resources to each process in some order and still avoid a
deadlock. Or a system is in a safe state only if there exists a safe sequence(a possible sequence
of processes for execution)

For eg P1, P2, …. Pn has a safe sequence if for each process (Pi) required resources can be
granted in some order.

A safe state is not a deadlock state. Conversely, a deadlock state is an unsafe state. Not all
unsafe states are deadlocks as shown in the below fig.

An unsafe state may lead to a deadlock. As long as the state is safe, an operating system can
avoid unsafe state. In an unsafe state, the operating system cannot prevent processes from
requesting resource such that a deadlock occurs.

Nirmala G, Dept of CSE, SSIT 9


Deadlocks

Fig Safe, unsafe and deadlocked state spaces

For ex: A system consists of 3 processes P1, P2 and P3 and one resource R1. Number of units
(instances) for R1 is 12.

Process Maximum Need Current Need


P1 10 5
P2 4 2
P3 9 2

At time t0, remaining resources = 12 – 9(Sum of current need)= 3

Now available resources(R1) are 3.

• The safe sequence for this is {P2, P1, P3}. This sequence satisfy the safety condition.
Safe sequence is calculated as follows.
• Process P2 can be allocated all its resources(R1) and then return to the system. [At
t1→P2→2, 3-2=1 instances remaining] Now available resource (R1) becomes 5 after
returning process P2.
• Now process P1 can get all its R1 and return them to the system. [ At t2→P1→5, 5-5=0]
Need of process P1 is 5 and in system R1 is available with 5. Now available resource
(R1) becomes 10 after returning process P1.
• Now process P3 could get all its R1 resource and return then to the system.
• Finally, the system will have 12 (R1) resources available.

Resource-allocation-graph Algorithm [ Deadlock avoidance for Single


Instance]
In this algorithm, the resource allocation graph will consists of one more type of edge apart from
request and assignment edge, this edge is known is claim edge and direction of the edge is from
process to resource i.e it is similar to request edge but the line is dashed line.

Nirmala G, Dept of CSE, SSIT 10


Deadlocks

Steps to draw graph:

1. First draw all claim edges.


2. Convert claim edge into request edge whenever request is made such that no cycle should
occur.
3. Convert request edge into assignment edge whenever resource is free such that no cycle
should occur.
4. When resource(Rj) is released by process (Pi), assignment edge (Rj→Pi) is reconverted
into claim edge.

Cycle detection algorithm is used for detecting cycle in the graph. If no cycle exists, then the
allocation of resources will leave the system in a safe state. If a cycle is found, then the allocation
puts the system is an unsafe state.

In the above fig 1, first draw all claim edges.

In the above fig 2, P1 is requesting for R1 therefore the claim edge is converted to request edge.

In the above fig 3, the request edge will be converted to assignment edge only if there are no
cycles in the graph. Therefore, allocating R1→P1 does not form any cycle. Request edge is
converted to assignment edge.

In the above fig 4, P2 will request for R1 then claim edge is converted to request edge. But this
request edge cannot be converted into assignment edge because already R1 is held by P1.

In the above fig 5, P2 will request for R2 then claim edge is converted to request edge.

In the above fig 6, OS will try to convert this request edge to assignment edge. By allocating R2
to P2 will lead to the formation of cycle in the resource allocation graph, creating a deadlock
between P1 and P2. Therefore R2 will not be assigned to P2 preventing from deadlock.

Then the resulting fig will be

Fig: Resource allocation graph for deadlock avoidance( no cycle)

Fig: An unsafe state in a resource allocation graph (forming a cycle)

Nirmala G, Dept of CSE, SSIT 11


Deadlocks

Banker’s Algorithm [ Deadlock avoidance for multiple instances]


Banker’s algorithm is suitable to a resource allocation system with multiple instances of each
resource type.

The name was chosen because the algorithm could be used in a banking system to ensure that the
bank never allocated its available cash in such a way that it could no longer satisfy the needs of
all its customers.

Several data structures must be maintained to implement the banker’s algorithm. Let n be the
number of processes in the system and m be the number of resource types. We need the
following data structures.

1. Available: This is a vector of length m that gives the information about the number of
resources currently available in the system of each type.
2. Max: This is a matrix of n*m that gives the information about the maximum demand of
each process w.r.t the resources.
3. Allocation: An n*m matrix that gives the information about the number of instances(or
resources) currently allocated to the process.
4. Need: This is a matrix of n*m that gives the information about the remaining number of
resources that the process will need to finish the task.
Need= Max - Allocation

Banker’s algorithm is divided into 2 algorithms

1. Safety algorithm
2. Resource request algorithm

Safety Algorithm
It is used to find the state of the system, i.e system may be in safe state or unsafe state.

1. Let Work and Finish be vectors of length m and n, respectively.


Initialize: Work = Available and Finish [i] = false for i = 0, 1, …, n- 1.

Nirmala G, Dept of CSE, SSIT 12


Deadlocks

2. Find an index i such that both:


Finish [i] = =false
Needi ≤ Work
If no such i exists, go to step 4.
3. Work = Work + Allocation
Finish[i] = true
go to step 2.
4. If Finish [i] == true for all i, then the system is in a safe state.

Resource-Request Algorithm
Let Requesti be the request vector for process Pi. If Requesti [j] = = k, then
process Pi wants k instances of resource type Rj.

When a request for resources is made by process Pi, the following actions are taken:

1. If Requesti ≤ Needi go to step 2. Otherwise, raise error condition, since process has
exceeded its maximum claim.
2. If Requesti ≤ Available, go to step 3. Otherwise, Pi must wait, since resources are not
available.
3. Pretend to allocate requested resources to Pi by modifying the state as follows:
Available = Available – Request;
Allocationi= Allocationi + Requesti;
Needi = Needi – Requesti;

If safe, the resources are allocated to Pi.


If unsafe, Pi must wait and the old resource-allocation state is restored.

Deadlock Detection
If the system is not using any deadlock avoidance and deadlock prevention algorithm, then a
deadlock situation may occur. In this the system must provide:

• An algorithm that examines the state of the system to determine whether a deadlock has
occurred.
• An algorithm to recover from the deadlock.

Single Instance of Each Resource Type


If all resources have only a single instance, then deadlock detection algorithm that uses a variant
of the resource-allocation graph, called a wait-for graph. This graph is obtained from the
resource allocation graph by removing the resource nodes and collapsing the appropriate
edges(i.e assignment edge and request edge)

Nirmala G, Dept of CSE, SSIT 13


Deadlocks

The below fig shows the resource allocation graph with corresponding wait for graph.

Fig: Resource Allocation graph (a) Corresponding wait for graph (b)

In this example cycle exists in wait-for graph, thus deadlock occurs.

To detect deadlock, the system needs to maintain the wait for graph and periodically invoke an
algorithm that searches for a cycle in the graph.

For detecting a cycle in the graph, algorithm requires n2 operation where n is the number of
vertices in the graph.

Several Instances of a Resource Type


It is similar to Banker’s algorithm. We need the following data structures:

1. Available: This is a vector of length m that gives the information about the number of
resources currently available in the system of each type.
2. Allocation: An n*m matrix that gives the information about the number of instances (or
resources) currently allocated to the process.
3. Request: An n*m matrix indicates the current request of each process.

Deadlock Detection Algorithm:

1. Let Work and Finish be vectors of length m and n, respectively Initialize:


(a) Work = Available
(b) For i = 0,1 …, n-1, if Allocationi ≠ 0, then
Finish[i] = false; otherwise, Finish[i] = true.
2. Find an index i such that both:
(a) Finish[i] == false
(b) Requesti ≤ Work
If no such i exists, go to step 4.
Nirmala G, Dept of CSE, SSIT 14
Deadlocks

3. Work = Work + Allocationi


Finish[i] = true
go to step 2.
4. If Finish[i] == false, for some i, 0 ≤ i ≤ n, then the system is in deadlock state. Moreover, if
Finish[i] == false, then Pi is deadlocked.

Algorithm requires an order of (m x n2) operations to detect whether the system is in deadlocked
state.

Detection-Algorithm Usage

To invoke the detection algorithm depends on two factors.


1. Frequency of deadlock occurrence.
2. Number of processes affected by deadlock.

As we know:

• Deadlock makes the resources idle.


• Number of processes may grow in deadlock.
• Any request may create deadlock condition.

Thus, if deadlock occurs frequently and many processes are getting affected, detection-algorithm
should be run frequently.

1. Whenever a process requests, we can invoke detection algorithm.


Advantages: Deadlock can be detected as soon as it occurs, thus low resource utilization
can be avoided.

Disadvantages: It will increase overhead in computation time.

2. Algorithm can be invoked at less frequent intervals for ex once per hour, when CPU
utilization drops down 40%.
Advantages: Computation overhead will be less.

Disadvantages: Low resource utilization and less throughput will be caused. Partial
computation finished by processes will be lost.

Recovery from Deadlock

Once a deadlock situation has been identified by the detection algorithm any of the following
actions can be taken:

1. Recover manually
2. System recovers automatically. There are 2 ways:
Nirmala G, Dept of CSE, SSIT 15
Deadlocks

a. Process termination
b. Resource preemption

Process Termination

The system reclaims all resources allocated to a process by aborting the process. Hence deadlock
is eliminated.

1. Abort all deadlocked processes: This method clearly will break the deadlock cycle, but
at great expense. Since processes may have computed for long time and all the partial
results are discarded.

2. Abort one process at a time: This process is repeated until the deadlock cycle is
eliminated. This is also very expensive, since after each process termination, a deadlock
detection algorithm must be invoked.

For a given set of deadlocked processes, we should find out which process should be terminated
to break the deadlock. There are various factors which governs the choice of process to be
aborted.

a. Priority of process.
b. How much computation is completed by process and how much remaining.
c. How many or what type resources are already used by process.
d. How many more resources are needed?
e. Nature of process – interactive or batch.
f. How many resources need to be terminated?

Resource Preemption

By preempting some resources from processes and reallocating these resources to other
processes, the deadlock can be eliminated.

There are 3 main issues:

1. Select a victim: Which process and which resources need to be preempted.


2. Rollback: When a process is preempted, the process can not continue its normal
execution, hence the process has to be roll backed to some safe state and restart it form
that state.
Since it is difficult to decide safe state, generally total rollback is allowed and process is
restarted again.
3. Starvation: Resources may be preempted from same process again and again due to low
priority. Thus, a particular process may feel starvation.

Nirmala G, Dept of CSE, SSIT 16


Deadlocks

Question Bank:

1. What is a deadlock? Explain the necessary conditions for its occurrence.


2. Explain how resource-allocation graph is used to describe deadlocks.
3. What are the different methods for handling deadlocks? Explain.
4. Define the terms: safe state and safe sequence. Give an algorithm to find whether or not a
system is in a safe state.
5. “A safe state is not a deadlock state but a deadlock state is an unsafe state”. Explain.
6. Explain Banker’s algorithm.
7. Write short notes on
a. Deadlock Prevention
b. Resource allocation graph algorithm
8. Explain deadlock detection.
9. Explain deadlock recovery.

Nirmala G, Dept of CSE, SSIT 17


CS4TH3 Operating Systems

Memory Management

Basic Hardware
Program must be
→ brought (from disk) into memory and placed within a process for it to be run.
• Main-memory and registers are only storage CPU can access directly.
• Registers are accessible within in one CPU clock.
• Completing a memory acess can take many cycles of the CPU clock.
• Cache sits between main-memory and CPU registers to ensure fast memory access.
• Protection of memory is required to ensure correct operation.
• The base register holds the smallest legal physical address and the limit register specifies the range of
addresses. A pair of base and limit-registers define the logical (virtual) address space shown in the fig below:

Fig: A base and a limit-register define a logical-address space

Fig: Hardware address protection with base and limit-registers

Protection of memory space is accomplished by having the CPU hardware compare every address generated
in user mode with the registers. The base and limit registers can be loaded only by the operating system which
Nirmala G, Dept. of CSE, SSIT 1
CS4TH3 Operating Systems

uses a special privileged instruction that can be executed only in kernel mode. Only the operating system can
load the base and limit registers.

Address Binding
Address binding of instructions to memory-addresses can happen at 3 different stages (Figure 3.3). It is
mapping from one address space to another.

1) Compile Time
• If memory-location known a priori, absolute code can be generated.
• Must recompile code if starting location changes.

2) Load Time
• Must generate relocatable code if memory-location is not known at compile time. In this case, final
binding is delayed until load time.

3) Execution Time
• Binding delayed until run-time if the process can be moved during its execution from one memory-
segment to another.

Fig: Multistep processing of a user-program

Nirmala G, Dept. of CSE, SSIT 2


CS4TH3 Operating Systems

Logical versus Physical Address Space


Address uniquely identifies a location in the memory. We have two types of addresses that are logical address
and physical address

Logical Address

• It is generated by the CPU while a program is running.


• The logical address is a virtual address and can be viewed by the user.
• The user can’t view the physical address directly. The logical address is used like a reference, to access
the physical address.

Physical Address

• It identifies a physical location of required data in a memory.


• The user never directly deals with the physical address but can access by its corresponding logical
address.

The user program generates the logical address and thinks that the program is running in this logical address
but the program needs physical memory for its execution, therefore, the logical address must be mapped to the
physical address by MMU before they are used.

Hardware device that maps virtual-address to physical-address (Figure 3.4).

Fig: Dynamic relocation using a relocation-register

Nirmala G, Dept. of CSE, SSIT 3


CS4TH3 Operating Systems

Comparison Chart:

PARAMENTER LOGICAL ADDRESS PHYSICAL ADDRESS

Basic generated by CPU location in a memory unit

Logical Address Space is set

of all logical addresses Physical Address is set of all

Address generated by CPU in reference physical addresses mapped to the

Space to a program. corresponding logical addresses.

User can view the logical User can never view physical

Visibility address of a program. address of program.

Generation generated by the CPU Computed by MMU

The user can use the logical

address to access the physical The user can indirectly access

Access address. physical address but not directly.

Dynamic Loading and Dynamic Linking

Dynamic Loading

All the programs are loaded in the main memory for execution. Sometimes complete program is loaded into
the memory, but sometimes a certain part or routine of the program is loaded into the main memory only when
it is called by the program, this mechanism is called Dynamic Loading.

Advantage is that a routine is loaded only when it is needed. This method is useful when large amounts of code
are needed to handle infrequently occurring cases such as error routines.

It does not require special support from OS. It is the responsibility of the users to design their programs to take
advantage of such method.

Nirmala G, Dept. of CSE, SSIT 4


CS4TH3 Operating Systems

Dynamic linking

Establishing the linking between all the modules or all the functions of the program in order to continue the
program execution is called linking.

Linking intakes the object codes generated by the assembler and combines them to generate the executable
module.

Differences between Linking and Loading

1. The key difference between linking and loading is that the linking generates the executable file of a
program whereas, the loading loads the executable file obtained from the linking into main memory for
execution.

2. The linking intakes the object module of a program generated by the assembler. However, the loading
intakes the executable module generated by the linking.

3. The linking combines all object modules of a program to generate executable modules it also links the
library function in the object module to built-in libraries of the high-level programming language. On the
other hand, loading allocates space to an executable module in main memory.

Shared Libraries

• Shared libraries are libraries that are linked dynamically.


• Shared libraries allow common OS code to be bundled into a wrapper and used by any application software on
the system without loading multiple copies into memory. All the applications on the system can use it without
using more memory.
• A shared library is a file that is intended to be shared by executable files and further shared object files.
• They are loaded into memory at load time or run time, rather than being copied by a linker during the
creation of the executable file.
• Shared Library are open files opened by a process.

Swapping

Swapping is one of the several methods of memory management. In swapping an idle or a blocked process
in the main memory is swapped out to the backing store (disk) and the process that is ready for execution in
the disk, is swapped in main memory for execution.

In simple Swapping is exchanging data between the hard disk and the RAM.

Working:

• A process must be in the main memory before it starts execution. So, a process that is ready for
execution is brought in the main memory. Now, if a running the process gets blocked.
• The memory manager temporarily swaps out that blocked process on to the disk. This makes the space
for another process in the main memory.
• So, the memory manager swaps in the process ready for execution, in the main memory, from the disk.
Nirmala G, Dept. of CSE, SSIT 5
CS4TH3 Operating Systems

• The swapped out process is also brought back into the main memory when it again gets ready for
execution.
• Ideally, the memory manager swaps the processes so fast, that the main memory always has processes
ready for execution.

Swapping of the processes also depends on the priority-based pre-emptive scheduling.

Whenever a process with higher priority arrives the memory manager swaps out the process with the lowest
priority to the disk and swaps in the process with the highest priority in the main memory for execution. When
the highest priority process is finished, the lower priority process is swapped back in memory and continues to
execute. This Variant of swapping is termed as roll-out, roll-in or swap-out swap-in.

Fig: Swapping of two processes using a disk as a backing-store

Swapping depends upon address-binding:


1) If binding is done at load-time, then process cannot be easily moved to a different location.
2) If binding is done at execution-time, then a process can be swapped into a different memory space, because
the physical-addresses are computed during execution-time.

Note: Major part of swap-time is transfer-time; i.e. total transfer-time is directly proportional to the amount
of memory swapped.

Advantages of Swapping

• It offers a higher degree of multiprogramming.


• Allows dynamic relocation. For example, if address binding at execution time is being used, then
processes can be swap in different locations. Else in case of compile and load time bindings,
processes should be moved to the same location.
• It helps to get better utilization of memory.
• Minimum wastage of CPU time on completion so it can easily be applied to a priority-based
scheduling method to improve its performance.

Nirmala G, Dept. of CSE, SSIT 6


CS4TH3 Operating Systems

Disadvantages of Swapping.

• In the case of heavy swapping activity, if the computer system loses power, the user might lose all the
information related to the program.
• If the swapping algorithm is not good, the overall method can increase the number of page faults and
decline the overall processing performance.
• Inefficiency may arise in the case where a resources or a variable is commonly used by the processes
which are participating in the swapping process

Contiguous Memory Allocation


In the contiguous memory allocation, both the operating system and the user must reside in the main memory.
The main memory is divided into two portions one portion is for the operating and other is for the user program.

In contiguous memory allocation, all the available memory space remains together in one place. It means
freely available memory partitions are not scattered here and there across the whole memory space.

In the non-contiguous memory allocation the available free memory space are scattered here and there and
all the free memory space is not at one place. So this is time-consuming.

Memory Protection

Memory-protection means
→ protecting OS from user-process and
→ protecting user-processes from one another.
• Memory-protection is done using
Relocation-register: contains the value of the smallest physical-address.
Limit-register: contains the range of logical-addresses.

Memory Allocation

1) Fixed-sized Partitioning
• The memory is divided into fixed-sized partitions.
• Each partition may contain exactly one process.
• The degree of multiprogramming is bound by the number of partitions.
• When a partition is free, a process is
→ selected from the input queue and
→ loaded into the free partition.
• When the process terminates, the partition becomes available for another process.
Nirmala G, Dept. of CSE, SSIT 7
CS4TH3 Operating Systems

Advantage: Management or book keeping is easy.

Disadvantage: Internal fragmentation

2) Variable-sized Partitioning

The OS keeps a table indicating


→ which parts of memory are available and
→ which parts are occupied.
A hole is a block of available memory.
Normally, memory contains a set of holes of various sizes.
Initially, all memory is
→ available for user-processes and
→ considered one large hole.
When a process arrives, the process is allocated memory from a large hole.
If we find the hole, we
→ allocate only as much memory as is needed and
→ keep the remaining memory available to satisfy future requests.

Advantage: There is no internal fragmentation.

Disadvantage: Management is very difficult as memory is becoming purely fragmented after some time.

Three strategies used to select a free hole from the set of available holes.

1) First Fit
• Allocate the first hole that is big enough.
• Searching can start either
→ at the beginning of the set of holes or
→ at the location where the previous first-fit search ended.
Advantage: Fastest algorithm because it searches as little as possible.

Disadvantage: The remaining unused memory areas left after allocation become waste if it is too smaller. Thus
request for larger memory requirement cannot be accomplished.

2) Best Fit

• Allocate the smallest hole that is big enough.


• We must search the entire list, unless the list is ordered by size.
• This strategy produces the smallest leftover hole.
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.

Nirmala G, Dept. of CSE, SSIT 8


CS4TH3 Operating Systems

3) Worst Fit

• Allocate the largest hole.


• Again, we must search the entire list, unless it is sorted by size.
• This strategy produces the largest leftover hole.
Advantage: Reduces the rate of production of small gaps.

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.
First-fit and best fit are better than worst fit in terms of decreasing time and storage utilization.

Fragmentation
Fragmentation is a phenomenon in which storage space is used inefficiently, reducing capacity or performance
and often both. Whenever a process is loaded or removed from the physical memory block, it creates a small
hole in memory space which is called fragment

Both the internal and external classification affects data accessing speed of the system. Two types of memory
fragmentation:

1) Internal fragmentation: When a process is assigned to a memory block and if that process is smaller than the
memory requested, it creates a free space in the assigned memory block. Then the difference between assigned
and requested memory is called the internal fragmentation.
2) External fragmentation: When the process is loaded or removed from the memory, a free space is created.
This free space creates an empty space in the memory which is called external fragmentation.

S.NO INTERNAL FRAGMENTATION EXTERNAL FRAGMENTATION

In internal fragmentation

fixed-sized memory, blocks In external fragmentation, variable-

square measure appointed to sized memory blocks square

1. process. measure appointed to method.

Internal fragmentation

happens when the method or External fragmentation happens

process is larger than the when the method or process is

2. memory. removed.

Nirmala G, Dept. of CSE, SSIT 9


CS4TH3 Operating Systems

S.NO INTERNAL FRAGMENTATION EXTERNAL FRAGMENTATION

Solution of external fragmentation is

The solution of internal compaction, paging and

3. fragmentation is best-fit block. segmentation.

External fragmentation occurs when

Internal fragmentation occurs memory is divided into variable size

when memory is divided into partitions based on the size of

4. fixed sized partitions. processes.

The unused spaces formed between

The difference between non-contiguous memory fragments

memory allocated and are too small to serve a new

required space or memory is process, is called External

5. called Internal fragmentation. fragmentation .

Nirmala G, Dept. of CSE, SSIT 10


CS4TH3 Operating Systems

The above diagram clearly shows the internal fragmentation because the difference between memory
allocated and required space or memory is called Internal fragmentation.

External fragmentation diagram:

In the above diagram, we can see that, there is enough space (55 KB) to run a process-07 (required 50 KB) but
the memory (fragment) is not contiguous. Here, we use compaction, paging or segmentation to use the free
space to run a process.

Paging
Paging is a memory management scheme. Paging allows a process to be stored in a memory in a non-
contiguous manner. Storing process in a non-contiguous manner solves the problem
of external fragmentation.

Characteristics:

• Paging is a fixed size partitioning scheme.


• In paging, secondary memory and main memory are divided into equal fixed size partitions.
• The partitions of logical(secondary) memory into blocks of the same size are called as pages.
• The partitions of main memory into fixed sized blocks are called as frames

Nirmala G, Dept. of CSE, SSIT 11


CS4TH3 Operating Systems

• Each process is divided into parts where size of each part is same as page size.
• The size of the last part may be less than the page size.
• The pages of process are stored in the frames of main memory depending upon their availability.
Example-

• Consider a process is divided into 4 pages P0, P1, P2 and P3.


• Depending upon the availability, these pages may be stored in the main memory frames in a non-
contiguous fashion as shown-

Translating Logical Address into Physical Address-

• CPU always generates a logical address.


• A physical address is needed to access the main memory.
Following steps are followed to translate logical address into physical address-

Nirmala G, Dept. of CSE, SSIT 12


CS4TH3 Operating Systems

Step-01:
CPU generates a logical address consisting of two parts-
1. Page Number
2. Page Offset

• Page Number specifies the specific page of the process from which CPU wants to read the data.
• Page Offset specifies the specific word on the page that CPU wants to read.
Step-02:
For the page number generated by the CPU,
• Page Table provides the corresponding frame number (base address of the frame) where that page is
stored in the main memory.

Step-03:

• The frame number combined with the page offset forms the required physical address.

• Frame number specifies the specific frame where the required page is stored.
• Page Offset specifies the specific word that has to be read from that page.

Nirmala G, Dept. of CSE, SSIT 13


CS4TH3 Operating Systems

Diagram-

The following diagram illustrates the above steps of translating logical address into physical address-

The advantages of paging are-


• It allows to store parts of a single process in a non-contiguous fashion.
• It solves the problem of external fragmentation.
• Easy to use memory management algorithm
• Swapping is easy between equal-sized pages and page frames.

The disadvantages of paging are-

• Complex memory management algorithm


• Page tables consume additonal memory.
• Multi-level paging may lead to memory reference overhead.

Protection
• Memory-protection is achieved by protection-bits for each frame.
• The protection-bits are kept in the page-table.
• One protection-bit can define a page to be read-write or read-only.
• Every reference to memory goes through the page-table to find the correct frame-number.
• Firstly, the physical-address is computed. At the same time, the protection-bit is checked to verify that no
writes are being made to a read-only page.
• An attempt to write to a read-only page causes a hardware-trap to the OS (or memory-protection violation).
Nirmala G, Dept. of CSE, SSIT 14
CS4TH3 Operating Systems

Valid Invalid Bit


• This bit is attached to each entry in the page-table .
1) Valid bit: The page is in the process’ logical-address space.
2) Invalid bit: The page is not in the process’ logical-address space.
• Illegal addresses are trapped by use of valid-invalid bit.
The OS sets this bit for each page to allow or disallow access to the page.

Hardware Support for Paging


A translation lookaside buffer (TLB) is a memory cache that stores recent translations of virtual
memory to physical addresses for faster retrieval.It can be used to reduce the time taken to access the page
table again and again.

Note: In other words, we can say that TLB is faster and smaller than the main memory but cheaper and bigger
than the register.

Working:
When a logical-address is generated by the CPU, its page-number is presented to the TLB.
If the page-number is found (TLB hit), its frame-number is
→ immediately available and
→ used to access memory.
If page-number is not in TLB (TLB miss), a memory-reference to page table must be made.
The obtained frame-number can be used to access memory (Figure 3.19).
In addition, we add the page-number and frame-number to the TLB, so that they will be
found quickly on the next reference.
• If the TLB is already full of entries, the OS must select one for replacement.
• Percentage of times that a particular page-number is found in the TLB is called hit ratio

Nirmala G, Dept. of CSE, SSIT 15


CS4TH3 Operating Systems

Some TLBs have wired down entries that can't be removed.


• Some TLBs store ASID (address-space identifier) in each entry of the TLB that uniquely
→ identify each process and
→ provide address space protection for that process

Advantage:
• Search operation is fast.
• TLB reduces the effective access time.
• Only one memory access is required when TLB hit occurs.
Disadvantage:
• TLB can hold the data of only one process at a time.
• When context switches occur frequently, the performance of TLB degrades due to low hit ratio.
• As it is a special hardware, it involves additional cost.

Structure of the Page Table


• Page table is a data structure.
• It maps the page number referenced by the CPU to the frame number where that page is stored.
Characteristics-
• Page table is stored in the main memory.
• Number of entries in a page table = Number of pages in which the process is divided.
• Page Table Base Register (PTBR) contains the base address of page table.
• Each process has its own independent page table.

Three types
1) Hierarchical Paging
2) Hashed Page-tables
3) Inverted Page-tables

Nirmala G, Dept. of CSE, SSIT 16


CS4TH3 Operating Systems

Hierarchical Paging

• Problem: Most computers support a large logical-address space (232 to 264). In these systems, the page-
table itself becomes excessively large.
Solution: Divide the page-table into smaller pieces.

Two Level Paging Algorithm


• The page-table itself is also paged (Figure 3.22).
• This is also known as a forward-mapped page-table because address translation works from the outer page-
table inwards.
• For example (Figure 3.23):
Consider the system with a 32-bit logical-address space and a page-size of 4 KB.
A logical-address is divided into
→ 20-bit page-number and
→ 12-bit page-offset.
Since the page-table is paged, the page-number is further divided into
→ 10-bit page-number and
→ 10-bit page-offset.
Thus, a logical-address is as follows:

Figure: A two-level page-table scheme

Nirmala G, Dept. of CSE, SSIT 17


CS4TH3 Operating Systems

Figure: Address translation for a two-level 32-bit paging architecture

Hashed Page Tables


This approach is used for handling address spaces larger than 32 bits.
• The hash-value is the virtual page-number.
• Each entry in the hash-table contains a linked-list of elements that hash to the same location (to
handle collisions).
• Each element consists of 3 fields:
1) Virtual page-number
2) Value of the mapped page-frame and
3) Pointer to the next element in the linked-list.
• The algorithm works as follows (Figure 3.24):
1) The virtual page-number is hashed into the hash-table.
2) The virtual page-number is compared with the first element in the linked-list.
3) If there is a match, the corresponding page-frame (field 2) is used to form the desired
physical-address.
4) If there is no match, subsequent entries in the linked-list are searched for a matching virtual
page-number.

Clustered Page Tables


• These are similar to hashed page-tables except that each entry in the hash-table refers to several
pages rather than a single page.
• Advantages:
1) Favourable for 64-bit address spaces.
2) Useful for address spaces, where memory-references are non-contiguous and scattered
throughout the address space.

Figure: Hashed page-table


Nirmala G, Dept. of CSE, SSIT 18
CS4TH3 Operating Systems

Inverted Page Tables

• Has one entry for each real page of memory.


• Each entry consists of
→ virtual-address of the page stored in that real memory-location and
→ information about the process that owns the page.

Figure Inverted page-table

Each virtual-address consists of a triplet


<process-id, page-number, offset>.
• Each inverted page-table entry is a pair <process-id, page-number>
• The algorithm works as follows:
1) When a memory-reference occurs, part of the virtual-address, consisting of <process-id,
page-number>, is presented to the memory subsystem.
2) The inverted page-table is then searched for a match.
3) If a match is found, at entry i-then the physical-address <i, offset> is generated.
4) If no match is found, then an illegal address access has been attempted.
• Advantage:
1) Decreases memory needed to store each page-table
• Disadvantages:
1) Increases amount of time needed to search table when a page reference occurs.
2) Difficulty implementing shared-memory.

Segmentation

Segmentation is a memory management technique which supports user's view of memory.

Nirmala G, Dept. of CSE, SSIT 19


CS4TH3 Operating Systems

Types of Segmentation

1. Virtual memory segmentation


Each processor job is divided into several segments, It is not essential all of which are resident at any
one point in time.
2. Simple segmentation
Each process is divided into many segments, and all segments are loaded into the memory at run time,
but not necessarily contiguously.

Basic Method

A logical-address space is a collection of segments.


• Each segment has a name and a length.
• The addresses specify both
→ segment-name and
→ offset within the segment.
• Normally, the user-program is compiled, and the compiler automatically constructs segments
reflecting the input program.
For ex:
• The code
• The heap, from which memory is allocated .
• The standard C library
• Global variables
• The stacks used by each thread

Fig: Programmer’s view of a program

Hardware Support

• Segment-table maps 2 dimensional user-defined addresses into one-dimensional physical-addresses.


• In the segment-table, each entry has following 2 fields:
Nirmala G, Dept. of CSE, SSIT 20
CS4TH3 Operating Systems

1) Segment-base contains starting physical-address where the segment resides in memory.


2) Segment-limit specifies the length of the segment (Figure 3.27).
• A logical-address consists of 2 parts:
1) Segment-number(s) is used as an index to the segment-table .
2) Offset(d) must be between 0 and the segment-limit.
• If offset is not between 0 & segment-limit, then we trap to the OS(logical-addressing attempt beyond
end of segment).
• If offset is legal, then it is added to the segment-base to produce the physical-memory address.

Figure 3.27 Segmentation hardware

Advantages of Segmentation
• It allows to divide the program into modules which provides better visualization.
• Segment table consumes less space as compared to Page Table in paging.
• It solves the problem of internal fragmentation.

Disadvantage of Segmentation
• There is an overhead of maintaining a segment table for each process.
• The time taken to fetch the instruction increases since now two memory accesses are required.
• Segments of unequal size are not suited for swapping.
• It suffers from external fragmentation as the free space gets broken down into smaller pieces with the
processes being loaded and removed from the main memory.

Nirmala G, Dept. of CSE, SSIT 21


CS4TH3 Operating Systems

BASIS FOR
PAGING SEGMENTATION
COMPARISON

Basic A page is of fixed block size. A segment is of variable size.

Fragmentation Paging may lead to internal Segmentation may lead to external

fragmentation. fragmentation.

Address The user specified address is The user specifies each address by two

divided by CPU into a page quantities a segment number and the

number and offset. offset (Segment limit).

Size The hardware decides the page The segment size is specified by the

size. user.

Table Paging involves a page table that Segmentation involves the segment table

contains base address of each that contains segment number and offset

page. (segment length).

Nirmala G, Dept. of CSE, SSIT 22


CS4TH3 OPERATING SYSTEMS

VIRTUAL MEMORY MANAGEMENT


Virtual memory is a technique that allows the execution of processes that may not be
completely in memory. Only part of the program needs to be in memory for execution. It means that
Logical address space can be much larger than physical address space. Virtual memory allows
processes to easily share files and address spaces, and it provides an efficient mechanism for process
creation.
Virtual memory is the separation of user logical memory from physical memory. This
separation allows an extremely large virtual memory to be provided for programmers when only a
smaller physical memory is available. Virtual memory makes the task of programming much easier,
because the programmer no longer needs to worry about the amount of physical memory available.

Fig: Diagram showing virtual memory that is larger than physical memory.

Virtual memory can be implemented via:


➢ Demand paging
DEMAND PAGING:

A demand-paging system is similar to a paging system with swapping. Generally, processes


reside on secondary memory (which is usually a disk). When we want to execute a process, we swap
it into memory. Rather than swapping the entire process into memory, it swaps the required page.
This can be done by a lazy swapper.
A lazy swapper never swaps a page into memory unless that page will be needed. A swapper
manipulates entire processes, whereas a pager is concerned with the individual pages of a process.

Page transfer Method: When a process is to be swapped in, the pager guesses which pages will
be used before the process is swapped out again. Instead of swapping in a whole process, the pager
brings only those necessary pages into memory. Thus, it avoids reading into memory pages that will
not be used anyway, decreasing the swap time and the amount of physical memory needed.

Nirmala G, Dept. of CSE, SSIT 1


CS4TH3 OPERATING SYSTEMS

Fig: Transfer of a paged memory to contiguous disk space.


PAGE TABLE:
➢ The valid-invalid bit scheme of Page table can be used for indicating which pages are currently
in memory.
➢ When this bit is set to "valid", this value indicates that the associated page is both legal and in
memory. If the bit is set to "invalid", this value indicates that the page either is not valid or is
valid but is currently on the disk.
➢ The page-table entry for a page that is brought into memory is set as usual, but the page- table

entry for a page that is not currently in memory is simply marked invalid, or contains the address
of the page on disk.
Nirmala G, Dept. of CSE, SSIT 2
CS4TH3 OPERATING SYSTEMS

When a page references an invalid page, then it is called Page Fault. It means that page is not in
main memory. The procedure for handling page fault is as follows:
1. We check an internal table for this process, to determine whether the reference was a valid
or invalid memory access.
2. If the reference was invalid, we terminate the process. If it was valid, but we have not yet
brought in that page in to memory.
3. We find a free frame (by taking one from the free-frame list).
4. We schedule a disk operation to read the desired page into the newly allocated frame.
5. When the disk read is complete, we modify the internal table kept with the process and the
page table to indicate that the page is now in memory.
6. We restart the instruction that was interrupted by the illegal address trap. The process can
now access the page as though it had always been in memory.

Fig: Steps in handling a page fault


Note: The pages are copied into memory, only when they are required. This mechanism is called
Pure Demand Paging.
Performance of Demand Paging
Let p be the probability of a page fault (0< p < 1). Then the effective access time is

Effective access time = (1 - p) x memory access time + p x page fault time


In any case, we are faced with three major components of the page-fault service time:
1. Service the page-fault interrupt.
2. Read in the page.
3. Restart the process.

Nirmala G, Dept. of CSE, SSIT 3


CS4TH3 OPERATING SYSTEMS

PAGE REPLACEMENT:
The page replacement is a mechanism that loads a page from disc to memory when a page of memory
needs to be allocated. Page replacement can be described as follows:
1. Find the location of the desired page on the disk.
2. Find a free frame:
a. If there is a free frame, use it.
b. If there is no free frame, use a page-replacement algorithm to select a victim frame.
c. Write the victim page to the disk; change the page and frame tables accordingly.
3. Read the desired page into the (newly) free frame; change the page and frame tables.
4. Restart the user process.

Page Replacement Algorithms: The page replacement algorithms decide which memory pages to
page out (swap out, write to disk) when a page of memory needs to be allocated. We evaluate an
algorithm by running it on a particular string of memory references and computing the number of
page faults. The string of memory references is called a reference string.
The different page replacement algorithms are described as follows:
1. First-In-First-Out (FIFO) Algorithm:
A FIFO replacement algorithm associates with each page the time when that page was brought
into memory. When a page must be replaced, the oldest page is chosen to swap out. We can
create a FIFO queue to hold all pages in memory. We replace the page at the head of the queue.
When a page is brought into memory, we insert it at the tail of the queue.
Example:

Nirmala G, Dept. of CSE, SSIT 4


CS4TH3 OPERATING SYSTEMS

Note: For some page-replacement algorithms, the page fault rate may increase as the number of
allocated frames increases. This most unexpected result is known as Belady's anomaly.

2. Optimal Page Replacement algorithm:


One result of the discovery of Belady's anomaly was the search for an optimal page
replacement algorithm. An optimal page-replacement algorithm has the lowest page-fault rate
of all algorithms, and will never suffer from Belady's anomaly. Such an algorithm does exist, and
has been called OPT or MIN.
It is simply “Replace the page that will not be used for the longest period of time”. Use of
this page-replacement algorithm guarantees the lowest possible pagefault rate for a fixed number
of frames.
Example:

(Optimal page-replacement algorithm)

3. LRU Page Replacement algorithm


If we use the recent past as an approximation of the near future, then we will replace the
page that has not been used for the longest period of time. This approach is the least- recently-
used (LRU) algorithm.
LRU replacement associates with each page the time of that page's last use. When a page
must be replaced, LRU chooses that page that has not been used for the longest period of time.
Example:

The major problem is how to implement LRU replacement. An LRU page-replacement algorithm
may require substantial hardware assistance. The problem is to determine an order for the frames
defined by the time of last use. Two implementations are feasible:
Counters: We associate with each page-table entry a time-of-use field, and add to the CPU a
logical clock or counter. The clock is incremented for every memory reference. Whenever a
reference to a page is made, the contents of the clock register are copied to the time-of-use field
in the page-table entry for that page. We replace the page with the smallest time value. This
scheme requires a search of the page table to find the LRU page, and a write to memory (to the
time-of-use field in the page table) for each memory access. The times must also be maintained
when page tables are changed (due to CPU scheduling).
Stack: Another approach to implementing LRU replacement is to keep a stack of page numbers.
Whenever a page is referenced, it is removed from the stack and put on the top. In this way, the top of

Nirmala G, Dept. of CSE, SSIT 5


CS4TH3 OPERATING SYSTEMS

the stack is always the most recently used page and the bottom is the LRU page. Because entries must
be removed from the middle of the stack, it is best implemented by a doubly linked list, with a head
and tail pointer. Each update is a little more expensive, but there is no search for a replacement; the
tail pointer points to the bottom of the stack which is the LRU page. This approach is particularly
appropriate for software or microcode implementations of LRU replacement. Example:

(Use of a stack to record the most recent page references)


4. LRU Approximation Page Replacement algorithm
In this algorithm, Reference bits are associated with each entry in the page table. Initially, all
bits are cleared (to 0) by the operating system. As a user process executes, the bit associated with each
page referenced is set (to 1) by the hardware. After some time, we can determine which pages have
been used and which have not been used by examining the reference bits.
This algorithm can be classified into different categories as follows:
i. Additional-Reference-Bits Algorithm: It can keep an 8-bit(1 byte) for each page in a page
table in memory. At regular intervals, a timer interrupt transfers control to the operating system. The
operating system shifts the reference bit for each page into the high- order bit of its 8-bit, shifting
the other bits right over 1 bit position, discarding the low-order bit. These 8 bits shift registers
contain the history of page use for the last eight time periods.
If we interpret these 8-bits as unsigned integers, the page with the lowest number is the LRU
page, and it can be replaced.
ii. Second-Chance Algorithm: The basic algorithm of second-chance replacement is a FIFO
replacement algorithm. When a page has been selected, we inspect its reference bit. If the value
is 0, we proceed to replace this page. If the reference bit is set to 1, we give that page a second
chance and move on to select the next FIFO page. When a page gets a second chance, its reference
bit is cleared and its arrival time is reset to the current time. Thus, a page that is given a second
chance will not be replaced until all other pages are replaced.
5. Counting-Based Page Replacement
We could keep a counter of the number of references that have been made to each page,
and develop the following two schemes.
i. LFU page replacement algorithm: The least frequently used (LFU) page- replacement
algorithm requires that the page with the smallest count be replaced. The reason for this selection
is that an actively used page should have a large reference count.
ii. MFU page-replacement algorithm: The most frequently used (MFU) page
replacement algorithm is based on the argument that the page with the largest count be replaced.

Nirmala G, Dept. of CSE, SSIT 6


CS4TH3 OPERATING SYSTEMS

ALLOCATION OF FRAMES:
When a page fault occurs, there is a free frame available to store new page into a frame. While
the page swap is taking place, a replacement can be selected, which is written to the disk as the
user process continues to execute. The operating system allocate all its buffer and table space from
the free-frame list for new page.
Two major allocation Algorithm/schemes.
1. equal allocation
2. proportional allocation
1. Equal allocation: The easiest way to split m frames among n processes is to give everyone an
equal share, m/n frames. This scheme is called equal allocation.
2. proportional allocation: Here, it allocates available memory to each process according to its
size. Let the size of the virtual memory for process pi be si, and define S= ∑ Si
Then, if the total number of available frames is m, we allocate ai frames to process pi, where
ai is approximately ai = Si/ S x m.

Global versus Local allocation:


We can classify page-replacement algorithms into two broad categories: global replacement
and local replacement.
Global replacement allows a process to select a replacement frame from the set of all frames,
even if that frame is currently allocated to some other process; one process can take a frame from
another.
Local replacement requires that each process select from only its own set of allocated frames.
THRASHING
The system spends most of its time shuttling pages between main memory and secondary
memory due to frequent page faults. This behavior is known as thrashing.
A process is thrashing if it is spending more time paging than executing. This leads to: low
CPU utilization and the operating system thinks that it needs to increase the degree of
multiprogramming.

Nirmala G, Dept. of CSE, SSIT 7


CS4TH3 OPERATING SYSTEMS

STORAGE MANAGEMENT
FILE SYSTEM
The file system provides the mechanism for on-line storage of and access to both
data and programs of the operating system and all the users of the computer system. The
file system consists of two distinct parts: a collection of files, each storing related data, and
a directory structure, which organizes and provides information about all the files in the
system.

File Concept:
A file is a collection of related information that is recorded on secondary storage.
From a user's perspective, a file is the smallest allotment of logical secondary storage and
data cannot be written to secondary storage unless they are within a file.
Four terms are in common use when discussing files: Field, Record, File and Database
• A field is the basic element of data. An individual field contains a single value, such as an
employee’s last name, a date, or the value of a sensor reading. It is characterized by its
length and data type.
• A record is a collection of related fields that can be treated as a unit by some
application program. For example, an employee record would contain such fields as
name, social security number, job classification, date of hire, and so on.
• A file is a collection of similar records. The file is treated as a single entity by users
and applications and may be referenced by name.
• A database is a collection of related data. A database may contain all of the
information related to an organization or project, such as a business or a scientific
study. The database itself consists of one or more types of files.
File Attributes:
A file has the following attributes:
• 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 those systems that support different types.
• 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,
modification and last use. These data can be useful for protection, security, and usage
monitoring.
File Operations:
The operating system can provide system calls to create, write, read, reposition, delete,
and truncate files. The file operations are described as followed:
• Creating a file: Two steps are necessary to create a file. First, space in the file system

Nirmala G, Dept. of CSE, SSIT 1


CS4TH3 OPERATING SYSTEMS

must be found for the file. Second, an entry for the new file must be made in the
directory. The directory entry records the name of the file and the location in the
file system, and possibly other information.
• 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 location of the file. 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 main memory) the next block of the file should be put. Again,
the directory is searched for the associated directory 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.
• Repositioning within a file: The directory is searched for the appropriate entry, and the
current-file-position is set to a given value. Repositioning within a file does not need to
involve any actual I/O. This file operation is also known as a file seeks.
• 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.

File Types: The files are classified into different categories as follows:

Nirmala G, Dept. of CSE, SSIT 2


CS4TH3 OPERATING SYSTEMS

The name is split into two parts-a name and an extension, The system uses the extension
to indicate the type of the file and the type of operations that can be done on that file.
ACCESS METHODS
When a file is used, this information must be accessed and read into computer
memory. The information in the file can be accessed in several ways. There are two major
access methods as follows:

Sequential Access: Information in the file is processed in order, one record after the
other. A read operation reads the next portion of the file and automatically advances a
file pointer, which tracks the I/O location. Similarly, a write appends to the end of the
file and advances to the end of the newly written material (the new end of file).
Sequential access is based on a tape model of a file, and works as well on sequential-
access devices as it does on random-access ones.
• Direct Access: A file is made up of fixed length logical records that allow programs
to read and write records rapidly in no particular order. The direct-access method is
based on a disk model of a file, since disks allow random access to any file block.
For direct access, the file is viewed as a numbered sequence of blocks or records. A
direct-access file allows arbitrary blocks to be read or written. There are no restrictions
on the order of reading or writing for a direct-access file. For the direct-access
method, the file operations must be modified to include the block number as a
parameter. Thus, we have read n, where n is the block number, rather than read next,
and write n rather than write next.
DIRECTORY STRUCTURE
A directory is an object that contains the names of file system objects. File system
allows the users to organize files and other file system objects through the use of directories.
The structure created by placement of names in directories can take a number of forms: Single-
level tree, Two-level tree, multi-level tree or cyclic graph.
Single-Level Directory: The simplest directory structure is the single-level directory. All
files are contained in the same directory, which is easy to support and understand. A
single-level directory has significant limitations, when the number of files increases or when
the system has more than one user. Since all files are in the same directory, they must have
unique names.

Two-Level Directory: In the two-level directory structure, each user has its own user file
directory (UFD). Each UFD has a similar structure, but lists only the files of a single user.
When a user job starts or a user logs in, the system's master file directory (MFD) is
searched. The MFD is indexed by user name or account number, and each entry points
to the UFD for that user.

Nirmala G, Dept. of CSE, SSIT 3


CS4TH3 OPERATING SYSTEMS

When a user refers to a particular file, only his own UFD is searched. Different
users may have files with the same name, as long as all the file names within each UFD
are unique.
To create a file for a user, the operating system searches only that user's UFD to
ascertain whether another file of that name exists. To delete a file, the operating system
confines its search to the local UFD; thus, it cannot accidentally delete another user's file
that has the same name.

Tree-structured directories: A tree structure is A more powerful and flexible approach to


organize files and directories in hierarchical. There is a master directory, which has under
it a number of user directories. Each of these user directories may have sub- directories and
files as entries. This is true at any level: That is, at any level, a directory may consist of
entries for subdirectories and/or entries for files.

Acyclic-Graph Directories:

An acyclic graph allows directories to have shared subdirectories and files. The
same file or subdirectory may be in two different directories. An acyclic graph is a natural
generalization of the tree structured directory scheme.

Nirmala G, Dept. of CSE, SSIT 4


CS4TH3 OPERATING SYSTEMS

A shared file (or directory) is not the same as two copies of the file. With two copies,
each programmer can view the copy rather than the original, but if one programmer changes
the file, the changes will not appear in the other's copy.
Shared files and subdirectories can be implemented in several ways. A common
way is to create a new directory entry called a link. A link is a pointer to another file or
subdirectory.

General Graph Directory:


When we add links to an existing tree-structured directory, the tree structure is destroyed,
resulting in a simple graph structure.

Nirmala G, Dept. of CSE, SSIT 5


CS4TH3 OPERATING SYSTEMS

MASS STORAGE STRUCTURES


DISK STRUCTURE
Magnetic disks provide the bulk of secondary storage for modern computer systems. Each
disk platter has a flat circular shape, like a CD. Common platter diameters range from1.8
to 5.25 inches. The two surfaces of a platter are covered with a magnetic material. We store
information by recording it magnetically on the platters.
A read-write head "flies" just above each surface of every platter. The heads are attached to a
disk arm, which moves all the heads as a unit. The surface of a platter is logically divided
into circular tracks, which are subdivided into sectors. The set of tracks that are at one arm
position forms a cylinder. There may be thousands of concentric cylinders in a disk drive, and each
track may contain hundreds of sectors. The storage capacity of common disk drives is measured
in gigabytes.

When the disk is in use, a drive motor spins it at high speed. Most drives rotate 60
to 200 times per second. Disk speed has two parts. The transfer rate is the rate at which
data flow between the drive and the computer. The positioning time (or random-access
time) consists of seek time and rotational latency. The seek time is the time to move the
disk armto the desired cylinder. And the rotational latency is the time for the desired sector
to rotate to the disk head. Typical disks can transfer several megabytes of data per second
and they have seek times and rotational latencies of several milliseconds.

Capacity of Magnetic disks(C) = S x T x P x N


Where S= no. of surfaces = 2 x no. of disks, T= no. of tracks in
a surface,P= no. of sectors per track, N= size of each sector
Transfer Time: The transfer time to or from the disk depends on the rotation speed of the
disk in the following fashion:

T = b / ( r x N)

Nirmala G, Dept. of CSE, SSIT 6


CS4TH3 OPERATING SYSTEMS

Where T= transfer time, b=number of bytes to be transferred, N= number of bytes on a track,


r= rotation speed, in revolutions per second.
Modern disk drives are addressed as large one-dimensional arrays of logical
blocks, where the logical block is the smallest unit of transfer. The one-dimensional array
of logical blocks is mapped onto the sectors of the disk sequentially. Sector 0 is the first
sector of the first track on the outermost cylinder. The mapping proceeds in order through
that track, then through the rest of the tracks in that cylinder, and then through the rest of
the cylinders from outermost to innermost.
By using this mapping, we can convert a logical block number into an old-style
disk address that consists of a cylinder number, a track number within that cylinder, and a
sector number within that track. In practical, it is difficult to perform this translation, for
two reasons. First, most disks have some defective sectors, but the mapping hides this by
substituting spare sectors from elsewhere on the disk. Second, the number of sectors per
track is not a constant on some drives.
The density of bits per track is uniform. This is called Constant linear velocity
(CLV). The disk rotation speed can stay constant and the density of bits decreases from
inner tracks to outer tracks to keep the data rate constant. This method is used in hard disks
and is known as constant angular velocity (CAV).
DISK SCHEDULING
The seek time is the time for the disk arm to move the heads to the cylinder containing
the desired sector. The rotational latency is the time waiting for the disk to rotate the desired
sector to the disk head. The disk bandwidth is the total number of bytes transferred
divided by the total time between the first request for service and the completion of the last
transfer.
We can improve both the access time and the bandwidth by scheduling the servicing
of disk I/O requests in a good order. Several algorithms exist to schedule the servicing of
disk I/O requests as follows:
FCFS Scheduling
The simplest form of scheduling is first-in-first-out (FIFO) scheduling, which
processes items from the queue in sequential order. We illustrate this with a request
queue (0-199):98, 183, 37, 122, 14, 124, 65, 67. Consider now the Head pointer is in
cylinder 53.

Nirmala G, Dept. of CSE, SSIT 7


CS4TH3 OPERATING SYSTEMS

SSTF Scheduling
It stands for shortest-seek-time-first (SSTF) algorithm. The SSTF algorithm selects the
request with the minimum seek time from the current head position. Since seek time increases
with the number of cylinders traversed by the head, SSTF chooses the pending request
closest to the current head position. We illustrate this with a request queue (0-199):
98, 183, 37, 122, 14, 124, 65, 67
Consider now the Head pointer is in cylinder 53.

SCAN Scheduling
In the SCAN algorithm, the disk arm starts at one end of the disk, and moves toward the
other end, servicing requests as it reaches each cylinder, until it gets to the other end of the
disk. At the other end, the direction of head movement is reversed, and servicing continues.
The head continuously scans back and forth across the disk. We illustrate this with a
request queue (0-199): 98, 183, 37, 122, 14, 124, 65, 67
Consider now the Head pointer is in cylinder 53.

C-SCAN Scheduling
Circular SCAN (C-SCAN) scheduling is a variant of SCAN designed to provide
a more uniform wait time. Like SCAN, C-SCAN moves the head from one end of the disk
to the other, servicing requests along the way. When the head reaches the other end, it
immediately returns to the beginning of the disk, without servicing any requests on the
return trip. The C- SCAN scheduling algorithm essentially treats the cylinders as a circular
list that wraps around from the final cylinder to the first one. We illustrate this with a request
queue (0-199): 98, 183, 37, 122, 14, 124, 65, 67. Consider now the Head pointer is in
cylinder 53.

Nirmala G, Dept. of CSE, SSIT 8


CS4TH3 OPERATING SYSTEMS

LOOK Scheduling
Practically, both SCAN and C-SCAN algorithm is not implemented this way. More
commonly, the arm goes only as far as the final request in each direction. Then, it reverses
direction immediately, without going all the way to the end of the disk. These versions of
SCAN and C-SCAN are called LOOK and C-LOOK scheduling, because they look for a
request before continuing to move in a given direction. We illustrate this with a request
queue (0-199): 98, 183, 37, 122, 14, 124, 65, 67
Consider now the Head pointer is in cylinder 53.

Selection of a Disk-Scheduling Algorithm


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

DISK MANAGEMENT
The operating system is responsible for several aspects of disk management.
Disk Formatting:
A new magnetic disk is a blank slate. It is just platters of a magnetic recording material. Before
a disk can store data, it must be divided into sectors that the disk controller can read and
write. This process is called low-level formatting (or physical formatting).

Nirmala G, Dept. of CSE, SSIT 9


CS4TH3 OPERATING SYSTEMS

Low-level formatting fills the disk with a special data structure for each sector. The data
structure for a sector consists of a header, a data area, and a trailer. The header and trailer
contain information used by the disk controller, such as a sector number and an error-
correcting code (ECC).
To use a disk to hold files, the operating system still needs to record its own data structures
on the disk. It does so in two steps. The first step is to partition the disk into one or more
groups of cylinders. The operating system can treat each partition as though it were a
separate disk. For instance, one partition can hold a copy of the operating system's
executable code, while another holds user files. After partitioning, the second step is logical
formatting (or creation of a file system). In this step, the operating system stores the initial
file-system data structures onto the disk.
Boot Block
When a computer is powered up or rebooted, it needs to have an initial program to run.
This initial program is called bootstrap program. It initializes all aspects of the system (i.e.
from CPU registers to device controllers and the contents of main memory) and then starts
the operating system.
To do its job, the bootstrap program finds the operating system kernel on disk, loads that
kernel into memory, and jumps to an initial address to begin the operating-system
execution.
For most computers, the bootstrap is stored in read-only memory (ROM). This location is
convenient, because ROM needs no initialization and is at a fixed location that the
processor can start executing when powered up or reset. And since ROM is read only, it
cannot be infected by a computer virus. The problem is that changing this bootstrap code
requires changing the ROM hardware chips.
For this reason, most systems store a tiny bootstrap loader program in the boot ROM, whose
only job is to bring in a full bootstrap program from disk. The full bootstrap program can
be changed easily: A new version is simply written onto the disk. The full bootstrap program
is stored in a partition (at a fixed location on the disk) is called the boot blocks. A disk
that has a boot partition is called a boot disk or system disk.
Bad Blocks

Since disks have moving parts and small tolerances, they are prone to failure. Sometimes
the failure is complete, and the disk needs to be replaced, and its contents restored from
backup media to the new disk.
More frequently, one or more sectors become defective. Most disks even come from the
factory with bad blocks. Depending on the disk and controller in use, these blocks are
handled in a variety of ways.
The controller maintains a list of bad blocks on the disk. The list is initialized during the low-
level format at the factory, and is updated over the life of the disk. The controller can be told to
replace each bad sector logically with one of the spare sectors. This scheme is known as sector
sparing or forwarding.

Nirmala G, Dept. of CSE, SSIT 10


CS4TH3 OPERATING SYSTEMS

Swap space management:


The swap-space management is a low-level task of the operating system. The
main goal for the design and implementation of swap space is to provide the best
throughput for the virtual-memory system.
Swap-Space Use
Swap space is used in various ways by different operating systems depending on the
implemented memory-management algorithms.
Those systems are implemented swapping, they may use swap space to hold the entire
process image, including the code and data segments. The amount of swap space needed
on a system can vary depending on the amount of physical memory,
Swap-Space Location
A swap space can reside in two places: Swap space can be carved out of the normal file
system, or it can be in a separate disk partition.
If the swap space is simply a large file within the file system, normal file-system routines
can be used to create it, name it, and allocate its space. This approach is easy to implement
and is also inefficient.
Alternatively, swap space can be created in a separate disk partition. No file system or
directory structure is placed on this space. A separate swap-space storage manager is used
to allocate and deallocate the blocks. This manager uses algorithms optimized for speed
and storage efficiency.

Nirmala G, Dept. of CSE, SSIT 11


CS4TH3 OPERATING SYSTEMS

Protection
Goals of Protection

• Obviously to prevent malicious misuse of the system by users or programs. See chapter 15
for a more thorough coverage of this goal.
• To ensure that each shared resource is used only in accordance with system policies, which
may be set either by system designers or by system administrators.
• To ensure that errant programs cause the minimal amount of damage possible.
• Note that protection systems only provide the mechanisms for enforcing policies and
ensuring reliable systems. It is up to administrators and users to implement those
mechanisms effectively.

Principles of Protection

• The principle of least privilege dictates that programs, users, and systems be given just
enough privileges to perform their tasks.
• This ensures that failures do the least amount of harm and allow the least of harm to be done.
• For example, if a program needs special privileges to perform a task, it is better to make it a
SGID program with group ownership of "network" or "backup" or some other pseudo group,
rather than SUID with root ownership. This limits the amount of damage that can occur if
something goes wrong.
• Typically each user is given their own account, and has only enough privilege to modify
their own files.
• The root account should not be used for normal day to day activities - The System
Administrator should also have an ordinary account, and reserve use of the root account for
only those tasks which need the root privileges

Domain of Protection

• A computer can be viewed as a collection of processes and objects ( both h/w & s/w ).
• The need to know principle states that a process should only have access to those objects it
needs to accomplish its task, and furthermore only in the modes for which it needs access
and only during the time frame when it needs access.
• The modes available for a particular object may depend upon its type.

Domain Structure

• A protection domain specifies the resources that a process may access.


• Each domain defines a set of objects and the types of operations that may be invoked
on each object.
• An access right is the ability to execute an operation on an object.
• A domain is defined as a set of < object, { access right set } > pairs, as shown below.
Note that some domains may be disjoint while others overlap.

Nirmala G, Dept. of CSE, SSIT 12


CS4TH3 OPERATING SYSTEMS

Fig: System with three protection domains.

The association between a process and a domain may be static or dynamic.

• If the association is static, then the need-to-know principle requires a way of changing the
contents of the domain dynamically.
• If the association is dynamic, then there needs to be a mechanism for domain switching.

Domains may be realized in different fashions - as users, or as processes, or as procedures. E.g. if


each user corresponds to a domain, then that domain defines the access of that user, and changing
domains involves changing user ID.

An Example: UNIX

• UNIX associates domains with users.


• Certain programs operate with the SUID bit set, which effectively changes the user ID, and
therefore the access domain, while the program is running. ( and similarly for the SGID bit. )
Unfortunately, this has some potential for abuse.
• An alternative used on some systems is to place privileged programs in special directories,
so that they attain the identity of the directory owner when they run. This prevents crackers
from placing SUID programs in random directories around the system.
• Yet another alternative is to not allow the changing of ID at all. Instead, special privileged
daemons are launched at boot time, and user processes send messages to these daemons
when they need special tasks performed.

An Example: MULTICS

• The MULTICS system uses a complex system of rings, each corresponding to a different
protection domain, as shown below:

Fig: MULTICS ring structure.

Nirmala G, Dept. of CSE, SSIT 13


CS4TH3 OPERATING SYSTEMS

• Rings are numbered from 0 to 7, with outer rings having a subset of the privileges of the
inner rings.
• Each file is a memory segment, and each segment description includes an entry that indicates
the ring number associated with that segment, as well as read, write, and execute privileges.
• Each process runs in a ring, according to the current-ring-number, a counter associated with
each process.
• A process operating in one ring can only access segments associated with higher ( farther
out ) rings, and then only according to the access bits. Processes cannot access segments
associated with lower rings.
• Domain switching is achieved by a process in one ring calling upon a process operating in
a lower ring, which is controlled by several factors stored with each segment descriptor:

➢ An access bracket, defined by integers b1 <= b2.


➢ A limit b3 > b2
➢ A list of gates, identifying the entry points at which the segments may be called.

• If a process operating in ring i calls a segment whose bracket is such that b1 <= i <= b2,
then the call succeeds and the process remains in ring i.
• Otherwise, a trap to the OS occurs, and is handled as follows:

➢ If i < b1, then the call is allowed, because we are transferring to a procedure with fewer
privileges. However if any of the parameters being passed are of segments below b1, then
they must be copied to an area accessible by the called procedure.
➢ If i > b2, then the call is allowed only if i <= b3 and the call is directed to one of the entries
on the list of gates.

• Overall, this approach is more complex and less efficient than other protection schemes.

Access Matrix

• The model of protection that we have been discussing can be viewed as an access matrix, in
which columns represent different system resources and rows represent different protection
domains. Entries within the matrix indicate what access that domain has to that resource.

Fig 1: Access matrix

Nirmala G, Dept. of CSE, SSIT 14


CS4TH3 OPERATING SYSTEMS

• Domain switching can be easily supported under this model, simply by providing "switch"
access to other domains:

Fig 2: Access matrix of fig. 1 with domains as objects.

• The ability to copy rights is denoted by an asterisk, indicating that processes in that domain
have the right to copy that access within the same column, i.e. for the same object. There
are two important variations:
o If the asterisk is removed from the original access right, then the right
is transferred, rather than being copied. This may be termed a transfer right as
opposed to a copy right.
o If only the right and not the asterisk is copied, then the access right is added to the
new domain, but it may not be propagated further. That is the new domain does not
also receive the right to copy the access. This may be termed a limited copy right, as
shown in Figure 14.5 below:

Fig 3: Access matrix with copy rights.

Nirmala G, Dept. of CSE, SSIT 15


CS4TH3 OPERATING SYSTEMS

• The owner right adds the privilege of adding new rights or removing existing ones:

Fig 4: Access matrix with owner rights.

• Copy and owner rights only allow the modification of rights within a column. The addition
of control rights, which only apply to domain objects, allow a process operating in one
domain to affect the rights available in other domains. For example in the table below, a
process operating in domain D2 has the right to control any of the rights in domain D4.

Fig: Modified access matrix of fig. 2

Nirmala G, Dept. of CSE, SSIT 16

You might also like