请帮我找到这个“分段错误: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*/
}