gecnilokheri.ac.ingecnilokheri.ac.in/GPContent/Operating System UNIT 3r… · Web viewSegmentation...

83
TOPIC Deadlock in Operating System: Deadlock is a situation where a set of processes are blocked because each process is holding a resource and waiting for another resource acquired by some other process. Consider an example when two trains are coming toward each other on same track and there is only one track, none of the trains can move once they are in front of each other. Similar situation occurs in operating systems when there are two or more processes hold some resources and wait for resources held by other(s). For example, in the below diagram, Process 1 is holding Resource 1 and waiting for resource 2 which is acquired by process 2, and process 2 is waiting for resource 1.

Transcript of gecnilokheri.ac.ingecnilokheri.ac.in/GPContent/Operating System UNIT 3r… · Web viewSegmentation...

Page 1: gecnilokheri.ac.ingecnilokheri.ac.in/GPContent/Operating System UNIT 3r… · Web viewSegmentation memory management works very similar to paging but here segments are of variable-length

TOPIC Deadlock in Operating System:

Deadlock is a situation where a set of processes are blocked because each process is holding a resource and waiting for another resource acquired by some other process.Consider an example when two trains are coming toward each other on same track and there is only one track, none of the trains can move once they are in front of each other. Similar situation occurs in operating systems when there are two or more processes hold some resources and wait for resources held by other(s). For example, in the below diagram, Process 1 is holding Resource 1 and waiting for resource 2 which is acquired by process 2, and process 2 is waiting for resource 1.

Page 2: gecnilokheri.ac.ingecnilokheri.ac.in/GPContent/Operating System UNIT 3r… · Web viewSegmentation memory management works very similar to paging but here segments are of variable-length

In an operating system, a deadlock is the occurs when a process enters into a waiting state because a resource request is being made by the other waiting process, which in turn become a waiting status for the other resource. When the process is unable to change its state indefinitely because the resources requested by it are being used by another waiting process, then the system in a deadlock state.So this was a general description about deadlock, technically we can define deadlock as a state where certain process rather we can say the inter related process wait for the resources which is used by the other process so the state where every process waits for the resources is called as the deadlock.

A process in operating systems uses different resources and uses resources in following way.1) Requests a resource2) Use the resource2) Releases the resource

Page 3: gecnilokheri.ac.ingecnilokheri.ac.in/GPContent/Operating System UNIT 3r… · Web viewSegmentation memory management works very similar to paging but here segments are of variable-length

So there are certain conditions due to which in a system deadlock occur

Mutual ExclusionThere should be a resource that can only be held by one process at a time. In the diagram below, there is a single instance of Resource 1 and it is held by Process 1 only.

Page 4: gecnilokheri.ac.ingecnilokheri.ac.in/GPContent/Operating System UNIT 3r… · Web viewSegmentation memory management works very similar to paging but here segments are of variable-length

Hold and WaitA process can hold multiple resources and still request more resources from other processes which are holding them. In the diagram given below, Process 2 holds Resource 2 and Resource 3 and is requesting the Resource 1 which is held by Process 1.

No PreemptionA resource cannot be preempted from a process by force. A process can only release a resource voluntarily. In the diagram below, Process 2 cannot preempt Resource 1 from Process 1. It will only be released when Process 1 relinquishes it voluntarily after its execution is complete.

Page 5: gecnilokheri.ac.ingecnilokheri.ac.in/GPContent/Operating System UNIT 3r… · Web viewSegmentation memory management works very similar to paging but here segments are of variable-length

Circular WaitA process is waiting for the resource held by the second process, which is waiting for the resource held by the third process and so on, till the last process is waiting for a resource held by the first process. This forms a circular chain. For example: Process 1 is allocated Resource2 and it is requesting Resource 1. Similarly, Process 2 is allocated Resource 1 and it is requesting Resource 2. This forms a circular wait loop.

Page 6: gecnilokheri.ac.ingecnilokheri.ac.in/GPContent/Operating System UNIT 3r… · Web viewSegmentation memory management works very similar to paging but here segments are of variable-length

Deadlock HandlingMost of the operating systems are not able to prevent the deadlocks to occur. When the condition of deadlock occurs then different operating systems respond to them in different and non-standard way. Most approaches work by preventing one of the four Coffman conditions from occurring, especially for the Current Wait. Major approaches are

1) Ignoring Deadlock :This approach assumed that a deadlock will never occur which is one of the most important application of Ostrich algorithm. The Ignoring deadlock is commonly used by MINIX and UNIX. When the time interval between the occurrences of deadlocks are seems to be large in addition to the increment in data loss incurred each time is tolerable then in such case Ignoring deadlock is used.

2) Detection :In this approach, deadlocks are allowed to occur. Then the state of the system is examined to detect that a deadlock has occurred and subsequently it is corrected. In this case, algorithm is employed that tracks resource allocation and process states, it rolls back and restarts one or more of the processes in order to remove the detected deadlock.

Deadlock Avoidance:Deadlock can be avoided if some of the information about the processes is well known by the Operating System before the

Page 7: gecnilokheri.ac.ingecnilokheri.ac.in/GPContent/Operating System UNIT 3r… · Web viewSegmentation memory management works very similar to paging but here segments are of variable-length

allocation of resources starts. For every resource request, the system sees whether granting the request will mean that the system will enter an unsafe state, meaning a state that could result in deadlock. The system then only grants requests that will lead to safe states.

Deadlock Prevention

 Deadlock PreventionWe can prevent Deadlock by eliminating any of the above four conditions.

Eliminate Mutual ExclusionIt is not possible to dis-satisfy the mutual exclusion because some resources, such as the tape drive and printer, are inherently non-shareable. Eliminate Hold and wait1. Allocate all required resources to the process before the start

of its execution, this way hold and wait condition is eliminated but it will lead to low device utilization. for example, if a process requires printer at a later time and we have allocated printer before the start of its execution printer will remain blocked till it has completed its execution.

2. The process will make a new request for resources after releasing the current set of resources. This solution may lead to starvation.

Page 8: gecnilokheri.ac.ingecnilokheri.ac.in/GPContent/Operating System UNIT 3r… · Web viewSegmentation memory management works very similar to paging but here segments are of variable-length

Eliminate No PreemptionPreempt resources from the process when resources required by other high priority processes. Eliminate Circular WaitEach resource will be assigned with a numerical number. A process can request the resources increasing/decreasing. order of numbering.For Example, if P1 process is allocated R5 resources, now next time if P1 ask for R4, R3 lesser than R5 such request will not be granted, only request for resources more than R5 will be granted.  Deadlock AvoidanceDeadlock avoidance can be done with Banker’s Algorithm.

Banker’s AlgorithmBankers’s Algorithm is resource allocation and deadlock avoidance algorithm which test all the request made by processes for resources, it checks for the safe state, if after

Page 9: gecnilokheri.ac.ingecnilokheri.ac.in/GPContent/Operating System UNIT 3r… · Web viewSegmentation memory management works very similar to paging but here segments are of variable-length

granting request system remains in the safe state it allows the request and if there is no safe state it doesn’t allow the request made by the process.Inputs to Banker’s Algorithm:1. Max need of resources by each process.2. Currently allocated resources by each process.3. Max free available resources in the system.

The request will only be granted under the below condition:1. If the request made by the process is less than equal to max

need to that process.2. If the request made by the process is less than equal to the

freely available resource in the system.Example:

Total resources in system:

A B C D

6 5 7 6

Available system resources are:

A B C D

3 1 1 2

Processes (currently allocated resources):

A B C D

P1 1 2 2 1

P2 1 0 3 3

P3 1 2 1 0

Processes (maximum resources):

A B C D

P1 3 3 2 2

P2 1 2 3 4

P3 1 3 5 0

Page 10: gecnilokheri.ac.ingecnilokheri.ac.in/GPContent/Operating System UNIT 3r… · Web viewSegmentation memory management works very similar to paging but here segments are of variable-length

Need = maximum resources - currently allocated resources.

Processes (need resources):

A B C D

P1 2 1 0 1

P2 0 2 0 1

P3 0 1 4 0

Recovery from Deadlock in Operating SystemWhen a Deadlock Detection Algorithm determines that a deadlock has occurred in the system, the system must recover from that deadlock. There are two approaches of breaking a Deadlock:

1. Process Termination:To eliminate the deadlock, we can simply kill one or more processes. For this, we use two methods: (a). Abort all the Deadlocked Processes:

Aborting all the processes will certainly break the deadlock, but with a great expenses. The deadlocked processes may have computed for a long time and the result of those partial computations must be discarded and there is a probability to recalculate them later.

b). Abort one process at a time untill deadlock is eliminated:Abort one deadlocked process at a time, untill deadlock cycle is eliminated from the system. Due to this method, there may be considerable overhead, because after aborting

Page 11: gecnilokheri.ac.ingecnilokheri.ac.in/GPContent/Operating System UNIT 3r… · Web viewSegmentation memory management works very similar to paging but here segments are of variable-length

each process, we have to run deadlock detection algorithm to check whether any processes are still deadlocked.

2. Resource Preemption:To eliminate deadlocks using resource preemption, we preepmt some resources from processes and give those resources to other processes. This method will raise three issues – (a). Selecting a victim:

We must determine which resources and which processes are to be preempted and also the order to minimize the cost.

(b). Rollback:We must determine what should be done with the process from which resources are preempted. One simple idea is total rollback. That means abort the process and restart it.

(c). Starvation:In a system, it may happen that same process is always picked as a victim. As a result, that process will never complete its designated task. This situation is called Starvation and must be avoided. One solution is that a process must be picked as a victim only a finite number of times.

Page 12: gecnilokheri.ac.ingecnilokheri.ac.in/GPContent/Operating System UNIT 3r… · Web viewSegmentation memory management works very similar to paging but here segments are of variable-length

TOPIC MEMORY MANAGEMENT Memory management is the functionality of an operating system which handles or manages primary memory and moves processes back and forth between main memory and disk during execution. Memory management keeps track of each and every memory location, regardless of either it is allocated to some process or it is free. It checks how much memory is to be allocated to processes. It decides which process will get memory at what time. It tracks whenever some memory gets freed or unallocated and correspondingly it updates the status.ORMain Memory refers to a physical memory that is the internal memory to the computer. The word main is used to distinguish it from external mass storage devices such as disk drives. Main memory is also known as RAM. The computer is able to change only data that is in main memory. Therefore, every program we execute and every file we access must be copied from a storage device into main memory.All the programs are loaded in the main memeory for execution. Sometimes complete program is loaded into the memory, but some times a certain part or routine of the program is loaded into the main memory only when it is called by the program, this mechanism is called Dynamic Loading, this enhance the performance.

Page 13: gecnilokheri.ac.ingecnilokheri.ac.in/GPContent/Operating System UNIT 3r… · Web viewSegmentation memory management works very similar to paging but here segments are of variable-length

Also, at times one program is dependent on some other program. In such a case, rather than loading all the dependent programs, CPU links the dependent programs to the main executing program when its required. This mechanism is known as Dynamic Linking.

Memory AllocationMemory allocation is a process by which computer programs are assigned memory or space. It is of three types :

1. First Fit:

The first hole that is big enough is allocated to program.

2. Best Fit:

The smallest hole that is big enough is allocated to program.

3. Worst Fit:

The largest hole that is big enough is allocated to program.

Logical and Physical Address in Operating System

Logical Address is generated by CPU while a program is running. The logical address is virtual address as it does not exist physically, therefore, it is also known as Virtual Address. This address is used as a reference to access the physical

Page 14: gecnilokheri.ac.ingecnilokheri.ac.in/GPContent/Operating System UNIT 3r… · Web viewSegmentation memory management works very similar to paging but here segments are of variable-length

memory location by CPU. The term Logical Address Space is used for the set of all logical addresses generated by a program’s perspective.The hardware device called Memory-Management Unit is used for mapping logical address to its corresponding physical address.

Physical Address identifies a physical location of required data in a memory. The user never directly deals with the physical address but can access by its corresponding logical address. The user program generates the logical address and thinks that the program is running in this logical address but the program needs physical memory for its execution, therefore, the logical address must be mapped to the physical address by MMU before they are used. The term Physical Address Space is used for all physical addresses corresponding to the logical addresses in a Logical address space.

Page 15: gecnilokheri.ac.ingecnilokheri.ac.in/GPContent/Operating System UNIT 3r… · Web viewSegmentation memory management works very similar to paging but here segments are of variable-length

Differences Between Logical and Physical Address in Operating System1. The basic difference between Logical and physical address is

that Logical address is generated by CPU in perspective of a program whereas the physical address is a location that exists in the memory unit.

2. Logical Address Space is the set of all logical addresses generated by CPU for a program whereas the set of all physical address mapped to corresponding logical addresses is called Physical Address Space.

3. The logical address does not exist physically in the memory whereas physical address is a location in the memory that can be accessed physically.

4. Identical logical addresses are generated by Compile-time and Load time address binding methods whereas they differs

Page 16: gecnilokheri.ac.ingecnilokheri.ac.in/GPContent/Operating System UNIT 3r… · Web viewSegmentation memory management works very similar to paging but here segments are of variable-length

from each other in run-time address binding method. Please refer this for details.

5. The logical address is generated by the CPU while the program is running whereas the physical address is computed by the Memory Management Unit (MMU).

Comparison Chart:Comparison Chart:

PARAMENTER LOGICAL ADDRESS PHYSICAL ADDRESS

Basic generated by CPU location in a memory unit

Address

Space

Logical Address Space is set

of all logical addresses

generated by CPU in reference

to a program.

Physical Address is set of all

physical addresses mapped to

the corresponding logical

addresses.

Visibility

User can view the logical

address of a program.

User can never view physical

address of program.

Generation generated by the CPU Computed by MMU

Access

The user can use the

logical address to access

the physical address.

The user can indirectly

access physical address but

not directly.

Translating Logical Address into Physical Address- 

Page 17: gecnilokheri.ac.ingecnilokheri.ac.in/GPContent/Operating System UNIT 3r… · Web viewSegmentation memory management works very similar to paging but here segments are of variable-length

CPU always generates a logical address. A physical address is needed to access the main memory. 

Following steps are followed to translate logical address into physical address-

 

Step-01: 

CPU generates a logical address consisting of two parts-

1. Page Number2. Page Offset 

Page Number specifies the specific page of the process from which CPU wants to read the data.

Page Offset specifies the specific word on the page that CPU wants to read.

 

Step-02: 

For the page number generated by the CPU,

Page 18: gecnilokheri.ac.ingecnilokheri.ac.in/GPContent/Operating System UNIT 3r… · Web viewSegmentation memory management works very similar to paging but here segments are of variable-length

Page Table  provides the corresponding frame number (base address of the frame) where that page is stored in the main memory.

 

Step-03: 

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

 

Diagram- 

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

 

Page 19: gecnilokheri.ac.ingecnilokheri.ac.in/GPContent/Operating System UNIT 3r… · Web viewSegmentation memory management works very similar to paging but here segments are of variable-length

The different types of memory management techniques and also the pros and cons of different memory management techniques.

Memory management

Memory is central to the operation of a computer system. It consists of a large array of words or bytes each with its own address. In uniprogramming system, main memory has two parts one for the operating system and another part is for the program currently being executed. In the multiprogramming system, the memory part of the user is further divided into accommodate processes. The task of the subdivision is cannot out by the operating system and is known as memory management.

Memory management techniques

Page 20: gecnilokheri.ac.ingecnilokheri.ac.in/GPContent/Operating System UNIT 3r… · Web viewSegmentation memory management works very similar to paging but here segments are of variable-length

The memory management techniques are divided into two parts...

1. Uniprogramming:In the uniprogramming technique, the RAM is divided into two parts one part is for the resigning the operating system and other portion is for the user process. Here the fence register is used which contain the last address of the operating system parts. The operating system will compare the user data addresses with the fence register and if it is different that means the user is not entering in the OS area. Fence register is also called boundary register and is used to prevent a user from entering in the operating system area. Here the CPU utilization is very poor and hence multiprogramming is used.

2. Multiprogramming:In the multiprogramming, the multiple users can share the memory simultaneously. By multiprogramming we mean there will be more than one process in the main memory and if the running process wants to wait for an event like I/O then instead of sitting ideal CPU will make a context switch and will pick another process.

a. Contiguous memory allocationb. Non-contiguous memory allocation

a) Contiguous memory allocationContiguous memory allocation is a memory allocation method that allocates a single contiguous section of memory to a process or a file. This method takes into account the size of the file or

Page 21: gecnilokheri.ac.ingecnilokheri.ac.in/GPContent/Operating System UNIT 3r… · Web viewSegmentation memory management works very similar to paging but here segments are of variable-length

a process and also estimates the maximum size, up to what the file or process can grow?

In contiguous memory allocation, all the available memory space remain together in one place. 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. 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.

Page 22: gecnilokheri.ac.ingecnilokheri.ac.in/GPContent/Operating System UNIT 3r… · Web viewSegmentation memory management works very similar to paging but here segments are of variable-length
Page 23: gecnilokheri.ac.ingecnilokheri.ac.in/GPContent/Operating System UNIT 3r… · Web viewSegmentation memory management works very similar to paging but here segments are of variable-length

→ 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

→ 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.

Advantage: There is no internal fragmentation.

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

b) Non-contiguous memory allocation

In the non-contiguous memory allocation the available free memory space are scattered here and there and all the free memory space is not at one place. So this is time-consuming. In the non-contiguous memory allocation, a process will acquire the memory space but it is not at one place it is at the different locations according to the process requirement. This technique of non-contiguous memory allocation reduces the wastage of memory which leads to internal and external fragmentation. This

Page 24: gecnilokheri.ac.ingecnilokheri.ac.in/GPContent/Operating System UNIT 3r… · Web viewSegmentation memory management works very similar to paging but here segments are of variable-length

utilizes all the free memory space which is created by a different process.

Non-contiguous memory allocation is of different types,

1. Paging2. Segmentation3. Segmentation with paging

Page 25: gecnilokheri.ac.ingecnilokheri.ac.in/GPContent/Operating System UNIT 3r… · Web viewSegmentation memory management works very similar to paging but here segments are of variable-length

i) Paging

ii)A non-contiguous policy with a fixed size partition is called paging. A computer can address more memory than the amount of physically installed on the system. This extra memory is actually called virtual memory. Paging technique is very important in implementing virtual memory. Secondary memory is divided into equal size partition (fixed) called pages. Every process will have a separate page table. The entries in the page table are the number of pages a process. At each entry either we have an invalid pointer which means the page is not in main memory or we will get the corresponding frame number. When the frame number is combined with instruction of set D than we will get the corresponding physical address. Size of a page table is generally very large so cannot be accommodated inside the PCB, therefore, PCB contains a register value PTBR( page table base register) which leads to the page table.

A computer can address more memory than the amount physically installed on the system. This extra memory is actually called virtual memory and it is a section of a hard that's set up to emulate the computer's RAM. Paging technique plays an important role in implementing virtual memory.

Page 26: gecnilokheri.ac.ingecnilokheri.ac.in/GPContent/Operating System UNIT 3r… · Web viewSegmentation memory management works very similar to paging but here segments are of variable-length

Paging is a memory management technique in which process address space is broken into blocks of the same size called pages (size is power of 2, between 512 bytes and 8192 bytes). The size of the process is measured in the number of pages.Similarly, main memory is divided into small fixed-sized blocks of (physical) memory called frames and the size of a frame is kept the same as that of a page to have optimum utilization of the main memory and to avoid external fragmentation.

Advantages: It is independent of external fragmentation.

Disadvantages:

1. It makes the translation very slow as main memory access two times.

2. A page table is a burden over the system which occupies considerable space.

Page 27: gecnilokheri.ac.ingecnilokheri.ac.in/GPContent/Operating System UNIT 3r… · Web viewSegmentation memory management works very similar to paging but here segments are of variable-length

Address Translation

Page address is called logical address and represented by page number and the offset.Logical Address = Page number + page offsetFrame address is called physical address and represented by a frame number and the offset.Physical Address = Frame number + page offsetA data structure called page map table is used to keep track of the relation between a page of a process to a frame in physical memory.

Page 28: gecnilokheri.ac.ingecnilokheri.ac.in/GPContent/Operating System UNIT 3r… · Web viewSegmentation memory management works very similar to paging but here segments are of variable-length

The internal fragmentation can be reduced by effectively assigning the smallest partition but large enough for the process.

When the system allocates a frame to any page, it translates this logical address into a physical address and create entry into the page table to be used throughout execution of the program.When a process is to be executed, its corresponding pages are loaded into any available memory frames. Suppose you have a program of 8Kb but your memory can accommodate only 5Kb at a given point in time, then the paging concept will come into picture. When a computer runs out of RAM, the operating

Page 29: gecnilokheri.ac.ingecnilokheri.ac.in/GPContent/Operating System UNIT 3r… · Web viewSegmentation memory management works very similar to paging but here segments are of variable-length

system (OS) will move idle or unwanted pages of memory to secondary memory to free up RAM for other processes and brings them back when needed by the program.This process continues during the whole execution of the program where the OS keeps removing idle pages from the main memory and write them onto the secondary memory and bring them back when required by the program. OR

Advantages and Disadvantages of Paging

Here is a list of advantages and disadvantages of paging − Paging reduces external fragmentation, but still suffer from

internal fragmentation. Paging is simple to implement and assumed as an efficient

memory management technique. Due to equal size of the pages and frames, swapping

becomes very easy. Page table requires extra memory space, so may not be

good for a system having small RAM.

ii) SegmentationSegmentation- 

Like Paging, Segmentation is another non-contiguous memory allocation technique.

Page 30: gecnilokheri.ac.ingecnilokheri.ac.in/GPContent/Operating System UNIT 3r… · Web viewSegmentation memory management works very similar to paging but here segments are of variable-length

In segmentation, process is not divided blindly into fixed size pages.

Rather, the process is divided into modules for better visualization.

 

Characteristics- 

Segmentation is a variable size partitioning scheme. In segmentation, secondary memory and main memory are

divided into partitions of unequal size. The size of partitions depend on the length of modules. The partitions of secondary memory are called as segments. 

Example- 

Consider a program is divided into 5 segments as-

Page 31: gecnilokheri.ac.ingecnilokheri.ac.in/GPContent/Operating System UNIT 3r… · Web viewSegmentation memory management works very similar to paging but here segments are of variable-length

 Segment Table- 

Segment table is a table that stores the information about each segment of the process.

It has two columns. First column stores the size or length of the segment. Second column stores the base address or starting address of

the segment in the main memory. Segment table is stored as a separate segment in the main

memory. Segment table base register (STBR) stores the base address of

the segment table. 

Page 32: gecnilokheri.ac.ingecnilokheri.ac.in/GPContent/Operating System UNIT 3r… · Web viewSegmentation memory management works very similar to paging but here segments are of variable-length

For the above illustration, consider the segment table is-

 

 

Here,

Limit indicates the length or size of the segment. Base indicates the base address or starting address of the

segment in the main memory.

Page 33: gecnilokheri.ac.ingecnilokheri.ac.in/GPContent/Operating System UNIT 3r… · Web viewSegmentation memory management works very similar to paging but here segments are of variable-length

 

 

Page 34: gecnilokheri.ac.ingecnilokheri.ac.in/GPContent/Operating System UNIT 3r… · Web viewSegmentation memory management works very similar to paging but here segments are of variable-length

Segmentation is a programmer view of the memory where instead of dividing a process into equal size partition we divided according to program into partition called segments. The translation is the same as paging but paging segmentation is independent of internal fragmentation but suffers from external fragmentation. Reason of external fragmentation is program can be divided into segments but segment must be contiguous in nature.

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.When a process is to be executed, its corresponding segmentation are loaded into non-contiguous memory though every segment is loaded into a contiguous block of available memory.Segmentation memory management works very similar to paging but here segments are of variable-length where as in paging pages are of fixed size.A program segment contains the program's main function, utility functions, data structures, and so on. The operating system maintains a segment map table for every process and a list of free memory blocks along with segment numbers, their size and corresponding memory locations in main memory. For each segment, the table stores the starting address of the segment and the length of the segment. A reference to a memory location includes a value that identifies a segment and an offset.

Page 35: gecnilokheri.ac.ingecnilokheri.ac.in/GPContent/Operating System UNIT 3r… · Web viewSegmentation memory management works very similar to paging but here segments are of variable-length

Advantages of Segmentation – No Internal fragmentation. Segment Table consumes less space in comparison to Page

table in paging.Disadvantage of Segmentation – As processes are loaded and removed from the memory, the

free memory space is broken into little pieces, causing External fragmentation.

iii) Segmentation with paging

Page 36: gecnilokheri.ac.ingecnilokheri.ac.in/GPContent/Operating System UNIT 3r… · Web viewSegmentation memory management works very similar to paging but here segments are of variable-length

In segmentation with paging, we take advantages of both segmentation as well as paging. It is a kind of multilevel paging but in multilevel paging, we divide a page table into equal size partition but here in segmentation with paging, we divide it according to segments. All the properties are the same as that of paging because segments are divided into pages.

OR

Segmentation with PagingBoth paging and segmentation have their advantages and disadvantages, it is better to combine these two schemes to improve on each. The combined scheme is known as 'Page the Elements'. Each segment in this scheme is divided into pages and each segment is maintained in a page table. So the logical address is divided into following 3 parts :

Segment numbers(S) Page number (P) The displacement or offset number (D)

Fragmentation

As processes are loaded and removed from memory, the free memory space is broken into little pieces. It happens after sometimes that processes cannot be allocated to memory blocks considering their small size and memory blocks remains unused. This problem is known as Fragmentation.Fragmentation is of two types −

Page 37: gecnilokheri.ac.ingecnilokheri.ac.in/GPContent/Operating System UNIT 3r… · Web viewSegmentation memory management works very similar to paging but here segments are of variable-length

S.N. Fragmentation & Description

1 External fragmentationTotal memory space is enough to satisfy a request or to reside a process in it, but it is not contiguous, so it cannot be used.

2 Internal fragmentationMemory block assigned to process is bigger. Some portion of memory is left unused, as it cannot be used by another process.

The following diagram shows how fragmentation can cause waste of memory and a compaction technique can be used to create more free memory out of fragmented memory −

Page 38: gecnilokheri.ac.ingecnilokheri.ac.in/GPContent/Operating System UNIT 3r… · Web viewSegmentation memory management works very similar to paging but here segments are of variable-length

External fragmentation can be reduced by compaction or shuffle memory contents to place all free memory together in one large block. To make compaction feasible, relocation should be dynamic.

TOPIC VIRTUAL MEMORY

the concept of virtual memory comes into rescue when the size of the process is very much bigger than the size of the main memory and we are unable to load the entire process into the Main Memory. Therefore, we load a part of the process

Page 39: gecnilokheri.ac.ingecnilokheri.ac.in/GPContent/Operating System UNIT 3r… · Web viewSegmentation memory management works very similar to paging but here segments are of variable-length

and run to completion; once that part is over, pull it back and allocate the rest part.

In earlier days, the concept of virtual memory was used only when the size of the process>>the size of the main memory. But later, it is used to take the advantage of the degree of multiprogramming.

The Degree of multiprogramming - many processes or a part of many processes possible present in the main memory.

Virtual Memory has introduced many advantages in Operating Systems but not that easy to implement! In simpler words, main memory is divided into several frames and there are many processes to be allocated inside main memory; the main concept behind virtual memory is too see many processes or a part of as many processes possible present in the main memory.

Without the concept of Virtual Memory, several processes cannot share one resource at the same time. For example, several friends sharing food from the same lunchbox. Think “friends” as PROCESSES and“lunchbox”as MAIN MEMORY. Without the advantage of virtual memory, it’s like one person eating from the lunch box alone i.e., the entire process will be present in the main memory which can be run till completion, then move it out and allocate another process and the story goes on...

Page 40: gecnilokheri.ac.ingecnilokheri.ac.in/GPContent/Operating System UNIT 3r… · Web viewSegmentation memory management works very similar to paging but here segments are of variable-length

Virtual Memory is a storage allocation scheme in which secondary memory can be addressed as though it were part of main memory. The addresses a program may use to reference memory are distinguished from the addresses the memory system uses to identify physical storage sites, and program generated addresses are translated automatically to the corresponding machine addresses.

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.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

Page 41: gecnilokheri.ac.ingecnilokheri.ac.in/GPContent/Operating System UNIT 3r… · Web viewSegmentation memory management works very similar to paging but here segments are of variable-length

into memory whenever required. Virtual memory is implemented using Demand Paging or Demand Segmentation.

Demand Paging :The process of loading the page into memory on demand (whenever page fault occurs) is known as demand paging.The process includes the following steps :

1. If CPU try to refer a page that is currently not available in the main memory, it generates an interrupt indicating memory access fault.

Page 42: gecnilokheri.ac.ingecnilokheri.ac.in/GPContent/Operating System UNIT 3r… · Web viewSegmentation memory management works very similar to paging but here segments are of variable-length

2. The OS puts the interrupted process in a blocking state. For the execution to proceed the OS must bring the required page into the memory.

3. The OS will search for the required page in the logical address space.

4. The required page will be brought from logical address space to physical address space. The page replacement algorithms are used for the decision making of replacing the page in physical address space.

5. The page table will updated accordingly.6. The signal will be sent to the CPU to continue the program

execution and it will place the process back into ready state.Hence whenever a page fault occurs these steps are followed by the operating system and the required page is brought into memory.

Advantages : More processes may be maintained in the main memory:

Because we are going to load only some of the pages of any particular process, there is room for more processes. This leads to more efficient utilization of the processor because it is more likely that at least one of the more numerous processes will be in the ready state at any particular time.

A process may be larger than all of main memory: One of the most fundamental restrictions in programming is lifted. A process larger than the main memory can be executed because of demand paging. The OS itself loads pages of a process in main memory as required.

It allows greater multiprogramming levels by using less of the available (primary) memory for each process.

Page 43: gecnilokheri.ac.ingecnilokheri.ac.in/GPContent/Operating System UNIT 3r… · Web viewSegmentation memory management works very similar to paging but here segments are of variable-length

Page Fault Service Time :The time taken to service the page fault is called as page fault service time. The page fault service time includes the time taken to perform all the above six steps.Let Main memory access time is: mPage fault service time is: sPage fault rate is : pThen, Effective memory access time = (p*s) + (1-p)*m

 Swapping:Swapping a process out means removing all of its pages from memory, or marking them so that they will be removed by the normal page replacement process. Suspending a process ensures that it is not runnable while it is swapped out. At some later time, the system swaps back the process from the secondary storage to main memory. When a process is busy swapping pages in and out then this situation is called thrashing.

Page 44: gecnilokheri.ac.ingecnilokheri.ac.in/GPContent/Operating System UNIT 3r… · Web viewSegmentation memory management works very similar to paging but here segments are of variable-length
Page 45: gecnilokheri.ac.ingecnilokheri.ac.in/GPContent/Operating System UNIT 3r… · Web viewSegmentation memory management works very similar to paging but here segments are of variable-length

 

At any given time, only few pages of any process are in main memory and therefore more processes can be maintained in memory. Furthermore time is saved because unused pages are not swapped in and out of memory. However, the OS must be clever about how it manages this scheme. In the steady state practically, all of main memory will be occupied with process’s pages, so that the processor and OS has direct access to as many processes as possible. Thus when the OS brings one page in, it must throw another out. If it throws out a page just before it is used, then it will just have to get that page again almost immediately. Too much of this leads to a condition called Thrashing. The system spends most of its time swapping pages rather than executing instructions. So a good page replacement algorithm is required.

Page 46: gecnilokheri.ac.ingecnilokheri.ac.in/GPContent/Operating System UNIT 3r… · Web viewSegmentation memory management works very similar to paging but here segments are of variable-length

In the given diagram, initial degree of multi programming upto some extent of point(lamda), the CPU utilization is very high and the system resources are utilized 100%. But if we further increase the degree of multi programming the CPU utilization will drastically fall down and the system will spent more time only in the page replacement and the time taken to complete the execution of the process will increase. This situation in the system is called as thrashing.

Causes of Thrashing :1. High degree of multiprogramming : If the number of

processes keeps on increasing in the memory than number of frames allocated to each process will be decreased. So, less number of frames will be available to each process. Due to this, page fault will occur more frequently and more CPU time will be wasted in just swapping in and out of pages and the utilization will keep on decreasing.For example:Let free frames = 400Case 1: Number of process = 100Then, each process will get 4 frames.Case 2: Number of process = 400Each process will get 1 frame.Case 2 is a condition of thrashing, as the number of processes are increased,frames per process are decreased. Hence CPU time will be consumed in just swapping pages.

Page 47: gecnilokheri.ac.ingecnilokheri.ac.in/GPContent/Operating System UNIT 3r… · Web viewSegmentation memory management works very similar to paging but here segments are of variable-length

Lacks of Frames:If a process has less number of frames then less pages of that process will be able to reside in memory and hence more frequent swapping in and out will be required. This may lead to thrashing. Hence sufficient amount of frames must be allocated to each process in order to prevent thrashing.

Recovery of Thrashing : Do not allow the system to go into thrashing by instructing

the long term scheduler not to bring the processes into memory after the threshold.

If the system is already in thrashing then instruct the mid term schedular to suspend some of the processes so that we can recover the system from thrashing.

Difference between Virtual memory and Cache memory:

S.N

O VIRTUAL MEMORY CACHE MEMORY

1.

Virtual memory increases the

capacity of main memory.

While cache memory

increase the accessing

speed of CPU.

2.

Virtual memory is not a memory unit,

its a technique.

Cache memory is exactly a

memory unit.

3.

The size of virtual memory is greater

than the cache memory.

While the size of cache

memory is less than the

virtual memory.

4. Operating System manages the On the other hand hardware

Page 48: gecnilokheri.ac.ingecnilokheri.ac.in/GPContent/Operating System UNIT 3r… · Web viewSegmentation memory management works very similar to paging but here segments are of variable-length

Virtual memory. manages the cache memory.

5.

In virtual memory, The program with

size larger than the main memory are

executed. While in cache memory,

Page Replacement Algorithms in Operating Systems

In a computer operating system that uses paging for virtual memory management, page replacement algorithms decide which memory pages to page out, sometimes called swap out, or 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.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

Page 49: gecnilokheri.ac.ingecnilokheri.ac.in/GPContent/Operating System UNIT 3r… · Web viewSegmentation memory management works very similar to paging but here segments are of variable-length

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.Example-1Consider page reference string 1, 3, 0, 3, 5, 6 with 3 page frames.Find number of page faults.

Initially all slots are empty, so when 1, 3, 0 came they are allocated to the empty slots —> 3 Page Faults.when 3 comes, it is already in  memory so —> 0 Page Faults.Then 5 comes, it is not available in  memory so it replaces the oldest page slot i.e 1. —>1 Page Fault.6 comes, it is also not available in memory so it replaces the oldest page slot i.e 3 —>1 Page Fault.Finally when 3 come it is not avilable so it replaces 0 1 page fault

Page 50: gecnilokheri.ac.ingecnilokheri.ac.in/GPContent/Operating System UNIT 3r… · Web viewSegmentation memory management works very similar to paging but here segments are of variable-length

Belady’s anomaly – Belady’s anomaly proves that it is possible to have more page faults when increasing the number of page frames while using the First in First Out (FIFO) page replacement algorithm.  For example, if we consider reference string 3, 2, 1, 0, 3, 2, 4, 3, 2, 1, 0, 4 and 3 slots, we get 9 total page faults, but if we increase slots to 4, we get 10 page faults.

Optimal Page replacement –In this algorithm, pages are replaced which would not be used for the longest duration of time in the future.Example-2:Consider the page references 7, 0, 1, 2, 0, 3, 0, 4, 2, 3, 0, 3, 2, with 4 page frame. Find number of page fault.

Initially all slots are empty, so when 7 0 1 2 are allocated to the empty slots —> 4 Page faults 0 is already there so —> 0 Page fault.

when 3 came it will take the place of 7 because it is not used for the longest duration of time in the future.—>1 Page fault.

Page 51: gecnilokheri.ac.ingecnilokheri.ac.in/GPContent/Operating System UNIT 3r… · Web viewSegmentation memory management works very similar to paging but here segments are of variable-length

0 is already there so —> 0 Page fault..4 will takes place of 1 —> 1 Page Fault.

Now for the further page reference string —> 0 Page fault because they are already available in the memory.Optimal page replacement is perfect, but not possible in practice as the operating system cannot know future requests. The use of Optimal Page replacement is to set up a benchmark so that other replacement algorithms can be analyzed against it.

  Least Recently Used –

In this algorithm page will be replaced which is least recently used.Example-3Consider the page reference string 7, 0, 1, 2, 0, 3, 0, 4, 2, 3, 0, 3, 2 with 4 page frames.Find number of page faults.

Page 52: gecnilokheri.ac.ingecnilokheri.ac.in/GPContent/Operating System UNIT 3r… · Web viewSegmentation memory management works very similar to paging but here segments are of variable-length

Find number of page faults.

Initially all slots are empty, so when 7 0 1 2 are allocated to the empty slots —> 4 Page faults0 is already their so —> 0 Page fault.when 3 came it will take the place of 7 because it is least recently used —>1 Page fault0 is already in memory so —> 0 Page fault.4 will takes place of 1 —> 1 Page FaultNow for the further page reference string —> 0 Page fault because they are already available in the memory.

Allocation of frames in Operating System

An important aspect of operating systems, virtual memory is implemented using demand paging. Demand paging necessitates the development of a page-replacement algorithm and a frame allocation algorithm. Frame allocation algorithms are used if you have multiple processes; it helps decide how many frames to allocate to each process.

Page 53: gecnilokheri.ac.ingecnilokheri.ac.in/GPContent/Operating System UNIT 3r… · Web viewSegmentation memory management works very similar to paging but here segments are of variable-length

There are various constraints to the strategies for the allocation of frames: You cannot allocate more than the total number of available

frames. At least a minimum number of frames should be allocated to

each process. This constraint is supported by two reasons. The first reason is, as less number of frames are allocated, there is an increase in the page fault ratio, decreasing the performance of the execution of the process. Secondly, there should be enough frames to hold all the different pages that any single instruction can reference.

Frame allocation algorithms –The two algorithms commonly used to allocate frames to a process are:

1. Equal allocation: In a system with x frames and y processes, each process gets equal number of frames, i.e. x/y. For instance, if the system has 48 frames and 9 processes, each process will get 5 frames. The three frames which are not allocated to any process can be used as a free-frame buffer pool. Disadvantage: In systems with processes of varying

sizes, it does not make much sense to give each process equal frames. Allocation of a large number of frames to a small process will eventually lead to the wastage of a large number of allocated unused frames.

2. Proportional allocation: Frames are allocated to each process according to the process size.For a process pi of size si, the number of allocated frames

Page 54: gecnilokheri.ac.ingecnilokheri.ac.in/GPContent/Operating System UNIT 3r… · Web viewSegmentation memory management works very similar to paging but here segments are of variable-length

is ai = (si/S)*m, where S is the sum of the sizes of all the processes and m is the number of frames in the system. For instance, in a system with 62 frames, if there is a process of 10KB and another process of 127KB, then the first process will be allocated (10/137)*62 = 4 frames and the other process will get (127/137)*62 = 57 frames. Advantage: All the processes share the available frames

according to their needs, rather than equally.Global vs Local Allocation –The number of frames allocated to a process can also dynamically change depending on whether you have used global replacement or local replacement for replacing pages in case of a page fault.1. Local replacement: When a process needs a page which is

not in the memory, it can bring in the new page and allocate it a frame from its own set of allocated frames only. Advantage: The pages in memory for a particular

process and the page fault ratio is affected by the paging behavior of only that process.

Disadvantage: A low priority process may hinder a high priority process by not making its frames available to the high priority process.

2. Global replacement: When a process needs a page which is not in the memory, it can bring in the new page and allocate it a frame from the set of all frames, even if that frame is currently allocated to some other process; that is, one process can take a frame from another. Advantage: Does not hinder the performance of

processes and hence results in greater system throughput. Disadvantage: The page fault ratio of a process can not

be solely controlled by the process itself. The pages in

Page 55: gecnilokheri.ac.ingecnilokheri.ac.in/GPContent/Operating System UNIT 3r… · Web viewSegmentation memory management works very similar to paging but here segments are of variable-length

memory for a process depends on the paging behavior of other processes as well.

Thrashing in Operating System (OS)

When a program need space larger than RAM or it need space when RAM is full, Operating System will try to allocate space from secondary memory and behaves like it has that much amount of memory by serving to that program. This concept is called virtual memory. To know about thrashing we first need to know what is page fault and swapping.

Page fault and swapping: We know every program divided into some pages. When a program need a page which is not in RAM that is called page fault. Whenever a page fault happens, operating system will try to fetch that page from secondary memory and try to swap it with one of the page in RAM. This is called swapping.

If this page fault and then swapping happening very frequently at higher rate, then operating system has to spend more time to swap these pages. This state is called thrashing. Because of this, CPU utilization is going to be reduced.

Page 56: gecnilokheri.ac.ingecnilokheri.ac.in/GPContent/Operating System UNIT 3r… · Web viewSegmentation memory management works very similar to paging but here segments are of variable-length

Effect of Thrashing

Whenever thrashing starts, operating system tries to apply either Global page replacement Algorithm or Local page replacement algorithm.Global Page Replacement

Since global page replacement can access to bring any page, it tries to bring more pages whenever thrashing found. But what actually will happen is, due to this, no process gets enough frames and by result thrashing will be increase more and more. So global page replacement algorithm is not suitable when thrashing happens.

Local Page Replacement

Unlike global page replacement algorithm, local page replacement will select pages which only belongs to that

Page 57: gecnilokheri.ac.ingecnilokheri.ac.in/GPContent/Operating System UNIT 3r… · Web viewSegmentation memory management works very similar to paging but here segments are of variable-length

process. So there is a chance to reduce the thrashing. But it is proven that there are many disadvantages if we use local page replacement. So local page replacement is just alternative than global page replacement in thrashing scenario.

Techniques to Handle Thrashing

We already seen Local replacement is better than Global replacement to avoid thrashing. But it also has disadvantages and not suggestable. Some more techniques areWorking Set Model

This model is based on locality. What locality is saying, the page used recently can be used again and also the pages which are nearby this page will also be used. Working set means set of pages in the most recent D time. The page which completed its D amount of time in working set automatically dropped from it. So accuracy of working set depends on D we have chosen. This working set model avoid thrashing while keeping the degree of multiprogramming as high as possible.

Page Fault Frequency

Page 58: gecnilokheri.ac.ingecnilokheri.ac.in/GPContent/Operating System UNIT 3r… · Web viewSegmentation memory management works very similar to paging but here segments are of variable-length

It is some direct approach than working set model. When thrashing occurring we know that it has few number of frames. And if it is not thrashing that means it has too many frames. Based on this property we assign an upper and lower bound for the desired page fault rate. According to page fault rate we allocate or remove pages. If the page fault rate become less than the lower limit, frames can be removed from the process. Similarly, if the page fault rate become more than the upper limit, more number of frames can be allocated to the process. And if no frames available due to high page fault rate, we will just suspend the processes and will restart them again when frames available.

Page 59: gecnilokheri.ac.ingecnilokheri.ac.in/GPContent/Operating System UNIT 3r… · Web viewSegmentation memory management works very similar to paging but here segments are of variable-length

 

 

1. FRAME ALLOCATION

This algorithm helps us to decide no. of frames to be allocated to any process. There’s always some requirement defined by the process/system architecture.

The maximum number of frames required by any process is the size of the process itself.

The minimum number of frames required depends on architecture for execution of the instruction

Strategies used from Frame Allocation

Page 60: gecnilokheri.ac.ingecnilokheri.ac.in/GPContent/Operating System UNIT 3r… · Web viewSegmentation memory management works very similar to paging but here segments are of variable-length

1.1. EQUAL ALLOCATION: each number of frames will be equally distributed among processes.  Not very much useful as not every process will require equal number of frames; some process may require extra frames whereas some process may require less number of frames.

                       For example, given no. of frames: 6

                       No. of processes available: 3

                       Therefore, each process will get 2 frames

Main Memory divided into frames in such a way that every processes gets equal no. of frames.

 

1.2 WEIGHTED ALLOCATION: Depending on the size of the processes, number of frames will be allocated accordingly. More number of frames given the process of larger size.

Page 61: gecnilokheri.ac.ingecnilokheri.ac.in/GPContent/Operating System UNIT 3r… · Web viewSegmentation memory management works very similar to paging but here segments are of variable-length

It is commonly used as compared to other algorithms.

For example: available processes of size P1: 20 Pages, P2: 30 Pages, P3: 50 Pages

                       Available frames: 10

                      Requirement: P1= 20/100*10=2 (P1+P2+P3:20+30+50=100)

                                              P2= 30/100*10=3

                                              P3=50/100*10=5

Therefore,P1,P2,P3 will get 2,3,5 no of frames respectively.

 

1.3. PRIORITY ALLOCATION: The processes with higher priority no. gets more frame. If the process with higher priorities wants more frames; then it can forcefully replace processes with

Page 62: gecnilokheri.ac.ingecnilokheri.ac.in/GPContent/Operating System UNIT 3r… · Web viewSegmentation memory management works very similar to paging but here segments are of variable-length

lower priorities. Suppose, Process P1 has higher priority than process P2 and requires more frame then P1 pulls out P2 and uses up the frame.

any page we have to consider everything for replacement, we can consider all the available frames to replace.

Local page-replacement are preferred more than global page-replacement and practically used in most of the operating system.