哲学家就餐问题代码
#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];
pthread_t t_threadInfo;
srand(getpid());
pthread_mutex_init(&mutex,NULL);
//ret = pthread_create(&t_threadInfo, NULL, (void *) threadInfo, (void *) NULL);
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);
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;
#define LEFT (i + PHILOSOPHER_NUM -1)%PHILOSOPHER_NUM //i的左领居编号
#define RIGHT (i + 1)%PHILOSOPHER_NUM //i的右领居编号
int state[PHILOSOPHER_NUM];
void think(int i)
pthread_mutex_init(&mutex,NULL);
for(i=0;i<PHILOSOPHER_NUM;i++)
{
threadId[i] = i;
sem_init(&semph[i],0,1);
pthread_mutex_lock(&mutex);
state[i] = THINKING;
test(LEFT);
test(RIGHT);
pthread_mutex_unlock(&mutex);
}
void philosopher(int *index)
{
int mythreadId = (*index);
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));
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;
#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>
pthread_t threads_p[NUM_THREADS_P]; /*philosopher*/
void* philosopher_thread(int tid);
int main(){
int i;
sem_init(&chair,0,CHAIR_NUM); /*set the value of semaphores*/
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];
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());
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>
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++){
}
}
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)
{
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);
case HUNGRY:
strcpy(strState,"HUNGRY");
if(!(sem_wait(&semph[leftIndex])))
{
if(!(sem_wait(&semph[rightIndex])))
{
myState = EATING;
strcpy(strState,"EATING");
rightIndex = (mythreadId + 1) % PHILOSOPHER_NUM;
myState = THINKING;
while(1)
{
switch(myState)
{
case THINKING:
myState = HUNGRY;
strcpy(strState,"HUNGRY");
break;
sem_post(&chair);
printf("philosopher %d leaves\n",(int)i);
return ((void*)0);
}
$$$$$$$$$$$$$$$$$$$$$$$$$
#include <unistd.h>
#include <stdio.h>
#include <stdlib.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;//设定信号量