C 当我运行代码时,有人能帮我吗?它给出了这个错误:分段错误这里是我的代码 /*此程序由12个函数组成,分别命名为、哲学家()、设置哲学家(), *take_Forks()、put_Forks()、downshipher()、writeFile()、think
当我运行代码时,有人能帮我吗?它给出了这个错误:分段错误这里是我的代码C 当我运行代码时,有人能帮我吗?它给出了这个错误:分段错误这里是我的代码 /*此程序由12个函数组成,分别命名为、哲学家()、设置哲学家(), *take_Forks()、put_Forks()、downshipher()、writeFile()、think,c,dining-philosopher,C,Dining Philosopher,当我运行代码时,有人能帮我吗?它给出了这个错误:分段错误这里是我的代码 /*此程序由12个函数组成,分别命名为、哲学家()、设置哲学家(), *take_Forks()、put_Forks()、downshipher()、writeFile()、thinking()、test()、setState(), *readFile()、eating()、argNo和main()。它允许哲学家使用函数进行思考 *思考,用take_fork函数拿叉子,用eat函数吃饭,然后 *吃完后用叉子放叉子。 *哲学家
/*此程序由12个函数组成,分别命名为、哲学家()、设置哲学家(),
*take_Forks()、put_Forks()、downshipher()、writeFile()、thinking()、test()、setState(),
*readFile()、eating()、argNo和main()。它允许哲学家使用函数进行思考
*思考,用take_fork函数拿叉子,用eat函数吃饭,然后
*吃完后用叉子放叉子。
*哲学家:该函数调用thinking()、take_fork()、eating()和put_forks()函数。
*设置哲学家:该函数设置哲学家是否在思考的状态
*或者吃东西。
*take_forks:该函数检查左、右叉子是否可用。
*放叉子:这个功能允许哲学家在吃完东西后放下叉子。
*downPhilospher:此函数允许哲学家“睡眠”。
*writeFile:此函数将数字写入文件。
*思考:这个功能允许哲学家思考。
*测试:此功能测试左右拨叉是否可用。
*setState:此函数用于设置哲学家的状态。
*readFile:此函数用于从文件中读取数字。吃:这个功能允许哲学家吃东西。
*/
#包括
#包括
#包括
#包括
#包括
#包括
#包括
#定义n5
#定义重复10
#定义EATTIME 3000000
#定义THINKTIME EATTIME*3
#定义左(i+N-1)%N
#定义右侧(i+1)%N
#定义饥饿1
#定义饮食2
#定义思维0
#定义互斥“互斥”
#定义互斥锁“mutex.lock”
#定义输出“输出”
#定义状态“状态”
#定义statesLock“states.lock”
#定义二进制信号量“信号量”
#定义binarySemaphoreLock“semaphore.lock”
#定义向上(锁定)取消链接(锁定)
#定义down(lock1,lock2)while(link(lock1,lock2)=-1);
void readFile(int numberFromFile[],char*文件);
void writeFile(int numberToFile[],char*文件);
无效设置(整数i,整数编号);
无效进货叉(int i);
虚空(int i);
虚无思维(intj);
无效设置状态(整数i,整数编号);
孔隙试验(int i);
虚空哲学家(int i);
虚空进食(intj);
无效放货叉(int i);
int argNo(字符*argv);
void main(int args,char*argv[]){
int i;
i=argNo(argv[1]);
如果((i<0)|(i>=N))
{
fprintf(stderr,“输入无效\n”);
}
其他的
{
如果((i=0))
哲学家(一);
}
}
整数argNo(字符*argv){
整数;
sscanf(argv、%d、&number);
返回号码;
}
虚空哲学家(int i){
int j;
对于(j=0;j<重复;j++)
{
思考(一);
拿叉子(我);
进食(i);
放叉子(一);
}
}
虚空思维(int j){
inti,pid;
FILE*fp=fopen(输出“a+”);
pid=getpid();
for(i=0;i/* 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);
}