c - 段错误: 11 Seems unusual

标签 c segmentation-fault

请帮我找到这个“段错误:11”。 argv 输入看起来不错。顺便说一句,这是一个就餐哲学家问题。它在一小时前工作,但在 minix 机器上,但现在在 Unix 机器上它无法运行。请帮我解决这个愚蠢的错误。

#include <stdio.h>
#include <string.h>
#include <stdlib.h>
#include <unistd.h>
#include <sys/types.h>

#define N   5 
#define REPETITIONS 10 
#define EATTIME 3000000
#define THINKTIME EATTIME * 3 
#define LEFT    (i+N-1)%N
#define RIGHT   (i+1)%N
#define HUNGRY   1
#define EATING   2
#define THINKING 0
#define mutex   "mutex"
#define mutexLock "mutex.lock"
#define Output "output" 
#define states "states"
#define statesLock "states.lock"
#define binarySemaphore "semaphore"
#define binarySemaphoreLock "semaphore.lock"
#define up(lock) unlink(lock)
#define down(lock1,lock2) while(link(lock1,lock2)== -1);

void readFile(int numberFromFile[],char *file); /* declaring readfile() */
void writeFile(int numberToFile[],char *file);  /* declaring writeFile() */
void setPhilosopher(int i,int number);      /* declaring setPhilosopher() */
void take_Forks(int i);             /* declaring take_Forks() */
void downPhilosopher(int i);            /* declaring downPhilosopher() */
void thinking(int j);               /* declaring thinking() */
void setState(int i,int number);        /* declaring setState() */
void test(int i);               /* declaring test() */
void philosopher(int i);            /* declaring philosopher() */
void eating(int j);             /* declaring eating() */
void put_Forks(int i);              /* declaring put_Forks() */
int argNo(char *argv);              /* declaring arg number() */



int main(int args,char *argv[])
{
    int i;              /* declaring i*/
    i = argNo(argv[1]);     /* assigning argument number to i*/ 
    if((i < 0) || (i >= N))
    {
        fprintf(stderr,"Input not valid\n");    /* displays an error message*/
                            /* when number is less than 0*/
                            /* number is more than N */
    }
    else
    {   
        if((i < N) && (i >= 0))     /* else calls the philosopher function*/
            philosopher(i);     /* and passes the number to it */
//          printf("Hello %d\n", i);
    }   
}


int argNo(char *argv)
{
    int number;         /* declaring number*/
    sscanf(argv,"%d",&number);  /* gets number from the command line */
    return number;          /* return number*/
}


void philosopher(int i)
{                               
    int j;                  /* declaring j*/

    for(j = 0; j < REPETITIONS; j++)
    {                           
        thinking(i);            /* invoking thinking function*/
        take_Forks(i);          /* invoking take_Forks function*/
        eating(i);          /* invoking eating function*/
        put_Forks(i);           /* invoking put_Forks function*/
    }
}

void thinking(int j)
{
    int i,pid;              /* declaring i and pid */
    FILE *fp = fopen(Output,"a+");      /* creating and opening a file*/
    pid = getpid();             /* getting process id*/
    for(i = 0;i < THINKTIME ; i++);     /* philosopher is thinking */
    fclose(fp);             /* closing the file*/
}


void take_Forks(int i)
{
    down(mutex,mutexLock);      /* entering critical region*/
    setState(i,HUNGRY);     /* setting State to hungry */
    test(i);            /* invoking test function*/
    up(mutexLock);          /* exit critical region*/
    downPhilosopher(i);         /* invoking downPhilosopher function*/

}



void eating(int j)
{
    int i;                      /* declaring i as an int */
    int pid = getpid();             /* getting the process ID */
    FILE *fp = fopen(Output,"a+");          /* creating and opening file */
    fprintf(fp,"%d %d eating\n",pid,j);         /* writing a message to a file*/
    fprintf(stdout,"%d %d eating\n",pid,j);     /* displaying to stdout*/
    fflush(fp);                 /* flushing file*/
    for(i = 0; i < EATTIME; i++);           /* philosopher eating*/ 
    fprintf(fp,"%d %d done eating\n",pid,j);    /* writing message to file*/    
    fprintf(stdout,"%d %d done eating\n",pid,j);    /* displaying to stdout*/
    fflush(fp);                 /* flushing file*/
    fclose(fp);                 /* closing file*/
}




void put_Forks(int i)
{               
    down(mutex,mutexLock);      /* entering critical region*/
    setState(i,THINKING);       /* setting state to thinking */ 
    test(LEFT);         /* checks if left and right */
    test(RIGHT);            /* philosophers want to eat */
    up(mutexLock);          /* exit critical region*/
}



void downPhilosopher(int i)
{               
   int semaphores[N];              /* declaring semaphore array*/   
   do
   {
     readFile(semaphores,binarySemaphore); /* reading binarySemaphore into semaphore */
   }while(semaphores[i] == 0);         /* spin locks if semaphore is 0 */

   setPhilosopher(i,0);            /* setting the philosopher's state to 0*/
}

void setState(int i,int number)
{                       
    int theStates[N];           /* declaring States array*/
    down(states,statesLock);        /* enters critical region*/
    readFile(theStates,states);     /* read states from file*/
    theStates[i] = number;          /* changing the state */
    writeFile(theStates,states);        /* writes a state to a file*/   
    up(statesLock);             /* exit critical region*/
}


void test(int i)
{               
    int theStates[N];           /* declaring theStates array*/
    down(states,statesLock);        /* enters critical region*/
    readFile(theStates,states);         /* read file states*/
    up(statesLock);             /* exit critical region*/
    if(theStates[i] == HUNGRY && theStates[LEFT] != EATING &&
                theStates[RIGHT] != EATING)
    {
        setState(i,EATING); /* set the state of philosopher to eating*/
        setPhilosopher(i,1);    /* set the semaphore to 1*/
    }
}



void setPhilosopher(int i,int number)
{
    int semaphores[N];                          /* declaring semaphores[]*/ 
    down(binarySemaphore,binarySemaphoreLock);  /* enters critical region*/
    readFile(semaphores,binarySemaphore);       /* reading from file*/
    semaphores[i] = number;             /* updates the semaphore array*/
    writeFile(semaphores,binarySemaphore);      /* writing semaphore to file*/
    up(binarySemaphoreLock);            /* exit critical region*/
}


void readFile(int numberFromFile[],char *file)
{ 
    FILE *fp = fopen(file,"r");         /* creating and opening file*/
    int i;                  /* declaring i as int */
    for(i = 0; i< N; i++)
    fscanf(fp,"%d",&numberFromFile[i]);     /* reading from file into*/
                        /* numberFromFile array*/
    fclose(fp);                 /* closing the file*/
}


void writeFile(int numberToFile[],char *file)
{
    FILE *fp = fopen(file,"w");     /* creating and opening a file */
    int i;                  /* declaring i as int */
    for(i = 0; i< N; i++)                   
        fprintf(fp,"%d\n",numberToFile[i]); /* writing  */ 
                                /* numberToFile array to file*/
    fclose(fp);                     /* closing the file*/
}

最佳答案

这里的逻辑没有完全验证输入:

i = argNo(argv[1]);

如果我没有提供任何参数,而您的代码需要数字作为命令行上的第一个参数,我可以复制段错误。

一个快速修复方法可能是在尝试引用 argv[1](可能尚未提供)之前检查 argc(或代码中的“args”)的大小。

关于c - 段错误: 11 Seems unusual,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/12939657/

相关文章:

c - 如何在具有英特尔双核的 Linux 上查找 C 代码部分的执行时间(如果可能的话以纳秒为单位)?

c++ - tinyxml2 的奇怪段错误

c - 将动态分配的二维数组分配给结构中的双指针时出现段错误

c++ - 当我尝试将 char* 分成标记时出现段错误

node.js - Docker 中运行的 Node.JS 应用程序出现段错误

c - 在 OpenGL 中围绕视线旋转时如何防止变形?

c++ - 尝试从 C 调用 ObjC 方法时出现链接器错误

c - Readline 完成问题

c - 我需要相对于三角形平移 3d 点,就好像三角形在其他地方一样

c - 由于 malloc 导致段错误