How segmentation works and how the physical memory address is calculated from segment table

3.1k views Asked by At

I was going through the topic of segmentation in operating systems.

I have learnt that the concept of segmentation came into existence because of the free spaces which might exist in the address space of the process loaded in the memory.

I will try to first explain few things which I understood from my study.Please correct me if anything is wrong.

Lets say for example where the segmentation is not used which might not be the ideal case

I have a process with small address space which was loaded in the memory.As the process address space might not be placed at the start of the physical memory.We need some mechanism to convert the addresses of the program code(code segment,from where the instruction to which the PC is pointed gets executed) to the actual physical memory addresses. So we use hardware registers(MMU) for this purpose which maintain base and bound values for the running process.Base value being the starting address of the physical memory from where the process is placed and bound being the total size of the running process or the last address of the running process.As MMU is global,we need to save base/bound values during context switching.

So here physical memory address = virtual memory address + base;

Now lets say we have process with large address space.We need some mechanism like segmentation for better utilization of free space as the process address space may contain large amount free spaces in between heap and stack or code and heap.

For segmentation we maintain a segment table for each process(need to store the table in PCB during context switching).

In segmentation we maintain base/bound values for each logical segment of the running process address space.So ideally we maintain base/bound values for each of code,stack and heap segments and store these values in segment table.

So the segment table should look somewhat like this..

     Segment  | Base | Bounds
   ---------------------------
     Code     | 16K  |   1K
     Heap     | 28K  |   1K
     Stack    | 20K  |   2K

and the physical memory might look some what like this..

0    |---------------------|
     |         OS          |
     |                     |
              ....
     |                     |
     |                     |
     |                     |
16K  |---------------------|
     |   (program code)    |              
     |                     |              
17K  |---------------------|
     |xxxxxxxxxxxxxxxxxxxxx|
     |xxxxxxx free xxxxxxxx|
     |xxxxxxxxxxxxxxxxxxxxx|
     |xxxxxxxxxxxxxxxxxxxxx|
20K  |---------------------|            
     |                     |
     |                     |
     |       (stack)       |            
     |                     |            
22K  |---------------------|            
     |xxxxxxxxxxxxxxxxxxxxx|
     |xxxxxxxxxxxxxxxxxxxxx|
     |xxxxxxxxxxxxxxxxxxxxx|
     |xxxxxxx free xxxxxxxx|
     |xxxxxxxxxxxxxxxxxxxxx|
     |xxxxxxxxxxxxxxxxxxxxx|
     |xxxxxxxxxxxxxxxxxxxxx|
28K  |---------------------|            
     |                     |
     |       (heap)        |            
29K  |---------------------|            
     |xxxxxxxxxxxxxxxxxxxxx|
     |xxxxxxxxxxxxxxxxxxxxx|
     |xxxxxxx free xxxxxxxx|
     |xxxxxxxxxxxxxxxxxxxxx|
     |xxxxxxxxxxxxxxxxxxxxx|
32K  |---------------------|   

So now if the processor needs to convert the virtual memory address to physical memory address while executing the code instruction,How will it get to know which base value to pick as there may be many base values in the segment table?

Suppose lets say the instruction is to update some stack variable..How will it pick the base value of the stack in this case?

So that the physical memory address = stack's base address + virtual memory address from the instruction?

And What are the disadvantages of segmentation apart from external fragmentation.

Any external links with detailed explaination to the concepts of segmentation//paging/virtualization would be really helpful.

1

There are 1 answers

0
AudioBubble On

In processors that support segmentation, there should be an internal or external registers the processor will use to select the right segment depending on the context. It something that depends on the processor architecture. The only processors I know of that support segmentation are Intel processors.

With x86 (see Intel 8086 : Segmentation) segment registers/selectors there is cs, ds, ss. The processor uses the code cs segment while executing instructions, data segment ds with memory, unless you state otherwise, or stack segment ss with stack instructions.

Most modern processors, apart from x86, only support paging. So to unify memory management, x86 segmentation effect is disabled in most modern operating systems, and rely on paging to manage the virtual memory.