-9
/* 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);                            
}
4

2 回答 2

3

您可能会考虑安装图形调试器而不是在 stackoverflow 上发布错误(Nemiver 是 linux 上最好的)

于 2012-10-14T23:57:09.703 回答
0

您正在尝试访问未分配给您的内存的某个地方。查看您的代码,以查找循环中您正在遍历数组的位置,并且您越过了该数组的边界。或者您可能正在测试您无权访问的东西的地方。既然你正在研究餐饮哲学家的问题,这可能是家庭作业,我建议你仔细看看你的设计,并注意每件事都应该做什么。如果您不想使用调试器(例如,如果您使用的是 OS161,我不怪您),但可以放入一些打印语句以找出您的程序在哪里给您 seg 错误。

于 2012-10-15T08:20:43.983 回答