/* 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);
}
-8
A
回答
3
你可能會考慮在計算器上安裝一個圖形化調試器,而不是發佈的bug(Nemiver是最好的在Linux上)
0
某處您正在嘗試訪問未分配給您的內存。通過你的代碼查找循環中的位置,在這裏你通過一個數組&來遍歷數組的邊界。或者您可能正在測試您無法訪問的內容的地方。既然你在用餐哲學家的問題&工作這可能是功課,我會建議翻翻你的設計&並注意哪些一切該做的。如果你不想使用調試器(例如,如果你使用的是OS161,我不會責怪你),但是要寫一些打印語句來找出你的程序在哪裏給你seg故障。
相關問題
- 1. 運行我的Java代碼,當我面對這個錯誤 - >
- 2. 我發現這個錯誤當我運行該Java代碼
- 3. 當我寫這行到flex代碼,我得到這個錯誤:
- 4. 爲什麼這個Python代碼給我錯誤,幫我
- 5. 我試圖運行,下面這段代碼,這是給我的錯誤
- 6. 誰能告訴我這個代碼是什麼錯誤
- 7. 幫我這個Java代碼
- 8. 我不能運行這個python代碼
- 9. 我需要這個亂碼的簡短php代碼。誰能幫我?
- 10. 我在哪裏出錯這個代碼
- 11. 這個代碼是什麼問題,我不知道誰能幫我:
- 12. 當我運行我的android UIAutomator代碼時,它顯示錯誤
- 13. 當我運行這個php代碼時發生致命錯誤
- 14. 我的代碼存在細分故障
- 15. 爲什麼這個C++代碼在運行時給我分段錯誤錯誤?
- 16. 誰能幫助解決我的代碼
- 17. 分割錯誤:11,當我運行C代碼
- 18. 錯誤我得到我的python代碼?錯誤是這一行:密碼= AES.new(鍵)
- 19. 誰能告訴我爲什麼我在這個C代碼中出現分割錯誤?
- 20. 我輸入了這段代碼,當我點擊構建時,它給了我這個錯誤?
- 21. 當我運行下面的代碼...我得到一個錯誤
- 22. 請幫助我瞭解這行代碼
- 23. 分割故障後小幅修改我的代碼
- 24. 有人可以幫我在這裏這個代碼dosent工作?
- 25. 這段代碼爲什麼會給我一個分段錯誤?
- 26. 當我運行這段代碼時,它給出了錯誤(smtp.Send(mm);)我不知道如何解決它
- 27. 我不斷收到一個錯誤,這是使用類我第一次來,這裏是我的代碼:
- 28. 幫助我瞭解C#這個代碼
- 29. 請幫助我在這個MySQL代碼
- 30. 請幫我簡化這個jQuery代碼
你的第一步應該是使用一個調試器,和/或將'printf'調用放到你的代碼中...... *不要*在StackOverflow上發佈你的源代碼,並期望別人爲你做。 – paddy
沒有*方法*我們正在調試那段代碼。只要調用它沒有參數是未定義的行爲,並可能seg-fault,它只會從那裏變得更糟。 – WhozCraig