my home work is making restaurent simulation program.
(我的家庭作业正在制作餐厅模拟程序。)
30 seat At the same time, 30 people can eat.
(30个座位同时,有30人可以吃饭。)
At lunchtime, about 200 guests go to eat. (午餐时间,大约有200位客人吃饭。)
Lunch starts at 11:30 and ends at 2:00. (午餐开始于11:30,结束于2:00。)
It is assumed that the meal time is from 10 minutes to 50 minutes. (假设进餐时间为10分钟至50分钟。)
Use event, semaphore, Timer, Critical Section function.
(使用事件,信号量,计时器,关键部分功能。)
Use Time function. (使用时间功能。)
Print the time each guest came in, the time it takes to eat, and the time to leave after a meal (打印每个客人进来的时间,吃饭的时间以及饭后离开的时间)
Additional score)
(额外分数))
- file output about customer.(Time to arrive at the restaurant, Meal time, etc...)
(关于客户的文件输出。(到达餐厅的时间??,用餐时间等))
- random number of guests
(随机人数)
- random time to arrive at the restaurent.
(随机时间到达餐厅。)
Please refer to my code when coding.(Go bottom to this Question)
(编码时请参考我的代码。)
please, give me solution.
(请给我解决方案。)
and I would also like to comment the comments. (我也想发表评论。)
thank you.
(谢谢。)
my code
(我的代码)
#include <stdio.h>
#include <stdlib.h>
#include <time.h>
#include <windows.h>
#include <process.h>
#include <tchar.h>
#define NUM_OF_CUSTOMER 50
#define RANGE_MIN 10
#define RANGE_MAX (30 - RANGE_MIN)
#define TABLE_CNT 10
HANDLE hSemaphore;
DWORD randTimeArr[50];
void TakeMeal(DWORD time)
{
WaitForSingleObject(hSemaphore, INFINITE);
_tprintf( _T("Enter Customer %d~
"), GetCurrentThreadId());
_tprintf(_T("Customer %d having launch~
"), GetCurrentThreadId());
Sleep(1000 * time); //eating simulation.
ReleaseSemaphore(hSemaphore, 1, NULL);
_tprintf( _T("Out Customer %d~
"), GetCurrentThreadId());
}
unsigned int WINAPI ThreadProc( LPVOID lpParam )
{
TakeMeal((DWORD)lpParam);
return 0;
}
int _tmain(int argc, TCHAR* argv[])
{
DWORD dwThreadIDs[NUM_OF_CUSTOMER];
HANDLE hThreads[NUM_OF_CUSTOMER];
srand( (unsigned)time( NULL ) ); // random function seed
// Generate a total of 50 random values ??to pass to the thread.
for(int i=0; i<NUM_OF_CUSTOMER ;i++)
{
randTimeArr[i] = (DWORD) (
((double)rand() / (double)RAND_MAX) * RANGE_MAX + RANGE_MIN
);
}
//Create Semaphore.
hSemaphore = CreateSemaphore (
NULL, // default secure manager
TABLE_CNT, // Semaphore Initial value
TABLE_CNT, // Semaphore Max value
NULL // unnamed Semaphore.
);
if (hSemaphore == NULL)
{
_tprintf(_T("CreateSemaphore error: %d
"), GetLastError());
}
// Customer thread
for(int i=0; i<NUM_OF_CUSTOMER; i++)
{
hThreads[i] = (HANDLE)
_beginthreadex (
NULL,
0,
ThreadProc,
(void*)randTimeArr[i],
CREATE_SUSPENDED,
(unsigned *)&dwThreadIDs[i]
);
if(hThreads[i] == NULL)
{
_tprintf(_T("Thread creation fault!
"));
return -1;
}
}
for(int i=0; i<NUM_OF_CUSTOMER; i++)
{
ResumeThread(hThreads[i]);
}
WaitForMultipleObjects(NUM_OF_CUSTOMER, hThreads, TRUE, INFINITE);
_tprintf(_T("----END-----------
"));
for(int i=0; i<NUM_OF_CUSTOMER; i++)
{
CloseHandle(hThreads[i]);
}
CloseHandle(hSemaphore);
return 0;
}
ask by ??? translate from so 与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…