2012-10-14 17 views
-8
/* 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);        
} 
+5

你的第一步應該是使用一個調試器,和/或將'printf'調用放到你的代碼中...... *不要*在StackOverflow上發佈你的源代碼,並期望別人爲你做。 – paddy

+2

沒有*方法*我們正在調試那段代碼。只要調用它沒有參數是未定義的行爲,並可能seg-fault,它只會從那裏變得更糟。 – WhozCraig

回答

3

你可能會考慮在計算器上安裝一個圖形化調試器,而不是發佈的bug(Nemiver是最好的在Linux上)

0

某處您正在嘗試訪問未分配給您的內存。通過你的代碼查找循環中的位置,在這裏你通過一個數組&來遍歷數組的邊界。或者您可能正在測試您無法訪問的內容的地方。既然你在用餐哲學家的問題&工作這可能是功課,我會建議翻翻你的設計&並注意哪些一切該做的。如果你不想使用調試器(例如,如果你使用的是OS161,我不會責怪你),但是要寫一些打印語句來找出你的程序在哪裏給你seg故障。

相關問題