哲學家就餐問題實現--多執行緒同步(unix
假設有 5 個哲學家,有 5 把叉子,每個哲學家有思考,飢餓,和吃飯三個狀態。我們用 5 個訊號量分別表示 5 把叉子
一. windows實現
原始碼如下:
// PhilosopherDining.cpp : Defines the entry point for the console application.
//
#include "stdafx.h"
#include "windows.h"
#include <process.h>
#include <time.h>
#include <stdlib.h>
#include <stdio.h>
#include <iostream>
using namespace std;
const unsigned int PHILOSOPHER_NUM=5;
const char THINKING=1;
const char HUNGRY=2;
const char DINING=3;
// each fork has a semaphore
HANDLE semph[PHILOSOPHER_NUM];
// Mutex for printing
HANDLE mutex;
void philosopherProc(void* param);
int main(int argc, char* argv[])
{
int i;
srand(time(0));
mutex = CreateMutex(NULL, false, NULL);
for (i=0; i<PHILOSOPHER_NUM; i++)
{
semph[i] = CreateSemaphore(NULL, 1, 1, NULL);
_beginthread(philosopherProc, 0, (void*)&i);
Sleep(10);
}
Sleep(2000);
return 0;
}
void philosopherProc(void* param)
{
int myid;
char idStr[128];
char stateStr[128];
char mystate;
int ret;
unsigned int leftFork;
unsigned int rightFork;
myid = *((int*)(param));
itoa(myid, idStr, 10);
//cout << "philosopher " << myid << " begin......" << endl;
Sleep(10);
// initial state is THINKING
mystate = THINKING;
leftFork = (myid) % PHILOSOPHER_NUM;
rightFork = (myid + 1) % PHILOSOPHER_NUM;
while (true)
{
switch(mystate)
{
case THINKING:
// changing my state
mystate = HUNGRY;
strcpy(stateStr, "HUNGRY");
break;
case HUNGRY:
strcpy(stateStr, "HUNGRY");
// first test the left fork ...
ret = WaitForSingleObject(semph[leftFork], 0);
if (ret == WAIT_OBJECT_0)
{
// left fork is ok, take it up !
// then test the right fork ...
ret = WaitForSingleObject(semph[rightFork], 0);
if (ret == WAIT_OBJECT_0)
{
// right fork is also ok !
// changing my state
mystate = DINING;
strcpy(stateStr, "DINING");
}
else
{
// right fork is being used by others, so I must put down
// the left fork.
ReleaseSemaphore(semph[leftFork], 1, NULL);
}
}
break;
case DINING:
// put down both the left and right fork
ReleaseSemaphore(semph[leftFork], 1, NULL);
ReleaseSemaphore(semph[rightFork], 1, NULL);
// changing my state
mystate = THINKING;
strcpy(stateStr, "THINKING");
break;
}
// print my state
WaitForSingleObject(mutex, INFINITE);
cout << "philosopher " << myid << " is : " << stateStr << endl;
ReleaseMutex(mutex);
// sleep a random time : between 1 - 5 s
int sleepTime;
sleepTime = 1 + (int)(5.0*rand()/(RAND_MAX+1.0));
Sleep(sleepTime*10);
}
}
二. linux實現
PhilosopherDining.cpp
#include <iostream>
#include <string>
#include <pthread.h>
#include <unistd.h>
#include <semaphore.h>
#include "RasUtil.h"
using namespace std;
const unsigned int PHILOSOPHER_NUM=5;
const char THINKING=1;
const char HUNGRY=2;
const char DINING=3;
// each fork has a semaphore
sem_t semph[PHILOSOPHER_NUM];
// Mutex for printing
pthread_mutex_t mutex;
void* philosopherProc(void* param);
int main(int argc, char* argv[])
{
int i;
srand(getpid());
pthread_t philosopherThread[PHILOSOPHER_NUM];
int phId[PHILOSOPHER_NUM];
pthread_mutex_init(&mutex, NULL);
for (i=0; i<PHILOSOPHER_NUM; i++)
{
phId[i] = i;
sem_init(&semph[i], 0, 1);
pthread_create(&philosopherThread[i], NULL, philosopherProc, (void*)(&phId[i]));
usleep(50);
}
sleep(3);
return 0;
}
void* philosopherProc(void* param)
{
int myid;
char idStr[128];
char stateStr[128];
char mystate;
int ret;
unsigned int leftFork;
unsigned int rightFork;
myid = *((int*)(param));
RasUtil::intToStr(myid, idStr);
cout << "philosopher " << myid << " begin......" << endl;
//usleep(10);
// initial state is THINKING
mystate = THINKING;
leftFork = (myid) % PHILOSOPHER_NUM;
rightFork = (myid + 1) % PHILOSOPHER_NUM;
while (true)
{
switch(mystate)
{
case THINKING:
// changing my state
mystate = HUNGRY;
strcpy(stateStr, "HUNGRY");
break;
case HUNGRY:
strcpy(stateStr, "HUNGRY");
// first test the left fork ...
ret = sem_trywait(&semph[leftFork]);
if (ret == 0)
{
// left fork is ok, take it up !
// then test the right fork ...
ret = sem_trywait(&semph[rightFork]);
if (ret == 0)
{
// right fork is also ok !
// changing my state
mystate = DINING;
strcpy(stateStr, "DINING");
}
else
{
// right fork is being used by others, so I must put down
// the left fork.
sem_post(&semph[leftFork]);
}
}
break;
case DINING:
// put down both the left and right fork
sem_post(&semph[leftFork]);
sem_post(&semph[rightFork]);
// changing my state
mystate = THINKING;
strcpy(stateStr, "THINKING");
break;
}
// print my state
pthread_mutex_lock(&mutex);
cout << "philosopher " << myid << " is : " << stateStr << endl;
pthread_mutex_unlock(&mutex);
// sleep a random time : between 1 - 5 s
int sleepTime;
sleepTime = 1 + (int)(5.0*rand()/(RAND_MAX+1.0));
usleep(sleepTime*10);
}
}
RasUtil.h : 完成整數轉換為字串
#ifndef _RasUtil_h_
#define _RasUtil_h_
class RasUtil
{
public:
static inline void intToStr(int i, char* str);
};
inline void RasUtil::intToStr(int i, char* str)
{
int dec;
int sign;
char* p;
p = ecvt(i, 10, &dec, &sign);
strcpy(str, p);
str[dec] = 0;
}
#endif