In 1965, Dijkstra posed and solved a synchronization problem he called the
dining philosophers problem. ........ The problem can be stated quite simply
as follows. Five philosophers are seated around a circular table. Each philo
sopher has a plate of spaghetti. The spaghetti is so slippery that a philoso
pher needs two forks to eat it. Between each pair of plates is one fork.
The life of a philosopher consists of alternate periods of eating and think
ing. When a philosopher gets hungry, she tries to acquire her left and right
fork, one at a time, in either order. If successful in acquiring two forks,
she eats for a while, then puts down the forks and continues to think. The
key question is: Can you write a program for each philosopher that does what
it is supposed to do and never gets stuck?
--from
1<operating and="" design="" implementation="" systems:="">
2written by Andrew S. Tanenbaum
3typed by foolball :-P
4
5Programme provided by ya
6
7: : 法一: 用公共文件,按照严格轮流执行
8: : #include <stdio.h>
9: : #include <stdlib.h>
10: : #include <unistd.h>
11: : #include <sys\type.h>
12: : #define N 5
13: : int i,j,t,status;
14: : FILE * f;
15: : char *state[N];
16: : main()
17: : {
18: : f=fopen("/share","w+");
19: : putc(j,f);
20: : if (fork())
21: : { if (fork())
22: : { if (fork())
23: : { if (fork())
24: : { if (fork())
25: : {waitpid(-1,*status,0);
26: : fclose(f);}
27: : else
28: : philosophy(4);}
29: : else
30: : philosophy(3);}
31: : else
32: : philosophy(2);}
33: : else
34: : philosophy(1);}
35: : else
36: : philosophy(0);
37: : }
38: : void philosophy(int i)
39: : {
40: : state[i ="thinking";
41: : printf("%d%s\n",i,"is thinking");
42: : for(t=0;t<=rand()+10000;t++);
43: : state[i ="hungry";
44: : printf("%d%s\n",i,"is hungry");
45: : for(t=0;t<=rand()+10000;t++);
46: : for(;i!=j;)
47: : {fseek(f,0l,0);
48: : j=getc(f);}
49: : state[i ="eating";
50: : printf("%d%s\n",i,"is eating");
51: : j=(j+1)% N
52: : fseek(f,0l,0);
53: : putc(j,f);
54: : }
55: : 法二:通过文件加锁实现
56: : #include <stdio.h>
57: : #include <stdlib.h>
58: : #define N 4
59: : FILE *f;
60: : int i, status;
61: : char *state[N];
62: : void philosofy(int i);
63: : void main()
64: : {
65: : if ((f=fopen("turn", "w+"))==NULL)
66: : {
67: : printf("Cann't open this file");
68: : exit(0);
69: : }
70: : if (fork())
71: : {
72: : if(fork())
73: : {
74: : if(fork())
75: : {
76: : if(fork())
77: : {
78: : if(fork())
79: : {
80: : waitpid(-1, &status, 0);
81: : fclose(f);
82: : }
83: : else
84: : philosofy(4);
85: : }
86: : else
87: : philosofy(3);
88: : }
89: : else
90: : philosofy(2);
91: : }
92: : else
93: : philosofy(1);
94: : }
95: : else
96: : philosofy(0);
97: : }//end of main
98: : void philosophy(int i)
99: : { int t;
100: : state[i ="thinking";
101: : printf("%d%s\n",i,"is thinking");
102: : for(t=0;t<=rand()+10000;t++);
103: : state[i ="hungry";
104: : printf("%d%s\n",i,"is hungry");
105: : for(t=0;t<=rand()+10000;t++);
106: : while ((f=fopen("turn.lock","r"))!=NULL);
107: : link ("turn","turn.lock");
108: : state[i ="eating";
109: : printf("%d%s\n",i," is eating");
110: : for(t=1; t<=10000+rand();t++) ;
111: : unlink ("turn");
112: : }</stdlib.h></stdio.h></sys\type.h></unistd.h></stdlib.h></stdio.h></operating>