:有五个哲学家,他们的生活方式是交替的进行思考和进餐。哲学家们共用一张圆桌,
查看图片
设五个哲学家分别编号为A,B,C,D,E,桌子上放着五把筷子,筷子分别编号为 0,1,
2,3,4,桌子中央有一盘饭菜。五个哲学家都很有礼貌,都要等同时拿到身旁的两只筷子
才进餐,不然就只是等着继续思考,而且吃了一口之后又马上放下拿起的两根筷子,继续思
考。
用P V原语的方式实现
每个哲学家可用一个线程来模拟,信号量及其P、V操作的实现
定义一个semaphore 类 封装 P V原语模拟函数
(不妨设有6个哲学家,6只筷子, 每只筷子各对应一个信号量且每个信号量初始值应为1)/***********************semaphore.h*****************************
Semaphore类用于模拟信号量
用法如:Semaphore sem(1)
(1)要求初始信号量值非负
(2)信号量P操作: sem.P()
(3)信号量V操作: sem.V()
#ifndef SEMAPHORE_H
#define SEMAPHORE_H
#include <windows.h>
class Semaphore{
protected:
HANDLE sem
public:
//Semaphore()
//void SetValve(int SemValue)
Semaphore(unsigned int SemValue)
virtual ~Semaphore()
void P()
void V()
#endif
/*****************************semaphore.cpp************************************/
#include <windows.h>
#include <LIMITS.H>
#include <assert.h>
#include "semaphore.h"
Semaphore::Semaphore(unsigned int semValue){
if(semValue >LONG_MAX)
semValue = LONG_MAX
sem = CreateSemaphore(NULL,semValue,LONG_MAX,NULL)
Semaphore::~Semaphore(){
CloseHandle(sem)
void Semaphore::P(){
DWORD dw = WaitForSingleObject(sem, INFINITE)
assert(dw == WAIT_OBJECT_0)
void Semaphore::V(){
ReleaseSemaphore(sem,1,NULL)
/*****************************thread.h*************************************
通过startThread开启线程
startThread(PTHREAD_START func,void * param)
用法:
//定义一个PTHREAD_START类型的函数func
unsigned int WINAPI func (void * param){
//启动一个线程使其执行func函数
startThread(func,NULL)
#ifndef THREAD_H
#define THREAD_H
#include <windows.h>
#include <process.h>
from MSDN :
Start address of a routine that begins execution of a new thread.
For _beginthread, the calling convention is either __cdecl or __clrcall
for _beginthreadex, it is either __stdcall or __clrcall.
WINAPI即为__stdcall
typedef unsigned int (WINAPI *PTHREAD_START) (void *)
chBEGINTHREADEX is From <Windows Via C &C++>
#define chBEGINTHREADEX(psa, cbStackSize, pfnStartAddr, \
pvParam, dwCreateFlags, pdwThreadId) \
((HANDLE)_beginthreadex( \
(void *)(psa), \
(unsigned) (cbStackSize), \
(PTHREAD_START) (pfnStartAddr),\
(void *)(pvParam), \
(unsigned) (dwCreateFlags), \
(unsigned *)(pdwThreadId)))
// rename chBEGINTHREADEX to startThread for simplicity
#define startThread(pfnStartAddr,pvParam)\
chBEGINTHREADEX(NULL, 0, (pfnStartAddr),(pvParam), 0, NULL)
#endif
/*****************************main函数(主函数)************************************/
假设偶数号哲学家先拿左边的筷子,右边的哲学家先拿起右边的筷子。
#include <windows.h>
#include "semaphore.h"
#include "thread.h"
#include <iostream.h>
#include <stdio.h>
#define N 6//哲学家的个数
#define LEFT(i)(i+N-1)%N//左边的筷子
#define RIGHT(i) (i==N-1)?0:(i+N)%N //右边的筷子 编号为N的哲学家的右边的筷子编号为0
/*******************************初始化信号量**************************************/
Semaphore ChopStick[N]={Semaphore(1),Semaphore(1),Semaphore(1),Semaphore(1),Semaphore(1),Semaphore(1)}
/*******************************哲学家状态*******************************************/
void Eating(int Ph_Id)
printf("Philosopher%d: \tI'm eating......\t",(int)Ph_Id)
void Sleeping(int Ph_Id)
printf("Philosopher%d:\tI'm sleeping......\t",(int)Ph_Id)
Sleep(rand()%10000)
void Thinking(int Ph_Id)
printf("Philosopher%d: \tI'm thinking......\t",(int)Ph_Id)
if (pid%2 == 0) //偶数号哲学家
Thinking(pid) //等待中
ChopStick[LEFT(pid)].P() //先拿起左边的筷子,再拿起右边的筷子
ChopStick[RIGHT(pid)].P()
Eating(pid) //获得的两个信号量则eating
ChopStick[LEFT(pid)].V() //先后释放左右信号量
ChopStick[RIGHT(pid)].V()
printf("\n")
else if(pid%2==1 ) //奇数号哲学家
Thinking(pid)
ChopStick[RIGHT(pid)].P() //先拿起右边的筷子,再拿起左边的筷子
ChopStick[LEFT(pid)].P()
Eating(pid) //左右都得到筷子后则eating
ChopStick[RIGHT(pid)].V() //先后释放右左信号量
ChopStick[LEFT(pid)].V()
printf("\n")
Sleeping(pid) //吃完睡上一会儿
int main(){
HANDLE hPhilosopher[N] //为每个哲学家分配一个线程
int count =0
for (int Philosopher_id =0 Philosopher_id<NPhilosopher_id++) //开启线程
hPhilosopher[Philosopher_id] = startThread(Philosopher,Philosopher_id)
if(count>5)
break
::WaitForMultipleObjects(N,hPhilosopher,TRUE,INFINITE)
for (Philosopher_id = 0 Philosopher_id<NPhilosopher_id++)
CloseHandle(hPhilosopher[Philosopher_id])
抱歉.复制的.不是我自己写的..曲名:The
Love
We
Got歌手:Karina
专辑:First
Love
Karina
Pasian
My
heart
he
takes
He'll
always
be
my
baby
We
may
bend,Won't
break
But
we
got
no
one
else
to
take
He's
the
sun
when
my
day
is
low
I'm
his
picture
in
front
front
row
His
love
is
the
center
of
my
attention
(oo
oo)
See
I
don't
know
what
he'd
do
for
me
...
That
guy
is
my
everything
...
We
argue,we
fight
Then
we
make
up
by
the
end
of
the
night
Don't
even
speak
it
speak
it
We
know
what
we
got
And
I
love
him,
he
loves
me
And
this
is
how
love
should
be
I'm
down
for
the
love
that
he
got
that,
he
got
he
stays
down
for
the
love
that
we
got,
that
we
got
I
love
him,
he
loves
me
And
this
is
how
love
should
be
I'm
down
for
the
love
that
he
got
that,
that
he
got
And
he
stays
down
for
the
love
that
we
got,
that
we
got
He's
embebed
in
myspace
Everytime
he
speaks
he
blows
me
away
Don't
care
what
people
say
As
long
as
hes
in
the
same
place
where
I
lay
See
I'll
be
as
that
goes
I'm
his
princess
hes
my
general
And
no
matter
what
you
think
about
love
Time
won't
outlast
us
See
yall
don't
know
what
he
do
for
me
And
I'm
tellin'
you
hes
my
everything
We
argue
we
fight
Then
we
make
up
by
the
end
of
the
night
Yall
don't
know
what
we
got
And
I
love
him,
he
loves
me
And
this
is
how
love
should
be
I'm
down
for
the
love
that
he
got
that,
he
got
And
he
stays
down
for
the
love
that
we
got,
that
we
got
I
love
him,
he
loves
me
And
this
is
how
love
should
be
I'm
down
for
the
love
that
he
got
that,
that
he
got
And
he
stays
down
for
the
love
that
we
got,
that
we
got
The
miles
I'd
walk
for
him
are
infinite
I'll
run
circles
around
the
world
And
everyday
I'm
beside
him
feals
heaven
sent
And
all
else
is
irrelevant
So
keep
on
exsuding
your
love
on
to
me
And
I
love
him,
he
loves
me
And
this
is
how
love
should
be
I'm
down
for
the
love
that
he
got
that,
he
got
And
he
stays
down
for
the
love
that
we
got,
that
we
got
I
love
him,
he
loves
me
And
this
is
how
love
should
be
I'm
down
for
the
love
that
he
got
that,
that
he
got
And
he
stays
down
for
the
love
that
we
got,
that
we
got
I
love
him,
he
loves
me
And
this
is
how
love
should
be
I'm
down
for
the
love
that
he
got
that,
that
he
got
And
he
stays
down
for
the
love
that
we
got,
that
we
got
...End...
☆
The
death
lyrics
is
from
Google,
SoundZ-X
09.07.2008
8Box/huchuhan
☆
欢迎分享,转载请注明来源:夏雨云
评论列表(0条)