<<

Operating Systems

Minati De

Department of Mathematics, Indian Institute of Technology Delhi, India.

Lecture 10: Segmentation

M. De Operating Systems Disadvantage of Base and Bound Method

M. De Operating Systems Disadvantage of Base and Bound Method

Internal Fragmentation: the stack and heap are not too big, all of the space between the two is simply wasted Can’t share code/data with other processes Can’t keep program from accidentally overwriting its own code Can’t grow stack/heap as needed

M. De Operating Systems Inefficiency of the Base and Bound Approach

M. De Operating Systems How do we support a large address space with (potentially) a lot of free space between the stack and the heap?

M. De Operating Systems Logically-different segment: code, stack, heap Each segment can be placed in different part of physical memory

Segmentation

Segment is just a contiguous portion of the address space of a particular length.

M. De Operating Systems Each segment can be placed in different part of physical memory

Segmentation

Segment is just a contiguous portion of the address space of a particular length. Logically-different segment: code, stack, heap

M. De Operating Systems Segmentation

Segment is just a contiguous portion of the address space of a particular length. Logically-different segment: code, stack, heap Each segment can be placed in different part of physical memory

M. De Operating Systems Placing Segment In Physical Memory

M. De Operating Systems Placing Segment In Physical Memory

M. De Operating Systems Address Translation on Segmentation

M. De Operating Systems Address Translation on Segmentation

M. De Operating Systems Address Translation on Segmentation

M. De Operating Systems Segmentation Fault or Violation

M. De Operating Systems Which Segment Are We Referring To?

The hardware uses segment registers during translation. How does it know the offset into a segment, and to which segment an address refers?

M. De Operating Systems Referring to Segment

M. De Operating Systems Referring to Segment

M. De Operating Systems If the address was generated from the program counter (i.e., it was an instruction fetch), then the address is within the ; if the address is based off of the stack or base pointer, it must be in the stack segment; any other address must be in the heap.

Referring to Segment: Implicit Approach

The hardware determines the segment by noticing how the address was formed:

M. De Operating Systems if the address is based off of the stack or base pointer, it must be in the stack segment; any other address must be in the heap.

Referring to Segment: Implicit Approach

The hardware determines the segment by noticing how the address was formed: If the address was generated from the program counter (i.e., it was an instruction fetch), then the address is within the code segment;

M. De Operating Systems any other address must be in the heap.

Referring to Segment: Implicit Approach

The hardware determines the segment by noticing how the address was formed: If the address was generated from the program counter (i.e., it was an instruction fetch), then the address is within the code segment; if the address is based off of the stack or base pointer, it must be in the stack segment;

M. De Operating Systems Referring to Segment: Implicit Approach

The hardware determines the segment by noticing how the address was formed: If the address was generated from the program counter (i.e., it was an instruction fetch), then the address is within the code segment; if the address is based off of the stack or base pointer, it must be in the stack segment; any other address must be in the heap.

M. De Operating Systems How to refer the stack segment?

M. De Operating Systems Referring to Stack Segment

M. De Operating Systems How to support sharing?

M. De Operating Systems Support for Sharing

M. De Operating Systems Segmentation

M. De Operating Systems Sharing

M. De Operating Systems Segments allow a more efficient implementation Copy segment table into child Mark parent and child segments read-only If child or parent writes to a segment (ex: stack, heap) into kernel make a copy of the segment and resume

Example of sharing: fork and Copy on Write

UNIX fork Makes a copy of a process

M. De Operating Systems Example of sharing: UNIX fork and Copy on Write

UNIX fork Makes a copy of a process Segments allow a more efficient implementation Copy segment table into child Mark parent and child segments read-only If child or parent writes to a segment (ex: stack, heap) trap into kernel make a copy of the segment and resume

M. De Operating Systems How much?

avoid accidentally leaking information!

Kernel allocates some memory

Zeros the memory

Modify segment table Resume process

When program uses memory beyond end of stack/heap

Segmentation fault into OS kernel

M. De Operating Systems avoid accidentally leaking information!

How much? Zeros the memory

Modify segment table Resume process

When program uses memory beyond end of stack/heap

Segmentation fault into OS kernel Kernel allocates some memory

M. De Operating Systems avoid accidentally leaking information!

Zeros the memory

Modify segment table Resume process

When program uses memory beyond end of stack/heap

Segmentation fault into OS kernel Kernel allocates some memory How much?

M. De Operating Systems avoid accidentally leaking information! Modify segment table Resume process

When program uses memory beyond end of stack/heap

Segmentation fault into OS kernel Kernel allocates some memory How much? Zeros the memory

M. De Operating Systems Modify segment table Resume process

When program uses memory beyond end of stack/heap

Segmentation fault into OS kernel Kernel allocates some memory How much? Zeros the memory avoid accidentally leaking information!

M. De Operating Systems Resume process

When program uses memory beyond end of stack/heap

Segmentation fault into OS kernel Kernel allocates some memory How much? Zeros the memory avoid accidentally leaking information! Modify segment table

M. De Operating Systems When program uses memory beyond end of stack/heap

Segmentation fault into OS kernel Kernel allocates some memory How much? Zeros the memory avoid accidentally leaking information! Modify segment table Resume process

M. De Operating Systems Pros?

Can share code/data segments between processes Can protect code segment from being overwritten Can transparently grow stack/heap as needed Can detect if need to copy-on-write

M. De Operating Systems Cons?

M. De Operating Systems Cons?

Complex Need to find chunk of a particular size May need to rearrange memory from time to time to make room for new segment or growing segment External fragmentation: wasted space between chunks

M. De Operating Systems External Fragmentation

M. De Operating Systems Compaction

M. De Operating Systems M. De Operating Systems