📜  进程堆栈和 CPU 堆栈之间的区别

📅  最后修改于: 2021-09-13 03:07:27             🧑  作者: Mango

临时数据如方法/函数参数、返回地址和局部变量存储在进程堆栈中,而另一方面,CPU 堆栈由一组数据字组成。它采用后进先出 (LIFO) 访问技术,这是大多数 CPU 中最常见的。在本文中,我们将找出这两者之间的详细区别。

1. 进程堆栈示例:

C
#include 
  
int main() {
   printf("Geeks for Geeks \n");
   return 0;
}


C
#include 
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;
}


输出
Geeks for Geeks 

分析:此代码仅打印出一行,因为这仅限于一个进程,该进程在完成后终止。

2. CPU 堆栈示例:

C

#include 
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;
}

输出 :

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]}

分析:如您所见,这些都是在运行此程序时显示的不同 CPU 寄存器,因为 CPU 堆栈是一个更大的进程。

进程堆栈和 CPU 堆栈之间的区别:

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.

综上所述,这些就是差异,希望本文能帮助您识别它们,另外请注意,尽管两者听起来有些相似,但细微的变化却无处不在。