Operating System Notes
Operating System Notes
Operating System
There is no specific definition of operating system. Many authors suggest different definition of
operating system. But basic definitions of operating systems are as under:
An operating system is system software that acts as an interface between the user of computer and
the computer hardware and controls the execution of all kinds of application programs.
It is system software that works as an interface between a user of computer and the computer
hardware.
An operating system is system software that manages computer hardware, software resources, and
provides common services for computer programs.
An operating system is system software that acts as an intermediatary between hardware and user.
It also act as resource manager which manages the system resources and also it provides an
environment on which user can execute its application program.
Explanation:
Is this is necessary to use operating system? Can we access the computer system without operating
system? The answer is yes but only few people those having knowledge to access the hardware only those
people can do. But this is not convenient and efficient. The general user unable to access the computer
system without operating system. The operating system is nothing but a system software and interface
between computer and user. Operating system is also act as a resource manager which manages resources
like CPU, Memory, registers etc. the operating system is responsible for the following:
The users of a batch operating system do not interact with the computer directly. Each user
prepares his job on an off-line device like punch cards and submits it to the computer operator. To
speed up processing, jobs with similar needs are batched together and run as a group. The
programmers leave their programs with the operator and the operator then sorts the programs with
similar requirements into batches.
Examples of Batch based Operating System: Payroll System, Bank Statements etc.
It is very difficult to guess or know the time required by any job to complete.
Processors of the batch systems know how long the job would be when it is in queue
Multiple users can share the batch systems
The idle time for batch system is very less
It is easy to manage large work repeatedly in batch systems
Problem of reliability.
Question of security and integrity of user programs and data.
Problem of data communication.
A real-time system is defined as a data processing system in which the time interval
required to process and respond to inputs is so small that it controls the environment. The
time taken by the system to respond to an input and display of required updated
information is termed as the response time. So in this method, the response time is very less
as compared to online processing.
Real-time systems are used when there are rigid time requirements on the operation
of a processor or the flow of data and real-time systems can be used as a control device in a
dedicated application. A real-time operating system must have well-defined, fixed time
constraints, otherwise the system will fail.
Hard real-time systems guarantee that critical tasks complete on time. In hard real-
time systems, secondary storage is limited or missing and the data is stored in ROM. In
these systems, virtual memory is almost never found.
Soft real-time systems are less restrictive. A critical real-time task gets priority over
other tasks and retains the priority until it completes. Soft real-time systems have limited
utility than hard real-time systems. For example, multimedia, virtual reality, Advanced
Scientific Projects like undersea exploration and planetary rovers, etc.
Advantages of RTOS:
Disadvantages of RTOS:
Limited Tasks: Very few tasks run at the same time and their concentration is very less
on few applications to avoid errors.
Use heavy system resources: Sometimes the system resources are not so good and
they are expensive as well.
Complex Algorithms: The algorithms are very complex and difficult for the designer
to write on.
Device driver and interrupt signals: It needs specific device drivers and interrupt
signals to response earliest to interrupts.
Thread Priority: It is not good to set thread priority as these systems are very less
prone to switching tasks.
Distributed Operating System.
Failure of one will not affect the other network communication, as all systems are
independent from each other
Electronic mail increases the data exchange speed
Since resources are being shared, computation is highly fast and durable
Load on host computer reduces
These systems are easily scalable as many systems can be easily added to the network
Delay in data processing reduces
A Network Operating System runs on a server and provides the server the capability
to manage data, users, groups, security, applications, and other networking functions. The
primary purpose of the network operating system is to allow shared file and printer access
among multiple computers in a network, typically a local area network (LAN), a private
network or to other networks.
Process Management „
Main Memory Management„
File Management„
I/O System Management„
Secondary Management„
Protection System
Process Management
The operating system is responsible for the following activities in connection with
process management.– Process creation and deletion.– process suspension and
resumption.– Provision of mechanisms for:• process synchronization• process
communication.
Memory is a large array of words or bytes, each with its own address. It is a
repository of quickly accessible data shared by the CPU and I/O devices. Main memory is a
volatile storage device. It loses its contents in the case of system failure.
The operating system is responsible for the following activities in connections with
memory management:– Keep track of which parts of memory are currently being used and
by whom.– Decide which processes to load when memory space becomes available.–
Allocate and deallocate memory space as needed.
File Management
The operating system is responsible for the following activities in connections with
file management:– File creation and deletion.– Directory creation and deletion.– Support of
primitives for manipulating files and directories.– Mapping files onto secondary storage.–
File backup on stable (nonvolatile) storage media.
One of the important use of an operating system that helps you to hide the
variations of specific hardware devices from the user.
The operating system is responsible for the following activities in connections with I/O
system management. It offers buffer caching system
It provides general device driver code
It provides drivers for particular hardware devices.
I/O helps you to knows the individualities of a specific device.
Secondary Management
This Memory of the computer is very small to store all data and programs
permanently. The computer system offers secondary storage to back up the main Memory.
Today modern computers use hard drives/SSD as the primary storage of both programs and
data. However, the secondary storage management also works with storage devices, like a
USB flash drive, and CD/DVD drives.
Programs like assemblers, compilers, stored on the disk until it is loaded into
memory, and then use the disk as a source and destination for processing.
Storage allocation
Free space management
Disk scheduling
Protection System
An Operating System provides services to both the users and to the programs.
Program execution
I/O operations
File System manipulation
Communication
Error Detection
Resource Allocation
Protection
Program execution
Operating systems handle many kinds of activities from user programs to system programs
like printer spooler, name servers, file server, etc. Each of these activities is encapsulated as a
process.
A process includes the complete execution context (code to execute, data to manipulate,
registers, OS resources in use). Following are the major activities of an operating system with
respect to program management −
I/O Operation
An I/O subsystem comprises of I/O devices and their corresponding driver software.
Drivers hide the peculiarities of specific hardware devices from the users.
An Operating System manages the communication between user and device drivers.
I/O operation means read or write operation with any file or any specific I/O device.
Operating system provides the access to the required I/O device when required.
A file system is normally organized into directories for easy navigation and usage.
These directories may contain files and other directions. Following are the major activities
of an operating system with respect to file management −
Communication
In case of distributed systems which are a collection of processors that do not share
memory, peripheral devices, or a clock, the operating system manages communications
between all the processes. Multiple processes communicate with one another through
communication lines in the network.
The OS handles routing and connection strategies, and the problems of contention and
security. Following are the major activities of an operating system with respect to
communication −
Error handling
Errors can occur anytime and anywhere. An error may occur in CPU, in I/O devices
or in the memory hardware. Following are the major activities of an operating system with
respect to error handling −
Protection
5. System Call
If a file system requires the creation or deletion of files. Reading and writing from files
also require a system call.
Creation and management of new processes.
Network connections also require system calls. This includes sending and receiving
packets.
Access to a hardware devices such as a printer, scanner etc. requires a system call.
Types of System Calls
There are mainly five types of system calls. These are explained in detail as follows
Process Control
These system calls deal with processes such as process creation, process termination etc.
File Management
These system calls are responsible for file manipulation such as creating a file,
reading a file, writing into a file etc.
Device Management
These system calls are responsible for device manipulation such as reading from
device buffers, writing into device buffers etc.
Information Maintenance
These system calls handle information and its transfer between the operating system
and the user program.
Communication
These system calls are useful for inter process communication. They also deal with
creating and deleting a communication connection.
The system program serves as a part of the operating system. It traditionally lies
between the user interface and the system calls. The user view of the system is actually
defined by system programs and not system calls because that is what they interact with
and system programs are closer to the user interface.
An image that describes system programs in the operating system hierarchy is as follows:
In the above image, system programs as well as application programs form a bridge
between the user interface and the system calls. So, from the user view the operating
system observed is actually the system programs and not the system calls.
Types of System Programs
System programs can be divided into seven parts. These are given as follows:
Status Information
The status information system programs provide required data on the current or past
status of the system. This may include the system date, system time, available memory in
system, disk space, logged in users etc.
Communications
These system programs are needed for system communications such as web
browsers. Web browsers allow systems to communicate and access information from the
network as required.
File Manipulation
These system programs are used to manipulate system files. This can be done using
various commands like create, delete, copy, rename, print etc. These commands can create
files, delete files, copy the contents of one file into another, rename files, print them etc.
The system programs that deal with program loading and execution make sure that
programs can be loaded into memory and executed correctly. Loaders and Linkers are a
prime example of this type of system programs.
File Modification
System programs that are used for file modification basically change the data in the
file or modify it in some other way. Text editors are a big example of file modification
system programs.
Application Programs
Application programs can perform a wide range of services as per the needs of the
users. These include programs for database systems, word processors, plotting tools,
spreadsheets, games, scientific applications etc.
Programming Language Support
Simple Structure
There are many operating systems that have a rather simple structure. These started
as small systems and rapidly expanded much further than their scope. A common example
of this is MS-DOS. It was designed simply for a niche amount for people. There was no
indication that it would become so popular.
Layered Structure
One way to achieve modularity in the operating system is the layered approach. In
this, the bottom layer is the hardware and the topmost layer is the user interface.
As seen from the image, each upper layer is built on the bottom layer. All the layers
hide some structures, operations etc from their upper layers.
One problem with the layered structure is that each layer needs to be carefully
defined. This is necessary because the upper layers can only use the functionalities of the
layers below them.
Virtual Machine
The layered approach of operating systems is taken to its logical conclusion in the
concept of virtual machine. The fundamental idea behind a virtual machine is to abstract
the hardware of a single computer (the CPU, Memory, Disk drives, Network Interface
Cards, and so forth) into several different execution environments and thereby creating the
illusion that each separate execution environment is running its own private computer. By
using CPU Scheduling and Virtual Memory techniques, an operating system can create the
illusion that a process has its own processor with its own (virtual) memory. Normally a
process has additional features, such as system calls and a file system, which are not
provided by the hardware. The Virtual machine approach does not provide any such
additional functionality but rather an interface that is identical to the underlying bare
hardware. Each process is provided with a (virtual) copy of the underlying computer.
Such software is now often referred to with the terms virtualization and virtual
servers. The host software which provides this capability is often referred to as a virtual
machine
An operating system is a construct that allows the user application programs to interact
with the system hardware. Operating system by itself does not provide any function but it
provides an atmosphere in which different applications and programs can do useful work.
There are many problems that can occur while designing and implementing an
operating system. These are covered in operating system design and implementation.
Operating System Design Goals
It is quite complicated to define all the goals and specifications of the operating
system while designing it. The design changes depending on the type of the operating
system i.e if it is batch system, time shared system, single user system, multi user system,
distributed system etc.
There are basically two types of goals while designing an operating system. These are:
User Goals
The operating system should be convenient, easy to use, reliable, safe and fast
according to the users. However, these specifications are not very useful as there is no set
method to achieve these goals.
System Goals
The operating system should be easy to design, implement and maintain. These are
specifications required by those who create, maintain and operate the operating system. But
there is not specific method to achieve these goals as well.
A subtle difference between mechanism and policy is that mechanism shows how to
do something and policy shows what to do. Policies may change over time and this would
lead to changes in mechanism. So, it is better to have a general mechanism that would
require few changes even when a policy change occurs.
For example - If the mechanism and policy are independent, then few changes are
required in mechanism if policy changes. If a policy favours I/O intensive processes over
CPU intensive processes, then a policy change to preference of CPU intensive processes
will not change the mechanism.
Using high level language for implementing an operating system leads to a loss in
speed and increase in storage requirements. However in modern systems only a small
amount of code is needed for high performance, such as the CPU scheduler and memory
manager. Also, the bottleneck routines in the system can be replaced by assembly language
equivalents if required.
9. System Generations
Operating Systems have evolved over the years. So, their evolution through the
years can be mapped using generations of operating systems. There are four generations of
operating systems. These can be described as follows:
The First Generation ( 1945 - 1955 ): Vacuum Tubes and Plugboards
Digital computers were not constructed until the second world war. Calculating
engines with mechanical relays were built at that time. However, the mechanical relays
were very slow and were later replaced with vacuum tubes. These machines were
enormous but were still very slow.
These early computers were designed, built and maintained by a single group of
people. Programming languages were unknown and there were no operating systems so all
the programming was done in machine language. All the problems were simple numerical
calculations.
By the 1950’s punch cards were introduced and this improved the computer system.
Instead of using plugboards, programs were written on cards and read into the system.
The Batch System was introduced to reduce the wasted time in the computer. A tray
full of jobs was collected in the input room and read into the magnetic tape. After that, the
tape was rewound and mounted on a tape drive. Then the batch operating system was
loaded in which read the first job from the tape and ran it. The output was written on the
second tape. After the whole batch was done, the input and output tapes were removed and
the output tape was printed.
Until the 1960’s, there were two types of computer systems i.e the scientific and the
commercial computers. These were combined by IBM in the System/360. This used
integrated circuits and provided a major price and performance advantage over the second
generation systems.
The advent of personal computers also led to the growth of networks. This created
network operating systems and distributed operating systems. The users were aware of a
network while using a network operating system and could log in to remote machines and
copy files from one machine to another.
UNIT –II
2. Process
A process is basically a program in execution. The execution of a process must progress in a
sequential fashion.
A process is defined as an entity which represents the basic unit of work to be
implemented in the system.
When a program is loaded into the memory and it becomes a process, it can be divided into
four sections ─ stack, heap, text and data. The following image shows a simplified layout of a
process inside main memory −
Text This includes the current activity represented by the value of Program Counter and the contents
3
of the processor's registers.
4 Data This section contains the global and static variables.
.
2.10 Scheduling Criteria
2.12 Multiprocessing scheduling
In the multiprocessor scheduling, there are multiple CPU’s which share the load so that
various process run simultaneously. In general, the multiprocessor scheduling is complex as
compared to single processor scheduling. In the multiprocessor scheduling, there are many
processors and they are identical and we can run any process at any time.
The multiple CPU’s in the system are in the close communication which shares a
common bus, memory and other peripheral devices. So we can say that the system is a
tightly coupled system. These systems are used when we want to process a bulk amount of
data. These systems are mainly used in satellite, weather forecasting etc.
The most important thing is that we can do more work in a short period of time. If
one system fails in the multiprocessor system the whole system will not halt only the speed
of the processor will be slow down.
These type of system allow the threads to run on more than one processor
simultaneously. In these systems the various process in the parallel so this is called parallel
processor. Parallel processing is the ability of the CPU to run various process
simultaneously. In the multiprocessing system, there is dynamically sharing of resources
among the various processors.
First Come First Serve is the full form of FCFS. It is the easiest and most simple CPU
scheduling algorithm. In this type of algorithm, the process which requests the CPU gets the CPU
allocation first. This scheduling method can be managed with a FIFO queue.
The full form of SRT is Shortest remaining time. It is also known as SJF preemptive
scheduling. In this method, the process will be allocated to the task, which is closest to its
completion. This method prevents a newer ready state process from holding the completion of an
older process.
This method is mostly applied in batch environments where short jobs are required to be given
preference.
This is not an ideal method to implement it in a shared system where the required CPU time is
unknown.
Associate with each process as the length of its next CPU burst. So that operating system uses these
lengths, which helps to schedule the process with the shortest possible time.
Round-Robin Scheduling
Round robin is the oldest, simplest scheduling algorithm. The name of this algorithm comes
from the round-robin principle, where each person gets an equal share of something in turn. It is
mostly used for scheduling algorithms in multitasking. This algorithm method helps for starvation
free execution of processes.
SJF is a full form of (Shortest job first) is a scheduling algorithm in which the process with the
shortest execution time should be selected for execution next. This scheduling method can be
preemptive or non-preemptive. It significantly reduces the average waiting time for other processes
awaiting execution.
This algorithm separates the ready queue into various separate queues. In this method,
processes are assigned to a queue based on a specific property of the process, like the process
priority, size of the memory, etc.
Process Synchronization is a technique which is used to coordinate the process that use
shared Data. There are two types of Processes in Operating Systems:-
Independent Process –
The process that does not affect or is affected by the other process while its execution
then the process is called Independent Process. Example The process that does not share
any shared variable, database, files, etc.
Cooperating Process –
The process that affect or is affected by the other process while execution, is called a
Cooperating Process. Example The process that share file, variable, database, etc are the
Cooperating Process.
This can lead to the inconsistency of shared data. So the change made by one process not
necessarily reflected when other processes accessed the same shared data. To avoid this
type of inconsistency of data, the processes need to be synchronized with each other.
Race Condition
It is the condition where several processes tries to access the resources and modify the
shared data concurrently and outcome of the process depends on the particular order of
execution that leads to data inconsistency, this condition is called Race Condition. This
condition can be avoided using the technique called Synchronization or Process
Synchronization, in which we allow only one process to enter and manipulates the shared
data in Critical Section.
For Example, process A changing the data in a memory location while another process B
is trying to read the data from the same memory location. There is a high probability that
data read by the second process will be erroneous.
Critical Section
Critical Section is the part of a program which tries to access shared resources. The critical
section cannot be executed by more than one process at the same time; operating system faces
the difficulties in allowing and disallowing the processes from entering the critical section.
Critical section is a code segment that can be accessed by only one process at a time.
Critical section contains shared variables which need to be synchronized to maintain.
Entry Section
It is part of the process which decides the entry of a particular process in the Critical
Section, out of many other processes.
Critical Section
It is the part in which only one process is allowed to enter and modify the shared variable.
This part of the process ensures that only no other process can access the resource of
shared data.
Exit Section
This process allows the other process that are waiting in the Entry Section, to enter into
the Critical Sections. It checks that a process that after a process has finished execution in
Critical Section can be removed through this Exit Section.
Remainder Section
The other parts of the Code other than Entry Section, Critical Section and Exit Section
are known as Remainder Section.
Critical Section Problem
A Critical Section is a code segment that accesses shared variables and has to be executed as an
atomic action. It means that in a group of cooperating processes, at a given point of time, only
one process must be executing its critical section.
Any solution to the critical section problem must satisfy three requirements:
Mutual Exclusion: If a process is executing in its critical section, then no other process
is allowed to execute in the critical section.
Progress: If no process is executing in the critical section and other processes are waiting
outside the critical section, then only those processes that are not executing in their
remainder section can participate in deciding which will enter in the critical section next,
and the selection cannot be postponed indefinitely.
Bounded Wait: A bound must exist on the number of times that other processes are
allowed to enter their critical sections after a process has made a request to enter its
critical section and before that request is granted.
Synchronization Hardware
Many systems provide hardware support for critical section code. The critical section
problem could be solved easily in a single-processor environment if we could disallow
interrupts to occur while a shared variable or resource is being modified.
In this manner, we could be sure that the current sequence of instructions would be
allowed to execute in order without pre-emption. Unfortunately, this solution is not
feasible in a multiprocessor environment.
Disabling interrupt on a multiprocessor environment can be time consuming as the
message is passed to all the processors.
This message transmission lag, delays entry of threads into critical section and the system
efficiency decreases.
Mutex Locks
As the synchronization hardware solution is not easy to implement for everyone, a strict
software approach called Mutex Locks was introduced. In this approach, in the entry
section of code, a LOCK is acquired over the critical resources modified and used inside
critical section, and in the exit section that LOCK is released.
As the resource is locked while a process executes its critical section hence no other
process can access it.
Semaphores
In 1965, Dijkstra proposed a new and very significant technique for managing concurrent
processes by using the value of a simple integer variable to synchronize the progress of
interacting processes. This integer variable is called semaphore. So it is basically a
synchronizing tool and is accessed only through two low standard atomic operations,
wait and signal designated by P(S) and V(S) respectively.
In very simple words, semaphore is a variable which can hold only a non-negative
Integer value, shared between all the threads, with operations wait and signal, which
work as follow:
P(S): if S ≥ 1 then S := S - 1
else <block and enqueue the process>;
Wait: Decrements the value of its argument S, as soon as it would become non-
negative(greater than or equal to 1).
Signal: Increments the value of its argument S, as there is no more process blocked on
the queue.
Properties of Semaphores
Types of Semaphores
Binary Semaphore:
Counting Semaphores:
Example of Use
Limitations of Semaphores
Below are some of the classical problem depicting flaws of process synchronaization in systems
where cooperating processes are present.
This problem is generalized in terms of the Producer Consumer problem, where a finite buffer
pool is used to exchange messages between producer and consumer processes.
Because the buffer pool has a maximum size, this problem is often called the Bounded
buffer problem.
Solution to this problem is, creating two counting semaphores "full" and "empty" to keep track
of the current number of full and empty buffers respectively.
The dining philosopher's problem involves the allocation of limited resources to a group of
processes in a deadlock-free and starvation-free manner.
There are five philosophers sitting around a table, in which there are five chopsticks/forks kept
beside them and a bowl of rice in the centre, When a philosopher wants to eat, he uses two
chopsticks - one from their left and one from their right. When a philosopher wants to think, he
keeps down both chopsticks at their original place.
In this problem there are some processes(called readers) that only read the shared data, and
never change it, and there are other processes(called writers) who may change the data in
addition to reading, or instead of reading it.
There are various type of readers-writers problem, most centred on relative priorities of readers
and writers.
Bounded Buffer Problem
Bounded buffer problem, which is also called producer consumer problem, is one of
the classic problems of synchronization. Let's start by understanding the problem here,
before moving on to the solution and program code.
There is a buffer of n slots and each slot is capable of storing one unit of data. There are
two processes running, namely, producer and consumer, which are operating on the
buffer.
A producer tries to insert data into an empty slot of the buffer. A consumer tries to
remove data from a filled slot in the buffer. As you might have guessed by now, those
two processes won't produce the expected output if they are being executed concurrently.
There needs to be a way to make the producer and consumer work in an independent
manner.
Here's a Solution
One solution of this problem is to use semaphores. The semaphores which will be used
here are:
do
{
// wait until empty > 0 and then decrement 'empty'
wait(empty);
// acquire lock
wait(mutex);
// release lock
signal(mutex);
// increment 'full'
signal(full);
}
while(TRUE)
i. Looking at the above code for a producer, we can see that a producer first waits until
there is atleast one empty slot.
ii. Then it decrements the empty semaphore because, there will now be one less empty
slot, since the producer is going to insert data in one of those slots.
iii. Then, it acquires lock on the buffer, so that the consumer cannot access the buffer until
producer completes its operation.
iv. After performing the insert operation, the lock is released and the value of full is
incremented because the producer has just filled a slot in the buffer.
do
{
// wait until full > 0 and then decrement 'full'
wait(full);
// acquire the lock
wait(mutex);
Consider there are five philosophers sitting around a circular dining table. The dining
table has five chopsticks and a bowl of rice in the middle as shown in the below figure.
From the problem statement, it is clear that a philosopher can think for an indefinite
amount of time. But when a philosopher starts eating, he has to stop at some point of
time. The philosopher is in an endless cycle of thinking and eating.
while(TRUE)
{
wait(stick[i]);
/*
mod is used because if i=5, next
chopstick is 1 (dining table is circular)
*/
wait(stick[(i+1) % 5]);
/* eat */
signal(stick[i]);
signal(stick[(i+1) % 5]);
/* think */
}
When a philosopher wants to eat the rice, he will wait for the chopstick at his left and
picks up that chopstick. Then he waits for the right chopstick to be available, and then
picks it too. After eating, he puts both the chopsticks down.
But if all five philosophers are hungry simultaneously, and each of them pickup one
chopstick, then a deadlock situation occurs because they will be waiting for another
chopstick forever. The possible solutions for this are:
i. A philosopher must be allowed to pick up the chopsticks only if both the left and right
chopsticks are available.
ii. Allow only four philosophers to sit at the table. That way, if all the four philosophers
pick up four chopsticks, there will be one chopstick left on the table.
iii. So, one philosopher can start eating and eventually, two chopsticks will be available. In
this way, deadlocks can be avoided.
What is Readers Writer Problem?
There is a shared resource which should be accessed by multiple processes. There are two
types of processes in this context. They are reader and writer. Any number of readers
can read from the shared resource simultaneously, but only one writer can write to the
shared resource. When a writer is writing data to the resource, no other process can
access the resource. A writer cannot write to the resource if there are non zero number of
readers accessing the resource at that time.
The Solution
From the above problem statement, it is evident that readers have higher priority than
writer. If a writer wants to write to the resource, it must wait until there are no readers
currently accessing that resource.
Here, we use one mutex m and a semaphore w. An integer variable read_count is used
to maintain the number of readers currently accessing the resource. The variable
read_count is initialized to 0. A value of 1 is given initially to m and w.
Instead of having the process to acquire lock on the shared resource, we use the mutex m
to make the process to acquire and release lock whenever it is updating the read_count
variable.
while(TRUE)
{
wait(w);
signal(w);
}
And, the code for the reader process looks like this:
while(TRUE)
{
//acquire lock
wait(m);
read_count++;
if(read_count == 1)
wait(w);
//release lock
signal(m);
// acquire lock
wait(m);
read_count--;
if(read_count == 0)
signal(w);
// release lock
signal(m);
}
As seen above in the code for the writer, the writer just waits on the w semaphore until it
gets a chance to write to the resource.
i. After performing the write operation, it increments w so that the next writer can access
the resource.
ii. On the other hand, in the code for the reader, the lock is acquired whenever the
read_count is updated by a process.
iii. When a reader wants to access the resource, first it increments the read_count value,
then accesses the resource and then decrements the read_count value.
iv. The semaphore w is used by the first reader which enters the critical section and the last
reader which exits the critical section.
v. The reason for this is, when the first readers enters the critical section, the writer is
blocked from the resource. Only new readers can access the resource now.
vi. Similarly, when the last reader exits the critical section, it signals the writer using the w
semaphore because there are zero readers now and a writer can have the chance to
access the resource.
The monitor is one of the ways to achieve Process synchronization. The monitor is
supported by programming languages to achieve mutual exclusion between processes.
For example Java Synchronized methods. Java provides wait() and notify()
constructs.
Condition Variables:
Two different operations are performed on the condition variables of the monitor.
Wait.
signal.
Wait operation
x.wait() : Process performing wait operation on any condition variable are suspended.
The suspended processes are placed in block queue of that condition variable.
Signal operation
x.signal(): When a process performs signal operation on condition variable, one of the
blocked processes is given chance.
Disadvantages of Monitor:
Monitors have to be implemented as part of the programming language . The compiler
must generate code for them. This gives the compiler the additional burden of having to
know what operating system facilities are available to control access to critical sections in
concurrent processes. Some languages that do support monitors are Java,C#,Visual
Basic,Ada and concurrent Euclid.
Peterson’s Solution
Peterson’s Solution is a classical software based solution to the critical section problem.
boolean flag[i] :Initialized to FALSE, initially no one is interested in entering the critical
section
int turn : The process whose turn is to enter the critical section.
Mutual Exclusion is assured as only one process can access the critical section at any
time.
Progress is also assured, as a process outside the critical section does not block other
processes from entering the critical section.
Bounded Waiting is preserved as every process gets a fair chance.
Disadvantages of Peterson’s Solution
Here are some widely used methods to solve the critical section problem.
Peterson Solution
Peterson's solution is widely used solution to critical section problems. This algorithm was
developed by a computer scientist Peterson that's why it is named as a Peterson's solution.
In this solution, when a process is executing in a critical state, then the other process only
executes the rest of the code, and the opposite can happen. This method also helps to make sure
that only a single process runs in the critical section at a specific time.
Example
PROCESS Pi
FLAG[i] = true
while( (turn != i) AND (CS is !free) ){ wait;
}
CRITICAL SECTION FLAG[i] = false
turn = j; //choose another process to go to CS
i. Assume there are N processes (P1, P2, ... PN) and every process at some point of
time requires to enter the Critical Section
ii. A FLAG[] array of size N is maintained which is by default false. So, whenever a
process requires to enter the critical section, it has to set its flag as true. For
example, If Pi wants to enter it will set FLAG[i]=TRUE.
iii. Another variable called TURN indicates the process number which is currently
wafting to enter into the CS.
iv. The process which enters into the critical section while exiting would change the
TURN to another number from the list of ready processes.
v. Example: turn is 2 then P2 enters the Critical section and while exiting turn=3 and
Semaphore Solution
It uses two atomic operations, 1)wait, and 2) signal for the process synchronization.
Example
WAIT ( S ):
while ( S <= 0 );
S = S - 1;
SIGNAL ( S ):
S = S + 1;
Summary:
Process synchronization is the task of coordinating the execution of processes in a way that no
two processes can have access to the same shared data and resources.
Four elements of critical section are 1) Entry section 2) Critical section 3) Exit section 4)
Reminder section
A critical section is a segment of code which can be accessed by a signal process at a specific
point of time.
Three must rules which must enforce by critical section are : 1) Mutual Exclusion 2) Process
solution 3)Bound waiting.
Mutual Exclusion is a special type of binary semaphore which is used for controlling access to
the shared resource.
Process solution is used when no one is in the critical section, and someone wants in.
In bound waiting solution, after a process makes a request for getting into its critical section,
there is a limit for how many other processes can get into their critical section.
Peterson's solution is widely used solution to critical section problems.
Problems of the Critical Section are also resolved by synchronization of hardware
Synchronization hardware is not a simple method to implement for everyone, so the strict
software method known as Mutex Locks was also introduced.
Semaphore is another algorithm or solution to the critical section problem.
UNIT-IV
Deadlocks
Deadlock – In Real world
1.2
Deadlock - In Real world
1.3
Deadlock – In OS
1.4
Deadlock and Starvation
Deadlock – two or more processes are waiting indefinitely for
an event that can be caused by only one of the waiting
processes
Let S and Q be two semaphores initialized to 1
P0 P1
wait(S); wait(Q);
wait(Q); wait(S);
... ...
signal(S); signal(Q);
signal(Q); signal(S);
1.5
System Model
1.6
Deadlock Characterization
Deadlock can arise if four conditions hold
simultaneously.
Mutual exclusion:
Resources can shareable or non sharable
e.g. Printer as a non sharable resource
e.g. Ethernet/Internet connection as a sharable resource
only one process at a time can use a resource i.e. mutual
exclusion
Hold and wait: a process holding at least one resource is
waiting to acquire additional resources held by other
processes
No preemption: a resource can be released only voluntarily
by the process holding it, after that process has completed
its task
Circular wait: there exists a set {P0, P1, …, Pn} of waiting
processes such that P0 is waiting for a resource that is held
by P1, P1 is waiting for a resource that is held by P2, …, Pn–1
is waiting for a resource that is held by Pn, and Pn is
See Video that is held by P0.
waiting for a resource 1.7
Deadlock Characterization Cont..
P1 P2 P3
1.8
Resource-Allocation Graph
A set of vertices V and a set of edges E.
V is partitioned into two types:
P = {P1, P2, …, Pn}, the set consisting of all the
processes in the system
1.9
Resource-Allocation Graph (Cont.)
Process
Pi requests instance of Rj
Pi
Rj
Pi is holding an instance of Rj
Pi
Rj
1.10
Example of a Resource Allocation Graph
1.11
Resource Allocation Graph With A Deadlock
1.12
Graph With A Cycle But No Deadlock
1.13
Basic Facts
1.14
Methods for Handling Deadlocks
1.15
Deadlock Prevention
Restrain the ways request can be made
1.16
Deadlock Prevention (Cont.)
No Preemption –
Allow process to forcefully preempt the resource holding
by other process.
If a process that is holding some resources requests
another resource that cannot be immediately allocated to
it, then all resources currently being held are released
Preempted resources are added to the list of resources for
which the process is waiting
Process will be restarted only when it can regain its old
resources, as well as the new ones that it is requesting
See Video
Circular Wait – impose a total ordering of all resource
types, and require that each process requests resources in an
increasing order of list
See Video
1.17
Deadlock Avoidance
Requires that the system has some additional a priori information
available
Simplest and most useful model requires that each
process declare the maximum number of resources of
each type that it may need
The deadlock-avoidance algorithm dynamically examines
the resource-allocation state to ensure that there
can never be a circular-wait condition
Resource-allocation state is defined by the number of
available and allocated resources, and the maximum
demands of the processes
1.18
Safe State
1.19
Basic Facts
1.20
Safe, Unsafe, Deadlock State
1.21
Avoidance Algorithms
1.22
Resource-Allocation Graph Scheme
Claim edge Pi Rj indicated that process Pj may request
resource Rj; represented by a dashed line
Claim edge converts to request edge when a process
requests a resource
Request edge converted to an assignment edge when the
resource is allocated to the process
When a resource is released by a process, assignment
edge reconverts to a claim edge
Resources must be claimed a priori in the system
1.23
Resource-Allocation Graph
Request Edge
Claim Edge
1.24
Unsafe State In Resource-Allocation Graph
Assignment Edge
1.25
Resource-Allocation Graph Algorithm
1.26
Banker’s Algorithm
Multiple instances
See Video
1.27
Data Structures for the Banker’s Algorithm
1.28
Safety Algorithm
1. Let Work and Finish be vectors of length m and n,
respectively. Initialize:
Work = Available
Finish [i] = false for i = 0, 1, …, n- 1
1.29
Example of Banker’s Algorithm
1.30
Example (Cont.)
The content of the matrix Need is defined to be Max –
Allocation
Need
A B C
P0 7 4 3
P1 1 2 2
P2 6 0 0
P3 0 1 1
P4 4 3 1
The system is in a safe state since the sequence < P1, P3, P4,
P2, P0> satisfies safety criteria
1.31
Resource-Request Algorithm for Process Pi
1.32
Example: P1 Request (1,0,2)
Check that Request Available (that is, (1,0,2) (3,3,2)
true
Allocation Need Available
A B C A B C A B C
P0 0 1 0 7 4 3 2 3 0
P1 3 0 2 0 2 0
P2 3 0 2 6 0 0
P3 2 1 1 0 1 1
P4 0 0 2 4 3 1
Executing safety algorithm shows that sequence < P1, P3, P4, P0,
P2> satisfies safety requirement
1.33
Deadlock Detection
Detection algorithm
Recovery scheme
1.34
Single Instance of Each Resource Type
1.35
Resource-Allocation Graph and Wait-for Graph
1.36
Several Instances of a Resource Type
1.37
Detection Algorithm
1.38
Detection Algorithm (Cont.)
3. Work = Work + Allocationi
Finish[i] = true
go to step 2
1.39
Example of Detection Algorithm
Five processes P0 through P4; three resource types
A (7 instances), B (2 instances), and C (6 instances)
Sequence <P0, P2, P3, P1, P4> will result in Finish[i] = true for
all i
1.40
Example (Cont.)
State of system?
Can reclaim resources held by process P0, but insufficient
resources to fulfill other processes; requests
Deadlock exists, consisting of processes P1, P2, P3, and P4
1.41
Detection-Algorithm Usage
When, and how often, to invoke depends on:
How often a deadlock is likely to occur?
How many processes will need to be rolled back?
one for each disjoint cycle
1.42
Recovery from Deadlock: Process Termination
1.43
Recovery from Deadlock: Resource Preemption
1.44
End
The image cannot be displayed. Your computer may not have enough memory to open the image, or the image may have been corrupted. Restart your computer, and then open the file again. If the red x still appears, you may have to delete the image and then
insert it again.
Chapter 7: Deadlocks!
Chapter 7: Deadlocks!
■ Process
"
■ Resource Type with 4 instances"
"
Pi!
"
R j!
■ Pi is holding an instance of Rj!
Pi"
R j!
■ Deadlock Prevention"
■ Deadlock Avoidance"
■ Deadlock Detection"
■ No Preemption –"
● If a process that is holding some resources requests
another resource that cannot be immediately allocated to
it, then all resources currently being held are released."
● Preempted resources are added to the list of resources
for which the process is waiting."
● Process will be restarted only when it can regain its old
resources, as well as the new ones that it is requesting.
"
■ Circular Wait – impose a total ordering of all resource types,
and require that each process requests resources in an
increasing order of enumeration."
■ Banker’s Algorithm"
■ Resource-Request Algorithm"
■ Safety Algorithm"
■ Multiple instances.
"
■ Each process must a priori claim maximum use.
"
■ When a process requests a resource it may have to wait.
"
■ When a process gets all its resources it must return them in
a finite amount of time."
End of Chapter 7!
UNIT-V
Part 1: Memory Management
Background
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
Memory unit only sees a stream of addresses + read requests, or
address + data and write requests
Register access in one CPU clock (or less)
Cache sits between main memory and CPU registers
Protection of memory required to ensure correct operation
1.2
Multistep Processing of a User Program
1.3
Binding of Instructions and Data to Memory
1.4
Base and Limit Registers
A pair of base or relocation register and limit registers define
the logical address space
CPU must check every memory access generated in user mode to
be sure it is between base and limit for that user
1.5
Logical vs. Physical Address Space
1.6
Dynamic relocation using a relocation register
See Video
1.7
Swapping
1.8
Schematic View of Swapping
1.9
Contiguous Allocation
Contiguous allocation is one early method
Main memory usually into two partitions:
Resident operating system, usually held in low memory with
interrupt vector
User processes then held in high memory
Each process contained in single contiguous section of
memory
Leads to External Fragmentation: Total memory space exists to
satisfy a request, but it is not contiguous.
See Video
1.10
Fixed Size-partition allocation
This scheme divided
memory into a
number of separate
fixed areas.
Each memory area
could hold one
process.
See Video
1.11
Multiple-partition allocation
Multiple-partition allocation
Degree of multiprogramming limited by number of partitions
Variable-partition sizes for efficiency (sized to a given process’ needs)
Hole – block of available memory; holes of various size are scattered
throughout memory
When a process arrives, it is allocated memory from a hole large enough to
accommodate it
Process exiting frees its partition, adjacent free partitions combined
Operating system maintains information about:
a) allocated partitions b) free partitions (hole)
See Video
1.12
Dynamic Storage-Allocation Problem
How to satisfy a request of size n from a list of free holes?
Worst-fit: Allocate the largest hole; must also search entire list
Produces the largest leftover hole
1.13
Fragmentation
External Fragmentation – total memory space exists to
satisfy a request, but it is not contiguous
Internal Fragmentation – allocated memory may be slightly
larger than requested memory; this size difference is memory
internal to a partition, but not being used
First fit analysis reveals that given N blocks allocated, 0.5 N
blocks lost to fragmentation
1/3 may be unusable -> 50-percent rule
1.14
Segmentation Architecture
Logical address consists of a two tuple:
<segment-number, offset> i.e. < S , d >
See Video
1.15
Segmentation Hardware
1.16
Segmentation Hardware
1.17
Paging
Physical address space of a process can be noncontiguous; process is
allocated physical memory whenever the latter is available
Avoids external fragmentation
Avoids problem of varying sized memory chunks
Divide physical memory into fixed-sized blocks called frames
Size is power of 2, between 512 bytes and 16 Mbytes
Divide logical memory into blocks of same size called pages
Keep track of all free frames
To run a program of size N pages, need to find N free frames and load
program
Set up a page table to translate logical to physical addresses
Backing store likewise split into pages
Still have Internal fragmentation
See Video
1.18
Address Translation Scheme
Address generated by CPU is divided into:
Page number (p) – used as an index into a page table which
contains base address of each page in physical memory
Page offset (d) – combined with base address to define the
physical memory address that is sent to the memory unit
1.19
Paging Hardware
1.20
Paging Model of Logical and Physical Memory
1.21
Paging Example
1.22
End of Part 1
UNIT-V
Part 2: Virtual Memory
Virtual Memory
Code needs to be in memory to execute, but entire program rarely used
Error code, unusual routines, large data structures
Entire program code not needed at same time
Consider ability to execute partially-loaded program
Program no longer constrained by limits of physical memory
Each program takes less memory while running -> more programs
run at the same time
Increased CPU utilization and throughput with no increase in
response time or turnaround time
Less I/O needed to load or swap programs into memory -> each user
program runs faster
1.2
Memory Management Unit (MMU)
MMU, is built into the hardware.
job is to translate virtual addresses into physical addresses.
1.3
Demand Paging
Quite similar to a paging system with swapping
processes reside in secondary memory and pages are loaded only on
demand, not in advance
1.4
Page Fault
1.5
Page Replacement Algorithm
1.6
First In First Out (FIFO) algorithm
Oldest page in main memory is the one which will be selected for
replacement.
1.7
Optimal Page algorithm
Also called OPT or MIN & has the lowest page-fault rate of all algorithms
Replace the page that will not be used for the longest period of time.
Use the time when a page is to be used.
1.8
Least Recently Used (LRU) algorithm
Page which has not been used for the longest time in main memory is the
one which will be selected for replacement.
keep a list, replace pages by looking back into time.
1.9
End of Part 2