In studying operating systems (primarily with Linux as reference), there are a few points that I don't find well explained in the material that I have studied.
Programs loaded into memory are often described as being divided into segments of text, data, stack etc., even in the context of operating systems like Linux where virtual memory is based purely on paging. Is it the case that it is just the program, and not the memory itself that is referred to as segmented? If so, I find the terminology confusing.
I saw that malloc can be implemented in Linux using the call 'sbrk' that increases the size of the data segment. Again, is this 'data segment' just a region of memory that is used for data by convention and not a 'real' segment? (Extra question: 'sbrk' does not seem to be able to decrease the size of the 'segment'. Does this mean that a process can never release memory to the OS other than quitting?)
Also I am interested in knowing why modern operating systems seems not to be using (paged) segmentation. Wouldn't it prevent certain kinds of attacks to have the code residing in it's own protected segment, thereby increasing security? On the other hand, would this make e.g. JIT compilation impossible/difficult?
Besides "yes"/"no" answers to the questions above, I am interested in any insightful elaboration on the subject.
The segment in "data segment" has nothing to do with hardware segmentation, which is a feature of little relevance to modern operating systems (i.e. redundant with respect to paging) which rely on paging to implement virtual memory. Segments also have severe drawbacks compared to paging (e.g. memory contiguous in a segment must be physically contiguous) without any benefit. By "segment" for user-space programs, one literally means a contiguous section of the virtual space of the process.
Many architectures do not have segmentation anymore. On x86, segmentation is just an historical payload and is set up to have a code and data segment that covers the entire address space because segmentation cannot be bypassed.
Your question about freeing memory obtained through sbrk is answered here: How do I free memory obtained by sbrk()?
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With