0% found this document useful (0 votes)
6 views

OS

The document discusses various memory management techniques, including paging and segmentation, and their roles in virtual memory. It also addresses synchronization problems such as the critical-section problem, readers-writers problem, and dining philosophers problem, along with deadlocks and their conditions. Additionally, it covers CPU scheduling algorithms like FCFS, SJF, priority scheduling, and round-robin scheduling, highlighting their characteristics and performance metrics.

Uploaded by

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

OS

The document discusses various memory management techniques, including paging and segmentation, and their roles in virtual memory. It also addresses synchronization problems such as the critical-section problem, readers-writers problem, and dining philosophers problem, along with deadlocks and their conditions. Additionally, it covers CPU scheduling algorithms like FCFS, SJF, priority scheduling, and round-robin scheduling, highlighting their characteristics and performance metrics.

Uploaded by

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

paging

Paging is a function of memory management where a computer


will store and retrieve data from a device's secondary storage
to the primary storage. Memory management is a crucial
aspect of any computing device, and paging specifically is
important to the implementation of virtual
memory. The main idea behind the paging is to divide each
process in the form of pages. The main memory will also be
divided in the form of frames.
segmentation
Segmentation is a memory management technique in
which each job is divided into several segments of
different sizes, one for each module that contains pieces
that perform related functions. Each segment is actually a
different logical address space of the program.
Segmentation can be divided into two types namely -
Virtual Memory Segmentation and Simple
Segmentation. A Segment Table is used to store the
information of all segments of the currently executing
process. The swapping of the segments of the process
results in the breaking of the free memory space into small
pieces.
page replacement algorithm.
page replacement algorithm decides which memory page
is to be replaced. The process of replacement is
sometimes called swap out or write to disk. Page
replacement is done when therequested page is not found
in the main memory (page fault). There are two main
aspects of virtual memory, Frame allocation and Page
Replacement. It is very important to have the optimal
frame allocation and page replacement algorithm. Frame
allocation is all about how many frames are to be
allocated to the process while the page replacement is all
about determining the page number which needs to be
replaced in order to make space for the requested page.
The Critical-Section Problem
Processes that are working together often share some common
storage that one can read and write. The shared storage may
be in main memory or it may be a shared file. Each process has
segment of code, called a critical section, which accesses
shared memory or files. The key issue involving shared memory
or shared files is to find way to prohibit more than one process
from reading and writing the shared data at the same
time. The important feature of the system is that, when one
process is executing in its critical section, no other process is to
be allowed to execute in its critical section. That is, no two
processes are executing in their critical sections at the same
time. The critical section problem is to design a protocol that
the processes can use to cooperate. Each process must request
permission to enter its critical section. The section of code
implementing this request is the entry section. The critical
section may be followed by an exit section. The remaining code
is the remainder section.
The Readers–Writers Problem
Suppose that a database is to be shared among several
concurrent processes. Some of these processes may want only
to read the database, whereas others may want to update (that
is, to read and write) the database. To distinguish between
these two types of processes by referring to the former as
readers and to the latter as writers. Obviously, if two readers
access the shared data simultaneously, no adverse effects will
result. However, if a writer and some other process (either a
reader or a writer) access the database simultaneously, chaos
may ensue. To ensure that these difficulties do not
arise, it is required to the writers have exclusive access to the
shared database while writing to the database. This
synchronization problem is referred to as the readers– writers.
Dining Philosophers Problem
The dining philosophers problem is another classic
synchronization problem which is used to evaluate situations
where there is a need of allocating multiple resources to
multiple processes. At any instant, a philosopher is either
eating or thinking. When a philosopher wants to
eat, he uses two chopsticks - one from their left and one from
their right. When a philosopher wants to think, he keeps down
both chopsticks at their original place. From the problem
statement, it is clear that a philosopher can think for an
indefinite amount of time. But when a philosopher starts eating,
he has to stop at some point of time. The philosopher is in an
endless cycle of thinking and eating. When a philosopher wants
to eat the rice, he will wait for the chopstick at his left and
picks up that chopstick. Then he waits for the right chopstick to
be available, and then picks it too. After eating, he puts both
the chopsticks down. But if all five philosophers are hungry
simultaneously, and each of them pickup one chopstick, then a
deadlock situation occurs because they will be waiting for
another chopstick forever. The possible solutions for this are:
A philosopher must be allowed to pick up the chopsticks
only if both the left and right chopsticks are available.
Allow only four philosophers to sit at the table. That way, if
all the four philosophers pick up four chopsticks, there will be
one chopstick left on the table.
So, one philosopher can start eating and eventually, two
chopsticks will be available. In this way, deadlocks can be
avoided.
DEADLOCKS
In a multiprogramming environment, several processes may
compete for a fifinite number of resources. A process requests
resources; if the resources are not available at that time, the
process enters a waiting state. Sometimes, a waiting process is
never again able to change state, because the resources it has
requested are held by other waiting processes. This situation is
called a deadlock. A process must request a resource before
using it and must release the resource after using it. A process
may request as many resources as it requires to carry out its
designated task. Obviously, the number of resources requested
may not exceed the total number of resources available in the
system. In other words, a process cannot request three printers
if the system has only two. Under the normal mode of
operation, a process may utilize a resource in only the
following sequence:
1. Request: The process requests the resource. If the request
cannot be granted immediately(for example, if the resource is
being used by another process), then the requesting process
must wait until it can acquire the resource.
2. Use: The process can operate on the resource (for example,
if the resource is a printer, the process can print on the printer).
3. Release: The process releases the resource.
The request and release of resources may be system calls,
Examples are the request() and release() device, open() and
close() fifile, and allocate() and free() memory system
calls. Similarly, the request and release of semaphores can be
accomplished through the wait() and signal() operations on
semaphores or through acquire() and release() of a mutex lock.
For each use of a kernel-managed resource by a process or
thread, the operating system checks to make sure that the
process has requested and has been allocated the
resource. A system table records whether each resource is free
or allocated. For each resource that is allocated, the table also
records the process to which it is allocated. If a process
requests a resource that is currently allocated to another
process, it can be added to a queue of processes waiting for
this resource. A set of processes is in a deadlocked state when
every process in the set is waiting for an event that can be
caused only by another process in the set. The events with
which we are mainly concerned here are resource acquisition
and release. The resources may be either physical resources(for
example, printers, tape drives, memory space, and CPU cycles)
or logical resources (for example, semaphores, mutex locks,
and fifiles).However, other types of events may result in
deadlocks.
Conditions for deadlock
1.Mutual exclusion: At least one resource must be held in a
non sharable mode; that is, only one process at a time can use
the resource. If another process requests that resource, the
requesting process must be delayed until the resource has
been released.
2. Hold and wait: A process must be holding at least one
resource and waiting to acquire additional resources that are
currently being held by other processes.
3. No preemption: Resources cannot be preempted; that is, a
resource can be released only voluntarily by the process
holding it, after that process has completed its task.
4. Circular wait: A set {P0, P1, ..., Pn} of waiting processes must
exist such that P0 is waiting for a resource held by P1, P1 is
waiting for a resource held by P2,..., Pn−1 is waiting for a
resource held by Pn, and Pn is waiting for a resource held by P0.
THREADS
Threads are lightweight processes(LWP), is a basic unitof CPU
utilization. It comparises a thread ID, a program mcounter, a
register set, and a stack. They improve performance by
weakening the process abstraction. A process(heavy weight) is
one thread of control executing one program in one address
space. A thread may have multiple threads of control running
different parts of a program in one address space.
Because threads expose multitasking to the user (cheaply) they
are more powerful, but more complicated. An application
typically is implemented as a separate process with several
threads of control. A web browser might have one thread
display images or text while another thread retrieves data from
the network, for example. A word processor may have a
thread for displaying graphics, another thread for responding
to keystrokes from the user, and a third thread for performing
spelling and grammar checking in the background.

CPU SCHEDULE
Scheduling refers to a set of policies and mechanisms built into
the operating system that govern the order in which the work
to be done by a computer system is completed. A scheduler is
an OS module that selects the next job to be admitted into the
system and the next process to run. The primary objective of
scheduling is to optimize system performance in accordance
with the criteria deemed most by the system designer.
There are three types of schedulers in a complex operating
system.
i. Long term scheduler
ii. Short term scheduler
iii. Medium term scheduler
Scheduling Criteria
• CPU utilization: We want to keep the CPU as busy as possible.
Conceptually, CPU utilization can range from 0 to 100 percent.
In a real system, it should range from 40 percent (for a lightly
loaded system) to 90 percent (for a heavily loaded system).
• Throughput: If the CPU is busy executing processes, then
work is being done. One measure of work is the number of
processes that are completed per time unit, called
throughput. For long processes, this rate may be one process
per hour; for short transactions, it may be ten processes per
second.
• Turnaround time: From the point of view of a particular
process, the important criterion is how long it takes to execute
that process. The interval from the time of
submission of a process to the time of completion is the
turnaround time. Turnaround time is the sum of the periods
spent waiting to get into memory, waiting in the ready
queue, executing on the CPU, and doing I/O.
• Waiting time: The CPU-scheduling algorithm does not affect
the amount of time during which a process executes or does
I/O. It affects only the amount of time that a
process spends waiting in the ready queue. Waiting time is the
sum of the periods spent waiting in the ready queue.
• Response time: In an interactive system, turnaround time
may not be the best criterion. Often, a process can produce
some output fairly early and can continue computing new
results while previous results are being output to the user. Thus,
another measure is the time from the submission of a request
until the fifirst response is produced. This measure, called
response time, is the time it takes to start responding,
not the time it takes to output the response. The turnaround
time is generally limited by the speed of the output device.
SCHEDULING ALGORITHMS
First-Come, First-Served Scheduling
By far the simplest CPU-scheduling algorithm is the fifirst-come,
fifirst-served (FCFS) scheduling algorithm. With this scheme,
the process that requests the CPU fifirst is allocated the CPU
fifirst. The implementation of the FCFS policy is easily managed
with a FIFO queue. When a process enters the ready queue, its
PCB is linked onto the tail of the queue. When the CPU is free,
it is allocated to the process at the head of the queue.
The running process is then removed from the queue. The code
for FCFS scheduling is simple to write and understand. On the
negative side, the average waiting time under the FCFS policy is
often quite long. Consider the following set of processes that
arrive at time 0, with the length of the CPU burst given in
milliseconds:
Process Burst Time
P1 24
P2 3
P3 3
If the processes arrive in the order P1, P2, P3, and are served in
FCFS order, we get the result shown in the following Gantt
chart, which is a bar chart that illustrates a particular schedule,
including the start and fifinish times of each of the participating
processes:

Shortest-Job-First Scheduling
A different approach to CPU scheduling is the shortest-job-
fifirst (SJF)scheduling algorithm. This algorithm associates with
each process the length of the process’s next CPU burst. When
the CPU is available, it is assigned to the process that has the
smallest next CPU burst. If the next CPU bursts of two
processes are the same, FCFS scheduling is used to break the
tie. Note that a more appropriate term for this scheduling
method would be the shortest-next- CPU-burst algorithm,
because scheduling depends on the length of the next CPU
burst of a process, rather than its total length.
Process Burst Time
P1 6
P2 8
P3 7
P4 3

The waiting time is 3 milliseconds for process P1, 16


milliseconds for process P2, 9 milliseconds for process P3, and 0
milliseconds for process P4. Thus, the average waiting time is (3
+ 16 + 9 + 0)/4 = 7 milliseconds. By comparison, if we were
using the FCFS scheduling scheme, the average waiting time
would be 10.25 milliseconds.
The SJF algorithm can be either preemptive or non preemptive.
The choice arises when a new process arrives at the ready
queue while a previous process is still executing. The next CPU
burst of the newly arrived process may be shorter than what is
left of the currently executing process. A preemptive SJF
algorithm will preempt the currently executing process,
whereas a non preemptive SJF algorithm will allow the
currently running process to fifinish its CPU burst. Preemptive
SJF scheduling is sometimes called shortest-remaining-time-
fifirst scheduling.
Priority Scheduling
The SJF algorithm is a special case of the general priority-
scheduling algorithm. A priority is associated with each process,
and the CPU is allocated to the process with the highest priority.
Equal-priority processes are scheduled in FCFS order. An SJF
algorithm is simply a priority algorithm where the priority (p) is
the inverse of the (predicted) next CPU burst. The larger the
CPU burst, the lower the priority, and vice versa.
As an example, consider the following set of processes,
assumed to have arrived at time 0 in the order P1, P2, ··· , P5,
with the length of the CPU burst given in milliseconds:
Process Burst Time Priority
P1 10 3
P2 1 1
P3 2 4
P4 1 5
P5 5 2

A major problem with priority scheduling algorithms is


indefifinite blocking, or starvation. A process that is ready to
run but waiting for the CPU can be considered blocked. A
priority scheduling algorithm can leave some low- priority
processes waiting indefifinitely.
Round-Robin Scheduling
The round-robin (RR) scheduling algorithm is designed
especially for time- sharing systems. It is similar to FCFS
scheduling, but preemption is added to enable the system
to switch between processes. A small unit of time, called a time
quantum or time slice, is defifined. A time quantum is generally
from 10 to 100 milliseconds in length. The ready
queue is treated as a circular queue. The CPU scheduler goes
around the ready queue, allocating the CPU to each process
for a time interval of up to 1 time quantum. To implement RR
scheduling, we again treat the ready queue as a FIFO queue of
processes. New processes are added to the tail of
the ready queue. The CPU scheduler picks the fifirst process
from the ready queue,sets a timer to interrupt after 1 time
quantum, and dispatches the process.
Process Burst Time
P1 24
P2 3
P3 3

Multilevel Queue Scheduling


Another class of scheduling algorithms has been created for
situations in which processes are easily classifified into
different groups. For example, a common division is
made between foreground (interactive) processes and
background (batch) processes. These two types of processes
have different response-time requirements and so may
have different scheduling needs. In addition, foreground
processes may have priority (externally defifined) over
background processes. A multilevel 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. For example, separate queues
might be used for foreground and background processes. The
foreground queue might be scheduled by an RR algorithm,
while the background queue is scheduled by an FCFS algorithm.
In addition, there must be scheduling among the queues, which
is commonly implemented as fifixed-priority preemptive
scheduling.
Multilevel Feedback Queue Scheduling
Normally, when the multilevel queue scheduling algorithm is
used, processes are permanently assigned to a queue when
they enter the system. If there are separate
queues for foreground and background processes, for example,
processes do not move from one queue to the other, since
processes do not change their foreground or
background nature. This setup has the advantage of low
scheduling over head,but it is inflexible. The multilevel
feedback queue scheduling algorithm, in contrast, allows a
process to move between queues. The idea is to separate
processes according to the characteristics of their CPU bursts. If
a process uses too much CPU time, it will be moved to a lower-
priority queue. This scheme leaves I/O-bound and interactive
processes in the higher-priority queues. In addition, a process
that waits too long in a lower-priority queue may be moved to
a higher-priority queue. This form of aging prevents starvation.
Memory Management Unit(MMU)
An address generated by the CPU is commonly referred to as a
logical address, whereas an address seen by the memory
unit—that is, the one loaded into the memory
address register of the memory—is commonly referred to as a
physical address. The compile-time and load-time address-
binding methods generate identical logical and physical
addresses. However, the execution-time addresses binding
scheme results in differing logical and physical addresses. In
this case, we usually refer to the logical address as a virtual
address.The set of all logical addresses generated by a program
is a logical address space. The set of all physical addresses
corresponding to these logical addresses is a physical address
space. Thus, in the execution-time address binding scheme,
the logical and physical address spaces differ. The run-time
mapping from virtual to physical addresses is done by a
hardware device called the memorymanagement unit (MMU).
The base register is now called are location register.
Swapping
Swapping is the act of moving processes between memory and
a backing store. This is done to free up available memory.
Swapping is necessary when there are more processes than
available memory. A process can be swapped temporarily out
of memory to a backing store, and then brought back into
memory for continued execution.
Swapping is used to implement multiprogramming in system
with little hardware support for memory management.
Swapping is helpful in improving processor utilization in
partitioned memory environment. Consider a
multiprogramming environment with a round-robin CPU
scheduling algorithm. When a quantum (time-slice) expires, the
memory manger will start to swap out processes that just
finished, and to swap in another process to the
memory space that has been freed. In the meantime, the CPU
scheduler will allocate a time slice to some other process in
memory. Thus when each process finishes its quantum, it will
be swapped with another process.
Memory Allocation
 Fixed sized partition
One of the simplest methods for allocating memory is to divide
memory into several fixed-sized partitions. Each partition may
contain exactly one process. Thus, the degree of
multiprogramming is bound by the number of partitions. In this
multiple partition method, when a partition is free, a process is
selected from the input queue and is loaded into the free
partition. When the process terminates, the partition becomes
available for another process. 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.
FRAGMENTATION
As processes are loaded and removed from the memory, the
free memory space is broken into little pieces. No other
processes can be loaded to this free space because
of its smaller size and hence, it remains unused. This problem is
known as Fragmentation.

Both the fifirst-fifit and best-fifit strategies for memory


allocation suffer from external fragmentation. As processes are
loaded and removed from memory, the free memory
space is broken into little pieces. External fragmentation exists
when there is enough total memory space to satisfy are quest
but the available spaces are not contiguous:
storage is fragmented into a large number of small holes. This
fragmentation problem can be severe. Wasting of memory
between partitions, due to scattering of free space
into a number of discontinuous areas, is called External
Fragmentation.
SEGMENTATION
In the paging scheme, pages are of a fixed size an alternate
approach, called segmentation, divides the process’s address
space into a number of segments - each of variable size. A
program is a collection of segments. A segment is a logical unit
such as: main program, procedure, function, method, object,
local variables, global variables, common block, stack, symbol
table, arrays etc.
VIRTUALMEMORY
Virtual memory is a memory management technique that
allows the execution of processes that may not be completely
in main memory and do not require contiguous
memory allocation. The address space of virtual memory can
be larger than that physical memory.
The virtual memory abstraction is implemented by using
secondary storage to augment (increase) the processor’s main
memory. Data is transferred from secondary to main storage as
and when necessary and the data replaced is written back to
the secondary storage according to a predetermined
replacement algorithm. If the data swapped is designated a
fixed size, this swapping is called paging; if variable sizes are
permitted and the data is split along logical lines such as
subroutines or matrices, it is called segmentation.
THRASHING
Too much or over allocation of memory can lead to a serious
performance problem known as thrashing. Thrashing occurs
when all of the pages that are memory resident
are high-demand pages that will be referenced in the near
future. Thus, when a page fault occurs, the page that is
removed from memory will soon give rise to a new fault,
which in turn removes a page that will soon give rise to a new
fault . In a system that is thrashing, a high percentage of the
system’s resources is devoted to paging, and overall
CPU utilization an throughput drop dramatically.
Error Handling
An operating system that uses protected memory can guard
against many kinds of hardware and application errors, so that
a complete system failure is not the usual result of each minor
mechanical malfunction. Devices and I/O transfers can fail in
many ways, either for transient reasons, as when a network
becomes overloaded, or for “permanent ” reasons, as when a
disk controller becomes defective. Operating systems can often
compensate effectively for transient failures. For instance, a
disk read() failure results in a read() retry, and a network send()
error results in a resend() , if the protocol so specifies.
Unfortunately, if an important component experiences a
permanent failure, the operating system is unlikely to recover.
Disk Scheduling
Disk scheduling is done by operating systems to schedule I/O
requests arriving for the disk. Disk scheduling is also known as
I/O scheduling. Disk scheduling is important because:
Multiple I/O requests may arrive by different processes and
only one I/O request can be served at a time by the disk
controller. Thus other I/O requests need to wait in the waiting
queue and need to be scheduled.
Two or more request may be far from each other so can
result in greater disk arm movement.
Hard drives are one of the slowest parts of the computer
system and thus need to be accessed in an efficient manner.
Some of the important terms:
Seek Time
:Seek time is the time taken to locate the disk arm to a
specified track where the data is to be read or write. So the disk
scheduling algorithm that gives minimum average seek time is
better.
Rotational Latency:
Rotational Latency is the time taken by the desired sector of
disk to rotate into a position so that it can access the
read/write heads. So the disk scheduling algorithm that gives
minimum rotational latency is better.
Transfer Time:Transfer time is the time to transfer the data.
It depends on the rotating speed of the disk and number of
bytes to be transferred.
Disk Access Time:

Disk Response Time:


Response Time is the average of time spent by a request
waiting to perform its I/O operation. Average Response time is
the response time of the all requests. Variance Response Time
is measure of how individual request are serviced
with respect to average response time. So the disk scheduling
algorithm that gives minimum variance response time is better.
1.FCFS Scheduling
The simplest form of disk scheduling is, the first-come, first-
served (FCFS) algorithm. This algorithm is fair, but it generally
does not provide the fastest service. Consider, for example, a
disk queue with requests for I/O to blocks on cylinders
98, 183, 37, 122, 14, 124, 65, 67. If the disk head is initially at
cylinder 53, it will first move from 53 to 98, then to 183, 37, 122,
14, 124, 65, and finally to 67, for a total head movement of 640
cylinders.
Advantages:
Every request gets a fair chance
No indefinite postponement
Disadvantages:
Does not try to optimize seek time
May not provide the best possible service
2. SSTF Scheduling
It service all the requests close to the current head position
before moving the head far away to service other requests. The
shortest-seek-time-first (SSTF) algorithm selects the request
with the least seek time from the current head position.
Consider the request queue, 98, 183, 37, 122, 14, 124, 65, 67
the closest request to the initial head position (53) is at cylinder
65. Once we are at cylinder 65, the next closest request is at
cylinder 67. From there, the request at cylinder 37 is closer
than the one at 98, so 37 is served next. Continuing, we service
the request at cylinder 14, then 98, 122, 124, and finally
183 .This scheduling method results in a total head movement
of only 236 cylinders .(65 67 37 14 98 122 124 183)

Advantages:
Average Response Time decreases
Throughput increases
Disadvantages:
Overhead to calculate seek time in advance Can cause
Starvation for a request if it has higher seek time as compared
to incoming requests
High variance of response time as SSTF favours only some
requests
SCAN Scheduling
In the SCAN algorithm, the disk arm starts at one end of the
disk and moves toward the other end, servicing requests as it
reaches each cylinder, until it gets to the other end of the disk.
At the other end, the direction of head movement is reversed,
and servicing continues. The head continuously scans back and
forth across the disk. The SCAN algorithm is sometimes called
the elevator algorithm , since the disk arm behaves just like an
elevator in a building, first servicing all the requests going up
and then reversing to service requests the other way.
Advantages:
High throughput
Low variance of response time
Average response time
Disadvantages:
Long waiting time for requests for locations just visited by
disk arm .
4.C-SCAN Scheduling
Circular SCAN (C-SCAN) scheduling is a variant of SCAN
designed to provide a more uniform wait time. Like SCAN,C-
SCAN moves the head from one end of the disk to the other,
servicing requests along the way. When the head reaches the
other end, however, it immediately returns
to the beginning of the disk without servicing any requests on
the return trip .The C-SCAN scheduling algorithm essentially
treats the cylinders as a circular list that wraps around from the
final cylinder to the first one.
parity
Parity is an interesting method used to rebuild data in case of
failure of one of the disks. Although its interesting to
understand, how parity works, you will find less documentation
about it on the internet. Parity makes use of a very famous
mathematical binary operation called as "XOR"

You might also like