当我运行时,任何人都可以帮助我我的代码,它给出了这个错误:这里的分段错误是我的代码

时间:2012-10-14 23:33:43

标签: c dining-philosopher

/* This program consists of 12 functions named, philosopher(), setPhilosopher(), 
   * take_Forks(), put_Forks(), downPhilosopher(), writeFile(), thinking(), test(), setState(),
   * readFile(), eating(),argNo, and main(). It allows a philosopher to think using the function
   * thinking, take forks using take_fork function, eat using the eating function, then
   * put forks using put_forks when done eating.     
   * philosopher: the function calls thinking(),take_fork(),eating(),and put_forks() functions.   
   * setphilosopher: this function sets the status of the philosopher whether he is thinking     
   *                or eating.
   * take_forks: this functions checks whether the left and right forks are available.
   * put_fork: this function it allows the philosopher to put down the forks when done eating.
   * downPhilospher: this function allows the philosopher to "sleep".
   * writeFile: this function writes a number to file.
   * thinking: this function allows the philosopher to think.
   * test: this function tests if the left and right forks are available.
   * setState: this function sets the state of the philosopher.
   * readFile: this function reads numbers from a file. eating: this function allows the philosophers to eat.
   */

#include<stdio.h>
#include <stdlib.h>
#include <sys/types.h>
#include <sys/wait.h>
#include <unistd.h>
#include <string.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);        
void writeFile(int numberToFile[],char *file);        
void setPhilosopher(int i,int number);                
void take_Forks(int i);                                
void downPhilosopher(int i);                        
void thinking(int j);                                
void setState(int i,int number);                    
void test(int i);                                    
void philosopher(int i);                            
void eating(int j);                                    
void put_Forks(int i);                                
int argNo(char *argv);    

void main(int args,char *argv[]){

    int i;                            
    i = argNo(argv[1]);                
    if((i < 0) || (i >= N))
    {
        fprintf(stderr,"Input not valid\n");
    }
    else
    {    
        if((i < N) && (i >= 0))                 
            philosopher(i);                        
    }

}

int argNo(char *argv){
    int number;                                    
    sscanf(argv,"%d",&number);                    
    return number;                                
}

void philosopher(int i){                                 
    int j;                                     

    for(j = 0; j < REPETITIONS; j++)
    {                             
        thinking(i);                        
        take_Forks(i);                        
        eating(i);                            
        put_Forks(i);                        
    }
}
void thinking(int j){
    int i,pid;                                
    FILE *fp = fopen(Output,"a+");            
    pid = getpid();                            
    for(i = 0;i < THINKTIME ; i++);            
    fclose(fp);                                
}

void take_Forks(int i){
    down(mutex,mutexLock);                
    setState(i,HUNGRY);                    
    test(i);                              
    up(mutexLock);                         
    downPhilosopher(i);                  

}

void eating(int j){
    int i, pid = getpid();                
    FILE *fp = fopen(Output,"a+");        
    fprintf(fp,"%d %d eating\n",pid,j); 
    fprintf(stdout,"%d %d eating\n",pid,j);            
    fflush(fp);                                        
    for(i = 0; i < EATTIME; i++);                    
    fprintf(fp,"%d %d done eating\n",pid,j);         
    fprintf(stdout,"%d %d done eating\n",pid,j);    fflush(fp);                                        
    fclose(fp);                                        
}

void put_Forks(int i){                 
    down(mutex,mutexLock);        
    setState(i,THINKING);        
    test(LEFT);                    
    test(RIGHT);                
    up(mutexLock);                
}


void downPhilosopher(int i){                 
    int semaphores[N];                             
    do
    {
        readFile(semaphores,binarySemaphore);     
    }while(semaphores[i] == 0);                 

    setPhilosopher(i,0);                         
}

void setState(int i,int number){                        
    int theStates[N];                    
    down(states,statesLock);            
    readFile(theStates,states);            
    theStates[i] = number;                 
    writeFile(theStates,states);         
    up(statesLock);                     
}

void test(int i){                 
    int theStates[N];                     
    down(states,statesLock);             
    readFile(theStates,states);         
    up(statesLock);                        
    if(theStates[i] == HUNGRY && theStates[LEFT] != EATING &&
        theStates[RIGHT] != EATING)
    {
        setState(i,EATING);                
        setPhilosopher(i,1);            
    }
}

void setPhilosopher(int i,int number){
    int semaphores[N];                                
    down(binarySemaphore,binarySemaphoreLock);        
    readFile(semaphores,binarySemaphore);             
    semaphores[i] = number;                         
    writeFile(semaphores,binarySemaphore);             
    up(binarySemaphoreLock);                         
}


void readFile(int numberFromFile[],char *file){ 
    FILE *fp = fopen(file,"r");                    
    int i;                                        
    for(i = 0; i< N; i++)
        fscanf(fp,"%d",&numberFromFile[i]);     
    fclose(fp);                                 
}

void writeFile(int numberToFile[],char *file){
    FILE *fp = fopen(file,"w");                
    int i;                                     
    for(i = 0; i< N; i++)                    
        fprintf(fp,"%d\n",numberToFile[i])
        fclose(fp);                            
}

2 个答案:

答案 0 :(得分:3)

您可以考虑安装图形化调试器,而不是在stackoverflow上发布错误(Nemiver是Linux上最好的)

答案 1 :(得分:0)

您正在尝试访问未分配给您的内存。查看代码中的循环中的位置,在这里循环遍历数组&amp;你超越了那个阵列的界限。或者您可能正在测试您无权访问的内容的地方。因为你正在研究餐饮哲学家的问题&amp;这可能是功课,我建议你仔细看看你的设计和并注意一切都应该做的事情。如果您不想使用调试器(例如,如果您正在使用OS161,我不会责怪您),而是放入一些打印语句,以找出程序给您的seg故障。