2

I am reading about processes. I came across a topic "How a process is created" wherein I encountered the following :-

The process image as viewed by the kernel runs in its own user address space – a protected space which can't be disturbed by other users. This address space has a number of segments:-

a. Text Segment
b. Data Segment
c. User Segment

I still don't understand what does it mean, could someone please enlighten me on this topic.

Anthon
  • 79,293
Ankit
  • 1,385

1 Answers1

4

Memory (RAM) for a linux or unix process, is just a linear array of bytes. Each byte has a number, from 0 to some maximum. That's the processes "address space". The size of an address space varies depending on whether it's a 32-bit or 64-bit CPU, but any byte in memory can be read or written based on that byte's address, which is just a number.

A process has different parts, say executable code, data that it works with, and a last-in, first-out stack of activation records for the function calls the executable code makes. Those are the segments. A piece of the address space is dedicated to executable code, that's the "code segment". Another piece of the address space is allocated for initialized variables (usually global variables), that might be the data segment, although its usually given the name "data segment". The "BSS segment" usually exists for uninitialized variables, and usually you can grow that segment. The LIFO stack of function activation records doesn't usually show up explicitly.

Some other pieces of a process' address space can be devoted to things like dynamically-load libraries, C++ constructors or destructors, code for the dynamic linker, it can get very complicated.

And then there's the issue of executable file format. Most Linux or Unix processes have an "ELF" file format, although Macs use the Mach-O format. A lot of the process' initial address space has corresponding pieces of the executable file.

I think this web page will give you an idea of what fills up a process' address space. Try to keep in mind that "address" is a number, and basically just names a byte in memory. Some authors also confuse the executing process with it's on-disk file, so try to keep that clear when thinking or reading.

As far as the purpose of an address space... a user address space exists for each running process. This can keep one process from writing to another process' data and crashing the 2nd process. That is, address spaces are used to keep buggy programs from wrecking other programs. Also, address spaces segment processes data from each other, allowing one user's programs to hold information in memory that you wouldn't want other users to see. Address spaces allow you to control access to in-memory data.

  • 1
    You got that backwards; the data segment is for initialized variables, bss is for uninitialized variables. The segments are also defined at link time so can not grow. – psusi May 10 '13 at 00:52
  • 1
    @psusi - thanks. Is my face red! I've corrected that, and added a link to a BSS explanation. –  May 10 '13 at 01:31
  • so, for example, what if I have a C array (named foo) of size 10 and decide (within an operating user address space) to write something like foo[21]=1? it would just write to whatever space in memory that corresponds to (maybe another memory space for another running C program. the way I read/interpret (i.e. "...This can keep one process from writing to another process' data and crashing the 2nd process...") is that the user address space would perhaps disallow this. – nate Sep 26 '22 at 20:08