哲学家就餐问题代码
合集下载
相关主题
- 1、下载文档前请自行甄别文档内容的完整性,平台不提供额外的编辑、内容补充、找答案等附加服务。
- 2、"仅部分预览"的文档,不可在线预览部分如存在完整性等问题,可反馈申请退款(可完整预览的文档不适用该条件!)。
- 3、如文档侵犯您的权益,请联系客服反馈,我们会尽快为您处理(人工客服工作时间:9:00-18:30)。
if(chairflg[i][0]==0){ /*the chair is empty*/
chairflg[i][0]=1;
chairflg[i][1]=(int)i;/*philosopher(i) toke the chair*/
break;
}
}
dining_num++;
sem_post(&mutex);
/*create the threads*/
for(i=0;i<NUM_THREADS_P;i++)
pthread_create(&threads_p[i],NULL,(void*)philosopher_thread,(void*)(i));
/*wait the threads to exit*/
case HUNGRY:
strcpy(strState,"HUNGRY");
if(!(sem_wait(&semph[leftIndex])))
{
if(!(sem_wait(&semph[rightIndex])))
{
myState = EATING;
strcpy(strState,"EATING");
}
}
void take_forks(int i)
{
pthread_mutex_lock(&mutex);
state[i] = HUNGRY;
test(i);
pthread_mutex_unlock(&mutex);
sem_wait(&semph[i]);
}
void put_forks(int i)
{
#include <pthread.h>
#include <semaphore.h>
#define PHILOSOPHER_NUM 5
#define THINKING 1
#define HUNGRY 2
#define EATING 3
pthread_mutex_t mutex;
sem_t semph[PHILOSOPHER_NUM];
sleep(rand()%3);
sem_wait(&print_mutex);
printf("philosopher %d is dining.when he is dining ,there are %d philosophers at table.\n",(int)i,dining_num);
for(i=0;i<CHAIR_NUM;i++){
#include <pthread.h>
#include <semaphore.h>
#define PHILOSOPHER_NUM 5
#define THINKING 0
#define HUNGRY 1
#define EATING 2
pthread_mutex_t mutex;
sem_t semph[PHILOSOPHER_NUM];
sem_wait(&chopsticks[i]);
printf("philosopher %d get chopstics %d\n",i,i);
sem_wait(&chopsticks[(i+1)%CHOP_NUM]);
printf("philosopher %d get chopstics %d\n",tid,(i+1)%CHOP_NUM);
}
else
{//不能同时拿起则都不拿起
sem_post(&semph[leftIndex]);
}
}
break;
case EATING:
sem_post(&semph[leftIndex]);
sem_post(&semph[rightIndex]);
myState=THINKING;
strcpy(strState,"THINKING");
if(ret)
{
errNum++;
}
usleep(20);
}
if(errNum)
{
printf("thread create err! errnum=%d \n",errNum);
}
sleep(1);
}
#include <unistd.h>
#include <stdio.h>
#include <stdlib.h>
for(i=0;i<CHOP_NUM;i++)
{
sem_init(&chopsticks[i],0,1);
}
sem_init(&mutex,0,1);
sem_init(&mutex1,0,1);
sem_init(&print_mutex,0,1);
for(i=0;i<4;i++)chairflg[i][0]=0;
usleep(sleepTime);
}
}
int main()
{
int i,ret;
int threadId[PHILOSOPHER_NUM];
int errNum=0;
pthread_t t_phThread[PHILOSOPHER_NUM];
//pthread_t t_threadInfo;
srand(getpid());
break;
}
pthread_mutex_lock(&mutex);
printf("pholosopher %d begin %s\n",mythreadId,strState);
pthread_mutex_unlock(&mutex);
sleepTime = 1+(int)(5.0*rand()/(RAND_MAX+1.0));
#define LEFT (i + PHILOSOPHER_NUM -1)%PHILOSOPHER_NUM //i的左领居编号
#define RIGHT (i + 1)%PHILOSOPHER_NUM //i的右领居编号
int state[PHILOSOPHER_NUM];
void think(int i)
#include<stdio.h>
#include<stdlib.h>
#include<string.h>
#include<pthread.h>
#include<semaphore.h>
#include<sys/types.h>
#include<errno.h>
#include<unistd.h>
#include<signal.h>
#define NUM_THREADS_P 5 /*define the number of philosopher*/
#define CHAIR_NUM 4
#define CHOP_NUM 5
int chairflg[CHAIR_NUM][2],dining_num=0;
sem_t chair,chopsticks[CHOP_NUM],mutex,mutex1,print_mutex;//设定信号量
sleepTime = 1 + (int)(5.0*rand()/(RAND_MAX+1.0));
usleep(sleepTime*10);
}
}
int main()
{
int i,ret;
int threadId[PHILOSOPHER_NUM];
int errNum=0;
pthread_t t_phThread[PHILOSOPHER_NUM];
sem_post(&chopsticks[(i+1)%CHOP_NUM]);
sem_wait(&mutex1);
for(i=0;i<CHAIR_NUM;i++){
if(chairflg[i][1]==(int)i){
chairflg[i][0]=0;
break;
}
}
dining_num--;
sem_post(&mutex1);
sem_destroy(&mutex1);
sem_destroy(&print_mutex);
return 0;
}
void* philosopher_thread(int tid)
{
int i;
i=tid;
sem_wait(&chair);
sem_wait(&mutex);
for(i=0;i<CHAIR_NUM;i++){
if( ret )
{
errNum++;
}
for(i=0;i<PHILOSOPHER_NUM;i++)
{
threadId[i] = i;
sem_init(&semph[i],0,1);
ret=pthread_create(&t_phThread[i], NULL, (void *)philosopher, (void*)(&threadId[i]));
pthread_t t_threadInfo;
srand(getpid());
pthread_mutex_init(&mutex,NULL);
//ret = pthread_create(&t_threadInfo, NULL, (void *) threadInfo, (void *) NULL);
pthread_mutex_lock(&mutex);
state[i] = THINKING;
test(LEFT);
test(RIGHT);
pthread_mutex_unlock(&mutex);
}
void philosopher(int *index)
{
int mythreadId = (*index);
sem_post(&chair);
printf("philosopher %d leaves\n",(int)i);
return ((void*)0);
}
$$$$$$$$$$$$$$$$$$$$$$$$$
#include <unistd.h>
#include <stdio.h>
#include <stdlib.h>
rightIndex = (mythreadId + 1) % PHILOSOPHER_NUM;
myState = THINKING;
while(1)
{
switch(myState)
{
case THINKING:
myState = HUNGRY;
strcpy(strState,"HUNGRY");
break;
void philosopher(int *index)
{
int mythreadId;
char myState,strState[128];
int leftIndex;
int rightIndex;
int sleepTime;
mythreadId = (*index);
leftIndex = mythreadId + PHILOSOPHER_NUM-1)% PHILOSOPHER_NUM;
{
}
void eat(int i)
{
}
void test(int i)
{
if((state[i] == HUNGRY )
&& (state[LEFT] != EATING)
&& (state[RIGHT]!= EATING))
{
state[i] = EATING;
sem_post(&semph[i]);
pthread_mutex_init(&mutex,NULL);
for(i=0;i<PHILOSOPHER_NUM;i++)
{
threadId[i] = i;
sem_init(&semph[i],0,1);
int sleepTime;
bzero(state,0);
while(1)
{
think(mythreadId);
take_forks(mythreadId);
eat(mythreadId);
put_forks(mythreadId);
// sleep a random time : between 1 - 5 s
pthread_t threads_p[NUM_THREADS_P]Fra Baidu bibliotek /*philosopher*/
void* philosopher_thread(int tid);
int main(){
int i;
sem_init(&chair,0,CHAIR_NUM); /*set the value of semaphores*/
for(i=0;i<NUM_THREADS_P;i++)
pthread_join(threads_p[i],NULL);
/*destroy the semaphores*/
sem_destroy(&chair);
sem_destroy(&chopsticks[CHOP_NUM]);
sem_destroy(&mutex);
if(chairflg[i][0]==1){
printf("poilosopher %d in chair %d. ",chairflg[i][1],i+1);
}
}
printf("\n\n");
sem_post(&print_mutex);
sleep(rand()%3);
sem_post(&chopsticks[i]);
chairflg[i][0]=1;
chairflg[i][1]=(int)i;/*philosopher(i) toke the chair*/
break;
}
}
dining_num++;
sem_post(&mutex);
/*create the threads*/
for(i=0;i<NUM_THREADS_P;i++)
pthread_create(&threads_p[i],NULL,(void*)philosopher_thread,(void*)(i));
/*wait the threads to exit*/
case HUNGRY:
strcpy(strState,"HUNGRY");
if(!(sem_wait(&semph[leftIndex])))
{
if(!(sem_wait(&semph[rightIndex])))
{
myState = EATING;
strcpy(strState,"EATING");
}
}
void take_forks(int i)
{
pthread_mutex_lock(&mutex);
state[i] = HUNGRY;
test(i);
pthread_mutex_unlock(&mutex);
sem_wait(&semph[i]);
}
void put_forks(int i)
{
#include <pthread.h>
#include <semaphore.h>
#define PHILOSOPHER_NUM 5
#define THINKING 1
#define HUNGRY 2
#define EATING 3
pthread_mutex_t mutex;
sem_t semph[PHILOSOPHER_NUM];
sleep(rand()%3);
sem_wait(&print_mutex);
printf("philosopher %d is dining.when he is dining ,there are %d philosophers at table.\n",(int)i,dining_num);
for(i=0;i<CHAIR_NUM;i++){
#include <pthread.h>
#include <semaphore.h>
#define PHILOSOPHER_NUM 5
#define THINKING 0
#define HUNGRY 1
#define EATING 2
pthread_mutex_t mutex;
sem_t semph[PHILOSOPHER_NUM];
sem_wait(&chopsticks[i]);
printf("philosopher %d get chopstics %d\n",i,i);
sem_wait(&chopsticks[(i+1)%CHOP_NUM]);
printf("philosopher %d get chopstics %d\n",tid,(i+1)%CHOP_NUM);
}
else
{//不能同时拿起则都不拿起
sem_post(&semph[leftIndex]);
}
}
break;
case EATING:
sem_post(&semph[leftIndex]);
sem_post(&semph[rightIndex]);
myState=THINKING;
strcpy(strState,"THINKING");
if(ret)
{
errNum++;
}
usleep(20);
}
if(errNum)
{
printf("thread create err! errnum=%d \n",errNum);
}
sleep(1);
}
#include <unistd.h>
#include <stdio.h>
#include <stdlib.h>
for(i=0;i<CHOP_NUM;i++)
{
sem_init(&chopsticks[i],0,1);
}
sem_init(&mutex,0,1);
sem_init(&mutex1,0,1);
sem_init(&print_mutex,0,1);
for(i=0;i<4;i++)chairflg[i][0]=0;
usleep(sleepTime);
}
}
int main()
{
int i,ret;
int threadId[PHILOSOPHER_NUM];
int errNum=0;
pthread_t t_phThread[PHILOSOPHER_NUM];
//pthread_t t_threadInfo;
srand(getpid());
break;
}
pthread_mutex_lock(&mutex);
printf("pholosopher %d begin %s\n",mythreadId,strState);
pthread_mutex_unlock(&mutex);
sleepTime = 1+(int)(5.0*rand()/(RAND_MAX+1.0));
#define LEFT (i + PHILOSOPHER_NUM -1)%PHILOSOPHER_NUM //i的左领居编号
#define RIGHT (i + 1)%PHILOSOPHER_NUM //i的右领居编号
int state[PHILOSOPHER_NUM];
void think(int i)
#include<stdio.h>
#include<stdlib.h>
#include<string.h>
#include<pthread.h>
#include<semaphore.h>
#include<sys/types.h>
#include<errno.h>
#include<unistd.h>
#include<signal.h>
#define NUM_THREADS_P 5 /*define the number of philosopher*/
#define CHAIR_NUM 4
#define CHOP_NUM 5
int chairflg[CHAIR_NUM][2],dining_num=0;
sem_t chair,chopsticks[CHOP_NUM],mutex,mutex1,print_mutex;//设定信号量
sleepTime = 1 + (int)(5.0*rand()/(RAND_MAX+1.0));
usleep(sleepTime*10);
}
}
int main()
{
int i,ret;
int threadId[PHILOSOPHER_NUM];
int errNum=0;
pthread_t t_phThread[PHILOSOPHER_NUM];
sem_post(&chopsticks[(i+1)%CHOP_NUM]);
sem_wait(&mutex1);
for(i=0;i<CHAIR_NUM;i++){
if(chairflg[i][1]==(int)i){
chairflg[i][0]=0;
break;
}
}
dining_num--;
sem_post(&mutex1);
sem_destroy(&mutex1);
sem_destroy(&print_mutex);
return 0;
}
void* philosopher_thread(int tid)
{
int i;
i=tid;
sem_wait(&chair);
sem_wait(&mutex);
for(i=0;i<CHAIR_NUM;i++){
if( ret )
{
errNum++;
}
for(i=0;i<PHILOSOPHER_NUM;i++)
{
threadId[i] = i;
sem_init(&semph[i],0,1);
ret=pthread_create(&t_phThread[i], NULL, (void *)philosopher, (void*)(&threadId[i]));
pthread_t t_threadInfo;
srand(getpid());
pthread_mutex_init(&mutex,NULL);
//ret = pthread_create(&t_threadInfo, NULL, (void *) threadInfo, (void *) NULL);
pthread_mutex_lock(&mutex);
state[i] = THINKING;
test(LEFT);
test(RIGHT);
pthread_mutex_unlock(&mutex);
}
void philosopher(int *index)
{
int mythreadId = (*index);
sem_post(&chair);
printf("philosopher %d leaves\n",(int)i);
return ((void*)0);
}
$$$$$$$$$$$$$$$$$$$$$$$$$
#include <unistd.h>
#include <stdio.h>
#include <stdlib.h>
rightIndex = (mythreadId + 1) % PHILOSOPHER_NUM;
myState = THINKING;
while(1)
{
switch(myState)
{
case THINKING:
myState = HUNGRY;
strcpy(strState,"HUNGRY");
break;
void philosopher(int *index)
{
int mythreadId;
char myState,strState[128];
int leftIndex;
int rightIndex;
int sleepTime;
mythreadId = (*index);
leftIndex = mythreadId + PHILOSOPHER_NUM-1)% PHILOSOPHER_NUM;
{
}
void eat(int i)
{
}
void test(int i)
{
if((state[i] == HUNGRY )
&& (state[LEFT] != EATING)
&& (state[RIGHT]!= EATING))
{
state[i] = EATING;
sem_post(&semph[i]);
pthread_mutex_init(&mutex,NULL);
for(i=0;i<PHILOSOPHER_NUM;i++)
{
threadId[i] = i;
sem_init(&semph[i],0,1);
int sleepTime;
bzero(state,0);
while(1)
{
think(mythreadId);
take_forks(mythreadId);
eat(mythreadId);
put_forks(mythreadId);
// sleep a random time : between 1 - 5 s
pthread_t threads_p[NUM_THREADS_P]Fra Baidu bibliotek /*philosopher*/
void* philosopher_thread(int tid);
int main(){
int i;
sem_init(&chair,0,CHAIR_NUM); /*set the value of semaphores*/
for(i=0;i<NUM_THREADS_P;i++)
pthread_join(threads_p[i],NULL);
/*destroy the semaphores*/
sem_destroy(&chair);
sem_destroy(&chopsticks[CHOP_NUM]);
sem_destroy(&mutex);
if(chairflg[i][0]==1){
printf("poilosopher %d in chair %d. ",chairflg[i][1],i+1);
}
}
printf("\n\n");
sem_post(&print_mutex);
sleep(rand()%3);
sem_post(&chopsticks[i]);