Professional Documents
Culture Documents
Module 1
2
INDEX:
Introduction Distributed Systems
Personal Computer
Parallel
3
An Operating System can be
defined as an interface between
4
Objective
Interface between hardware and software
Efficiency
5
Structure of a Computer system
User
Application Programs
System programs
Operating system
Hardware
6
Types of Operating System
7
Simple Batch
1. Firstly, user prepares his job using punch cards.
2. Then, he submits the job to the computer operator.
3. Operator collects the jobs from different users and
sort the jobs into batches with similar needs.
4. Then, operator submits the batches to the processor
one by one.
5. All the jobs of one batch are executed together.
ADVANTAGES:
It saves the time that was being wasted earlier for each Disadvantages:
individual process in context switching from one
environment to another environment. Priority can not be set for the jobs.
Advantages:
Advantages:
10
Multitasking Operating System
The multitasking operating system is a logical
extension of a multiprogramming system that
enables multiple programs simultaneously. It
allows a user to perform more than one
computer task at the same time.
Advantages:
Disadvantages:
Advantages:
12
Real time Operating System
In Real-Time Systems, each job carries a certain
deadline within which the job is supposed to be
completed, otherwise, the huge loss will be there,
or even if the result is produced, it will be
completely useless.The Application of a Real-Time
system exists in the case of military applications,
if you want to drop a missile, then the missile is
supposed to be dropped with a certain precision.
Advantages:
Disadvantages:
Easy to layout, develop and execute real-time
applications under the real-time operating Real-time operating systems are very costly to
system. develop.
In a Real-time operating system, the maximum Real-time operating systems are very complex and
utilization of devices and systems. can consume critical CPU cycles.
13
Time sharing operating system
In the Time Sharing operating system, computer resources
are allocated in a time-dependent fashion to several
programs simultaneously. Thus it helps to provide a large
number of user's direct access to the main computer. It is a
logical extension of multiprogramming. In time-sharing, the
CPU is switched among multiple programs given by
different users on a scheduled basis.A time-sharing
operating system allows many users to be served
simultaneously, so sophisticated CPU scheduling schemes
and Input/output management are required.Time-sharing
operating systems are very difficult and expensive to build.
Disadvantages:
Advantages:
Data transmission rates are very high in comparison to other methods.
The time-sharing operating system provides effective
utilization and sharing of resources. Security and integrity of user programs loaded in memory and data need
to be maintained as many users access the system at the same time.
This system reduces CPU idle and response time.
14
Distributed Operating System
The Distributed Operating system is not installed on a
single machine, it is divided into parts, and these parts
are loaded on different machines. A part of the
distributed Operating system is installed on each
machine to make their communication possible.
Distributed Operating systems are much more
complex, large, and sophisticated than Network
operating systems because they also have to take care
of varying networking protocols.
Advantages:
Disadvantages:
16
An operating system is a large and complex system that can only be created by partitioning into small parts. These pieces
should be a well-defined part of the system, carefully defining inputs, outputs, and functions. Although Windows, Mac, UNIX,
Linux, and other OS do not have the same structure, most operating systems share similar OS system components, such as file,
memory, process, I/O device management. The components of an operating system play a key role to make a variety of
computer system parts work together. There are the following components of an operating system, such as:
Process Management
File Management
Network Management
Main Memory Management
Secondary Storage Management
I/O Device Management
Security Management
Command Interpreter System
Operating system components help you get the correct computing by detecting CPU and memory hardware errors.
17
Process Management
The process management component is a procedure
for managing many processes running simultaneously
on the operating system. Every running software
application program has one or more processes
associated with them.
18
Process Management
Functions of process management
Here are the following functions of process management in the operating system, such as:
● Synchronization process
● Communication process
19
File management
A file is a set of related information defined by its
creator. It commonly represents programs (both
source and object forms) and data. Data files can
be alphabetic, numeric, or alphanumeric.
20
Network Management
Network management is the process of administering and
managing computer networks. It includes performance
management, provisioning of networks, fault analysis, and
maintaining the quality of service.
21
Network Management
Functions of Network management
● Distributed systems help you to various computing resources in size and function. They may involve
● A distributed system also offers the user access to the various resources the network shares.
● It helps to access shared resources that help computation to speed up or offers data availability and
reliability.
22
Main memory Management
Main memory is a large array of storage or bytes, which
has an address. The memory management process is
conducted by using a sequence of reads or writes of
specific memory addresses.
23
Main memory Management
Functions of Memory management
An Operating System performs the following functions for Memory Management in the operating
system:
● Determine what part of it are in use by whom, what part is not in use.
● In a multiprogramming system, the OS decides which process will get memory and how much.
● It also de-allocates the memory when a process no longer requires or has been terminated.
24
Secondary Storage Management
Today modern computers use hard drives/SSD as
The most important task of a computer system is
the primary storage of both programs and data.
to execute programs. These programs help you to
However, the secondary storage management
access the data from the main memory during
also works with storage devices, such as USB
execution. This memory of the computer is very
flash drives and CD/DVD drives. Programs like
small to store all data and programs permanently.
assemblers and compilers are stored on the disk
The computer system offers secondary storage to
until it is loaded into memory, and then use the
back up the main memory.
disk is used as a source and destination for
processing.
25
Secondary Storage Management
Functions of Secondary storage management
Here are some major functions of secondary storage management in the operating system:
● Storage allocation
● Disk scheduling
26
I/O Device Management
One of the important use of an operating system that helps to
hide the variations of specific hardware devices from the user.
device.
27
Security Management
The various processes in an operating system need to be secured from
other activities. Therefore, various mechanisms can ensure those
processes that want to operate files, memory CPU, and other hardware
resources should have proper authorization from the operating system.
28
Command Interpreter System
One of the most important components of an operating system is its
command interpreter. The command interpreter is the primary interface
between the user and the rest of the system.
Its function is quite simple, get the next command statement, and
execute it. The command statements deal with process management,
I/O handling, secondary storage management, main memory
management, file system access, protection, and networking.
29
Operating System Following are the services provided by
The Operating system offers the structures and An Operating device collects utilization records for numerous assets and
tracks the overall performance parameters and responsive time to enhance
tools, including editors and debuggers, to help overall performance. These personal records are beneficial for additional
the programmer create, modify, and debugging upgrades and tuning the device to enhance overall performance.
programs. 31
Security and Protection
Operating device affords safety to the statistics and packages of a person and protects any interference from unauthorized users.
The safety feature counters threats, which are published via way of individuals out of doors the manage of the running device.
For Example: When a user downloads something from the internet, that program may contain malicious code that may harm the
already existing programs. The operating system ensures that proper checks are applied while downloading such programs.
If one computer system is shared amongst a couple of users, then the various processes must be protected from another
intrusion. For this, the operating system provides various mechanisms that allow only those processes to use resources that have
gained proper authorization from the operating system. The mechanism may include providing unique users ids and passwords
to each user.
File management
Computers keep data and information on secondary storage devices like magnetic tape, magnetic disk, optical disk, etc. Each
storage media has its capabilities like speed, capacity, data transfer rate, and data access methods.
For file management, the operating system must know the types of different files and the characteristics of different storage
devices. It has to offer the proportion and safety mechanism of documents additionally.
Communication
The operating system manages the exchange of data and programs among different computers connected over a network. This
communication is accomplished using message passing and shared memory. 32
System Calls
33
What is System Call?
A system call is a method for a computer program to request a service from the
kernel of the operating system on which it is running. A system call is a method of
interacting with the operating system via programs. A system call is a request
from computer software to an operating system's kernel.
34
How are System Calls Made?
When a computer software needs to access the operating system's kernel, it makes a system call. The
system call uses an API to expose the operating system's services to user programs. It is the only
method to access the kernel system. All programs or processes that require resources for execution
must use system calls, as they serve as an interface between the operating system and user
programs.
A system call function may create and use kernel processes to execute the asynchronous processing.
A system call has greater authority than a standard subroutine. A system call with kernel-mode
privilege executes in the kernel protection domain.
System calls are not permitted to use shared libraries or any symbols that are not present in the
kernel protection domain.
The code and data for system calls are stored in global kernel memory.
35
Why do you need system calls in Operating System?
There are various situations where you must require system calls in the
operating system. Following of the situations are as follows:
It is must require when a file system wants to create or delete a file.
Network connections require the system calls to sending and receiving data
packets.
If you want to read or write a file, you need to system calls.
If you want to access hardware devices, including a printer, scanner, you need
a system call.
System calls are used to create and manage new processes.
36
Process Control
Process control is the system call that is used to direct the processes. Some process control examples include creating, load, abort, end, execute,
process, terminate the process, etc.
File Management
File management is a system call that is used to handle the files. Some file management examples include creating files, delete files, open, close,
read, write, etc.
Device Management
Device management is a system call that is used to deal with devices. Some examples of device management include read, device, write, get
device attributes, release device, etc.
Information Maintenance
Information maintenance is a system call that is used to maintain information. There are some examples of information maintenance, including getting system
data, set time or date, get time or date, set system data, etc.
Communication
Communication is a system call that is used for communication. There are some examples of communication, including create, delete
communication connections, send, receive messages, etc.
37
Process Windows Unix
ExitProcess() Exit()
WaitForSingleObject() Wait()
ReadFile() Read()
WriteFile() Write()
CloseHandle() Close()
ReadConsole() Read()
WriteConsole() Write()
38
Information Maintenance GetCurrentProcessID() Getpid()
SetTimer() Alarm()
Sleep() Sleep()
CreateFileMapping() Shmget()
MapViewOfFile() Mmap()
InitializeSecurityDescriptor() Umask()
SetSecurityDescriptorgroup() Chown()
39
open()
The open() system call allows you to access a file on a file system. It allocates
resources to the file and provides a handle that the process may refer to. Many processes can open a file at once or by a single process only. It's all based on
the file system and structure.
read()
It is used to obtain data from a file on the file system. It accepts three arguments in general:
● A file descriptor.
● A buffer to store read data.
● The number of bytes to read from the file.
The file descriptor of the file to be read could be used to identify it and open it using open() before reading.
wait()
In some systems, a process may have to wait for another process to complete its execution before proceeding. When a parent process makes a
child process, the parent process execution is suspended until the child process is finished. The wait() system call is used to suspend the parent
process. Once the child process has completed its execution, control is returned to the parent process.
40
write()
It is used to write data from a user buffer to a device like a file. This system call is one way for a program to generate data. It
takes three arguments in general:
● A file descriptor.
● A pointer to the buffer in which data is saved.
● The number of bytes to be written from the buffer.
fork()
Processes generate clones of themselves using the fork() system call. It is one of the most common ways to create
processes in operating systems. When a parent process spawns a child process, execution of the parent process is
interrupted until the child process completes. Once the child process has completed its execution, control is returned to the
parent process.
close()
It is used to end file system access. When this system call is invoked, it signifies that the program no longer requires the file,
and the buffers are flushed, the file information is altered, and the file resources are de-allocated as a result.
41
exec()
When an executable file replaces an earlier executable file in an already executing process, this system function is invoked.
As a new process is not built, the old process identification stays, but the new process replaces data, stack, data, head, etc.
exit()
The exit() is a system call that is used to end program execution. This call indicates that the thread execution is complete,
which is especially useful in multi-threaded environments. The operating system reclaims resources spent by the process
following the use of the exit() system function.
42
Module 2
Process and CPU Scheduling
Process and CPU Scheduling -
Operations on processes
Cooperating Processes
INDEX:
Threads
Interprocess Communication
Scheduling Criteria
Scheduling Algorithms
44
Process
45
Process
● An operating system executes a variety of programs:
● Batch system – jobs
● Time-shared systems – user programs or tasks
● Textbook uses the terms job and process almost interchangeably
● Process – a program in execution; process execution must progress in sequential fashion
● Multiple parts
● The program code, also called text section
● Current activity including program counter, processor registers
● Stack containing temporary data
Function parameters, return addresses, local variables
● Data section containing global variables
● Heap containing memory dynamically allocated during run time
46
Process
● Program is passive entity stored on disk (executable file), process is active
● Program becomes process when executable file loaded into memory
● Execution of program started via GUI mouse clicks, command line entry of its
name, etc
● One program can be several processes
● Consider multiple users executing the same program
47
Process in Memory
48
Process State
● As a process executes, it changes state
● new: The process is being created
● running: Instructions are being executed
● waiting: The process is waiting for some event to occur
● ready: The process is waiting to be assigned to a processor
● terminated: The process has finished execution
49
Diagram of Process State
50
Process Control Block (PCB)
Information associated with each process
(also called task control block)
● Process state – running, waiting, etc
● Program counter – location of instruction to next execute
● CPU registers – contents of all process-centric registers
● CPU scheduling information- priorities, scheduling queue
pointers
● Memory-management information – memory allocated to the
process
● Accounting information – CPU used, clock time elapsed since
start, time limits
● I/O status information – I/O devices allocated to process, list of
open files
51
CPU Switch From Process to Process
52
Schedulers
● Short-term scheduler (or CPU scheduler) – selects which process should be executed next and allocates
CPU
● Sometimes the only scheduler in a system
● Short-term scheduler is invoked frequently (milliseconds) ⇒ (must be fast)
● Long-term scheduler (or job scheduler) – selects which processes should be brought into the ready
queue
● Long-term scheduler is invoked infrequently (seconds, minutes) ⇒ (may be slow)
● The long-term scheduler controls the degree of multiprogramming
● Processes can be described as either:
● I/O-bound process – spends more time doing I/O than computations, many short CPU bursts
● CPU-bound process – spends more time doing computations; few very long CPU bursts
● Long-term scheduler strives for good process mix
53
Context Switch
● When CPU switches to another process, the system must save the state of
the old process and load the saved state for the new process via a context
switch
● Context of a process represented in the PCB
● Context-switch time is overhead; the system does no useful work while
switching
● The more complex the OS and the PCB the longer the context switch
● Time dependent on hardware support
● Some hardware provides multiple sets of registers per CPU 🡺 multiple
contexts loaded at once
54
Operations on Processes
● System must provide mechanisms for:
● process creation,
● process termination
55
Process Creation
● Parent process create children processes, which, in turn create other
processes, forming a tree of processes
● Generally, process identified and managed via a process identifier (pid)
● Resource sharing options
● Parent and children share all resources
● Children share subset of parent’s resources
● Parent and child share no resources
● Execution options
● Parent and children execute concurrently
● Parent waits until children terminate
56
Process Creation
● Address space
● Child duplicate of parent
● Child has a program loaded into it
● UNIX examples
● fork() system call creates new process
● exec() system call used after a fork() to replace the process’ memory
space with a new program
57
Process Termination
● Process executes last statement and then asks the operating system to delete
it using the exit() system call.
● Returns status data from child to parent (via wait())
● Process’ resources are deallocated by operating system
● Parent may terminate the execution of children processes using the abort()
system call. Some reasons for doing so:
● Child has exceeded allocated resources
● Task assigned to child is no longer required
● The parent is exiting and the operating systems does not allow a child to
continue if its parent terminates
58
Process Termination
● Some operating systems do not allow child to exists if its parent has terminated. If
a process terminates, then all its children must also be terminated.
● cascading termination. All children, grandchildren, etc. are terminated.
● The termination is initiated by the operating system.
● The parent process may wait for termination of a child process by using the
wait()system call. The call returns status information and the pid of the
terminated process
pid = wait(&status);
● If no parent waiting (did not invoke wait()) process is a zombie
● If parent terminated without invoking wait , process is an orphan
59
Multi Process Architecture – Chrome Browser
● Many web browsers ran as single process (some still do)
● If one web site causes trouble, entire browser can hang or crash
● Google Chrome Browser is multiprocess with 3 different types of processes:
● Browser process manages user interface, disk and network I/O
● Renderer process renders web pages, deals with HTML, Javascript. A new renderer created for
each website opened
4 Runs in sandbox restricting disk and network I/O, minimizing effect of security exploits
● Plug-in process for each type of plug-in
60
Interprocess Communication
61
Interprocess Communication
● Processes within a system may be independent or cooperating
● Cooperating process can affect or be affected by other processes, including sharing data
● Reasons for cooperating processes:
● Information sharing
● Computation speedup
● Modularity
● Convenience
● Cooperating processes need interprocess communication (IPC)
● Two models of IPC
● Shared memory
● Message passing
62
Communications Models
(a) Message passing. (b) shared memory.
63
Cooperating Processes
● Independent process cannot affect or be affected by the execution of another
process
● Cooperating process can affect or be affected by the execution of another
process
● Advantages of process cooperation
● Information sharing
● Computation speed-up
● Modularity
● Convenience
64
Producer-Consumer Problem
● Paradigm for cooperating processes, producer process produces information
that is consumed by a consumer process
● unbounded-buffer places no practical limit on the size of the buffer
● bounded-buffer assumes that there is a fixed buffer size
65
Certain conditions must be met by the Producer and the Consumer processes to have consistent data
synchronization:
1. The Producer process must not produce an item if the shared buffer is full.
2. The Consumer process must not consume an item if the shared buffer is empty.
3. Access to the shared buffer must be mutually exclusive; this means that at any given instance, only
one process should be able to access the shared buffer and make changes to it.
Solution
semaphore Full: Tracks the space filled by the Producer process. It is initialized with a value of as the
buffer will have filled spaces at the beginning
semaphore Empty: Tracks the empty space in the buffer. It is initially set to buffer_size as the whole
buffer is empty at the beginning.
semaphore mutex: Used for mutual exclusion so that only one process can access the shared buffer at a
time.
66
Bounded-Buffer – Shared-Memory Solution
● Shared data
#define BUFFER_SIZE 10
typedef struct {
. . .
} item;
item buffer[BUFFER_SIZE];
int in = 0;
int out = 0;
68
Bounded Buffer – Consumer
item next_consumed;
while (true) {
while (in == out)
; /* do nothing */
next_consumed = buffer[out];
out = (out + 1) % BUFFER_SIZE;
69
Interprocess Communication – Shared Memory
● An area of memory shared among the processes that wish to communicate
● The communication is under the control of the users processes not the
operating system.
● Major issues is to provide mechanism that will allow the user processes to
synchronize their actions when they access shared memory.
70
Interprocess Communication – Message Passing
● Mechanism for processes to communicate and to synchronize their actions
71
Message Passing
● If processes P and Q wish to communicate, they need to:
● Establish a communication link between them
● Exchange messages via send/receive
● Implementation issues:
● How are links established?
● Can a link be associated with more than two processes?
● How many links can there be between every pair of communicating processes?
● What is the capacity of a link?
● Is the size of a message that the link can accommodate fixed or variable?
● Is a link unidirectional or bi-directional?
72
Message Passing
74
Indirect Communication
● Messages are directed and received from mailboxes (also referred to as ports)
● Each mailbox has a unique id
● Processes can communicate only if they share a mailbox
● Properties of communication link
● Link established only if processes share a common mailbox
● A link may be associated with many processes
● Each pair of processes may share several communication links
● Link may be unidirectional or bi-directional
75
Indirect Communication
● Operations
● create a new mailbox (port)
● send and receive messages through mailbox
● destroy a mailbox
● Primitives are defined as:
send(A, message) – send a message to mailbox A
receive(A, message) – receive a message from mailbox A
76
Indirect Communication
● Mailbox sharing
● P1, P2, and P3 share mailbox A
● P1, sends; P2 and P3 receive
● Who gets the message?
● Solutions
● Allow a link to be associated with at most two processes
● Allow only one process at a time to execute a receive operation
● Allow the system to select arbitrarily the receiver. Sender is notified who
the receiver was.
77
Synchronization
● Message passing may be either blocking or non-blocking
● Blocking is considered synchronous
● Blocking send -- the sender is blocked until the message is received
● Blocking receive -- the receiver is blocked until a message is available
● Non-blocking is considered asynchronous
● Non-blocking send -- the sender sends the message and continue
● Non-blocking receive -- the receiver receives:
● A valid message, or
● Null message
● Different combinations possible
● If both send and receive are blocking, we have a rendezvous
78
Synchronization
● Producer-consumer becomes trivial
message next_produced;
while (true) {
/* produce an item in next produced */
send(next_produced);
}
message next_consumed;
while (true) {
receive(next_consumed);
80
Thread
81
● Thread is an execution unit that consists of its own program counter, a
stack, and a set of registers where the program counter mainly keeps
track of which instruction to execute next, a set of registers mainly hold
its current working variables, and a stack mainly contains the history of
execution.
82
● Threads are also known as Lightweight processes. Threads are a
popular way to improve the performance of an application through
parallelism. Threads are mainly used to represent a software approach
in order to improve the performance of an operating system just by
reducing the overhead thread that is mainly equivalent to a classical
process.
83
● The CPU switches rapidly back and forth among the threads giving the
illusion that the threads are running in parallel.
● As each thread has its own independent resource for process execution;
thus Multiple processes can be executed parallelly by increasing the
number of threads.
84
● It is important to note here that each thread belongs to exactly one
process and outside a process no threads exist. Each thread basically
represents the flow of control separately. In the implementation of
network servers and web servers threads have been successfully used.
Threads provide a suitable foundation for the parallel execution of
applications on shared-memory multiprocessors.
85
86
Process Thread
A Process simply means any program in execution. Thread simply means a segment of a process.
The process requires more time for creation. Thread requires comparatively less time for creation than process.
The process takes more time to terminate The thread takes less time to terminate.
A thread mainly shares the data segment, code segment, files, etc.
Processes have independent data and code segments
with its peer threads.
The process takes more time for context switching. The thread takes less time for context switching.
Communication between processes needs more time as compared to Communication between threads needs less time as compared to
thread. processes.
For some reason, if a process gets blocked then the remaining In case if a user-level thread gets blocked, all of its peer threads also
processes can continue their execution get blocked. 87
Advantages of Thread
● Responsiveness
● Scalability. One thread runs on one CPU. In Multithreaded processes, threads can be distributed over a series of
processors to scale.
● Context Switching is smooth. Context switching refers to the procedure followed by the CPU to change from one
task to another.
88
User Level threads Kernel Level Threads
These threads are implemented by users. These threads are implemented by Operating systems
These threads are not recognized by operating systems, These threads are recognized by operating systems,
These threads are mainly designed as dependent threads. These threads are mainly designed as independent threads.
In User Level threads, if one user-level thread performs a blocking On the other hand, if one kernel thread performs a blocking operation
operation then the entire process will be blocked. then another thread can continue the execution.
Example of User Level threads: Java thread, POSIX threads. Example of Kernel level threads: Window Solaris.
Implementation of User Level thread is done by a thread library and is While the Implementation of the kernel-level thread is done by the
easy. operating system and is complex.
This thread is generic in nature and can run on any operating system. This is specific to the operating system. 89
Multithreading Models
The user threads must be mapped to kernel threads, by one of the following
strategies:
90
Many to One Model
● In the many to one model, many user-level threads
are all mapped onto a single kernel thread.
● Thread management is handled by the thread library
in user space, which is efficient in nature.
● In this case, if user-level thread libraries are
implemented in the operating system in some way
that the system does not support them, then the
Kernel threads use this many-to-one relationship
model.
91
One to One Model
● The one to one model creates a separate
kernel thread to handle each and every user
thread.
● Most implementations of this model place a
limit on how many threads can be created.
● Linux and Windows from 95 to XP
implement the one-to-one model for
threads.
● This model provides more concurrency than
that of many to one Model.
92
Many to Many Model
● The many to many model multiplexes any
number of user threads onto an equal or
smaller number of kernel threads,
combining the best features of the
one-to-one and many-to-one models.
● Users can create any number of threads.
● Blocking the kernel system calls does not
block the entire process.
● Processes can be split across multiple
processors.
93
What are Thread Libraries?
● Thread libraries provide programmers with API for the creation and management of threads.
● Thread libraries may be implemented either in user space or in kernel space. The user space involves API functions
implemented solely within the user space, with no kernel support. The kernel space involves system calls and
requires a kernel with thread library support.
Three types of Thread
● POSIX Pitheads may be provided as either a user or kernel library, as an extension to the POSIX standard.
● Java threads: Since Java generally runs on a Java Virtual Machine, the implementation of threads is based upon
whatever OS and hardware the JVM is running on, i.e. either Pitheads or Win32 threads depending on the system.
94
Multiple-Processor Scheduling
● CPU scheduling more complex when multiple CPUs are available
● Homogeneous processors within a multiprocessor
● Asymmetric multiprocessing – only one processor accesses the system data
structures, alleviating the need for data sharing
● Symmetric multiprocessing (SMP) – each processor is self-scheduling, all processes
in common ready queue, or each has its own private queue of ready processes
○ Currently, most common
● Processor affinity – process has affinity for processor on which it is currently running
○ soft affinity
○ hard affinity
○ Variations including processor sets
95
Multiple-Processor Scheduling – Load Balancing
96
Scheduling Algorithms
97
First Come First Serve
98
Characteristics of FCFS method
● It supports non-preemptive and preemptive scheduling algorithm.
● This method is poor in performance, and the general wait time is quite
high.
99
FCFS ALGORITHM
● 1- Input the processes along with their burst time (bt).
● 3- As first process that comes need not to wait so waiting time for process 1 will be 0 i.e. wt[0] = 0.
● 4- Find waiting time for all other processes i.e. for process i -> wt[i] = bt[i-1] + wt[i-1] .
100
Advantages
● It doesn't include any complex logic, it just puts the process requests in
a queue and executes it one by one.
101
Disadvantages
● There is no option for preemption of a process. If a process is started,
then CPU executes the process until it ends.
102
Shortest Job Next
103
Characteristics of SJF Scheduling
● It is associated with each job as a unit of time to complete.
● This algorithm method is helpful for batch-type processing, where waiting for jobs to
complete is not critical.
● It can improve process throughput by making sure that shorter jobs are executed first, hence
possibly have a short turnaround time.
● It improves job output by offering shorter jobs, which should be executed first, which mostly
have a shorter turnaround time.
104
Advantages
● According to the definition, short processes are executed first and then
followed by longer processes.
105
Disadvantages
● The time taken by a process must be known by the CPU beforehand,
which is not possible.
● Longer processes will have more waiting time, eventually they'll suffer
starvation.
106
Round Robin (RR)
107
Characteristics of Round Robin Scheduling
● A preemptive scheduling algorithm.
● CPU shifts to the next process after a fixed time interval known as time quantum or
time-slice.
● Preempted processes are added to the end of the queue.
● A real time algorithm that responds to an event within a specific time limit.
108
Advantages
● Each process is served by the CPU for a fixed time quantum, so all
processes are given the same priority.
● Starvation doesn't occur because for each round robin cycle, every
process is given a fixed time to execute. No process is left behind.
109
Disadvantages
● The throughput in RR largely depends on the choice of the length of the
time quantum. If time quantum is longer than needed, it tends to
exhibit the same behavior as FCFS.
● If time quantum is shorter than needed, the number of times that CPU
switches from one process to another process, increases. This leads to
decrease in CPU efficiency.
110
Priority based Scheduling
111
Characteristics of Priority Scheduling
● Schedules processes on the basis of priority.
● In the case of two processes with similar priorities, we use FCFS and
Round-Robin to choose between them.
● If a higher priority task arrives when a lower priority task is executing, the
higher priority task replaces the one with lower priority and the
113
Disadvantages
● A second scheduling algorithm is required to schedule the processes
which have same priority.
114
Usage of Scheduling Algorithms in Different Situations
● Every scheduling algorithm has a type of a situation where it is the best choice. Let's look at
different such situations:
● Situation 1:
● The incoming processes are short and there is no need for the processes to execute in a
specific order.
● In this case, FCFS works best when compared to SJF and RR because the processes are short
which means that no process will wait for a longer time. When each process is executed one
by one, every process will be executed eventually.
115
Usage of Scheduling Algorithms in Different Situations
● Situation 2:
● The processes are a mix of long and short processes and the task will only be
completed if all the processes are executed successfully in a given time.
● Round Robin scheduling works efficiently here because it does not cause
starvation and also gives equal time quantum for each process.
116
Usage of Scheduling Algorithms in Different Situations
● Situation 3:
● The processes are a mix of user based and kernel based processes.
● Priority based scheduling works efficiently in this case because generally kernel based
processes have higher priority when compared to user based processes.
● For example, the scheduler itself is a kernel based process, it should run first so that it can
schedule other processes.
117
Multilevel Queue
118
Multilevel Queue
● Ready queue is partitioned into separate queues, eg:
○ foreground (interactive)
○ background (batch)
120
Multilevel Queue
● A multi-level queue scheduling algorithm partitions the ready queue into
several separate queues. The processes are permanently assigned to
one queue, generally based on some property of the process, such as
memory size, process priority, or process type. Each queue has its own
scheduling algorithm.
121
● The Description of the processes in the above diagram is as follows:
● System Process The Operating system itself has its own process to run
and is termed as System Process.
122
Advantages of Multilevel Queue Scheduling
● With the help of this scheduling we can apply various kind of scheduling for different kind of
processes:
123
Disadvantages of Multilevel Queue Scheduling
Starvation:
124
System call interface for process management
125
In computing, a system call is the programmatic way in
which a computer program requests a service from the
kernel of the operating system on which it is executed.
127
fork
A parent process uses fork to create a new child process. The child process
is a copy of the parent. After fork, both parent and child executes the
same program but in separate processes.
exec
Replaces the program executed by a process. The child may use exec after
a fork to replace the process’ memory space with a new program
executable making the child execute a different program than the
parent.
exit
Terminates the process with an exit status.
wait
The parent may use wait to suspend execution until a child terminates.
Using wait the parent can obtain the exit status of a terminated child.
128
Fork()
129
Parent and child
The process invoking fork is called the parent. The new process created as
the result of a fork is the child of the parent.
After a successful fork, the child process is a copy of the parent. The parent
and child processes executes the same program but in separate
processes.
Fork
The fork system call is the primary (and historically, only) method of
process creation in Unix-like operating systems.
130
#include <unistd.h>
pid_t fork(void);
Return value
On success, the PID of the child process is returned in the parent, and 0 is
returned in the child. On failure, -1 is returned in the parent, no child
process is created, and errno is set appropriately.
131
Fork returns twice on success
On success fork returns twice: once in the parent and once in the child.
After calling fork, the program can use the fork return value to tell
whether executing in the parent or child.
If the return value is 0 the program executes in the new child process.
If the return value is greater than zero, the program executes in the parent
process and the return value is the process ID (PID) of the created child
process.
On failure fork returns -1.
132
133
1 #include <stdio.h> // perror() 15
2 #include <stdlib.h> // exit(), 16 case 0:
EXIT_SUCCESS, EXIT_FAILURE 17 // On success fork() returns 0 in the child.
18
3 #include <unistd.h> // fork()
19 // Add code for the child process here.
4 20
5 int main(void) { 21 exit(EXIT_SUCCESS);
6 22
7 pid_t pid; 23 default:
24 // On success fork() returns the pid of the
8 child to the parent.
9 switch (pid = fork()) { 25
10 26 // Add code for the parent process here.
11 case -1: 27
28 exit(EXIT_SUCCESS);
12 // On error fork() returns -1.
29 }
13 perror("fork failed"); 30 }
14 exit(EXIT_FAILURE);
134
Header files
On lines 1-3 a number of header files are included to get access to a few functions and constants from the C Standard
library.
pid_t
One line 7 the variable pid of type pid_t is declared. The pid_t data type is the data type used for process IDs.
fork
On line 9 the parent process calls fork and stores the return value in the variable pid.
switch
On line 9 a switch-statement is used to check the return value of fork.
Error (case -1)
On failure fork returns -1 and execution continues in the case -1 branch of the switch statement (line 11). The operating
system was not able to create a new process. The parent uses perror to print an error message (line 13) and then
terminates with exit status EXIT_FAILURE (line 14).
Child (case 0)
On success fork returns 0 in the new child process and execution continues in the case 0 branch of the switch statement
(line 16). Any code to be executed only by the child is placed here (line 19). The child terminates with exit
status EXIT_SUCCESS (line 21).
Parent (default)
If the value fork returned by fork was neither -1 (error) nor 0 (child), execution continues in the parent process in
the default branch of the switch statement (line 23). In this case, the value returned by fork is the process ID (PID) of
the newly created child process.
135
int main(void) {
pid_t pid;
switch (pid = fork()) {
case -1:
// On error fork() returns -1.
perror("fork failed");
exit(EXIT_FAILURE);
case 0:
// On success fork() returns 0 in the child.
child();
default:
// On success fork() returns the pid of the child to the parent.
parent(pid);
}
}
136
void child() {
printf(" CHILD <%ld> I'm alive! My PID is <%ld> and my parent got PID <%ld>.\n",
(long) getpid(), (long) getpid(), (long) getppid());
printf(" CHILD <%ld> Goodbye!\n",
(long) getpid());
exit(EXIT_SUCCESS);
}
void parent(pid_t pid) {
printf("PARENT <%ld> My PID is <%ld> and I spawned a child with PID <%ld>.\n",
(long) getpid(), (long) getpid(), (long) pid);
printf("PARENT <%ld> Goodbye!\n",
(long) getpid());
exit(EXIT_SUCCESS);
}
137
● PARENT <87628> Spawned a child with PID = 87629.
138
Orphans
An orphan process is a process whose parent process has terminated, though it remains
running itself. Any orphaned process will be immediately adopted by the special init
system process with PID 1.
Processes execute concurrently
Both the parent process and the child process competes for the CPU with all other processes
in the system. The operating systems decides which process to execute when and for how
long. The process in the system execute concurrently.
In our example program:
most often the parent terminates before the child and the child becomes an orphan process
adopted by init (PID = 1) and therefore reports PPID = 1
sometimes the child process terminates before its parent and then the child is able to report
PPID equal to the PID of the parent. 139
Wait()
The wait system call blocks the caller
until one of its child process
terminates. If the caller doesn’t
have any child
processes, wait returns
immediately without blocking the
caller. Using wait the parent can
obtain the exit status of the
terminated child.
140
#include <sys/types.h>
#include <sys/wait.h>
pid_t wait(int *status);
status
If status is not NULL, wait store the exit status of the terminated child in
the int to which status points. This integer can be inspected using
the WIFEXITED and WEXITSTATUS macros.
Return value
On success, wait returns the PID of the terminated child. On failure (no
child), wait returns -1.
141
#include <sys/types.h>
#include <sys/wait.h>
WIFEXITED(status);
status
The integer status value set by the wait system call.
Return value
Returns true if the child terminated normally, that is, by calling exit or by
returning from main.
142
#include <sys/types.h>
#include <sys/wait.h>
int WEXITSTATUS(status);
status
The integer status value set by the wait system call.
Return value
The exit status of the child. This consists of the least significant 8 bits of the
status argument that the child specified in a call to exit or as the
argument for a return statement in main. This macro should be
employed only if WIFEXITED returned true.
143
1 void parent(pid_t pid) {
2
3 printf("PARENT <%ld> Spawned a child with PID = %ld.\n",
4 (long) getpid(), (long) pid);
5
6 wait(NULL);
7
8 printf("PARENT <%ld> Child with PID = %ld terminated.\n",
9 (long) getpid(), (long) pid);
10
11 printf("PARENT <%ld> Goodbye.\n",
12 (long) getpid());
13
14 exit(EXIT_SUCCESS);
15 }
144
On line 6 the parent calls wait(NULL) to wait for the child process to
terminate.
Compile and run the program. Now the parent should always wait for the
child to terminate before terminating itself. As a consequence the child
should:
newer be adopted by init
new report PPID = 1
always report PPID equal to the PID of the parent.
145
Example using wait to obtain the exit status of the
child
1 void parent(pid_t pid) {
2 int status;
3
4 printf("PARENT <%ld> Spawned a child with PID = %ld.\n",
5 (long) getpid(), (long) pid);
6
7 wait(&status);
8
9 if (WIFEXITED(status)) {
10 printf("PARENT <%ld> Child with PID = %ld and exit status = %d terminated.\n",
11 (long) getpid(), (long) pid, WEXITSTATUS(status));
12 }
13
14 printf("PARENT <%ld> Goodbye.\n",
15 (long) getpid());
16
17 exit(EXIT_SUCCESS);
18 }
146
● One line 2 the parent creates the variable status. On line 7 the parent
calls wait(&status) to wait for the child process to terminate. The & is the
address-of operator and &status returns the address of
the status variable. When the child terminates the exit status of the child
will be stored in variable status.
147
● PARENT <99340> Spawned a child with PID = 99341.
● PARENT <99340> Child with PID = 99341 and exit status = 42 terminated.
148
Zombies
A terminated process is said to be a zombie or defunct until the parent
does wait on the child.
When a process terminates all of the memory and resources associated with it
are deallocated so they can be used by other processes.
However, the exit status is maintained in the PCB until the parent picks up the
exit status using wait and deletes the PCB.
A child process always first becomes a zombie.
In most cases, under normal system operation zombies are immediately waited
on by their parent.
Processes that stay zombies for a long time are generally an error and cause a
resource leak.
149
1 void parent(pid_t pid) {
2
3 printf("PARENT <%ld> Spawned a child with PID = %ld.\n",
4 (long) getpid(), (long) pid);
5
6 printf("PARENT <%ld> Press any key to reap a zombie!\n",
7 (long) getpid());
8
9 getchar();
10
11 pid = wait(NULL);
12
13 printf("PARENT <%ld> Zombie child with PID = %ld",
14 (long) getpid(), (long) pid);
15
16 exit(EXIT_SUCCESS);
17 }
150
● On line 9 the parent uses getchar to block itself until the user presses a
key on the keyboard.
● When the child terminates, the exit status of the child is stored in the
child process control block (PCB). The operating system deallocates all
memory used by the child but the PCB cannot be deallocated until the
parent does wait on the child.
151
● PARENT <4636> Spawned a child with PID = 4637.
152
Module 3
153
Process Management and Synchronization -
Synchronization Hardware
Semaphores
Deadlocks -
Classical Problems of Synchronization
System Model
Critical Regions
Deadlocks Characterization Monitors
shared memory.
154
Deadlock
155
System Model
Every process needs some resources to complete its execution. However, the
resource is granted in a sequential order.
● The process requests for some resource.
● OS grant the resource if it is available otherwise let the process waits.
● The process uses it and release on the completion.
A Deadlock is a situation where each of the computer process waits for a
resource which is being assigned to some another process. In this situation,
none of the process gets executed since the resource it needs, is held by some
other process which is also waiting for some other resource to be released.
156
Let us assume that there are three processes P1,
P2 and P3. There are three different resources
R1, R2 and R3. R1 is assigned to P1, R2 is
assigned to P2 and R3 is assigned to P3.
157
Sr. Deadlock Starvation
1 Deadlock is a situation where no process got blocked and no process Starvation is a situation where the low
infinite.
4 The requested resource is blocked by the other process. The requested resource is continuously
5 Deadlock happens when Mutual exclusion, hold and wait, No preemption It occurs due to the uncontrolled priority
158
Deadlock Characterization
Mutual Exclusion
A resource can only be shared in mutually exclusive manner. It implies, if two process cannot use the
same resource at the same time.
A process waits for some resources while holding another resource at the same time.
No preemption
The process which once scheduled will be executed till the completion. No other process can be
scheduled by the scheduler meanwhile.
Circular Wait
All the processes must be waiting for the resources in a cyclic manner so that the last process is
waiting for the resource which is being held by the first process.
159
Methods of handling Deadlock
160
Deadlock Ignorance
Deadlock Ignorance is the most widely used approach among all the mechanism. This is
being used by many operating systems mainly for end user uses. In this approach, the
Operating system assumes that deadlock never occurs. It simply ignores deadlock. This
approach is best suitable for a single end user system where User uses the system only for
browsing and all other normal stuff.
There is always a tradeoff between Correctness and performance. The operating systems
like Windows and Linux mainly focus upon performance. However, the performance of the
system decreases if it uses deadlock handling mechanism all the time if deadlock happens 1
out of 100 times then it is completely unnecessary to use the deadlock handling mechanism
all the time.
In these types of systems, the user has to simply restart the computer in the case of
deadlock. Windows and Linux are mainly using this approach.
161
Deadlock Prevention
Deadlock happens only when Mutual Exclusion, hold and wait, No preemption
and circular wait holds simultaneously. If it is possible to violate one of the
four conditions at any time then the deadlock can never occur in the system.
The idea behind the approach is very simple that we have to fail one of the
four conditions but there can be a big argument on its physical
implementation in the system.
162
Mutual section from the resource point
of view is the fact that a resource can
Deadlock
never be used by more than one
process simultaneously which is fair
enough but that is the main reason
Prevention
behind the deadlock. If a resource could
have been used by more than one
process at the same time then the
process would have never been waiting
Mutual Exclusion for any resource.
However, if we can be able to violate
resources behaving in the mutually
exclusive manner then the deadlock can
be prevented.
163
For a device like printer, spooling can work. There is a
Prevention
FCFS. By using this mechanism, the process doesn't
have to wait for the printer and it can continue
whatever it was doing. Later, it collects the output
when it is produced.
Mutual Exclusion Although, Spooling can be an effective approach to
Spooling violate mutual exclusion but it suffers from two kinds
of problems.
164
Hold and wait condition lies when a process
holds a resource and waiting for some other
resource to complete its task. Deadlock
occurs because there can be more than one
process which are holding one resource and
Prevention
mechanism by which a process either
doesn't hold any resource or doesn't wait.
That means, a process must be assigned all
the necessary resources before the
Hold and Wait execution starts. A process must not wait
for any resource once the execution has
been started.
165
This can be implemented practically if a
process declares all the resources initially.
However, this sounds very practical but
can't be done in the computer system
because a process can't determine
Prevention
executed by the CPU. Each of the instruction
may demand multiple resources at the
multiple times. The need cannot be fixed by
the OS.
Hold and Wait The problem with the approach is:
166
Deadlock arises due to the fact that a
process can't be stopped once it starts.
However, if we take the resource away from
the process which is causing deadlock then
we can prevent deadlock.
Prevention
the process then all the work which it has
done till now can become inconsistent.
167
To violate circular wait, we can assign
a priority number to each of the
168
169
Deadlock Avoidance
In deadlock avoidance, the operating system checks whether the system is in safe
state or in unsafe state at every step which the operating system performs. The
process continues until the system is in safe state. Once the system moves to unsafe
state, the OS has to backtrack one step.
In simple words, The OS reviews each allocation so that the allocation doesn't cause
the deadlock in the system.
If the system cannot fulfill the request of all processes then the state of the system is
called unsafe.
The key of Deadlock avoidance approach is when the request is made for resources
then the request must only be approved in the case if the resulting state is also a
safe state.
170
Resource allocation Graph
The resource allocation graph is the pictorial representation of the state of a
system. As its name suggests, the resource allocation graph is the complete
information about all the processes which are holding some resources or
waiting for some resources.
It also contains the information about all the instances of all the resources
whether they are available or being used by the processes.
171
172
Vertices are mainly of two types, Resource and process. Each of them will be
represented by a different shape. Circle represents process while rectangle
represents resource.
A resource can have more than one instance. Each instance will be
represented by a dot inside the rectangle.
Edges in RAG are also of two types, one represents assignment and other
represents the wait of a process for a resource. The above image shows each
of them.
A resource is shown as assigned to a process if the tail of the arrow is
attached to an instance to the resource and the head is attached to a process.
A process is shown as waiting for a resource if the tail of an arrow is attached
to the process while the head is pointing towards the resource.
173
174
Deadlock Detection using RAG
If a cycle is being formed in a
Resource allocation graph where all
the resources have the single
instance then the system is
deadlocked.
In Case of Resource allocation graph
with multi-instanced resource types,
Cycle is a necessary condition of
deadlock but not the sufficient
condition.
175
Allocation Matrix
Allocation matrix can be formed by Process R1 R2 R3
176
Request Matrix
In request matrix, an entry will be Process R1 R2 R3
P3 0 0 1
177
Neither we are having any resource available in the system nor a process
going to release. Each of the process needs at least single resource to
complete therefore they will continuously be holding each one of them.
We cannot fulfill the demand of at least one process using the available
resources therefore the system is deadlocked as determined earlier when we
detected a cycle in the graph.
178
Banker's Algorithm
179
Banker's algorithm is a deadlock avoidance algorithm. It is named so because
this algorithm is used in banking systems to determine whether a loan can be
granted or not.
Consider there are n account holders in a bank and the sum of the money in
all of their accounts is S. Every time a loan has to be granted by the bank, it
subtracts the loan amount from the total money the bank has. Then it checks
if that difference is greater than S. It is done because, only then, the bank
would have enough money even if all the n account holders draw all their
money at once.
Banker's algorithm works in a similar way in computers.
Whenever a new process is created, it must specify the maximum instances of
each resource type that it needs, exactly.
180
Characteristics of Banker's Algorithm
If any process requests for a resource, then it has to wait.
In this algorithm, if any process gets all the needed resources, then it is that it
should return the resources in a restricted period.
Various resources are maintained in this algorithm that can fulfill the needs of
at least one client.
181
Data Structures used to implement the Banker’s Algorithm
1. Available
It is an array of length m. It represents the number of available resources of each type. If Available[j] = k, then there are k
instances available, of resource type Rj.
2. Max
It is an n x m matrix which represents the maximum number of instances of each resource that a process can request. If
Max[i][j] = k, then the process Pi can request at most k instances of resource type Rj.
3. Allocation
It is an n x m matrix which represents the number of resources of each type currently allocated to each process. If
Allocation[i][j] = k, then process Pi is currently allocated k instances of resource type Rj.
4. Need
It is a two-dimensional array. It is an n x m matrix which indicates the remaining resource needs of each process. If Need[i][j]
= k, then process Pi may need k more instances of resource type Rj to complete its task.
182
Banker’s algorithm comprises of two algorithms:
Safety algorithm
183
If there is no such i present, then proceed to step 4.
2. Find an index i such that both That means if all processes are finished, then the system is in
safe state.
Finish[i] ==false
This algorithm may require an order of mxn² operations in
Needi <= Work order to determine whether a state is safe or not.
184
Resource Request Algorithm
Let Requesti be the request vector for the process Pi. If Requesti[j]==k, then process Pi wants k instance of Resource type
Rj.When a request for resources is made by the process Pi, the following are the actions that will be taken:
1. If Requesti <= Needi, then go to step 2;else raise an error condition, since the process has exceeded its maximum claim.
2.If Requesti <= Availablei then go to step 3; else Pi must have to wait as resources are not available.
3.Now we will assume that resources are assigned to process Pi and thus perform the following steps:
Available= Available-Requesti;
Allocationi=Allocationi +Requesti;
If the resulting resource allocation state comes out to be safe, then the transaction is completed and, process Pi is allocated
its resources. But in this case, if the new state is unsafe, then Pi waits for Requesti, and the old resource-allocation state is
restored.
185
Example:
P1 212 322
P2 401 902
P3 020 753
P4 112 112
186
1. The Content of the need matrix can be calculated by using the formula given below:
187
Safe Sequence:
2. For Process P1, Need = (1, 1, 0)
So, the system will move to the next process. Available = Available +Allocation
= (2, 1, 0) + (2, 1, 2)
188
3. For Process P2, Need = (5, 0, 1) 4. For Process P3, Need = (7, 3, 3)
So, the system will move to the next process. So, the system will move to the next process.
189
5. For Process P4, Need = (0, 0, 0) 6. Now again check for Process P2, Need =
(5, 0, 1)
Available = (4, 2, 2)
Available = (5, 3, 4)
Need <= Available = True
Need <= Available = True
Request of P4 is granted.
Request of P2 is granted.
Available = Available + Allocation
Available = Available + Allocation
= (4, 2, 2) + (1, 1, 2)
= (5, 3, 4) + (4, 0, 1)
= (5, 3, 4) now, (New Available)
= (9, 3, 5) now, (New Available)
190
7. Now again check for Process P3, Need = 8. Now again check for Process P0, = Need
(7, 3, 3) (3, 2, 1)
The request for P3 is granted. So, the request will be granted to P0.
Available = Available +Allocation Safe sequence: < P1, P4, P2, P3, P0>
191
3. The total amount of resources will be calculated by the following formula:
= [8 5 7] + [2 1 0] = [10 6 7]
192
Disadvantages of Banker's Algorithm
Some disadvantages of this algorithm are as follows:
During the time of Processing, this algorithm does not permit a process to
change its maximum need.
Another disadvantage of this algorithm is that all the processes must know in
advance about the maximum resource needs.
193
Deadlock Detection and Recovery
In this approach, The OS doesn't apply any mechanism to
avoid or prevent the deadlocks. Therefore the system
considers that the deadlock will definitely occur. In order to
get rid of deadlocks, The OS periodically checks the system
for any deadlock. In case, it finds any of the deadlock then
the OS will recover the system using some recovery
techniques.
194
195
For Resource
Preempt the resource
We can snatch one of the resources from the owner of the resource (process)
and give it to the other process with the expectation that it will complete the
execution and will release this resource sooner. Well, choosing a resource
which will be snatched is going to be a bit difficult.
Rollback to a safe state
System passes through various states to get into the deadlock state. The
operating system can rollback the system to the previous safe state. For this
purpose, OS needs to implement check pointing at every state.
The moment, we get into deadlock, we will rollback all the allocations to get
into the previous safe state.
196
For Process
Kill a process
Killing a process can solve our problem but the bigger concern is to decide
which process to kill. Generally, Operating system kills a process which has
done least amount of work until now.
197
Process Management and Synchronization
198
What is Process Synchronization?
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.
199
200
201
Race Condition
counter++ could be implemented as Consider this execution interleaving with “count = 5” initially:
S0: producer execute register1 = counter
{register1 = 5}
S1: producer execute register1 = register1
register1 = counter + 1 {register1 = 6}
register1 = register1 + 1 S2: consumer execute register2 = counter
{register2 = 5}
counter = register1 S3: consumer execute register2 = register2
– 1 {register2 = 4}
counter-- could be implemented as S4: producer execute counter = register1
{counter = 6 }
S5: consumer execute counter = register2
register2 = counter {counter = 4}
register2 = register2 - 1
counter = register2
202
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.
To guard against the race condition above, we need to ensure that only one
process at a time can be manipulating the variable counter. To make such a
guarantee, we require that the processes be synchronized in some way
203
204
Critical Section Problem
205
Entry Section: It is part of the process
which decides the entry of a particular
process.
Program
Exit Section: Exit section allows the other
process that are waiting in the Entry Section,
to enter into the Critical Sections. It also
checks that a process that finished its
execution should be removed through this
Section.
206
do {
General structure of entry section
exit section
remainder section
} while (true);
207
What is critical section problem?
A critical section is a segment of code which can be accessed by a signal process at a specific
point of time. The section consists of shared data resources that required to be accessed by
other processes.
The entry to the critical section is handled by the wait() function, and it is represented as
P().
The exit from a critical section is controlled by the signal() function, represented as V().
In the critical section, only a single process can be executed. Other processes, waiting to
execute their critical section, need to wait until the current process completes its execution.
208
Rules for Critical Section:
Mutual Exclusion: Mutual Exclusion is a special type of binary semaphore which is used
for controlling access to the shared resource. It includes a priority inheritance mechanism
to avoid extended priority inversion problems. Not more than one process can execute in
its critical section at one time.
Progress: This solution is used when no one is in the critical section, and someone wants
in. Then those processes not in their reminder section should decide who should go in, in
a finite time.
Bound Waiting: When a process makes a request for getting into critical section, there is a
specific limit about number of processes can get into their critical section. So, when the
limit is reached, the system must allow request to the process to get into its critical
section.
209
At a given point in time, many kernel-mode processes may be active in the
operating system. As a result, the code implementing an operating system
(kernel code) is subject to several possible race conditions. Consider as an
example a kernel data structure that maintains a list of all open files in the
system. This list must be modified when a new file is opened or closed (adding
the file to the list or removing it from the list). If two processes were to open
files simultaneously, the separate updates to this list could result in a race
condition.
210
Two general approaches are used to handle critical sections in operating
systems: preemptive kernels and non preemptive kernels.
A non preemptive kernel does not allow a process running in kernel mode to
be preempted; a kernel-mode process will run until it exits kernel mode,
blocks, or voluntarily yields control of the CPU.
211
Solutions To The Critical Section
Peterson Solution
Synchronization Hardware
Mutex Locks
Semaphore Solution
212
Peterson’s Solution
Good algorithmic description of solving the problem
Two process solution
Assume that the load and store machine-language instructions are atomic; that is,
cannot be interrupted
The two processes share two variables:
int turn;
Boolean flag[2]
l
The variable turn indicates whose turn it is to enter the critical section
The flag array is used to indicate if a process is ready to enter the critical section.
flag[i] = true implies that process Pi is ready!
213
Peterson solution:
Peterson’s solution is widely used solution to PROCESS Pi
critical section problems. This algorithm was
FLAG[i] = true
developed by a computer scientist Peterson
that’s why it is named as a Peterson’s while( (turn != i) AND (CS is !free) ){ wait;
solution.
}
In this solution, when a process is executing in
a critical state, then the other process only CRITICAL SECTION FLAG[i] = false
executes the rest of the code, and the
turn = j; //choose another process to go to CS
opposite can happen. This method also helps
to make sure that only a single process runs
in the critical section at a specific time.
214
Synchronization Hardware
Many systems provide hardware support for implementing the critical section code.
All solutions below based on idea of locking
Protecting critical regions via locks
Uniprocessors – could disable interrupts
Currently running code would execute without preemption
Generally too inefficient on multiprocessor systems
Operating systems using this not broadly scalable
Modern machines provide special atomic hardware instructions
Atomic = non-interruptible
Either test memory word and set value
Or swap contents of two memory words
215
Solution to Critical-section Problem Using Locks
do {
acquire lock
critical section
release lock
remainder section
} while (TRUE);
216
test_and_set Instruction
Definition: Executed atomically
boolean test_and_set (boolean Returns the original value of passed
*target) parameter
{
Set the new value of passed
boolean rv = *target; parameter to “TRUE”.
*target = TRUE;
return rv:
217
Solution using test_and_set()
Shared Boolean variable lock, initialized to FALSE
Solution:
do {
while (test_and_set(&lock))
; /* do nothing */
/* critical section */
lock = false;
/* remainder section */
} while (true);
218
compare_and_swap Instruction
Definition: Executed atomically
int compare _and_swap(int *value, int
expected, int new_value) { Returns the original value of passed
parameter “value”
int temp = *value;
Set the variable “value” the value
if (*value == expected)
of the passed parameter
“new_value” but only if “value”
*value = new_value;
==“expected”. That is, the swap
return temp; takes place only under this
} condition.
219
Solution using compare_and_swap
Shared integer “lock” initialized to 0;
Solution:
do {
while (compare_and_swap(&lock, 0, 1) != 0)
; /* do nothing */
/* critical section */
lock = 0;
/* remainder section */
} while (true);
220
Bounded-waiting Mutual Exclusion with test_and_set
do { j = (j + 1) % n;
waiting[i] = true;
if (j == i)
key = true;
while (waiting[i] && key) lock = false;
key = test_and_set(&lock); else
waiting[i] = false; waiting[j] = false;
/* critical section */ /* remainder section */
j = (i + 1) % n; } while (true);
while ((j != i) && !waiting[j])
221
Mutex Locks
Previous solutions are complicated and generally inaccessible to application programmers
Protect a critical section by first acquire() a lock then release() the lock
222
acquire() and release()
acquire() { release() {
while (!available)
available = true;
; /* busy wait */
available = false;
}
}
223
acquire() and release()
do {
acquire lock
critical section
release lock
remainder section
} while (true);
224
semaphore
Synchronization tool that provides more sophisticated ways (than Mutex
locks) for process to synchronize their activities.
225
semaphore
Definition of the wait() operation Definition of the signal() operation
wait(S) {
signal(S) {
while (S <= 0)
S++;
; // busy wait
}
S--;
226
semaphore
Counting semaphore – integer value can range over an unrestricted domain
227
semaphore
Can solve various synchronization problems P2:
Consider P1 and P2 that require S1 to
wait(synch);
happen before S2
228
Semaphore Implementation
Must guarantee that no two processes can execute the wait() and signal() on
the same semaphore at the same time
Thus, the implementation becomes the critical section problem where the
wait and signal code are placed in the critical section
Could now have busy waiting in critical section implementation
But implementation code is short
Little busy waiting if critical section rarely occupied
Note that applications may spend lots of time in critical sections and therefore
this is not a good solution
229
Semaphore Implementation with no Busy waiting
With each semaphore there is an associated block – place the process invoking the
waiting queue operation on the appropriate waiting queue
Each entry in a waiting queue has two data wakeup – remove one of processes in the
items: waiting queue and place it in the ready queue
} semaphore;
230
Implementation with no Busy waiting (Cont.)
wait(semaphore *S) { signal(semaphore *S) {
S->value--; S->value++;
block(); wakeup(P);
} }
} }
231
Deadlock and Starvation
Deadlock – two or more processes are waiting Starvation – indefinite blocking
indefinitely for an event that can be caused by
only one of the waiting processes A process may never be removed from the
Let S and Q be two semaphores initialized to 1 semaphore queue in which it is suspended
signal(S); signal(Q);
signal(Q); signal(S);
232
Classical Problems
Bounded-Buffer Problem
of Synchronization Readers and Writers Problem
Dining-Philosophers Problem
233
Bounded-Buffer Problem
n buffers, each can hold one item
234
Bounded Buffer Problem (Cont.)
The structure of the producer process /* add next produced
do { to the buffer */
... ...
235
Bounded Buffer Problem (Cont.)
The structure of the consumer process signal(mutex);
Do { signal(empty);
wait(full); ...
/* consume the item in next consumed */
wait(mutex);
...
...
} while (true);
/* remove an item from buffer to
next_consumed */
236
Readers-Writers Problem
A data set is shared among a number of Several variations of how readers and writers
concurrent processes are considered – all involve some form of
priorities
Readers – only read the data set; they do not
perform any updates Shared Data
237
Readers-Writers Problem(cont.)
The structure of a writer process
do {
wait(rw_mutex);
...
/* writing is performed */
...
signal(rw_mutex);
} while (true);
238
Readers-Writers Problem(cont.)
The structure of a reader process wait(mutex);
read count--;
do {
if (read_count == 0)
wait(mutex);
read_count++; signal(rw_mutex);
if (read_count == 1)
signal(mutex);
wait(rw_mutex);
} while (true);
signal(mutex);
...
/* reading is performed */
239
Readers-Writers Problem(cont.)
First variation – no reader kept waiting unless writer has permission to use
shared object
240
Dining philosopher problem
The dining philosophers 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 center, 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.
241
Dining philosopher solution using semaphore
do { Deadlock handling
wait (chopstick[i] ); Allow at most 4 philosophers to be sitting
wait (chopStick[ (i + 1) % simultaneously at the table.
5] );
Allow a philosopher to pick up the forks
// eat only if both are available (picking must be
signal (chopstick[i] ); done in a critical section.
signal (chopstick[ (i + 1) Use an asymmetric solution -- an
% 5] ); odd-numbered philosopher picks up first
// think the left chopstick and then the right
} while (TRUE); chopstick. Even-numbered philosopher
picks up first the right chopstick and then
the left chopstick.
242
Problems with semaphore
Incorrect use of semaphore operations:
243
Monitors
A high-level abstraction that provides a monitor monitor-name
convenient and effective mechanism for
{
process synchronization
// shared variable declarations
Abstract data type, internal variables only
accessible by code within the procedure procedure P1 (…) { …. }
246
Monitor with Condition Variables
247
Condition Variables Choices
If process P invokes x.signal(), and process Q is suspended in x.wait(), what should happen next?
Options include
Signal and wait – P waits until Q either leaves the monitor or it waits for another condition
Signal and continue – Q waits until P either leaves the monitor or it waits for another condition
248
Monitor Solution to Dining Philosophers
249
void test (int i) { initialization_code() {
if ((state[(i + 4) % 5] != EATING) && for (int i = 0; i < 5; i++)
(state[i] == HUNGRY) && state[i] = THINKING;
}
(state[(i + 1) % 5] != EATING) ) {
}
state[i] = EATING ;
self[i].signal () ;
250
Each philosopher i invokes the operations pickup() and putdown() in the
following sequence:
DiningPhilosophers.pickup(i);
EAT
DiningPhilosophers.putdown(i);
251
Monitor Implementation Using Semaphores
Variables wait(mutex);
body of F;
semaphore next; // (initially = 0)
…
int next_count = 0;
if (next_count > 0)
Each procedure F will be replaced by signal(next)
else
signal(mutex);
252
Monitor Implementation – Condition Variables
For each condition variable x, we have: The operation x.wait can be implemented as:
x_count++;
else
signal(mutex);
wait(x_sem);
x_count--;
253
The operation x.signal can be implemented as:
if (x_count > 0) {
next_count++;
signal(x_sem);
wait(next);
next_count--;
}
254
Resuming Processes within a Monitor
If several processes queued on condition x, and x.signal() executed, which
should be resumed?
255
Single Resource allocation
Allocate a single resource among competing processes using priority numbers
that specify the maximum time a process plans to use the resource
R.acquire(t);
...
access the resurce;
...
R.release;
Where R is an instance of type ResourceAllocator
256
A Monitor to Allocate Single Resource
monitor ResourceAllocator void release() {
{ busy = FALSE;
boolean busy;
x.signal();
condition x;
}
void acquire(int time) {
initialization code() {
if (busy)
busy = FALSE;
x.wait(time);
}
busy = TRUE;
} }
257
Interprocess communication
258
Inter-process communication is
used for exchanging useful
information between numerous
Definition
threads in one or more processes
(or programs).
259
It is a set of programming interface which allow a programmer to coordinate
activities among various program processes which can run concurrently in an
operating system. This allows a specific program to handle many user
requests at the same time.
Since every single user request may result in multiple processes running in the
operating system, the process may require to communicate with each other.
Each IPC protocol approach has its own advantage and limitation, so it is not
unusual for a single program to use all of the IPC methods.
260
Role of Synchronization in Inter Process Communication
It is one of the essential parts of inter process communication. Typically, this is
provided by interprocess communication control mechanisms, but sometimes
it can also be controlled by communication processes.
These are the following methods that used to provide the synchronization:
Mutual Exclusion
Semaphore
Barrier
Spinlock
261
Mutual Exclusion:-
It is generally required that only one process thread can enter the critical section at a time.
This also helps in synchronization and creates a stable state to avoid the race condition.
Semaphore:-
Semaphore is a type of variable that usually controls the access to the shared resources by
several processes. Semaphore is further divided into two types which are as follows:
Binary Semaphore
Counting Semaphore
262
Barrier:-
A barrier typically not allows an individual process to proceed unless all the
processes does not reach it. It is used by many parallel languages, and
collective routines impose barriers.
Spinlock:-
Spinlock is a type of lock as its name implies. The processes are trying to
acquire the spinlock waits or stays in a loop while checking that the lock is
available or not. It is known as busy waiting because even though the process
active, the process does not perform any functional operation (or task).
263
Approaches for Inter-Process Communication
264
These are a few different approaches for Inter- Process Communication:
Pipes
Message Passing
Shared Memory
Message Queue
Direct Communication
Indirect communication
FIFO
265
Pipes:
The pipe is a type of data channel that is unidirectional in nature. It means
that the data in this type of data channel can be moved in only a single
direction at a time. Still, one can use two-channel of this type, so that he can
able to send and receive data in two processes. Typically, it uses the standard
methods for input and output. These pipes are used in all types of POSIX
systems and in different versions of window operating systems as well.
Pipe is widely used for communication between two related processes. This is
a half-duplex method, so the first process communicates with the second
process. However, in order to achieve a full-duplex, another pipe is needed.
266
Message passing:
It is a type of mechanism that allows processes to synchronize and
communicate with each other. Using message passing, the process
communicates with each other without resorting to shared variables.
Received (message)
267
Shared memory:
Shared memory is a memory shared between two or more processes that are
established using shared memory between all the processes. This type of
memory requires to protected from each other by synchronizing access across
all the processes.
268
Message queue:
269
Message queue:
A message queue is a linked list of messages stored within the kernel. It is
identified by a message queue identifier. This method offers communication
between single or multiple processes with full-duplex capacity.
In general, several different messages are allowed to read and write the data
to the message queue. In the message queue, the messages are stored or stay
in the queue unless their recipients retrieve them. In short, we can also say
that the message queue is very helpful in inter-process communication and
used by all operating systems.
270
Direct communication:
In this type of communication process, usually, a link is created or established
between two communicating processes. However, in every pair of
communicating processes, only one link can exist.
271
Indirect communication:
Indirect communication establishes like only when processes share a common
mailbox each pair of processes sharing several communication links. A link
can communicate with many processes. The link may be bi-directional or
unidirectional.
272
FIFO:
Communication between two unrelated processes. It is a full-duplex method,
which means that the first process can communicate with the second process,
and the opposite can also happen.
273
Some other different approaches
Socket:-
It acts as a type of endpoint for receiving or sending the data in a network. It is correct for data sent between
processes on the same computer or data sent between different computers on the same network. Hence, it
used by several types of operating systems.
File:-
A file is a type of data record or a document stored on the disk and can be acquired on demand by the file
server. Another most important thing is that several processes can access that file as required or needed.
Signal:-
As its name implies, they are a type of signal used in inter process communication in a minimal way. Typically,
they are the massages of systems that are sent by one process to another. Therefore, they are not used for
sending data but for remote commands between multiple processes.
274
Why we need interprocess communication?
There are numerous reasons to use inter-process communication for sharing
the data. Here are some of the most important reasons that are given below:
It helps to speedup modularity
Computational
Privilege separation
Convenience
Helps operating system to communicate with each other and synchronize
their actions.
275
UNIT - IV
Memory Management and Virtual Memory
276
Index:
Logical versus Physical Address Space
Swapping
Contiguous Allocation
Paging
Segmentation
Demand Paging
Page Replacement
277
Logical and Physical Address space
Address uniquely identifies a location in the memory. We have two types of
addresses that are logical address and physical address. The logical address is
a virtual address and can be viewed by the user. The user can’t view the
physical address directly. The logical address is used like a reference, to access
the physical address. The fundamental difference between logical and physical
address is that logical address is generated by CPU during a program
execution whereas, the physical address refers to a location in the memory
unit.
278
Difference Between Logical and Physical Address
BASIS FOR COMPARISON LOGICAL ADDRESS PHYSICAL ADDRESS
Address Space Set of all logical addresses Set of all physical addresses
generated by CPU in reference mapped to the corresponding
to a program is referred as logical addresses is referred as
Logical Address Space. Physical Address.
Visibility The user can view the logical The user can never view physical
address of a program. address of program
Access The user uses the logical The user can not directly access
address to access the physical physical address.
address.
280
Definition of Physical Address
Physical Address identifies a physical location in a memory. MMU
(Memory-Management Unit) computes the physical address for the corresponding
logical address. MMU also uses logical address computing physical address. The user
never deals with the physical address. Instead, the physical address is accessed by
its corresponding logical address by the user. The user program generates the
logical address and thinks that the program is running in this logical address. But the
program needs physical memory for its execution. Hence, the logical address must
be mapped to the physical address before they are used.
The logical address is mapped to the physical address using a hardware called
Memory-Management Unit. The set of all physical addresses corresponding to the
logical addresses in a Logical address space is called Physical Address Space.
281
Key Differences Between Logical and Physical Address
1. The basic difference between Logical and physical address is that Logical address is generated by CPU in
perspective of a program. On the other hand, the physical address is a location that exists in the memory unit.
2. The set of all logical addresses generated by CPU for a program is called Logical Address Space. However, the
set of all physical address mapped to corresponding logical addresses is referred as Physical Address Space.
3. The logical address is also called virtual address as the logical address does not exist physically in the memory
unit. The physical address is a location in the memory unit that can be accessed physically.
4. Identical logical address and physical address are generated by Compile-time and Load time address binding
methods.
5. The logical and physical address generated while run-time address binding method differs from each other.
6. The logical address is generated by the CPU while program is running whereas, the physical addres is
computed by the MMU (Memory Management Unit).
282
Swapping
Swapping is a mechanism in which a process can be swapped temporarily out of
main memory (or move) to secondary storage (disk) and make that memory
available to other processes. At some later time, the system swaps back the process
from the secondary storage to main memory.
Swapping is a mechanism in which a process can be swapped temporarily out of
main memory (or move) to secondary storage (disk) and make that memory
available to other processes. At some later time, the system swaps back the process
from the secondary storage to main memory.
Though performance is usually affected by swapping process but it helps in running
multiple and big processes in parallel and that's the reason Swapping is also known
as a technique for memory compaction.
283
284
Contiguous memory allocation
It means freely available memory partitions are not scattered here and there across the whole
memory space. In the contiguous memory allocation, both the operating system and the user must
reside in the main memory. A single process is allocated in that fixed sized single partition.
In the contiguous memory allocation, both the operating system and the user must reside in the
main memory. The main memory is divided into two portions one portion is for the operating and
other is for the user program.
In the contiguous memory allocation when any user process request for the memory a single section
of the contiguous memory block is given to that process according to its need. We can achieve
contiguous memory allocation by dividing memory into the fixed-sized partition.
A single process is allocated in that fixed sized single partition. But this will increase the degree of
multiprogramming means more than one process in the main memory that bounds the number of
fixed partition done in memory. Internal fragmentation increases because of the contiguous memory
allocation.
285
286
Fixed sized partition
In the fixed sized partition the system divides memory into fixed size partition
(may or may not be of the same size) here entire partition is allowed to a
process and if there is some wastage inside the partition is allocated to a
process and if there is some wastage inside the partition then it is called
internal fragmentation.
Advantage: Management or book keeping is easy.
Disadvantage: Internal fragmentation
287
Variable size partition
In the variable size partition, the memory is treated as one unit and space
allocated to a process is exactly the same as required and the leftover space
can be reused again.
288
Paging
In computer operating systems, paging is a memory management scheme by
which a computer stores and retrieves data from secondary storage for use in
main memory. In this scheme, the operating system retrieves data from
secondary storage in same-size blocks called pages.Paging is a memory
management scheme that eliminates the need for contiguous allocation of
physical memory. This scheme permits the physical address space of a
process to be non –contiguous.
289
Logical Address or Virtual Address (represented in bits): An address
generated by the CPU
Logical Address Space or Virtual Address Space( represented in words or
bytes): The set of all logical addresses generated by a program
Physical Address (represented in bits): An address actually available on
memory unit
Physical Address Space (represented in words or bytes): The set of all
physical addresses corresponding to the logical addresses
The Physical Address Space is conceptually divided into a number of fixed-size
blocks, called frames.
The Logical address Space is also splitted into fixed-size blocks, called pages.
Page Size = Frame Size
290
Address generated by CPU is divided into
Page number(p): Number of bits required to represent the pages in Logical
Address Space or Page number
Page offset(d): Number of bits required to represent particular word in a
page or page size of Logical Address Space or word number of a page or page
offset.
Physical Address is divided into
Frame number(f): Number of bits required to represent the frame of Physical
Address Space or Frame number.
Frame offset(d): Number of bits required to represent particular word in a
frame or frame size of Physical Address Space or word number of a frame or
frame offset.
291
The hardware implementation of page table can be done by using dedicated
registers. But the usage of register for the page table is satisfactory only if
page table is small. If page table contain large number of entries then we can
use TLB(translation Lookaside buffer), a special, small, fast look up hardware
cache.
292
Segmentation
Segmentation is a memory management technique in which, the memory is divided into the variable
size parts. Each part is known as segment which can be allocated to a process. The details about each
segment are stored in a table called as segment table.
The details about each segment are stored in a table called as segment table. Segment table is stored
in one (or many) of the segments.
293
Why segments are required?
Till now, we were using Paging as our main memory management technique. Paging
is more close to Operating system rather than the User. It divides all the process into
the form of pages regardless of the fact that a process can have some relative parts
of functions which needs to be loaded in the same page.
Operating system doesn't care about the User's view of the process. It may divide
the same function into different pages and those pages may or may not be loaded at
the same time into the memory. It decreases the efficiency of the system.
It is better to have segmentation which divides the process into the segments. Each
segment contain same type of functions such as main function can be included in
one segment and the library functions can be included in the other segment.
294
Translation of Logical address into physical address by
segment table
CPU generates a logical address which contains two parts:
1. Segment Number
2. Offset
The Segment number is mapped to the segment table. The limit of the
respective segment is compared with the offset. If the offset is less than the
limit then the address is valid otherwise it throws an error as the address is
invalid. In the case of valid address, the base address of the segment is added
to the offset to get the physical address of actual word in the main memory.
295
Advantages of Segmentation
1. No internal fragmentation
2. Average Segment Size is larger than the actual page size.
3. Less overhead
4. It is easier to relocate segments than entire address space.
5. The segment table is of lesser size as compare to the page table in paging.
Disadvantages
1. It can have external fragmentation.
2. it is difficult to allocate contiguous memory to variable sized partition.
3. Costly memory management algorithms.
296
Virtual Memory
Virtual memory is a memory management capability of an operating system (OS) that uses
hardware and software to allow a computer to compensate for physical memory shortages
by temporarily transferring data from random access memory (RAM) to disk storage.
The size of virtual storage is limited by the addressing scheme of the computer system and
amount of secondary memory is available not by the actual number of the main storage
locations.
297
It is a technique that is implemented using both hardware and software. It maps
memory addresses used by a program, called virtual addresses, into physical
addresses in computer memory.
1. All memory references within a process are logical addresses that are dynamically
translated into physical addresses at run time. This means that a process can be
swapped in and out of main memory such that it occupies different places in main
memory at different times during the course of execution.
2. A process may be broken into number of pieces and these pieces need not be
continuously located in the main memory during execution. The combination of
dynamic run-time address translation and use of page or segment table permits this.
If these characteristics are present then, it is not necessary that all the pages or
segments are present in the main memory during execution. This means that the
required pages need to be loaded into memory whenever required. Virtual memory
is implemented using Demand Paging or Demand Segmentation.
298
Hardware and Control Structures
Memory references are dynamically translated into physical addresses at run
time –
299
Execution of a Program
Operating system brings into main memory a few pieces of the program
Piece of process that contains the logical address is brought into main memory –
Operating system issues a disk I/O Read request – Another process is dispatched to
run while the disk I/O takes place – An interrupt is issued when disk I/O complete
which causes the operating system to place the affected process in the Ready state.
300
Advantages of Breaking up a Process
301
Types of Memory
302
Thrashing
• The processor spends most of its time swapping pieces rather than
executing user instructions
303
Paging
• Each page table entry contains the frame number of the corresponding page
in main memory
• Modify bit is needed to indicate if the page has been altered since it was last
loaded into main memory
• If no change has been made, the page does not have to be written to the
disk when it needs to be swapped out
304
Locality of Reference
Locality of reference, also known as the principle of locality, is the tendency of a processor to access
the same set of memory locations repetitively over a short period of time. There are two basic types
of reference locality – temporal and spatial locality.
Locality of reference is accessing a value or related storage frequently. And the types are temporal,
spatial and sequential.
Temporal: It tells us whether memory locations in a program are likely to be accessed again in the
near future.
Locality of reference refers to a phenomenon in which a computer program tends to access same set
of memory locations for a particular time period. In other words, Locality of Reference refers to the
tendency of the computer program to access instructions whose addresses are near one another.
The property of locality of reference is mainly shown by loops and subroutine calls in a program.
305
306
1. In case of loops in program control processing unit repeatedly refers to the
set of instructions that constitute the loop.
3. References to data items also get localized that means same data item is
referenced again and again.
307
308
309
In the above figure, you can see that the CPU wants to read or fetch the data or
instruction. First, it will access the cache memory as it is near to it and provides very
fast access. If the required data or instruction is found, it will be fetched. This
situation is known as a cache hit. But if the required data or instruction is not found
in the cache memory then this situation is known as a cache miss. Now the main
memory will be searched for the required data or instruction that was being
searched and if found will go through one of the two ways:
1. First way is that the CPU should fetch the required data or instruction and use it
and that’s it but what, when the same data or instruction is required again.CPU again
has to access the same main memory location for it and we already know that main
memory is the slowest to access.
2. The second way is to store the data or instruction in the cache memory so that if it
is needed soon again in the near future it could be fetched in a much faster way.
310
Cache Operation:
It is based on the principle of locality of reference. There are two ways with which data or instruction is fetched
from main memory and get stored in cache memory. These two ways are the following:
1. Temporal Locality –
Temporal locality means current data or instruction that is being fetched may be needed soon. So we should
store that data or instruction in the cache memory so that we can avoid again searching in main memory for the
same data. When CPU accesses the current main memory location for reading required data or instruction, it
also gets stored in the cache memory which is based on the fact that same data or instruction may be needed in
near future. This is known as temporal locality. If some data is referenced, then there is a high probability that it
will be referenced again in the near future.
2. Spatial Locality –
Spatial locality means instruction or data near to the current memory location that is being fetched, may be
needed soon in the near future. This is slightly different from the temporal locality. Here we are talking about
nearly located memory locations while in temporal locality we were talking about the actual memory location
that was being fetched.
311
Cache Performance:
The performance of the cache is measured in terms of hit ratio. When CPU
refers to memory and find the data or instruction within the Cache Memory, it
is known as cache hit. If the desired data or instruction is not found in the
cache memory and CPU refers to the main memory to find that data or
instruction, it is known as a cache miss.
312
Demand Paging
According to the concept of Virtual Memory, in order to execute some process, only a part of the process needs
to be present in the main memory which means that only a few pages will only be present in the main memory
at any time. However, deciding, which pages need to be kept in the main memory and which need to be kept in
the secondary memory, is going to be difficult because we cannot say in advance that a process will require a
particular page at particular time. Therefore, to overcome this problem, there is a concept called Demand
Paging is introduced. It suggests keeping all pages of the frames in the secondary memory until they are
required. In other words, it says that do not load any page in the main memory until it is required. Whenever
any page is referred for the first time in the main memory, then that page will be found in the secondary
memory.
In computer operating systems, demand paging (as opposed to anticipatory paging) is a method of virtual
memory management. In a system that uses demand paging, the operating system copies a disk page into
physical memory only if an attempt is made to access it and that page is not already in memory (i.e., if a page
fault occurs). It follows that a process begins execution with none of its pages in physical memory, and many
page faults will occur until most of a process's working set of pages are located in physical memory. This is an
example of a lazy loading technique.
313
Basic concept
Demand paging follows that pages should only be brought into memory if the executing process demands them. This is often referred to as
lazy evaluation as only those pages demanded by the process are swapped from secondary storage to main memory. Contrast this to pure
swapping, where all memory for a process is swapped from secondary storage to main memory during the process startup.
Commonly, to achieve this process a page table implementation is used. The page table maps logical memory to physical memory. The page
table uses a bitwise operator to mark if a page is valid or invalid. A valid page is one that currently resides in main memory. An invalid page is
one that currently resides in secondary memory. When a process tries to access a page, the following steps are generally followed:
Check if the memory reference is a valid reference to a location on secondary memory. If not, the process is terminated (illegal memory
access). Otherwise, we have to page in the required page.
Schedule disk operation to read the desired page into main memory.
Restart the instruction that was interrupted by the operating system trap.
314
Advantages
As there is more space in main memory, more processes can be loaded, reducing the context switching time,
which utilizes large amounts of resources.
Less loading latency occurs at program startup, as less information is accessed from secondary storage and less
information is brought into main memory.
As main memory is expensive compared to secondary memory, this technique helps significantly reduce the bill
of material (BOM) cost in smart phones for example. Symbian OS had this feature.
Disadvantages
Individual programs face extra latency when they access a page for the first time.
Low-cost, low-power embedded systems may not have a memory management unit that supports page
replacement.
Memory management with page replacement algorithms becomes slightly more complex.
315
Page replacement algorithms
are the techniques using which an Operating System decides which memory pages to swap out, write
to disk when a page of memory needs to be allocated.
In an operating system that uses paging for memory management, a page replacement algorithm is
needed to decide which page needs to be replaced when new page comes in.
Page Fault – A page fault happens when a running program accesses a memory page that is mapped
into the virtual address space, but not loaded in physical memory. Since actual physical memory is
much smaller than virtual memory, page faults happen. In case of page fault,
Operating System might have to replace one of the existing pages with the newly needed page.
Different page replacement algorithms suggest different ways to decide which page to replace. The
target for all algorithms is to reduce the number of page faults.
316
Page replacement algorithms:
First In First Out (FIFO) –
This is the simplest page replacement algorithm. In this algorithm, the operating system
keeps track of all pages in the memory in a queue, the oldest page is in the front of the
queue. When a page needs to be replaced page in the front of the queue is selected for
removal.
Optimal Page replacement –
In this algorithm, pages are replaced which would not be used for the longest duration of
time in the future.
Least Recently Used –
In this algorithm page will be replaced which is least recently used.
317