|
今天写了一个模拟页面淘汰的FIFO程序.好像已经执行完成了.但不知道为什么在后面多了一个Segmentation fault.
- #include <unistd.h>
- #include <stdio.h>
- #include <stdlib.h>
- #define AMOUNT 10
- struct PAGE{
- int No;
- int DATA;
- };
- void FIFO(int k,int m,struct PAGE Vmemory[],struct PAGE memory[],int *shortage);
- void first(int k,struct PAGE Vmemory[],struct PAGE memory[]);
- void randarray(int a[],int k);
- main()
- {
- struct PAGE memory[3],Vmemory[AMOUNT];
- int k=AMOUNT,shortage3=3,aVmemory[AMOUNT];
- int shortage4=4,shortage5=5,shortage6=6,shortage7=7,shortage8=8;
- int i,j;
- randarray(aVmemory,k);
- for(i=0;i<k;i++)
- Vmemory[i].No=aVmemory[i];
- for(i=0;i<k;i++)
- printf("%5d",Vmemory[i].No);
- printf("\n");
- first(3,Vmemory,memory);
- FIFO(3,AMOUNT,Vmemory,memory,&shortage3);
- printf("shortage3=%d\n",shortage3);
- first(4,Vmemory,memory);
- FIFO(4,AMOUNT,Vmemory,memory,&shortage4);
- printf("shortage4=%d\n",shortage4);
- first(5,Vmemory,memory);
- FIFO(5,AMOUNT,Vmemory,memory,&shortage5);
- printf("shortage5=%d\n",shortage5);
- first(6,Vmemory,memory);
- FIFO(6,AMOUNT,Vmemory,memory,&shortage6);
- printf("shortage6=%d\n",shortage6);
- first(7,Vmemory,memory);
- FIFO(7,AMOUNT,Vmemory,memory,&shortage7);
- printf("shortage7=%d\n",shortage7);
- first(8,Vmemory,memory);
- FIFO(8,AMOUNT,Vmemory,memory,&shortage8);
- printf("shortage8=%d\n",shortage8);
- }
- void randarray(int a[],int k)
- {
- int i,s=0;
- srand(getpid());
- s=(float)319.0*(rand()%32767)/32767.0+1.0;
- for(i=0;i<k;i+=4)
- {
- a[i]=s+512;
- a[i+1]=a[i]+513;
- a[i+2]=(float)a[i]*(rand()%32767)/32767.0+512;
- a[i+3]=a[i+2]+1;
- s=(float)(rand()%32767)*(318.0-a[i+2])/32767.0+a[i+2]+2.0+512;
- }
- }
- void FIFO(int k,int m,struct PAGE Vmemory[],struct PAGE memory[],int *shortage)
- {
- int i,j,poiter=0;
- struct PAGE temp;
- for(j=k;j<m;j++)
- {
- for(i=0;i<k;i++)
- if(Vmemory[j].No==memory[i].No) break; /*If the page is found,l
- eave the circle*/
- if(i==k) /*If the page is not found,enchange the pages*/
- {
- temp=memory[poiter];
- memory[poiter]=Vmemory[j];
- Vmemory[j]=memory[poiter];
- poiter+=1;*shortage+=1;
- if(poiter==k) poiter=0;
- }
- }
- }
- void first(int k,struct PAGE a[],struct PAGE b[])
- {
- int i;
- for(i=0;i<k;i++)
- b[i]=a[i];
- }
复制代码
运行的结果是:
shortage3=10
shortage4=10
shortage5=10
shortage6=10
shortage7=10
shortage8=10
Segmentation fault
我就是不明白.最终的运行结果都正确地出现了.却还是多了个可能是堆栈错误.
用GDB调试也能够执行到最后,但同样提示:
Program received signal SIGSEGV, Segmentation fault.
0x400272b8 in ?? ()
而且我只打印
shortage3=10
shortage4=10
shortage5=10
则没有提示错误.
|
|