I am learning about CPU and memory interactions in my Operating Systems class and have been confused about how the memory stack works. I understand that the stack goes down one line at a time and either stores the data in the Accumulator or sends an instruction to the IR.

My question is: does every process have its own memory stack or does each processor have one gigantic memory stack that continually switches between different processes (and is occasionally interrupted by I/O calls)? If there is one gigantic stack is there only one stack because most modern processors can execute so many instructions per second that it only looks like processes are running simultaneously to our slow human brains?


Most modern CPU's do not technically have a hardware stack (that is, there is no dedicated memory used just for the stack, largely because it's inefficient and you can't be sure exactly how much space you need on any real system). Almost all of them do have a stack pointer register however, which is what gets used by instructions that operate on the stack. However, each process still has it's own stack (actually, usually at least two stacks, but that gets into the intricacies of privilege separation between the OS and user programs, and isn't entirely relevant to the question), but the stack pointer register is shared by all processes (this register is part of what is saved and restored during a context switch, together with most of the rest of the CPU state).

To answer the second half of your question somewhat bluntly, multitasking is an illusion, it just depends on the scale. Each individual CPU core can run one process at a time, and it (usually) will switch between all processes on the system that aren't waiting on something at frequencies high enough that they're functionally running simultaneously from a human perspective (most operating systems with processes at a rate of 100 to 1000 times a second, which is still more than enough time for hundreds of millions of CPU instructions to be executed in each process). Looking at this a bit differently, a single core CPU is like a person, it has to switch its focus regularly to 'multi-task', and a multi-core CPU is like a team of people, each person can only do one thing at a time reliably, but taken as a whole, the team can easily work as many tasks as there are team members.

However, for the purposes of your class, all of this is either irrelevant (if the class is taught using a minimalistic OS like CP/M or DOS, which don't do multitasking, and therefore have a single stack, period) or will be covered in due time (if it's taught using a real OS like UNIX, MINIX, or even one of the various modern commercial offerings, all of which do multitasking, which is often covered near the end of the class because understanding it requires understanding the basics (like how a stack works)).

  • I must have not phrased it well but what I was trying to ask is since a CPU can only run one process at a time does that mean that each of these processes would be placed in the same stack? – Darien Springer Oct 4 '17 at 21:58
  • 1
    @DarienSpringer Ah, sorry, I misunderstood that. The short answer is no, each process has it's own stack, but the stack pointer is part of the processor context that gets saved and restored during a context switch. – Austin Hemmelgarn Oct 5 '17 at 11:27

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