linux 고수님들 도와주세염 ㅠㅠ
단순드립
//생각하는 철학자라는 소스인데 (a book on c)
//왜 에러가 나는 걸까요?/* The dining philosopher program. */
#includestdio.h
#includeunistd.h
#includestdlib.h
#includepthread.h /* for calloc() and exit() */
#define N 5 /* number of philosophers */
#define Busy_Eating 1
#define Busy_Thinking 1
#define Left(p) (p) /* chopstick macros */
#define Right(p) (((p) + 1) % N)
typedef int * semaphore;
semaphore chopstick[N]; /* global array */
int fork(void);
semaphore make_semaphore(void);
void philosopher(int me);
void pick_up(int me);
int pipe(int pd[2]);
void put_down(int me);
int read(int fd, void *buf, unsigned len);
void signal(semaphore s);
void sleep(unsigned seconds);
void wait(semaphore s);
int write(int fd, void *buf, unsigned len);
int main(void)
{
int i;
for (i = 0; i N; ++i) { /* put chopsticks on the table */
chopstick[i] = make_semaphore();
signal(chopstick[i]);
}
for (i = 0; i N - 1; ++i) /* create philosophers */
if (fork() == 0)
break;
philosopher(i); /* all executing concurrently */
return 0;
}
/* Acquire chopsticks, input is philosopher number. */
void pick_up(int me)
{
if (me == 0) {
wait(chopstick[Right(me)]);
printf(Philosopher %d picks up right chopstick\n, me);
sleep(1); /* simulate slow picking up to encourage deadlock */
wait(chopstick[Left(me)]);
printf(Philosopher %d picks up left chopstick\n, me);
}
else {
wait(chopstick[Left(me)]);
printf(Philosopher %d picks up left chopstick\n, me);
sleep(1); /* simulate slow picking up to encourage deadlock */
wait(chopstick[Right(me)]);
printf(Philosopher %d picks up right chopstick\n, me);
}
}
/* Relinquish chopsticks, input is the philosopher number. */
void put_down(int me)
{
signal(chopstick[Left(me)]);
signal(chopstick[Right(me)]);
}
/* Philosopher process, input is the philosopher number. */
void philosopher(int me)
{
char *s;
int i = 1;
for ( ; ; ++i) { /* forever */
pick_up(me);
s = i == 1 ? st : i == 2 ? nd : i == 3 ? rd : th;
printf(Philosopher %d eating for the %d%s time\n, me, i, s);
sleep(Busy_Eating);
put_down(me);
printf(Philosopher %d thinking\n, me);
sleep(Busy_Thinking);
}
}
semaphore make_semaphore(void)
{
int *sema;
sema = calloc(2, sizeof(int)); /* permanent storage */
pipe(sema);
return sema;
}
void wait(semaphore s)
{
int junk;
if (read(s[0], &junk, 1) = 0) {
printf(ERROR: wait() failed, check semaphore creation.\n);
exit(1);
}
}
void signal(semaphore s)
{
if (write(s[1], x, 1) = 0) {
printf(ERROR: write() failed, check semaphore creation.\n);
exit(1);
}
}
-
조히
제 생각엔요
void sleep(unsigned seconds);
int write(int fd, void *buf, unsigned len);
이 두개를 선언할대 문제가 생긴것 같은데요
혹시 헤더파일에 이미 선언되서 그런거 아니에요?
번호 | 제 목 | 글쓴이 | 날짜 |
---|---|---|---|
2700562 | 함수포인터에서요 (7) | 소심한여자 | 2025-07-06 |
2700530 | 전처리문 질문입니다. (1) | 아놀드 | 2025-07-05 |
2700510 | c언어를 어케하면 잘할수 있을까요.. | 연연두 | 2025-07-05 |
2700484 | 두 개가 차이가 뭔지 알려주세요...(소수 찾는 프로그램) (2) | 날위해 | 2025-07-05 |
2700426 | 인터넷 창 띄우는 질문이요 (1) | 정훈 | 2025-07-04 |
2700400 | 원넓이를 계산이요 ㅜㅜ | 천칭자리 | 2025-07-04 |
2700368 | if에 관해서 질문이요... | Orange | 2025-07-04 |
2700339 | 이거 결과값이 왜이런건지.. (4) | 그댸와나 | 2025-07-04 |
2700313 | 파일 읽어서 저장하는데 빈파일일 경우 문재가 발생하네요.. (2) | 크나 | 2025-07-03 |
2700287 | 구조체 동적할당 연습을 하는데 오류가 뜹니다...(해결) (3) | 아련나래 | 2025-07-03 |
2700264 | 문자와 숫자 동시에 입력??? | 글고운 | 2025-07-03 |
2700236 | txt파일로만 쓰고 읽게 하려면 어떻게 해야 하나요..?? (8) | 미국녀 | 2025-07-03 |
2700211 | 전위 연산자 (2) | 어른처럼 | 2025-07-02 |
2700183 | C에서 파일이름을 받고, 그 파일의 사이즈를 출력해줘야하는데 내용이 출력이 안되네요 ;ㅅ; | 피스케스 | 2025-07-02 |
2700150 | 꼭좀 도와주세요ㅠㅠㅠ | 호습다 | 2025-07-02 |
2700095 | 연산문제...질문... | 오빤테앵겨 | 2025-07-01 |
2700070 | while문 , 3의배수 출력하는 프로그램좀 짜주세욤. | 횃불 | 2025-07-01 |
2700041 | 초보인데요 ㅎ 배열안에 배열을 집어넣을수 있나요?? | 헛장사 | 2025-07-01 |
2700012 | 배열// (1) | 전갈자리 | 2025-07-01 |
2699895 | 무한루프에 빠집니다.!! 해결좀부탁드려요 (10) | 선아 | 2025-06-30 |