Does Each program has its own separate segments (code, stack, data) in memory?
Asked Answered
J

3

7

Say there are many programs running at the same time.
Does each executable program that runs has its own code, data, and stack segment in RAM or is the complete RAM divided into 4 segments and each of theses segments holds respective segments for each programs.

Juristic answered 30/7, 2013 at 13:37 Comment(4)
How it's handled in hardware and the operating system differs depending on, well, hardware and operating system. But generally yes, each process has it's own code, heap, stack etc.Intelligencer
In the Apollo guidance computer, all processes use the same memory.Bogeyman
Generally? C'mon, aren't we talking like 99.9%? With specific exceptions like shared memory, etc. In general, programs can't botch with each while running in terms of their own processing space.Politesse
I don't do much programming on my Apollo guidance computer....Politesse
F
12

This depends on two things:

  • Your deployment architecture
  • Your OS that runs your process

If you are running on x86 on common mainstream Linux, Windows, BSD then

  • each process has it's own private virtual RAM
  • All segments text (code), heap, data are in the same logical address space (virtual RAM segment)

Historically x86 was designed with the ability to support segmented memory, but OSes never made use of it and support for it was initially dropped in AMD64. Though recent Processors support it again for use in virtualisation and Hypervisors. Userland applications are commonly deployed without segmentation today.

Furfur answered 30/7, 2013 at 13:41 Comment(3)
So that means each program is given a amount of memory say 100 MB and that program divide that 100 MB into stack, data, code segments?Juristic
@user1660982 More or less. Each program is given today commonly 256TB of virtual RAM (which is unusable in it's raw state). Program code is placed automatically somewhere and calls that request memory (mmap and family) place your new memory mappings anywhere in that virtual space which is yet unused. Commonly the OS decides where new mappings are put, but your program may hint where it wants them.Furfur
You could even argue that each program is "only" given 128TB of VRAM because the kernel will take the other half, but again, that's OS-dependentHassanhassell
H
7

Short answer: it depends on the hardware and OS it's running on.

Usually, each process gets its own virtual memory space, giving it the illusion of it seeming like the only process running on the computer. That virtual memory space is divided into code/data/stack segments by the OS (these usually have fixed/common addresses).

As far as physical RAM usage, that depends entirely on the OS - it can use different sections of RAM as it pleases, and the programs won't even know about it.

Hassanhassell answered 30/7, 2013 at 13:41 Comment(0)
A
0

This question is Operating System specific and not C specific.

In general, OSes maintain an execution context per process, which holds the stack and runtime flags, but how memory is allocated by the likes of malloc varies between systems.

For more information you may wish to delve deeper into the malloc documentation.

Amortization answered 30/7, 2013 at 13:45 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.