Computer memory is divided into small partitions that are all the same size and referred to as, page frames. Then when a process is loaded it gets divided into pages which are the same size as those previous frames. The process pages are then loaded into the frames.

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.

Paging is a fixed size partitioning scheme.

In paging, secondary memory and main memory are divided into equal fixed size partitions.

The partitions of secondary memory are called as pages.

The partitions of main memory are called as frames.

Each process is divided into parts where size of each part is same as page size.

The size of the last part may be less than the page size.

The pages of process are stored in the frames of main memory depending upon their availability.

For implementing paging the physical and logical memory spaces are divided into the same fixed-sized blocks. These fixed-sized blocks of physical memory are called frames, and the fixed-sized blocks of logical memory are called pages.

When a process needs to be executed the process pages from logical memory space are loaded into the frames of physical memory address space. Now the address generated by CPU for accessing the frame is divided into two parts i.e. page number and page offset.

The page table uses page number as an index; each process has its separate page table that maps logical address to the physical address. The page table contains base address of the page stored in the frame of physical memory space. The base address defined by page table is combined with the page offset to define the frame number in physical memory where the page is stored.

Watch this video :

Segmentation :

Computer memory is allocated in various sizes (segments) depending on the need for address space by the process. These segments may be individually protected or shared between processes. Commonly you will see what are called “Segmentation Faults” in programs, this is because the data that’s is about to be read or written is outside the permitted address space of that process.

Like Paging, Segmentation is also a memory management scheme. It supports the user’s view of the memory. The process is divided into the variable size segments and loaded to the logical memory address space.

The logical address space is the collection of variable size segments. Each segment has its name and length. For the execution, the segments from logical memory space are loaded to the physical memory space.

watch this video :

Difference between Paging and Segmentation :

1. The basic difference between paging and segmentation is that a page is always of fixed block size whereas, a segment is of variable size.

2. Paging may lead to internal fragmentation as the page is of fixed block size, but it may happen that the process does not acquire the entire block size which will generate the internal fragment in memory. The segmentation may lead to external fragmentation as the memory is filled with the variable sized blocks.

3. In paging the user only provides a single integer as the address which is divided by the hardware into a page number and Offset. On the other hands, in segmentation the user specifies the address in two quantities i.e. segment number and offset.

4. The size of the page is decided or specified by the hardware. On the other hands, the size of the segment is specified by the user.

5. In paging, the page table maps the logical address to the physical address, and it contains base address of each page stored in the frames of physical memory space. However, in segmentation, the segment table maps the logical address to the physical address, and it contains segment number and offset (segment limit).

Deadlock :

Deadlocks are a set of blocked processes each holding a resource and waiting to acquire a resource held by another process.

Watch Below videos :

Above videos are more than enough to solve any Deadlock question in ccat exam .

How to avoid Deadlocks :

Deadlocks can be avoided by avoiding at least one of the four conditions, because all this four conditions are required simultaneously to cause deadlock.

1. Mutual Exclusion :
Resources shared such as read-only files do not lead to deadlocks but resources, such as printers and tape drives, requires exclusive access by a single process.

2. Hold and Wait :
In this condition processes must be prevented from holding one or more resources while simultaneously waiting for one or more others.

3. No Preemption :
Preemption of process resource allocations can avoid the condition of deadlocks, where ever possible.

4. Circular Wait :
Circular wait can be avoided if we number all resources, and require that processes request resources only in strictly increasing(or decreasing) order

How to Handle Deadlock :

Above informations for avoid deadlock but what if I want to handle deadlock ? :

Then use below methods :
1. Preemption :
We can take a resource from one process and give it to other. This will resolve the deadlock situation, but sometimes it does causes problems.

2. Rollback :
In situations where deadlock is a real possibility, the system can periodically make a record of the state of each process and when deadlock occurs, roll everything back to the last checkpoint, and restart, but allocating resources differently so that deadlock does not occur.

3. Kill one or more processes :
Very easy method but still it works .


Fragmentation :

Whenever a process is loaded or removed from the physical memory block, it creates a small hole in memory space which is called fragment. Due to fragmentation, the system fails in allocating the contiguous memory space to a process even though it have the requested amount of memory but, in a non-contiguous manner.

Types of Fragmentation :

1. Internal Fragmentation
2. External Fragmentation

1. Internal Fragmentation :
Internal fragmentation occurs when fixed sized memory blocks are allocated to the process without concerning about the size of the process .

Whenever a process request for the memory, the fixed sized block is allocated to the process. In case the memory assigned to the process is somewhat larger than the memory requested, then the difference between assigned and requested memory is the Internal fragmentation.

2. External Fragmentation :
External fragmentation occurs when the processes are allocated memory dynamically .

External fragmentation occurs when there is a sufficient amount of space in the memory to satisfy the memory request of a process. But the process’s memory request can not be satisfied as the memory available is in a non-contiguous manner. Either you apply first-fit or best-fit memory allocation strategy it will cause external fragmentation.

When a process is loaded and removed from the memory the free space creates the hole in the memory space, and there are many such holes in the memory space, this is called External fragmentation. Although the first fit and best fit can affect the amount of external fragmentation, it can not be totally eliminated. Compaction may be the solution for external fragmentation.

Contiguous Memory Allocation :

The operating system and the user’s processes both must be accommodated in the main memory. Hence the main memory is divided into two partitions: at one partition the operating system resides and at other the user processes reside. In usual conditions, the several user processes must reside in the memory at the same time, and therefore, it is important to consider the allocation of memory to the processes.

The Contiguous memory allocation is one of the methods of memory allocation. In contiguous memory allocation, when a process requests for the memory, a single contiguous section of memory blocks is assigned to the process according to its requirement.

The contiguous memory allocation can be achieved by dividing the memory into the fixed-sized partition and allocate each partition to a single process only. But this will cause the degree of multiprogramming, bounding to the number of fixed partition done in the memory. The contiguous memory allocation also leads to the internal fragmentation.

Non-Contiguous Memory Allocation :

The Non-contiguous memory allocation allows a process to acquire the several memory blocks at the different location in the memory according to its requirement. The noncontiguous memory allocation also reduces the memory wastage caused due to internal and external fragmentation. As it utilizes the memory holes, created during internal and external fragmentation.

Paging and segmentation are the two ways which allow a process’s physical address space to be non-contiguous. In non-contiguous memory allocation, the process is divided into blocks (pages or segments) which are placed into the different area of memory space according to the availability of the memory.