0
$\begingroup$

Without assuming ASLR...

Processes have virtual addresses, and they manage stacks & heaps of their own. However, these all get mapped to physical addresses somewhere on the memory.

Let's say we have Process A and Process B. Process A starts before Process B. Process A puts some stuff on the stack. We switch to Process B. It puts some stuff on the stack.

However, Process A finishes, and all its elements need to be removed off the stack. However, Process B's elements are still on the stack, so we can't just force the stack pointer down.

How is this relieved? Or is physical memory really all heap?

$\endgroup$
3
$\begingroup$

The point you make at the very end of your question is the right one.

No modern computer has a stack. In hardware terms it turns out to be superfluous, wasteful and useless.

A stack is superfluous because one can easily perform the operations “write a value”, “retrieve the last value written”, and so on by having a pointer to a memory location. “Push” can be implemented by advancing the pointer and writing to the location it points to; “pop” by reading the pointed-to location and undoing the advance of the pointer. (On Intel architectures the pointer is a register called SP, and “advancing” decrements SP before writing to memory). This emulation of a stack requires no hardware at all - only memory, which exists already.

A hardware stack is wasteful because it requires special mechanisms over above the “read at location”, “write at location” which we already have because we have RAM. (Some early or embedded microprocessor architectures have gone the other way, having a very few levels of stack on-chip).

A hardware stack is useless because fashionable computer languages such as C cannot function with a stack in the strict sense - they need to access not just the last pushed value (such as a return address) but values pushed earlier on, which can be the arguments to a subroutine, for example.

Returning therefore to your main question: among the memory assigned to a process there is an area of memory which the process will use for its stack. Its stack pointer will point into that area. Switching from one process to another involves saving the whole state of the old process including the stack pointer and loading the whole state of the new process including the stack pointer. Because there is no special stack as such, on process termination everything is simply forgotten: nothing else needs to be done.

$\endgroup$
1
$\begingroup$

Each process has its own stack, its own stack pointer, and so on. They are at independent locations. So whatever happens with Process A has no effect on Process B.

$\endgroup$
1
$\begingroup$

The operating system assigns memory space to each process. The memory space is assigned to real memory, or to swap space, or when not used at all not represented anywhere. Memory space is virtual, so multiple processes can have what looks like the same address space.

Each process is responsible for how its address space is used. So if multiple processes each have a stack, each process is responsible for its stack and doesn't care about others at all. The operating system doesn't care about any of the stacks. The operating system just makes sure that each process can access its address space and nothing else.

$\endgroup$

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.