0% found this document useful (0 votes)
39 views33 pages

OS Full Notes

Uploaded by

Barathkumar K
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)
39 views33 pages

OS Full Notes

Uploaded by

Barathkumar K
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/ 33

Unit 1

Introduction to OperatingSystem
Introduction
An Operating System (OS) is an interface
between computer user and computer hardware. An
operating system is software which performs all the
basic tasks like file management, memory management,
process management, handling input and output, and
controlling peripheral devices such as disk drives and The problems with Batch Systems are as follows −
• Lack of interaction between the user and the job.
printers.
Some popular Operating Systems include Linux • CPU is often idle, because the speed of the
Operating System, Windows Operating System, VMS,
mechanical I/O devices is slower than the CPU.
OS/400, AIX, z/OS, etc.
Operating System Definition • Difficult to provide the desired priority.
A program that acts as an intermediary between a
2. Time-sharing operatingsystems
user of a computer and the computer hardware.
• OS is a resource allocator Time-sharing is a technique which enables
• Manages all resources many people, located at various terminals, to use a
• Decides between conflicting requests for particular computer system at the same time. Time-
efficient and fair resource use
• OS is a control program sharing or multitasking is a logical extension of
• Controls execution of programs to prevent errors multiprogramming. Processor's time which is shared
and improper use of the computer
among multiple users simultaneously is termed as time-
Following are some of important functions of an sharing.
operating System. The main difference between Multi-
• Memory Management
• Processor Management programmed Batch Systems and Time-Sharing Systems
• Device Management is that in case of Multi-programmed batch systems, the
• File Management objective is to maximize processor use, whereas in
• Security
Time-Sharing Systems, the objective is to minimize
• Control over system performance
• Job accounting response time.
• Error detecting aids
• Coordination between other software and users

Types of Operating System


1. Batchoperating system
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 Advantages of Timesharing operating systems
• Provides the advantage of quick response.
submits it to the computer operator. To speed up • Avoids duplication of software.
processing, jobs with similar needs are batched together • Reduces CPU idle time.
and run as a group. The programmers leave their Disadvantages of Time-sharing operating systems
• Problem of reliability.
programs with the operator and the operator then sorts
• Question of security and integrity of user
the programs with similar requirements into batches. programs and data.
• Problem of data communication.

www.ourcreativeinfo.in
• Remote access to servers is possible from
3. Distributed operatingSystem
different locations and types of systems.
Distributed systems use multiple central
The disadvantages of network operating systems
processors to serve multiple real-time applications and • High cost of buying and running a server.
multiple users. Data processing jobs are distributed • Dependency on a central location for most
operations.
among the processors accordingly. • Regular maintenance and updates are required.
5. RealTimeoperatingSystem
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
The advantages of distributed systems
• With resource sharing facility, a user at one site input and display of required updated information is
may be able to use the resources available at termed as the response time. So in this method, the
another.
• Speedup the exchange of data with one another response time is very less as compared to online
via electronic mail. processing.
• If one site fails in a distributed system, the
A real-time operating system must have well-
remaining sites can potentially continue
operating. defined, fixed time constraints, otherwise the system
• Better service to the customers. will fail.
• Reduction of the load on the host computer.
• Reduction of delays in data processing. There are two types of real-time operating systems.
4. Network operatingSystem • Hard real-time systems
A Network Operating System runs on a server Hard real-time systems guarantee that critical tasks
and provides the server the capability to manage data, complete on time. In hard real-time systems, secondary
users, groups, security, applications, and other storage is limited or missing and the data is stored in
networking functions. ROM. In these systems, virtual memory is almost never
The primary purpose of the network operating found.
system is to allow shared file and printer access among • Soft real-time systems
multiple computers in a network, typically a local area Soft real-time systems are less restrictive. A critical
network (LAN), a private network or to other networks. 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.

The advantages of network operating systems


• Centralized servers are highly stable.
• Security is server managed.
• Upgrades to new technologies and hardware can
be easily integrated into the system.

www.ourcreativeinfo.in
Operating-System Services Some systems may include complex error
avoidance or recovery systems, including
backups, RAID drives, and other redundant
systems. Debugging and diagnostic tools aid
users and administrators in tracing down the
cause of problems.
Other systems aid in the efficient operation of the
OS itself:
o Resource Allocation –
E.g. CPU cycles, main memory, storage
space, and peripheral devices. Some resources
OS provide environments in which programs run, are managed with generic systems and others
and services for the users of the system, including: with very carefully designed and specially tuned
systems, customized for a particular resource
• User Interfaces – and operating environment.
Means by which users can issue
commands to the system. Depending on the o Accounting –
system these may be a command-line interface ( Keeping track of system activity and
e.g. sh, csh, ksh, tcsh, etc. ), a GUI interface ( resource usage, either for billing purposes or for
e.g. Windows, X-Windows, KDE, Gnome, etc. statistical record keeping that can be used to
), or a batch command systems. optimize future performance.
• Program Execution – o Protection and Security –
The OS must be able to load a program Preventing harm to the system and to
into RAM, run the program, and terminate the resources, either through wayward internal
program, either normally or abnormally. processes or malicious outsiders.
Authentication, ownership, and restricted access
• I/O Operations – are obvious parts of this system. Highly secure
The OS is responsible for transferring systems may log all process activity down to
data to and from I/O devices, including excruciating detail, and security regulation
keyboards, terminals, printers, and storage dictate the storage of those records on
devices permanent non-erasable medium for extended
. times in secure ( off-site ) facilities.
• File-System Manipulation – System Calls
In addition to raw data storage, the OS • System calls provide a means for user or
is also responsible for maintaining directory and
subdirectory structures, mapping file names to application programs to call upon the services of
specific blocks of data storage, and providing the operating system.
tools for navigating and utilizing the file
system. • The interface between a process and an
operating system is provided by system calls. In
• Communications –
Inter-process communications, IPC, general, system calls are available as assembly
either between processes running on the same language instructions.
processor, or between processes running on
separate processors or separate machines. May • System calls are usually made when a process in
be implemented as either shared memory or user mode requires access to a resource. Then it
message passing, ( or some systems may offer
both. ) requests the kernel to provide the resource via a
system call.
• Error Detection –
Both hardware and software errors • Generally written in C or C++, although some
must be detected and handled appropriately, are written in assembly for optimal performance.
with a minimum of harmful repercussions.

www.ourcreativeinfo.in
• Figure illustrates the sequence of system calls 2. File Management
required to copy a file: These system calls are responsible for file
manipulation such as
o create file, delete file
o open, close
o read, write, reposition
3. Device Management
These system calls are responsible for device
manipulation such as
o read, write, reposition
o request device, release device
o logically attach or detach device
4. Information Maintenance
These system calls handle information and its
transfer between the operating system and the user
• Most programmers do not use the low-level
program.
system calls directly, but instead use an o Get time and date, set time and date
"Application Programming Interface", API. o Get process, file, or device attribute
o Set process, file, or device attribute
• The use of APIs instead of direct system calls 5. Communication
provides for greater program portability between These system calls are useful for inter-process
different systems. The API then makes the communication. They also deal with creating and
deleting a communication connection.
appropriate system calls through the system call o Create, delete communication connection
interface o Send, receive message
o Attach or detach remote devices
In general, system calls are required in the following
Operating System Structure
situations:
1. Simple Structure
• If a file system requires the creation or deletion • MS-DOS – written to provide the most
of files. Reading and writing from files also functionality in the least space
• Not divided into modules
require a system call.
• Although MS-DOS has some structure, its
• Creation and management of new processes. interfaces and levels of Functionality are not
• Network connections also require system calls. well separated
MS-DOS Layer Structure
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:
1. Process Control
These system calls deal with processes such as
process
o end, abort
o load, execute
o create process, terminate process
o wait for time
o allocate and free memory

www.ourcreativeinfo.in
2. Layered Approach • Overall, similar to layers but with more flexible
• The operating system is divided into a number
of layers (levels), each built on top of lower
layers.
• The bottom layer (layer 0), is the hardware; the
highest (layer N) is the user interface.
• With modularity, layers are selected such that
Solaris Modular Approach
each uses functions (operations) and services of
only lower-level layers
Virtual Machines
• Virtual machines first appeared as the VM
Operating System for IBM mainframes in 1972.
• The concept of a virtual machine is to provide an
interface that looks like independent hardware, to
multiple different OSes running simultaneously on
the same physical hardware.
3. Microkernel System Structure • Each OS believes that it has access to and control
• Moves as much from the kernel into ―user‖ over its own CPU, RAM, I/O devices, hard drives,
space etc.
• Communication takes place between user • One obvious use for this system is for the
modules using message passing development and testing of software that must run
Benefits: on multiple platforms and/or OSes.
• Easier to extend a microkernel • One obvious difficulty involves the sharing of
• Easier to port the operating system to new hard drives, which are generally partitioned into
architectures
separate smaller virtual disks for each operating
• More reliable (less code is running in kernel
mode) OS.
• More secure
Detriments:
• Performance overhead of user space to kernel
space communication

Mac OS X Structure

Figure 16.1 - System models. (a) Nonvirtual machine.


(b)Virtual machine.
Benefits
4. Modules • Each OS runs independently of all the others,
• Most modern operating systems implement offering protection and security benefits.
kernel modules • ( Sharing of physical resources is not commonly
• Uses object-oriented approach implemented, but may be done as if the virtual
• Each core component is separate machines were networked together. )
• Each talks to the others over known interfaces • Virtual machines are a very useful tool for OS
• Each is loadable as needed within the kernel development, as they allow a user full access to

www.ourcreativeinfo.in
and control over a virtual machine, without Implementation
affecting other users operating the real machine. • Implementation may be challenging, partially due
• As mentioned before, this approach can also be to the consequences of user versus kernel mode.
useful for product development and testing of SW • Each of the simultaneously running kernels needs
that must run on multiple OSes / HW platforms. to operate in kernel mode at some point, but the
Simulation virtual machine actually runs in user mode.
• An alternative to creating an entire virtual machine • So the kernel mode has to be simulated for each of
is to simply run an emulator, which allows a the loaded OSes, and kernel system calls passed
program written for one OS to run on a different through the virtual machine into a true kernel
OS. mode for eventual HW access.
• For example, a UNIX machine may run a DOS • The virtual machines may run slower, due to the
emulator in order to run DOS programs, or vice- increased levels of code between applications and
versa. the HW, or they may run faster, due to the benefits
• Emulators tend to run considerably slower than the of caching. ( And virtual devices may also be
native OS, and are also generally less than perfect. faster than real devices, such as RAM disks which
are faster than physical disks. )
Para-virtualization
• Para-virtualization is another variation on the Example
theme, in which an environment is provided for The Java Virtual Machine
the guest program that is similar to its native OS, • Example of programming-environment
without trying to completely mimic it. virtualization
• Guest programs must also be modified to run on • Very popular language / application environment
the para-virtual OS. invented by Sun Microsystems in 1995
• Solaris 10 uses a zone system, in which the low- • Write once, run anywhere
level hardware is not virtualized, but the OS and • Includes language specification (Java), API
its devices ( device drivers ) are. library, Java virtual machine (JVM)
• Within a zone, processes have the view of an • Java objects specified by class construct, Java
isolated system, in which only the processes and program is one or more objects
resources within that zone are seen to exist. • Each Java object compiled into architecture-
• Figure 2.18 shows a Solaris system with the neutral bytecode output (.class) which JVM class
normal "global" operating space as well as two loader loads
additional zones running on a small virtualization • JVM compiled per architecture, reads bytecode
layer. and executes
• Includes garbage collection to reclaim memory no
longer in use
• Made faster by just-in-time (JIT) compiler that
turns bytecodes into native code and caches them

www.ourcreativeinfo.in
Unit II ➢ waiting: The process is waiting for some event
to occur
Process Management
➢ ready: The process is waiting to be assigned to
Process Definition:
a processor
o A process is basically a program in execution.
➢ terminated: The process has finished execution
The execution of a process must progress in a
Process state transitions / Life Cycle of a process
sequential fashion.
o A process is defined as an entity which
represents the basic unit of work to be implemented
in the system.
o Example : we write our computer programs in a
text file and when we execute this program, it
becomes a process which performs all the tasks
mentioned in the program.
o 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 Process Control Block (PCB)
main memory − o Each process is represented in the operating system
by a process control block
o It is also called as task control block
o It contains many piece of information associated
with a specific process
o Below figure illustrate the process control block

a. Stack: The process Stack contains the


temporary data such as method/function
parameters, return address and local variables.
b. Heap: This is dynamically allocated memory to
a process during its run time.
c. Text: This includes the current activity
represented by the value of Program Counter
and the contents of the processor's registers.
➢ Process state: The state may be new, ready,
d. Data: This section contains the global and static
running, waiting, etc…
variables.
➢ Program Counter: The counter indicates the
Process States:
address of the next instruction to be executed for
o As a process executes, its changes state.
o The state of a process is defined in part by the this process
current activity of that process. ➢ CPU Registers: Includes index registers, stack
Each process may be in one of the following states: pointers, and general purpose registers etc
➢ new: The process is being created ➢ CPU-Scheduling information: Includes
➢ running: Instructions are being executed priorities, scheduling queue pointers etc..

www.ourcreativeinfo.in
➢ Memory Management Information: Includes o For example, in a browser, multiple tabs can be
information about memory allocated to the different threads. MS Word uses multiple threads:
process one thread to format the text, another thread to
➢ Accounting Information: CPU used, clock process inputs, etc.
time elapsed since start, time limits etc Multithread Models
➢ I/O Status Information: I/O devices allocated
to process, list of open files
Context Switching
A context switch occurs when a computer‘s
CPU switches from one process to a different process or
from one thread to a different thread.
o Context switching allows for one CPU
to handle numerous processes or
threads without the need for additional processors.
o A context switch is the mechanism to store and
restore the state or context of a CPU in Process
Control block so that a process execution can be
resumed from the same point at a later time.
o Any operating system that allows for multitasking
relies heavily on the use of context switching
to allow different processes to run at the same time.
o There are three situations that a context switch is
necessary, as shown below.
➢ Multitasking - When the CPU needs to switch
processes in and out of memory, so that more
than one process can be running.
➢ Kernel/User Switch - When switching between
user mode to kernel mode, it may be used (but
isn't always necessary).
➢ Interrupts - When the CPU is interrupted to
return data from a disk read.
Threads
o Thread is a single sequence stream within a
process. Threads have same properties as of the
process so they are called as light weight processes.
o Threads are executed one after another but give the
illusion as if they are executing in parallel.
o Each thread has different states.
o Each thread has
1. A program counter
2. A register set
3. A stack space
Multithreads
o A thread is also known as lightweight process.
The idea is to achieve parallelism by dividing a
process into multiple threads.

www.ourcreativeinfo.in
Advantages of Thread
o Responsiveness: If the process is divided into
multiple threads, if one thread completes its
execution, then its output can be immediately
returned.
o Faster context switch: Context switch time
between threads is lower compared to process
context switch. Process context switching requires
more overhead from the CPU.
o Effective utilization of multiprocessor system: If
we have multiple threads in a single process, then
we can schedule multiple threads on multiple
processor. This will make process execution faster.
o Resource sharing: Resources like code, data, and
files can be shared among all threads within a
process.
o Similarity between Threads and Processes –
Note: stack and registers can‘t be shared among the
threads. Each thread has its own stack and registers.
➢ Only one thread or process is active at a time
o Communication: Communication between multiple
➢ Within process both execute sequential
threads is easier, as the threads shares common
➢ Both can create children
address space. while in process we have to follow
some specific communication technique for
o Differences between Threads and Processes –
Sl. communication between two process.
Process Thread
No
Process is heavy Thread is light weight, Types of Threads:
1 weight or resource taking lesser resources
intensive. than a process. 1. User Level thread (ULT) –
Process switching Thread switching does not Is implemented in the user level library, they are
2 needs interaction with need to interact with not created using the system calls. Thread switching
operating system. operating system.
does not need to call OS and to cause interrupt to
In multiple processes One thread can read, write
each process operates or change another thread's Kernel. Kernel doesn‘t know about the user level
3 thread and manages them as if they were single-
independently of the data.
others. threaded processes.
processes are Threads are not Advantages
4
independent independent ➢ Can be implemented on an OS that does‘t
Example: support multithreading.
➢ Simple representation since thread has only
program counter, register set, stack space.
➢ Simple to create since no intervention of kernel.
➢ Thread switching is fast since no OS calls need
5 to be made.
Disadvantages of ULT –
➢ No or less co-ordination among the threads and
Kernel.
➢ If one thread causes a page fault, the entire
process blocks.

www.ourcreativeinfo.in
2. Kernel Level Thread (KLT) – UNIT III
Kernel knows and manages the threads. Instead of
thread table in each process, the kernel itself has Process Synchronization
thread table (a master one) that keeps track of all the Inter process communication
threads in the system. In addition kernel also Inter Process Communication (IPC) is a
maintains the traditional process table to keep track mechanism that involves communication of one process
of the processes. OS kernel provides system call to with another process. This usually occurs only in one
create and manage threads. system.
Advantages Communication can be of two types −
➢ Since kernel has full knowledge about the • Between related processes initiating from only one
threads in the system, scheduler may decide to process, such as parent and child processes.
give more time to processes having large • Between unrelated processes, or two or more
number of threads. different processes.
Following are some important terms that we need to
➢ Good for applications that frequently block.
know before proceeding further on this topic.
Disadvantages • Pipes − Communication between two related
➢ Slow and inefficient. processes. The mechanism is half duplex meaning
➢ It requires thread control block so it is an the first process communicates with the second
overhead. process. To achieve a full duplex i.e., for the second
process to communicate with the first process
another pipe is required.
• FIFO − Communication between two unrelated
processes. FIFO is a full duplex, meaning the first
process can communicate with the second process
and vice versa at the same time.
• Message Queues − Communication between two or
more processes with full duplex capacity. The
processes will communicate with each other by
posting a message and retrieving it out of the queue.
• Shared Memory − Communication between two or
more processes is achieved through a shared piece of
memory among all processes
• Semaphores − Semaphores are meant for
synchronizing access to multiple processes. When
one process wants to access the memory (for reading
or writing), it needs to be locked (or protected) and
released when the access is removed. This needs to
be repeated by all the processes to secure data.
• Signals − Signal is a mechanism to communication
between multiple processes by way of signaling.
A process can be of two type:
• Independent process.
• Co-operating process.

Independent process
An independent process is not affected by the
execution of other processes while a co-operating
process can be affected by other executing processes.
Cooperating Processes:
• A Cooperating Process is one, that can effect or to be
affected by other processes executing in the system
• Cooperating Processes can either directly share a
logical address space or be allowed to share data
only through file or messages.

www.ourcreativeinfo.in
• Example consistency demands mechanisms to ensure
synchronized execution of cooperating processes.
Race Condition
A situation, where several processes access and
manipulate the same data concurrently and the outcome
of the execution depends on the particular order in which
the access takes place is called a race condition
Critical Section
• The critical section is a code segment where the
shared variables can be accessed.
• If one process fails it will effect to all the processes. • An atomic action is required in a critical section i.e.
Processes can communicate with each other using only one process can execute in its critical section at
these two ways: a time.
1. Shared Memory • All the other processes have to wait to execute in
2. Message passing their critical sections.
In the shared-memory model, a region of • A diagram that demonstrates the critical section is as
memory which is shared by cooperating processes gets follows:
established. Processes can be then able to exchange
information by reading and writing all the data to the
shared region.
In the message-passing form, communication
takes place by way of messages exchanged among the
cooperating processes.

In the above diagram,


• The entry section handles the entry into the critical
section. It acquires the resources needed for
execution by the process.
• The exit section handles the exit from the critical
section. It releases the resources and also informs the
other processes that the critical section is free.

Solution to the Critical Section Problem


Process Synchronization The critical section problem needs a solution to
Problem arises in concurrent access to shared synchronize the different processes.
data and it may lead to inconsistency. The solution to the critical section problem must
satisfy the following conditions:
• Mutual Exclusion
Mutual exclusion implies that only one process
can be inside the critical section at any time. If any
other processes require the critical section, they must
wait until it is free.
• Progress
Process Synchronization needs between
Progress means that if a process is not using the
processes, whether, when and how to access the
critical section, then it should not stop any other
particular shared resource and also for better utilization
process from accessing it. In other words, any
of process.
process can enter a critical section if it is free.
Process Synchronization means sharing system
• Bounded Waiting
resources by processes in such a way that, Concurrent
Bounded waiting means that each process must
access to shared data is handled thereby minimizing the
have a limited waiting time. It should not wait
chance of inconsistent data. Maintaining data
endlessly to access the critical section.

www.ourcreativeinfo.in
Mutual Exclusion: Algorithm for Process Pi
Requirements for Mutual Exclusion do
• Only one process at a time is allowed in the critical {
section for a resource flag[i] = TRUE;
turn = j;
• A process that halts in its noncritical section must do
while (flag[j] && turn == j);
so without interfering with other processes
• A process must not be delayed access to a critical critical section
section when there is no other process using it
• No assumptions are made about relative process flag[i] = FALSE;
speeds or number of processors
• A process remains inside its critical section for a remainder section
finite time only
} while (true)
Mutual Exclusion Using Hardware Support
• Interrupt Disabling
Peterson‘s Solution preserves all three conditions:
o A process runs until it invokes an operating • Mutual Exclusion is assured as only one process can
system service or until it is interrupted access the critical section at any time.
o Disabling interrupts guarantees mutual exclusion • Progress is also assured, as a process outside the
• Disadvantages: critical section does not block other processes from
o Processor is limited in its ability to interleave entering the critical section.
programs • Bounded Waiting is preserved as every process gets
o Will not work in multiprocessor architecture a fair chance.
• Compare&Swap Instruction Disadvantages of Peterson‘s Solution
int compareAndSwap (int *word, int testval, int • It involves Busy waiting
newval) • It is limited to 2 processes.
{ Semaphores:
int oldval; • Considered the simplest of synchronization tools.
oldval = *word; • A semaphore S is an integer variable that, apart from
if (oldval == testval) *word = newval; initialization, is accessed only through two standard
return oldval; atomic operations: wait() and signal().
} • The wait() operation was originally termed P (from
• Exchange instruction the Dutch proberen, ―to test‖);
void exchange (int *register, int *memory) • signal() was originally called V (from verhogen, ―to
{ increment‖). The definition of wait() is as follows:
int temp;
temp = *memory; wait(S) signal(S)
*memory = *register; { {
*register = temp; while (S <= 0) ; // busy wait S++;
} S--; }
}
Peterson’s Solution
• Two process solution
• Assume that the LOAD and STORE instructions are • All modifications to the integer value of the
atomic; that is, cannot be interrupted. semaphore in the wait() and signal() operations must
• The two processes share two variables: be executed indivisibly.
o int turn; • That is, when one process modifies the semaphore
o Boolean flag[2] value, no other process can simultaneously modify
• The variable turn indicates whose turn it is to enter that same semaphore value.
the critical section. • In addition, in the case of wait(S), the testing of the
• The flag array is used to indicate if a process is ready integer value of S (S ≤ 0), as well as its possible
to enter the critical section. flag[i] = true implies that modification (S--), must be executed without
process Pi is ready! interruption.
• Operating systems often distinguish between
counting and binary semaphores.

www.ourcreativeinfo.in
• The value of a counting semaphore can range over an • If producer access the counter then producer will
unrestricted domain. increase the Counter value (Counter ++)
• The value of a binary semaphore can range only • If consumer access the counter then consumer will
between 0 and 1. decrease the Counter value (Counter --)
• Thus, binary semaphores behave similarly to mutex Solution using semaphore:
locks. Binary semaphores can be used instead for • Assume that the pool consists of n buffers, each
providing mutual exclusion. capable of holding one value/item.
• Counting semaphores can be used to control access • The ―mutex‖ semaphore provides mutual exclusion
to a given resource consisting of a finite number of for accesses to the buffer pool and is initialized to the
instances. The semaphore is initialized to the number value 1.i,e: mutex = 1.
of resources available. • The ―empty‖ and ―full‖ semaphores count the
• Each process that wishes to use a resource performs number of empty and full buffers.
a wait() operation on the semaphore (thereby • The semaphore empty is initialized to the value n;
decrementing the count). When a process releases a The semaphore full is initialized to the value 0.
resource, it performs a signal() operation Producer Consumer
(incrementing the count). do do
Disadvantage { {
• Busy waiting time : When a process is in its critical Wait(empty); Wait(full);
section, any other process that tries to enter its Wait(mutex); Wait(mutex);
critical section must loop continuously in the entry // Add item to buffer // Consume item
code. Signal(mutex); Signal(mutex);
Classical Problems of Synchronization Signal(full); Signal(empty);
Below is some of the classical problem of process }while(TRUE) }while(TRUE)
synchronization in systems where cooperating processes
are present. 2. Dining Philosophers Problem
Problem Statement
1. Bounded Buffer (Producer-Consumer) Problem
Consider there are five philosophers sitting
2. Dining Philosophers Problem around a circular dining table. The dining table has five
chopsticks and a bowl of rice in the middle as shown in
3. The Readers Writers Problem the below figure.

1. Bounded Buffer Problem


Bounded buffer problem, which is also
called producer consumer problem, is one of the
classic problems of synchronization.
Problem Statement
• 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.
• At any instant, a philosopher is either eating or
thinking.
• 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
• A producer tries to insert data into an empty slot of both chopsticks at their original place.
the buffer. Solution using semaphore:
• A consumer tries to remove data from a filled slot in • A philosopher can think for an indefinite amount of
the buffer. time. But when a philosopher starts eating, he has to
• Assume that we have a shared resource counter stop at some point of time.
(Number of elements in the buffer at that time). • The philosopher is in an endless cycle of thinking
• Initially the counter value is 3 and eating.

www.ourcreativeinfo.in
• An array of five semaphores, stick[5], for each of the • An integer variable read_count is used to maintain
five chopsticks. the number of readers currently accessing the
• The code for each philosopher looks like: resource.
do • The variable read_count is initialized to 0.
{
Writer Reader
wait(stick[i]);
do
wait(stick[(i+1) % 5]);
{
/* eat */ //acquire lock
signal(stick[i]); wait(mutex);
do
signal(stick[(i+1) % 5]); { read_count++;
wait(S); if(read_count == 1)
/* think */ wait(S);
/* perform the write
} while(TRUE) //release lock
operation */
signal(S); signal(mutex);
/* perform the reading
Consider the situation when all five philosophers } while(TRUE)
operation */
are hungry simultaneously, and each of them pickup one
chopstick, then a deadlock situation occurs because they // acquire lock
will be waiting for another chopstick forever. wait(mutex);
The possible solutions for this are: read_count--;
• A philosopher must be allowed to pick up the if(read_count == 0)
chopsticks only if both the left and right chopsticks signal(S);
are available. // release lock
• Allow only four philosophers to sit at the table. That
signal(mutex);
way, if all the four philosophers pick up four
chopsticks, there will be one chopstick left on the } while(TRUE)
table. So, one philosopher can start eating and Code explanation
eventually, two chopsticks will be available. In this • As seen above in the code for the writer, the writer
way, deadlocks can be avoided. just waits on the w semaphore until it gets a chance
to write to the resource.
3. Readers Writer Problem • After performing the write operation, it
increments w so that the next writer can access the
Problem Statement
resource.
• There is a shared resource which should be accessed
• On the other hand, in the code for the reader, the lock
by multiple processes.
is acquired whenever the read_count is updated by a
• There are two types of processes in this context.
process.
They are reader and writer.
• When a reader wants to access the resource, first it
• Any number of readers can read from the shared
increments the read_count value, then accesses the
resource simultaneously, but only one writer can
resource and then decrements the read_count value.
write to the shared resource.
• The semaphore w is used by the first reader which
• When a writer is writing data to the resource, no
enters the critical section and the last reader which
other process can access the resource.
exits the critical section.
• A writer cannot write to the resource if there are
• The reason for this is, when the first readers enters
nonzero numbers of readers accessing the resource at
the critical section, the writer is blocked from the
that time.
Solution using semaphore: resource. Only new readers can access the resource
now.
• From the above problem statement, If a writer wants
• Similarly, when the last reader exits the critical
to write to the resource, it must wait until there are
section, it signals the writer using the w semaphore
no readers currently accessing that resource.
because there are zero readers now and a writer can
• Here, we use one mutex = 1and a semaphore S=1.
have the chance to access the resource.

www.ourcreativeinfo.in
Monitor:
• It is characterized as a set of programmer defined
operators. Its representation consists of declaring of
variables, whose value defines the state of an
instance.
• The syntax of monitor is as follows.

Monitor monitor_name
{
Shared variable declarations
Procedure body P1 (………)
{
........
}
Procedure body P2 (………)
{ Introduction to Deadlock
........ Every process needs some resources to complete
} its execution. However, the resource is granted in a
. sequential order.
.
.
Procedure body Pn (………) 1. The process requests for some resource.
{
........ 2. OS grant the resource if it is available otherwise let
}
{ the process waits.
Initialization Code 3. The process uses it and release on the completion.
} Deadlock is a situation where a set of processes
}
are blocked because each process is holding a resource
and waiting for another resource acquired by some other
process.
Example:

Here,
• Process P1 holds resource R1 and waits for resource
R2 which is held by process P2.
• Process P2 holds resource R2 and waits for resource
R1 which is held by process P1.
• None of the two processes can complete and release
their resource.
• Thus, both the processes keep waiting infinitely.

Necessary conditions for Deadlocks

o Mutual Exclusion

www.ourcreativeinfo.in
At least one resource must be held in a non- ▪ One protocol that can be used requires each
sharable mode; that is, only one process at a time can process to request and be allocated all its
use the resource. If another process requests that resources before it begins execution.
resource, the requesting process must be delayed until ▪ The other protocol allows a process to
the resource has been released. request resources only when the process
o Hold and Wait has no resource.
A process must be holding at least one resource ✓ These protocols have two main disadvantages.
and waiting to acquire additional resources that are ▪ First, resource utilization may be low, since
currently being held by other processes. many of the resources may be allocated but
o No preemption unused for a long period.
Resources cannot be preempted; that is, a ▪ Second, starvation is possible.
resource can be released only voluntarily by the ✓ A process that needs several popular resources
process holding it, after that process has completed its may have to wait indefinitely, because at least
task. one of the resources that it needs is always
o Circular Wait allocated to some other process.
A set {P0, P1, ..., Pn} of waiting processes must No Preemption:
exist such that P0 is waiting for a resource held by
✓ To ensure that this condition does not hold, a
P1, P1 is waiting for a resource held by P2, ..., Pn−1
protocol is used.
is waiting for a resource held by Pn, and Pn is waiting
for a resource held by P0. ✓ If a process is holding some resources and
request another resource, that cannot be
immediately allocated to it.
Deadlock Prevention:
✓ The preempted one added to a list of resources
Deadlock prevention is a set of methods for ensuring for which the process is waiting. The process will
that at least one of these necessary conditions cannot restart only when it can regain its old resources,
as well as the new ones that it is requesting.
hold.
✓ Alternatively if a process requests some
Mutual Exclusion: resources, we first check whether they are
✓ The mutual exclusion condition holds for non available. If they are, we allocate them. If they
sharable resources. are not available, we check whether they are
✓ The example is a printer cannot be allocated to some other process that is waiting for
simultaneously shared by several processes. additional resources. If so, we preempt the
✓ Sharable resources do not require mutual desired resources from the waiting process and
exclusive access and thus cannot be involved in a allocate them to the requesting process.
dead lock. ✓ If the resources are not either available or held by
✓ The example is read only files which are in a waiting process, the requesting process must
sharing condition. If several processes attempt to wait.
open the read only file at the same time they can Circular Wait:
be guaranteed simultaneous access. ✓ One way to avoid circular wait is to number all
Hold and wait: resources, and to require that processes request
✓ To ensure that the hold and wait condition never resources only in strictly increasing (or
occurs in the system, we must guaranty that decreasing ) order.
whenever a process requests a resource it does ✓ In other words, in order to request resource Rj, a
not hold any other resources. process must first release all Ri such that i >= j.
✓ There are two protocols to handle these problems ✓ One big challenge in this scheme is determining
such as the relative ordering of the different resources
✓ For example: Set priorities for R1=1, R2=2, R3=3
and R4=4. With these priorities, if process P

www.ourcreativeinfo.in
wants to use R1 and R3, it should first request R1, ✓ This algorithm is used only if we have one
then R3. Another protocol is ―Whenever a process instance of a resource type.
requests a resource Rj, it must have released all ✓ In addition to the request edge and the
resources Rk with priority(Rk) >= priority(Rj). assignment edge a new edge called claim edge is
used.
Deadlock avoidance ✓ For eg:-A claim edge Pi,Rj indicates that process
Pi may request Rj in future. The claim edge is
✓ Deadlock prevention algorithm may lead to low represented by a dotted line.
device utilization and reduces system throughput. ▪ When a process Pi requests the
✓ Avoiding deadlocks requires additional resource Rj, the claim edge is
information about how resources are to be converted to a request edge. x When
requested. resource Rj is released by process Pi,
✓ With the knowledge of the complete sequences of the assignment edge Rj Pi is replaced
requests and releases we can decide for each by the claim edge Pi,Rj.
requests whether or not the process should wait.
✓ A deadlock avoidance algorithm dynamically ✓ When a process Pi requests resource Rj the
examines the resources allocation state to ensure request is granted only if converting the request
that a circular wait condition never exists. edge Pi Rj to as assignment edge Rj Pi do not
✓ The resource allocation state is defined by the result in a cycle.
number of available and allocated resources and ✓ Cycle detection algorithm is used to detect the
the maximum demand of each process. cycle. If there are no cycles then the allocation of
Safe State: the resource to process leave the system in safe
state
✓ A state is a safe state in which there exists at least
one order in which all the process will run Banker’s Algorithm:
completely without resulting in a deadlock.
✓ This algorithm is applicable to the system with
✓ A system is in safe state if there exists a safe
multiple instances of each resource types, but this
sequence.
is less efficient then the resource allocation graph
✓ A sequence of processes <P1,P2,… Pn> is a
algorithm.
safe sequence for the current allocation state if
✓ When a new process enters the system it must
for each Pi the resources that Pi can request can
declare the maximum number of resources that it
be satisfied by the currently available resources.
may need. This number may not exceed the total
✓ If the resources that Pi requests are not currently
number of resources in the system.
available then Pi can obtain all of its needed
✓ The system must determine that whether the
resource to complete its designated task.
allocation of the resources will leave the system
✓ A safe state is not a deadlock state.
in a safe state or not. If it is so resources are
✓ Whenever a process request a resource i.e.,
allocated else it should wait until the process
currently available, the system must decide
release enough resources.
whether resources can be allocated immediately
✓ Several data structures are used to implement the
or whether the process must wait. The request is
banker‘s algorithm.
granted only if the allocation leaves the system in
✓ Let ‗n‘ be the number of processes in the system
safe state.
and ‗m‘ be the number of resources types. We
In this, if a process requests a resource i.e., currently
need the following data structures:
available it must still have to wait. Thus resource
▪ Available:- A vector of length m indicates
utilization may be lower than it would be without a
deadlock avoidance algorithm. the number of available resources. If
Available[i]=k, then k instances of resource
Resource Allocation Graph Algorithm:
type Rj is available.

www.ourcreativeinfo.in
▪ Max:- An n*m matrix defines the Available = Available – Request(i)
maximum demand of each process if Allocation(i) = Allocation(i) + Request(i)
Max[i,j]=k, then Pi may request at most k Need(i) = Need(i) – Request(i)
instances of resource type Rj. ▪ If the resulting resource allocation state is
▪ Allocation:- An n*m matrix defines the safe, the transaction is complete and Pi is
number of resources of each type currently allocated its resources. If the new state is
allocated to each process. If unsafe then Pi must wait for Request(i) and
Allocation[i,j]=k, then Pi is currently k old resource allocation state is restored.
instances of resource type Rj.
▪ Need:- An n*m matrix indicates the
remaining resources need of each process.
If Need[i,j]=k, then Pi may need k more
instances of resource type Rj to compute its
task. So Need[i,j]=Max[i,j]-Allocation[i]

Safety Algorithm:
This algorithm is used to find out whether or not a
system is in safe state or not.
Step 1. Let work and finish be two vectors of length
M and N respectively. Initialize work = available and
Finish[i]=false for i=1,2,3,…….n
Step 2. Find i such that both Finish[i]=false Need i<=
work If no such i exist then go to step 4
Step 3. Work = work + Allocation Finish[i]=true Go
to step 2
Step 4. If finish[i]=true for all i, then the system is in
safe state.
This algorithm may require an order of m*n*n operation
to decide whether a state is safe.
Resource Request Algorithm:
✓ Let Request(i) be the request vector of process Pi.
✓ If Request[i][j]=k,then process Pi wants K
instances of the resource type Rj.
✓ When a request for resources is made by process
Pi the following actions are taken.
▪ If Request(i) <= Need(i) go to step 2
otherwise raise an error condition since the
process has exceeded its maximum claim. x
If Request(i) <= Available go to step 3
otherwise Pi must wait. Since the resources
are not available.
▪ If the system want to allocate the requested
resources to process Pi then modify the
state as follows.

www.ourcreativeinfo.in
Unit IV • Protection of memory space is accomplished by
having the CPU hardware compare every address
Memory Management
generated in user mode with the registers.
Memory Management
• Any attempt by a program executing in user mode to
• Memory consists of a large array of words or bytes,
access operating-system memory or other users‘
each with its own address.
memory results in a trap to the operating system,
• The CPU fetches instructions from memory
which treats the attempt as a fatal error as shown in
according to the value of the program counter. These
below figure.
instructions may cause additional loading from and
storing to specific memory addresses.
• Memory unit sees only a stream of memory
addresses. It does not know how they are generated.
• Program must be brought into memory and placed
within a process for it to be run.
• Input queue – collection of processes on the disk that
are waiting to be brought into memory for execution.
• User programs go through several steps before being
run. • This scheme prevents a user program from
Base and Limit Register (accidentally or deliberately) modifying the code or
• Each process has a separate memory space. data structures of either the operating system or other
• Separate per-process memory space protects the users.
processes from each other and is fundamental to Address Binding
having multiple processes loaded in memory for • Usually, a program resides on a disk as a binary
concurrent execution. executable file. To be executed, the program must be
• To separate memory spaces, we need the ability to brought into memory and placed within a process.
determine the range of legal addresses that the • Depending on the memory management in use, the
process may access and to ensure that the process can process may be moved between disk and memory
access only these legal addresses. during its execution.
• We can provide this protection by using two registers, • The processes on the disk that are waiting to be
usually a base and a limit, as illustrated in Figure 8.1 brought into memory for execution form the input
• The base register holds the smallest legal physical queue.
memory address • Addresses in the source program are generally
• The limit register specifies the size of the range. symbolic (such as the variable count).
• For example, if the base register holds 300040 and • A compiler typically binds these symbolic addresses
the limit register is 120900, then the program can to relocatable addresses.
legally access all addresses from 300040 through • The linkage editor or loader in turn binds the
420939 (inclusive). relocatable addresses to absolute.
• Each binding is a mapping from one address space to
another.
• Address binding of instructions and data to memory
addresses can happen at three different stages.
✓ Compile time: If memory location known a
priori, absolute code can be generated; must
recompile code if starting location changes.
Example: .COM-format programs in MS-DOS.
✓ Load time: Must generate relocatable code if
memory location is not known at compile time.

www.ourcreativeinfo.in
✓ Execution time: Binding delayed until run time • This method requires hardware support slightly
if the process can be moved during its execution different from the hardware configuration. The base
from one memory segment to another. Need register is now called a relocation register. The value
hardware support for address maps (e.g., in the relocation register is added to every address
relocation registers). generated by a user process at the time it is sent to
memory.

• The user program never sees the real physical


addresses. The program can create a pointer to
location 346, store it in memory, manipulate it and
compare it to other addresses.
• The user program deals with logical addresses. The
memory mapping hardware converts logical
addresses into physical addresses.
• The final location of a referenced memory address is
Logical Versus Physical Address Space not determined until the reference is made.
• The concept of a logical address space that is bound Dynamic Loading
to a separate physical-address space is central to • Routine is not loaded until it is called.
proper memory management. • All routines are kept on disk in a relocatable load
✓ Logical address – address generated by the CPU; format.
also referred to as virtual address. • The main program is loaded into memory and is
✓ Physical address – address seen by the memory executed. When a routine needs to call another
unit. routine, the calling routine first checks to see whether
• The set of all logical addresses generated by a the other the desired routine into memory and to
program is a logical address space; the set of all update the program‘s address tables to reflect this
physical addresses corresponding to these logical change. Then control is passed to the newly loaded
addresses are a physical address space. routine.
• Logical and physical addresses are the same in • Advantages
compile-time and load-time address-binding schemes; ✓ Better memory-space utilization; unused routine
logical (virtual) and physical addresses differ in is never loaded.
execution-time address-binding scheme. ✓ Useful when large amounts of code are needed to
• The run-time mapping from virtual to physical handle infrequently occurring cases.
addresses is done by a hardware device called the ✓ No special support from the operating system is
memory management unit (MMU). required.
✓ Implemented through program design.

www.ourcreativeinfo.in
Dynamic Linking
• Linking is postponed until execution time.
• Small piece of code, stub, is used to locate the
appropriate memory-resident library routine, or to
load the library if the routine is not already present.
• When this stub is executed, it checks to see whether
the needed routine is already in memory. If not, the
program loads the routine into memory.
• Stub replaces itself with the address of the routine,
and executes the routine.
• Thus the next time that code segment is reached, the
library routine is executed directly, incurring no cost
for dynamic linking.
• Operating system is needed to check if routine is in • For example, assume a multiprogramming
processes‘ memory address. environment with a round robin CPU scheduling
• Dynamic linking is particularly useful for libraries. algorithm. When a quantum expires, the memory
Swapping manager will start to swap out the process that just
• A process must be in memory to be executed. finished, and to swap in another process to the
• A process can be swapped temporarily out of memory memory space that has been freed. In the mean time,
the CPU scheduler will allocate a time slice to some
to a backing store and then brought back into memory
for continued execution. other process in memory. When each process finished
its quantum, it will be swapped with another process
• Swapping makes it possible for the total physical
Memory Allocation
address space of all processes to exceed the real
• Memory allocation is a process by which computer
physical memory of the system, thus increasing the
programs and services are assigned with physical or
degree of multiprogramming in a system.
virtual memory space.
• Roll out, roll in – swapping variant used for priority-
• Memory allocation has two core types;
based scheduling algorithms. If a higher priority
✓ Static Memory Allocation: The program is
process arrives and wants service, the memory
allocated memory at compile time.
manager can swap out the lower priority process so
✓ Dynamic Memory Allocation: The programs are
that it can load and execute lower priority process can
allocated with memory at run time.
be swapped back in and continued. This variant is
Contiguous Memory Allocation
sometimes called roll out, roll in.
• Main memory is usually divided into two partitions:
• Backing store: The backing store is commonly a fast
✓ Resident operating system, usually held in low
disk. It must be large enough to accommodate copies
memory with interrupt vector.
of all memory images for all users, and it must
✓ User processes, held in high memory.
provide direct access to these memory images.
• In contiguous memory allocation, each process is
• Major part of swap time is transfer time; total transfer
contained in a single contiguous section of memory
time is directly proportional to the amount of memory
1. Single-partition allocation
swapped.
• Relocation-register scheme used to protect user
• Modified versions of swapping are found on many
processes from each other, and from changing
systems (i.e., UNIX, Linux, and Windows).
operating-system code and data.
• Relocation register contains value of smallest
physical address; limit register contains range of
logical addresses – each logical address must be less
than the limit register.

www.ourcreativeinfo.in
✓ 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.
✓ 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, which may be more useful than the smaller
leftover hole from a best-fit approach.
Fragmentation
2. Multiple-partition allocation • As processes are loaded and removed from memory,
• Hole – block of available memory; holes of various the free memory space is broken into little pieces.
size are scattered throughout memory. • It happens after sometimes that processes cannot be
• When a process arrives, it is allocated memory from a allocated to memory blocks considering their small
hole large enough to accommodate it. size and memory blocks remains unused. This
• Operating system maintains information about: problem is known as Fragmentation.
a) allocated partitions b) free partitions (hole) • There are two types of Fragmentation:-
• When a process arrives and needs memory, the 1. External Fragmentation
system searches this set for a hole that is large enough 2. Internal Fragmentation
for this process. • External Fragmentation – total memory space
• If the hole is too large, it is split into two: one part is exists to satisfy a request, but it is not contiguous.
allocated to the arriving process; the other is returned • Internal Fragmentation – allocated memory may be
to the set of holes. slightly larger than requested memory; this size
• When a process terminates, it releases its block of difference is memory internal to a partition, but not
memory, which is then placed back in the set of being used.
holes. • Compaction
✓ Reduce external fragmentation by compaction
✓ Shuffle memory contents to place all free
memory together in one large block.
✓ Compaction is possible only if relocation is
dynamic, and is done at execution time.
• This procedure is a particular instance of the general
dynamic storage allocation problem, which is how to
satisfy a request of size n from a list of free holes.
• There are many solutions to this problem. The set of
holes is searched to determine which hole is best to
allocate.
• The first-fit, best-fit and worst-fit strategies are the
most common ones used to select a free hole from the
set of available holes.
✓ First fit: Allocate the first hole that is big Paging
enough. Searching can start either at the • Paging is a memory management scheme that permits
beginning of the set of holes or at the location the physical address space of a process to be non
where the previous first-fit search ended. We can contiguous.
stop searching as soon as we find a free hole that • Divides physical memory into fixed-sized blocks
is large enough. called frames.

www.ourcreativeinfo.in
• Divide logical memory into blocks of same size • The selection of a power of 2 as a page size makes
called pages. the translation of a logical address into a page number
• When a process is to be executed, its pages are loaded and page offset particularly easy.
into any available memory frames from the backing • If the size of the logical address space is 2m, and a
store. page size is 2n bytes, then the high-order m − n bits
• The backing store is divided into fixed sized blocks of a logical address designate the page number, and
that are of the same size as the memory frames. the n low-order bits designate the page offset.
• The hardware support for paging is illustrated in • Thus, the logical address is as follows:
below figure.

• For example, consider the memory in below Figure,


✓ Here, in the logical address, n= 2 and m = 4.
✓ Using a page size of 4 bytes and a physical
memory of 32 bytes (8 pages).

• Every address generated by the CPU is divided into


two parts: a page number (p) and a page offset (d).
• The page number is used as an index into a page
table. The page table contains the base address of
each page in physical memory.
• This base address is combined with the page offset to
define the physical memory address that is sent to the
memory unit.
• The paging model of memory is shown in Figure

✓ Logical address 0 is page 0, offset 0.


✓ Indexing into the page table, we find that page 0
is in frame 5. Thus, logical address 0 maps to
physical address 20 [= (5 × 4) + 0].
✓ Logical address 3 (page 0, offset 3) maps to
physical address 23 [= (5 × 4) + 3].
✓ Logical address 4 is page 1, offset 0; according to
the page table, page 1 is mapped to frame 6.
Thus, logical address 4 maps to physical address
• The page size (like the frame size) is defined by the 24 [= (6 × 4) + 0].
hardware. ✓ There for , Physical address = [(page size of
• The size of a page is a power of 2, varying between logical memory X frame number) + offset]
512 bytes and 1 GB per page, depending on the • When a process arrives in the system to be executed,
computer architecture. its size expressed in pages is examined.

www.ourcreativeinfo.in
• Each page of the process needs one frame. Thus if the • Advantages of Virtual Memory
process requires n pages, at least n frames must be ✓ The degree of Multiprogramming will be
available in memory. increased.
• If n frames are available, they are allocated to this ✓ User can run large application with less real
arriving process. RAM.
• The first page of the process is loaded into one of the ✓ There is no need to buy more memory RAMs.
allocated frames, and the frame number is put in the • Disadvantages of Virtual Memory
page table for this process. The next page is loaded ✓ The system becomes slower since swapping takes
into another frame, and its frame number is put into time.
the page table and so on as in below figure. ✓ It takes more time in switching between
applications.
✓ The user will have the lesser hard disk space for
its use.
1. Demand Paging
• The basic idea behind demand paging is that when a
process is swapped in, its pages are not swapped in
all at once. Rather they are swapped in only when the
process needs them. ( on demand. ) This is termed
a lazy swapper, although a pager is a more accurate
term.
• A demand paging system is similar to the paging
system with swapping feature.
• When we want to execute a process we swap it into
Virtual Memory
the memory. A swapper manipulates entire process
• It is a technique which allows execution of process
where as a pager is concerned with the individual
that may not be compiled within the primary
pages of a process.
memory.
• The demand paging concept is using pager rather than
• It separates the user logical memory from the
swapper.
physical memory. This separation allows an
• When a process is to be swapped in, the pager
extremely large memory to be provided for program
guesses which pages will be used before the process
when only a small physical memory is available.
is swapped out again.
• Virtual memory makes the task of programming
• Instead of swapping in a whole process, the pager
much easier because the programmer no longer needs
brings only those necessary pages into memory.
to working about the amount of the physical memory
• The transfer of a paged memory to contiguous disk
is available or not.
space is shown in below figure.
• The virtual memory allows files and memory to be
shared by different processes by page sharing.
• It is most commonly implemented by demand paging.

www.ourcreativeinfo.in
• Thus it avoids reading into memory pages that will ✓ We restart the instruction that was interrupted by
not used any way decreasing the swap time and the the illegal address trap. Now the process can
amount of physical memory needed. access the page as if it had always been in
• In this technique we need some hardware support to memory.
distinct between the pages that are in memory and 2. Page Replacement
those that are on the disk. • Each process is allocated frames (memory) which
• A valid and invalid bit is used for this purpose. hold the process‘s pages (data)
• When this bit is set to valid it indicates that the • Frames are filled with pages as needed – this is called
associate page is in memory. If the bit is set to invalid demand paging
it indicates that the page is either not valid or is valid • Over-allocation of memory is prevented by
but currently not in the disk. modifying the page-fault service routine to replace
pages
• The job of the page replacement algorithm is to
decide which page gets victimized to make room for
a new page
• Page replacement completes separation of logical and
physical memory.

• Marking a page invalid will have no effect if the


process never attempts to access that page. So while a
process executes and access pages that are memory
resident, execution proceeds normally. Basic Page Replacement
• Access to a page marked invalid causes a page fault • Page replacement takes the following approach.
trap. It is the result of the OS‘s failure to bring the • If no frame is free, we find one that is not currently
desired page into memory. being used and free it.
• Procedure to handle page fault • We can free a frame by writing its contents to swap
If a process refers to a page that is not in space and changing the page table (and all other
physical memory then tables) to indicate that the page is no longer in
✓ We check an internal table (page table) for this memory (Figure 9.10).
process to determine whether the reference was
valid or invalid.
✓ If the reference was invalid, we terminate the
process, if it was valid but not yet brought in, we
have to bring that from main memory.
✓ Now we find a free frame in memory.
✓ Then we read the desired page into the newly
allocated frame.
✓ When the disk read is complete, we modify the
internal table to indicate that the page is now in
memory.

www.ourcreativeinfo.in
• We can now use the freed frame to hold the page for ✓ The first reference to 3 results in replacement of
which the process faulted. page 0, since it is now first in line. Because of
• We modify the page-fault service routine to include this replacement, the next reference, to 0, will
page replacement: fault. Page 1 is then replaced by page 0.
✓ Find the location of the desired page on the disk. ✓ This process continues as shown in above Figure.
✓ Find a free frame: ✓ Every time a fault occurs, we show which pages
▪ If there is a free frame, use it. are in our three frames.
▪ If there is no free frame, use a page- ✓ There are fifteen faults altogether.
replacement algorithm to select a victim II. Optimal Page Replacement
frame. • Replace the page that will not be used for the longest
▪ Write the victim frame to the disk; change the period of time.
page and frame tables accordingly. • Use of this page-replacement algorithm guarantees
✓ Read the desired page into the newly freed frame; the lowest possible page fault rate for a fixed number
change the page and frame tables. of frames.
✓ Continue the user process from where the page • For example:
fault occurred. Reference string : 7,0,1,2,0,3,0,4,2,3,0,7,1.
Page Replacement Algorithms Number of Frames : 3
I. First In First Out (FIFO) Algorithm Solution:
• Replaces pages based on their order of arrival: oldest Three frames are initially empty.
page is replaced
• The simplest page-replacement algorithm is a first-in,
first-out (FIFO) algorithm.
• When a page must be replaced, the oldest page is
chosen.
• We replace the page at the head of the queue.
• When a page is brought into memory, we insert it at Number of page faults = (3+1+1+1+1+1+1) = 9.
the tail of the queue. Explanation:
✓ The first three references cause faults that fill the
• For example:
three empty frames.
Reference string : 7,0,1,2,0,3,0,4,2,3,0,7,1.
✓ The reference to page 2 replaces page 7, because
Number of Frames : 3
Solution: page 7 will not be used until reference 18,
Three frames are initially empty. whereas page 0 will be used at 5, and page 1 at
14.
✓ The reference to page 3 replaces page 1, as page
1 will be the last of the three pages in memory to
be referenced again.
✓ In fact, no replacement algorithm can process this
reference string in three frames with fewer than
Number of page faults nine faults
= (3+1+1+1+1+1+1+1+1+1+1+1) = 15 page faults III. LRU Page Replacement
Explanation: • If we use the recent past as an approximation of the
✓ The first three references (7, 0, 1) cause page near future, then we can replace the page that has not
faults and are brought into these empty frames. been used for the longest period of time. This
✓ The next reference (2) replaces page 7, because approach is the least recently used (LRU) algorithm.
page 7 was brought in first. • Replaces pages based on their most recent reference –
✓ Since 0 is the next reference and 0 is already in replace the page with the greatest backward distance
memory, we have no fault for this reference. in the reference string

www.ourcreativeinfo.in
UNIT V
Input Output (I/O) Management
DISK STRUCTURE

• Disks provide the bulk of secondary storage for


Explanation: modern computer systems.
• The LRU algorithm produces twelve faults. When the • Magnetic tape was used as an early secondary-storage
reference to page 4 occurs, however, LRU medium, but the access time is much slower than for
replacement sees that, of the three frames in memory, disks.
page 2 was used least recently. Thus, the LRU • Modern disk drives are addressed as large one-
algorithm replaces page 2, not knowing that page 2 is dimensional arrays of logical blocks, where the
about to be used. logical block is the smallest unit of transfer.
• When it then faults for page 2, the LRU algorithm • The size of a logical block is usually 512 bytes,
replaces page 3, since it is now the least recently used although some disks can be low-level formatted to
of the three pages in memory. choose a different logical block size, such as 1,024
• Despite these problems, LRU replacement with bytes.
twelve faults. • The one-dimensional array of logical blocks is
Thrashing mapped onto the sectors of the disk sequentially.
• If a process does not have ―enough‖ pages, the page- Sector 0 is the first sector of the first track on the
fault rate is very high outermost cylinder.
✓ low CPU utilization • The mapping proceeds in order through that track,
✓ OS thinks it needs increased multiprogramming then through the rest of the tracks in that cylinder,
✓ adds another process to system and then through the rest of the cylinders from
• Thrashing is when a process is busy swapping pages outermost to innermost.
in and out
• Thrashing results in severe performance problems. DISK SCHEDULING
Consider the following scenario, which is based on
the actual behavior of early paging systems. • One of the responsibilities of the OS is to use the
hardware efficiently. This will help in achieving fast
access time and disk bandwidth.
• The access time has two major components:
✓ 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 additional time
waiting for the disk to rotate the desired sector to
• The operating system monitors CPU utilization. If the disk head.
CPU utilization is too low, we increase the degree of • The disk bandwidth is the total number of bytes
multiprogramming by introducing a new process to transferred, divided by the total time between the first
the system. request for service and the completion of the last
• A global page replacement algorithm is used; it transfer.
replaces pages with no regard to the process to which • We can improve both the access time and the
they belong. bandwidth by scheduling the servicing of disk I/O
• Now suppose that a process enters a new phase in its requests in a good order.
execution and needs more frames.

www.ourcreativeinfo.in
• Whenever a process needs I/O to or from the disk, it
issues a system call to the operating system. The
request specifies several pieces of information:
✓ Whether this operation is input or output
✓ What the disk address for the transfer is
✓ What the memory address for the transfer is
✓ What the number of bytes to be transferred is
• If the desired disk drive and controller are available,
the request can be serviced immediately. If the drive
or controller is busy, any new requests for service
will be placed on the queue of pending requests for
that drive. II. SSTF Scheduling
• For a multiprogramming system with many
processes, the disk queue may often have several • The SSTF (shortest-seek-time-first) algorithm selects
pending requests. Thus, when one request is the request with the minimum seek time from the
completed, the operating system chooses which current head position.
pending request to service next. • Since seek time increases with the number of
cylinders traversed by the head, SSTF chooses the
Disk Scheduling Algorithms pending request closest to the current head position.
I. FCFS Scheduling Example:
• The simplest form of disk scheduling is, of course, Consider a disk queue with requests for I/O to
the first-come, first-served (FCFS) algorithm. blocks on cylinders: 98, 183, 37, 122, 14, 124, 65, 67
• This algorithm is intrinsically fair, but it generally in that order. There are 200 cylinders numbered from 0
does not provide the fastest service. to 199. The disk head is initially at the cylinder 53.
Example: Compute total head movements.
Consider a disk queue with requests for I/O to Solution:
blocks on cylinders:98, 183, 37, 122, 14, 124, 65, 67 in The closest request to the initial head position 53
that order. There are 200 cylinders numbered from 0 to is at cylinder 65. Once we are at cylinder 65, the next
199. The disk head is initially at the cylinder 53. closest request is at cylinder 67. From there, the request
Compute total head movements. at cylinder 37 is closer than 98, so 37 is served next.
Solution: Continuing, we service the request at cylinder 14, then
Starting with cylinder 53, it will first move from 98, 122, 124, and finally 183. It is shown in Figure.
53 to 98, then to 183, 37, 122, 14,124, 65, and finally to
67 as shown in Figure. Head movement from 53 to 65 = 12
Head movement from 65 to 67 = 2
Head movement from 53 to 98 = 45 Head movement from 67 to 37 = 30
Head movement from 98 to 183 = 85 Head movement from 37 to 14 =23
Head movement from 183 to 37 = 146 Head movement from 14 to 98 =84
Head movement from 37 to 122 =85 Head movement from 98 to 122 =24
Head movement from 122 to 14 =108 Head movement from 122 to 124 =2
Head movement from 14 to 124 =110 Head movement from 124 to 183 = 59
Head movement from 124 to 65 =59 Total head movement = 236
Head movement from 65 to 67 = 2
Total head movement = 640

www.ourcreativeinfo.in
III. SCAN Scheduling
• In the SCAN algorithm, the disk arm starts at one end IV. C- SCAN Scheduling
• Circular SCAN (C-SCAN) scheduling is a variant of
of the disk, and moves toward the other end,
SCAN designed to provide a more uniform wait time.
servicing requests as it reaches each cylinder, until it
• Like SCAN, C-SCAN moves the head from one end
gets to the other end of the disk.
of the disk to the other, servicing requests along the
• At the other end, the direction of head movement is
way.
reversed, and servicing continues. The head
• When the head reaches the other end, however, it
continuously scans back and forth across the disk.
immediately returns to the beginning of the disk,
Example:
Consider a disk queue with requests for I/O to without servicing any requests on the return trip.
blocks on cylinders: 98, 183, 37, 122, 14, 124, 65, 67 in • The C-SCAN scheduling algorithm essentially treats
that order. There are 200 cylinders numbered from 0 to the cylinders as a circular list that wraps around from
199. The disk head is initially at the cylinder 53. the final cylinder to the first one.
Compute total head movements. Example:
Solution: Consider a disk queue with requests for I/O to
blocks on cylinders: 98, 183, 37, 122, 14, 124, 65, 67 in
• Before applying SCAN algorithm, we need to know
that order. There are 200 cylinders numbered from 0 to
the current direction of head movement. Assume that
199. The disk head is initially at the cylinder 53.
disk arm is moving toward 0, the head will service 37
Compute total head movements.
and then 14.
Solution:
• At cylinder 0, the arm will reverse and will move
Before applying C - SCAN algorithm, we need
toward the other end of the disk, servicing the
to know the current direction of head movement.
requests at 65,67,98, 122, 124, and 183 It is shown in
Assume that disk arm is moving toward 199, the
Figure.
head will service 65, 67, 98, 122, 124, 183.Then it will
move to 199 and the arm will reverse and move towards
Head movement from 53 to 37 = 16
0. While moving towards 0, it will not serve. But, after
Head movement from 37 to 14 = 23
reaching 0, it will reverse again and then serve 14 and
Head movement from 14 to 0 = 14
37. It is shown in Figure
Head movement from 0 to 65 =65
Head movement from 65 to 67 =2
Head movement from 53 to 65 = 12
Head movement from 67 to 98 =31
Head movement from 65 to 67 = 2
Head movement from 98 to 122 =24
Head movement from 67 to 98 = 31
Head movement from 122 to 124 = 2
Head movement from 98 to 122 =24
Head movement from 124 to 183 = 59
Head movement from 122 to 124 =2
Total head movement = 236
Head movement from 124 to 183 =59
Head movement from 183 to 199 =16
Head movement from 199 to 0 = 199

www.ourcreativeinfo.in
Head movement from 0 to 14 = 14 Head movement from 14 to 37 = 23
Head movement from 14 to 37 = 23 Total head movement = 322
Total head movement = 382

DISK MANAGEMENT
V. Look Scheduling The OS is responsible for several other aspects
• As we described them, both SCAN and C-SCAN of disk management as well.
move the disk arm across the full width of the disk.
• In practice, neither algorithm is implemented this Disk Formatting
way. More commonly, the arm goes only as far as the • A new magnetic disk is a blank slate: It is just platters
final request in each direction then, it reverses of a magnetic recording material.
direction immediately, without going all the way to • Before a disk can store data, it must be divided into
the end of the disk. sectors that the disk controller can read and write.
• These versions of SCAN and C-SCAN are called This process is called low-level formatting (or
LOOK and C-LOOK scheduling, because they look physical formatting).
for a request before continuing to move in a given • Low-level formatting fills the disk with a special data
direction. structure for each sector.
Example: • To use a disk to hold files, the OS needs to record its
Consider a disk queue with requests for I/O to own data structures on the disk.
blocks on cylinders: 98, 183, 37, 122, 14, 124, 65, 67 in • It does so in two steps.
that order. There are 200 cylinders numbered from 0 to ✓ The first step is to partition the disk into one or
199. The disk head is initially at the cylinder 53. more groups of cylinders. The OS treats each
Compute total head movements. partition as though it were a separate disk.
Solution: ✓ After partitioning, the second step is logical
Assume that disk arm is moving toward 199, the formatting (or creation of a file system). In this
head will service 65, 67, 98,122, 124, 183. Then the arm step, the OS stores the initial file-system data
will reverse and move towards 14. Then it will serve 37. structures onto the disk. These data structures
It is shown in Figure may include maps of free and allocated space and
an initial empty directory.
Head movement from 53 to 65 = 12
Head movement from 65 to 67 = 2 Boot Blocks
Head movement from 67 to 98 = 31 • For a computer to start running, it needs to have an
Head movement from 98 to 122 =24 initial program to run. This bootstrap program
Head movement from 122 to 124 =2 initializes all aspects of the system, from CPU
Head movement from 124 to 183 =59 registers to device controllers and the contents of
Head movement from 183 to 14 = 169 main memory, and then starts the OS.

www.ourcreativeinfo.in
• To do its job, the bootstrap program finds the OS • The important file concepts include:
kernel on disk, loads that kernel into memory, and 1. File attributes:
jumps to an initial address to begin the operating- A file has certain attributes which vary from one
system execution. operating system to another.
✓ Name: Every file has a name by which it is
Bad Blocks referred.
• Because disks have moving parts and small ✓ Identifier: It is unique number that identifies the
tolerances, they are prone to failure. file within the file system.
• Sometimes the failure is complete, and the disk needs ✓ Type: This information is needed for those
to be replaced, and its contents restored from backup systems that support different types of files.
media to the new disk. ✓ Location: It is a pointer to a device & to the
• More frequently, one or more sectors become location of the file on that device
defective. Most disks even come from the factory ✓ Size: It is the current size of a file in bytes,
with bad blocks. words or blocks.
• Depending on the disk and controller in use, these ✓ Protection: It is the access control information
blocks are handled in a variety of ways: that determines who can read, write & execute a
✓ On simple disks with IDE, bad blocks are file.
handled manually (like formatting the disk) ✓ Time, date & user identification: It gives
✓ The controller maintains a list of bad blocks on information about time of creation or last
the disk. The list is initialized during the low- modification & last use.
level format at the factory, and is updated over
the life of the disk. 2. File operations:
The operating system can provide system calls to
create, read, write, reposition, delete and truncate files.
SWAP SPACE MANAGEMENT
✓ Creating files: Two steps are necessary to create
• Swap-space — Virtual memory uses disk space as an
a file. First, space must be found for the file in
extension of main memory.
the file system. Secondly, an entry must be made
• Swap-space can be carved out of the normal file
in the directory for the new file.
system, or, more commonly, it can be in a
✓ Reading a file: Data & read from the file at the
separate disk partition.
current position. The system must keep a read
• BSD allocates swap space when process starts; holds
pointer to know the location in the file from
text segment (the program) and data segment.
where the next read is to take place. Once the
• Kernel uses swap maps to track swap-space use.
read has been taken place, the read pointer is
Solaris 2 allocates swap space only when a page is
updated.
forced out of physical memory, not when the virtual
✓ Writing a file: Data are written to the file at the
memory page is first created. current position. The system must keep a write
pointer to know the location in the file where the
File concept: next write is to take place. The write pointer must
• A file is a collection of related information that is be updated whenever a write occurs.
stored on secondary storage. ✓ Repositioning within a file (seek): The directory
• Information stored in files must be persistent i.e. not is searched for the appropriate entry & the current
file position is set to a given value. After
affected by power failures & system reboots. Files
repositioning data can be read from or written
may be of free from such as text files or may be into that position.
formatted rigidly. ✓ Deleting a file: To delete a file, we search the
• Files represent both programs as well as data. directory for the required file. After deletion, the
• Part of the OS dealing with the files is known as file space is released so that it can be reused by other
system. files.

www.ourcreativeinfo.in
✓ Truncating a file: The user may erase the c) Tree:
contents of a file but allows all attributes to • In this organization, a file consists of a tree of
remain unchanged expect the file length which is records of varying lengths.
rest to ‗O‘ & the space is released • Each record consists of a key field.
3. File types:
• The tree is stored on the key field to allow first
• The file name is spilt into 2 parts, Name & extension.
searching for a particular key.
Usually these two parts are separated by a period. The
5. Access methods:
user & the OS can know the type of the file from the
Basically, access method is divided into 2 types:
extension itself.
Sequential access:
• Listed below are some file types along with their
• It is the simplest access method. Information in the
extension:
file is processed in order i.e. one record after another.
• A process can read all the data in a file in order
File Type Extension
starting from beginning but can‘t skip & read
✓ Executable File exe, bin, com
arbitrarily from any location.
✓ Object File obj, o (compiled)
• Sequential files can be rewound.
✓ Source Code file C, C++, Java, pas
• It is convenient when storage medium was magnetic
✓ Batch File bat, sh
tape rather than disk.
✓ Text File txt, doc
Direct access:
✓ Archive File arc, zip, tar
• A file is made up of fixed length-logical records that
✓ Multimedia File mpeg
allow programs to read & write records rapidly in no
4. File structure: particular O order.
Files can be structured in several ways. Three common • This method can be used when disk are used for
possible are: storing files.
a) Byte sequence: • This method is used in many applications e.g.
• The figure shows an unstructured sequence of database systems.
bytes. The OS doesn‘t care about the content of • If an airline customer wants to reserve a seat on a
file. It only sees the bytes. particular flight, the reservation program must be able
• This structure provides maximum flexibility. to access the record for that flight directly without
• Users can write anything into their files & name reading the records before it.
them according to their convenience. • In a direct access file, there is no restriction in the
• Both UNIX & windows use this approach. order of reading or writing.
• For example, we can read block 14, then read block
50 & then write block 7 etc. Direct access files are
very useful for immediate access to large amount of
information.
b) Record sequence:
• In this structure, a file is a sequence of fixed Directory structure:
length records. • The file system of computers can be extensive. Some
• Here the read operation returns one records & the systems store thousands of file on disk. To manage all
write operation overwrites or append or record. these data, we need to organize them.
The organization is done in 2 steps. The file system is
broken into partitions. Each partition contains
information about file within it.

www.ourcreativeinfo.in
Operation on a directory: 3. Tree structured directories:
✓ Search for a file: We need to be able to search a • It is the most common directory structure.
directory for a particular file. • A two level directory is a two level tree. So, the
✓ Create a file: New files are created & added to generalization is to extend the directory structure
the directory. to a tree of arbitrary height.
✓ Delete a file: When a file is no longer needed, we • It allows users to create their own subdirectories &
may remove it from the directory. organize their files.
✓ List a directory: We should be able to list the • Every file in the system has a unique path name.
files of the directory. • It is the path from the root through all the sub-
✓ Rename a file: The name of a file is changed directories to a specified file.
when the contents of the file changes. • A directory is simply another file but it is treated
✓ Traverse the file system: It is useful to be able in a special way. One bit in each directory entry
to access every directory & every file within a defines the entry as a file (O) or as sub-
directory. directories.

Structure of a directory:
The most common schemes for defining the
structure of the directory are:
1. Single level directory:
• It is the simplest directory structure.
• All files are present in the same directory.
• So it is easy to manage & understand. Limitation:
A single level directory is difficult to manage
when the no. of files increases or when there is
more than one user.
• Since all files are in same directory, they must
have unique names. So, there is confusion of file
names between different users.
2. Two level directories:
• The solution to the name collision problem in
single level directory is to create a separate
directory for each user.
• In a two level directory structure, each user has its
own user file directory. When a user logs in, then
master file directory is searched. It is indexed by
user name & each entry points to the UFD of that
user.
• Limitation: It solves name collision problem. But
it isolates one user from another.
• It is an advantage when users are completely
independent. But it is a disadvantage when the
users need to access each other‘s files & co-
operate among themselves on a particular task.

www.ourcreativeinfo.in

You might also like