0

编辑 - 我算出算术错误,但我仍然有返回错误

出于某种原因,我的程序给了我两个错误。第一个错误是我的每个方法()末尾的“返回”并没有结束该方法并将我带回 main。我的第二个问题在第 23 行,其中 pfNum = mainSize/pageSize; 给我一个“SIGFPE,算术异常”,不知道为什么这两种情况都会发生,谁能帮帮我?

谢谢

#include <stdio.h>
#include <stdlib.h>
#include <math.h>

/* Define page table as dynamic structure containing virtual page and page frame 
   and initialize variable as pointer to structure */
struct table{
    int vp;
    int pf;
}*pageTable = NULL;
/* Declare global var's */
int mainSize,pageSize,policy,pfNum;
/**********************************************************************/
void option1(){
/* Declare local var's */
    int k;
/* Prompt for main memory size, page size, and replacement policy */
    printf("Enter main memory size(words): ");
    scanf("%d",&mainSize);
    printf("Enter page size(words/page): ");
    scanf("%d",&pageSize);
    printf("Enter replacement policy(0=LRU, 1=FIFO): ");
    scanf("%d",&policy);
    pfNum = mainSize/pageSize;
/* Allocate and initialize page table based on number of entries */
    pageTable = malloc(pfNum *sizeof(pageTable));
    for(k=0;k<pfNum;k++){
        pageTable[k].vp=-1;
        pageTable[k].pf=k;
    }
return;
}
/**********************************************************************/
void option2(){
/* Declare local var's */
    int va,page,offset,i=0,temp;
/* Prompt for virtual address */
    printf("Enter virtual memory address to access: ");
    scanf("%d",&va);
/* Translate virtual mem addr to virtual page and offset*/
    page = va/pageSize;
    offset = va%pageSize;
/* Check for end of table, unallocated entry, or matched entry in table 
and update table appropriately; while none of three cases, keep looping */

    while(i<pfNum && pageTable[i].vp!=1 && pageTable[i].vp!=page)
        i++;
    if(i<=pfNum){
        int j;
        temp = pageTable[0].pf;
        for(j=1;j<pfNum;j++)
            pageTable[j-1]=pageTable[j];
        pageTable[j].vp=page;
        pageTable[j].pf=temp;
        printf("Page Fault!");
    }
    else if(pageTable[i].vp==-1){
        pageTable[i].vp = page;
        printf("Page fault!");
    }
    else if(pageTable[i].vp==page){
        temp = pageTable[i].pf;
        int l,address;
        for(l=i+1;l<pfNum-1;l++)
            pageTable[l-1]=pageTable[l];
        pageTable[l].vp = page;
        pageTable[l].pf = temp;
        address = (temp*pageSize)+offset;
        printf("Virtual address %d maps to physical address %d",va,address);
    }
 return;
}
 /**********************************************************************/
void option3(){
 /* Declare local var's */
    int u;
    for(u=0;u<pfNum;u++ && pageTable[u].vp!=-1)
        printf("VP %d --> PF %d",pageTable[u].vp,pageTable[u].pf);
 /* Print out each valid virtual page and page frame pair in table */

 return;
}


 /**********************************************************************/
 int main(){
 /* Declare local var's */
    int choice;
 /* Until user quits, print menu of options, prompt for user input, and select     appropriate option */
    printf("/n");
    printf("Virtual memory to Main memory mapping:\n");
    printf("--------------------------------------\n");
    printf("1) Set parameters\n");
    printf("2) Map virtual address\n");
    printf("3) Print page table\n");
    printf("4) Quit\n");
    printf("\n");
    printf("Enter Selection: ");
    scanf("%d",&choice);
    printf("\n");
    while(choice!=4){
        if(choice==1)
            option1();
        else if(choice==2){
            option2();
        }
        else if(choice==3)
            option3();
    }
    printf("Goodbye. Have a nice day.");
return 1;
}
4

2 回答 2

1

“SIGFPE,算术异常”异常很可能是由被零除引起的。

于 2012-11-03T01:15:42.183 回答
1

一个问题是,一旦您做出初始选择,就不会再改变选择,因此程序会循环执行您的初始选择(可能什么都不做,因为您不验证零、否定选择或值大于四)。这可能使您的函数看起来“不返回”,但实际上它们确实返回了;他们几乎立即再次被呼叫。

您可能需要在每次循环时提示一个新的选择,这会建议一个函数来提示并返回您从while循环中调用的选择。

你至少有一个"/n"你可能想要的地方"\n"。您的告别信息缺少换行符;许多其他消息(例如"Page Fault!"消息)也是如此。您没有检查您的输入功能是否成功。您不检查内存分配是否成功。

您的 SIGFPE 可能来自被零除;在执行除法之前打印您正在处理的值。

于 2012-11-03T01:31:48.440 回答