📄 phil.cc
字号:
/***************************************************************************
*
* Copyright (c) 1993 READY SYSTEMS CORPORATION.
*
* All rights reserved. READY SYSTEMS' source code is an unpublished
* work and the use of a copyright notice does not imply otherwise.
* This source code contains confidential, trade secret material of
* READY SYSTEMS. Any attempt or participation in deciphering, decoding,
* reverse engineering or in any way altering the source code is
* strictly prohibited, unless the prior written consent of
* READY SYSTEMS is obtained.
*
*
* Module Name: phil.cc
*
* Identification: @(#) 1.17 phil.cc
*
* Date: 2/22/94 14:43:02
*
****************************************************************************
*/
#include <iostream.h>
#include <stdlib.h>
#include <stdio.h>
#include "sem.h"
#include "task.h"
int main();
// const char ESC = 27;
extern Semaphore fourmax;
extern Semaphore stick[];
extern Semaphore console;
void message(void);
void philosopher(void);
Semaphore fourmax (4); // ensure no deadlock
Semaphore stick[5]; // stick use mutual exclusion
Semaphore console; // synchronize access to the console
Task clr_scr (message, 1, 6); // print message
Task phil1 (philosopher, 10, 1); // start philosophers
Task phil2 (philosopher, 10, 2);
Task phil3 (philosopher, 10, 3);
Task phil4 (philosopher, 10, 4);
Task phil5 (philosopher, 10, 5);
// const char clear_screen[] = {ESC, '[', '2', 'J', '\0'};
// const char goto_home[] = {ESC, '[', '0', ';', '0', 'H', '\0'};
// const char goto_center[] = {ESC, '[', '1', '2', ';', '4', '0', 'H', '\0'};
// const char goto_legend[] = {ESC, '[', '2', '0', ';', '0', 'H', '\0'};
// void
//at (int id) // position cursor
//{
//static int r[] = {14, 12, 10, 10, 12};
//static int c[] = {40, 44, 42, 38, 36};
//cursor_pos(c[id-1],r[id-1]);
//}
void
message () // put the message on the console
{
console.pend ();
cout
<< "Dining Philosophers C++/VRTX Demo\n";
cout
<< "H - hungry\n"
<< "E - eating\n"
<< "T - thinking\n";
console.post ();
Task::exit (); // make VRTX32 happy
}
void
hungry (int id) // philosopher is hungry
{
console.pend ();
cout << id << 'H';
console.post ();
}
void
eating (int id) // philosopher eats
{
console.pend ();
cout << id << 'E';
console.post ();
Task::delay (rand() % 200);
}
void
thinking (int id) // philosopher thinks
{
console.pend ();
cout << id << 'T';
console.post ();
Task::delay (rand() % 200);
}
void
philosopher ()
{
int i = Task::this_id();
int left = i - 1;
int right = i % 5;
for (;;)
{
hungry (i);
fourmax.pend(); // sit at the table
stick[left].pend(); // get the left stick
stick[right].pend(); // get the right stick
eating (i);
stick[right].post(); // release the right stick
stick[left].post(); // release the left stick
fourmax.post(); // leave the table
thinking (i);
}
}
void user_main()
{
int a=0;
int b=1;
int c;
// while(0){
c=b;
b=a;
a=c;
// }
}
int main()
{
int err;
sc_tcreate (user_main,253,1,&err);
}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -