Processes & the Process Abstraction

the data segment

Some ingredients come pre-measured: a packet labeled '200 grams of flour' that you set on the counter before you start. The data segment is the part of a process's memory that holds variables which already have a starting value baked into the program, ready and waiting from the moment the process begins.

Concretely, the data segment stores global and static variables that are initialized to a non-zero value in the source code, for example a global counter set to 1 or a string constant. Because the starting values are known at compile time, they are stored in the executable file and copied into the data segment in memory when the program is loaded. Unlike the text segment, the data segment is read-write: the program can change these variables as it runs. It sits just above the text segment in the classic layout, and it is distinct from the BSS, which holds variables that start at zero (and so need no stored values, only reserved space).

The distinction matters mostly for understanding memory use and program startup. Data that must be initialized takes up room in the executable file on disk (the values have to be stored somewhere), whereas zero-initialized data does not. So splitting initialized data from uninitialized data keeps executables smaller. A common confusion is to lump 'data' and 'BSS' together; they are both for global/static variables, but one carries explicit starting values and the other is simply zeroed when the process starts.

A line like 'static int limit = 100;' puts limit in the data segment with its stored value 100. A line like 'static int total;' puts total in the BSS, where it simply starts at 0.

Non-zero starting value goes in data; zero-start goes in BSS.

The data segment is read-write, so it is not shareable between processes the way the read-only text segment is; each process gets its own writable copy.

Also called
initialized data segmentdata section資料區段已初始化資料區