- In my previous post, I had discussed about how Paging scheme can be helpful to minimize the amount of fragmentation. In this post I will continue my discussion with segmentation scheme as an alternate way for paging.
- A segment is a logical entity in a program like a function, a data structure, or an object. A process consists of a set of segments. The user’s view of memory is not same as the actual physical memory. The physical view consists of non-adjacent memory areas allocated to segments.
- A process Q consists of four logical entities with the symbolic names main, database, search and update. There may also be various data structures, symbol tables, arrays, stacks, variables and so on.
- Each segment is of variables length and each segment executes within the length of segment and performs some specific task.
- In this memory management scheme a logical address space is a collection of segment and each segment has a name and length. Each logical address used in the process has the form (si,bi) where si is the id of a segment and bi is the byte offset in that segment.
- For easy address translation, the kernel prepares a segment table for each process. It contains segment name, its size and starting memory address allocated to it. The MMU uses the segment table to perform address translation.
- User can refer the modules or the segments in the program by two dimensional address and we must define an implementation to map this address into one dimensional physical address.
- Actual physical memory is still a one dimension sequence of bytes this mapping is affected by a segment table.
- The segment number is used as an indexed into the segment table. The offset of the logical address must be between 0 and segment limit. If it is not, we trapped to the o/s (logical addressing go beyond that segment)
- If this offset is legal, it is added to segment base to produce the address in physical memory of the desired byte of base and limit register pairs.
- The situation of fragmentation is similar to paging except that the segments are of variable length and pages are of same size.
- With variable size partition scheme memory allocation is a dynamic storage allocation problem usually solved with best fit or worst fit algorithm.
- Segmentation may cause an external fragmentation when all blocks of free memory are too small to accommodate a segment.
- In this case the process may simply have to wait until more memory becomes available or a compaction creates a large block.
- This problem can be solved and depend on average segment size. At one extreme we could define on average segment size. At one extreme we could define each process to be a one segment.
- This approach reduces the variable size partition scheme.
- Every byte could be in its own segment relocated separately.
- It helps to eliminate the external fragmentation problem.
- If the average fragment size is small external fragmentation will also be small.