Open In App

Difference between a process stack and a CPU stack

Last Updated : 16 Jul, 2021
Improve
Improve
Like Article
Like
Save
Share
Report

Temporary data like as method/function arguments, return address, and local variables are stored on the process Stack, whereas on the other hand, the CPU stack consists of a collection of data words. It employs the Last In First Out (LIFO) access technique, which is the most common in most CPUs. In this article we are going to find out the detailed difference between both of these.

1.  Example of a Process Stack :

C




#include <stdio.h>
  
int main() {
   printf("Geeks for Geeks \n");
   return 0;
}


Output

Geeks for Geeks 

Analysis : This code only prints out one line as this is only limited to a process, which gets terminated after its completion. 

2. Example of a CPU Stack :

C




#include <stdio.h>
int GeeksforGeeks(int parameter1, char parameter2)
{
    int local1 = 9;
    char local2 = 'Z';
    return 0;
}
int main(int argc, char* argv[])
{
    GeeksforGeeks(7, '9');
    return 0;
}


Output :

int main(int argc, char *argv[])
{
00401060   push        ebp
00401061   mov         ebp, esp
00401063   sub         esp, 40h
00401066   push        ebx
00401067   push        esi
00401068   push        edi
00401069   lea         edi, [ebp-40h
0040106C   mov         ecx, 10h
00401071   mov         eax, 0CCCCCCCCh
00401076   rep stos    dword ptr [edi]}

Analysis : As you can see these all are the different CPU registers which are displayed on running this, as the CPU Stack is a bigger process.

Difference between a process stack and a CPU stack :

Process Stack

CPU Stack

Each process has its own Process Control Block (PCB) to save such information on a context switch, allowing the scheduling algorithm to function on a basic process ID. Each process doesn’t own its own PCB, hence status of a process is also contained in the registers to be stored for eviction.
The PCB associated with that ID is restored when a process obtains the CPU. The PCB associated with that ID is not restored unlike Process Stack.
A stack is nothing more than a memory block. It consists of several memory blocks grouped together.
Each processor mode generally has its own stack in a process. Each thread in multithreading has its own stack. Each CPU mode generally posses a unique stack. There can be several stacks in a process.
When a context transition happens, the kernel “kicks out” the old process and brings in a new one. When context transition happens, its prior state is restored in order for it to resume execution where it left off.
There are a finite number of registers in every architecture. Saving registers on the stack is for efficiency’s sake, so all you have to do now is re-enter the values.
Local variables are stored in the Stack. When local variables are declared, space on the stack is set aside for them. The stack pointer increases to the next physical memory location when a new data item is inserted or “pushed” onto the top of a stack, and the new item is copied to that address.

Counting on all of the above, those were the differences, hope this article helped you in identifying them, also do note that though the two may sound somewhat similar, but the small changes make it ubiquitous. 



Like Article
Suggest improvement
Previous
Next
Share your thoughts in the comments