VS2019创建的项目时创建的是Windows桌面向导,之后选择空项目,在源文件里面添加了.cpp文件。之后写入了相关代码。之后会有三个报错,在项目中点击属性,“高级”选项中的多字符集选择使用多字符集即可。
/*
* 实验10-1:哲学家进食问题
*/
#include<Windows.h>
#include<process.h>
#include<math.h>
#include<time.h>
#define DEGRESS_TO_RADIAN(x) (x)*3.14/180
#pragma comment(lib, "user32.lib")
#pragma comment(lib, "gdi32.lib")
#define PHIL_NUM 5 //哲学家数据,可变为其他数目
#define START_POINT 150
#define DESK_DIAMETER 200 //圆桌直径
#define PHIL_DIAMETER 30 //哲学家圆圈直径
#define CHOP_LENGHT 50 //筷子长度
#define TIME 10000 //持续时间长度
unsigned int __stdcall DineMany(LPVOID pParam);
int WINAPI WinMain(HINSTANCE hInstance, HINSTANCE hPreInstance, LPSTR pszCmdLine, int nCmdShow);
LRESULT CALLBACK WindowProc(HWND hWnd, UINT uMsgId, WPARAM wParam, LPARAM lParam);
typedef struct {
HWND hWnd; //窗口句柄
int chopXY[8];
int Id; //哲学家编号
int isEating;
}PARAM;
int chopSticks[PHIL_NUM]; //筷子的初态
int Finished[PHIL_NUM]; //用餐是否完成
HANDLE hMutex; //互斥量
HANDLE hEvent; //事件
int WINAPI WinMain(HINSTANCE hInstance, HINSTANCE hPreInstance, LPSTR pszCmdLine, int nCmdShow) {
static char szAppName[] = "哲学家进餐";
HWND hWnd;
MSG msg;
WNDCLASS wndClass;
wndClass.style = CS_VREDRAW | CS_HREDRAW;
wndClass.lpfnWndProc = WindowProc;
wndClass.cbClsExtra = 0;
wndClass.cbWndExtra = 0;
wndClass.hInstance = hInstance;
wndClass.hIcon = LoadIcon(NULL, IDI_QUESTION);
wndClass.hCursor = LoadCursor(NULL, IDC_ARROW);
wndClass.hbrBackground = (HBRUSH)GetStockObject(WHITE_BRUSH);
wndClass.lpszMenuName = NULL;
wndClass.lpszClassName = szAppName;
if (0 == RegisterClass(&wndClass))
return 0;
hWnd = CreateWindow(szAppName, szAppName, WS_OVERLAPPEDWINDOW, CW_USEDEFAULT, CW_USEDEFAULT, CW_USEDEFAULT, CW_USEDEFAULT, NULL, NULL, hInstance, NULL);
if (0 == hWnd) return 0;
ShowWindow(hWnd, nCmdShow);
UpdateWindow(hWnd);
while (GetMessage(&msg, NULL, 0, 0)) {
TranslateMessage(&msg);
DispatchMessage(&msg);
}
return msg.wParam;
}
LRESULT CALLBACK WindowProc(HWND hWnd, UINT uMsgId, WPARAM wParam, LPARAM lParam) {
HDC hDC;
PAINTSTRUCT paintStruct;
HANDLE hThread[PHIL_NUM];
int left, right;
static int chopStartX[PHIL_NUM], chopStartY[PHIL_NUM]; //筷子的起始坐标
static int chopEndX[PHIL_NUM], chopEndY[PHIL_NUM]; //筷子的结束坐标
static int philX[PHIL_NUM], philY[PHIL_NUM]; //哲学家的位置
static PARAM pParam[PHIL_NUM];
HPEN hPenWhite, hOldPen;
int i;
switch (uMsgId){
case WM_CREATE:
hMutex = CreateMutex(NULL, FALSE, NULL); //创建互斥量
hEvent = CreateEvent(NULL, FALSE, FALSE, NULL); //创建事件
for (i = 0; i < PHIL_NUM; i++) {
chopStartX[i] = (int)((START_POINT)+DESK_DIAMETER / 2 + DESK_DIAMETER / 4 * sin(DEGRESS_TO_RADIAN(90 + (360.0 / PHIL_NUM) * i)));
chopStartY[i] = (int)((START_POINT)+DESK_DIAMETER / 2 + DESK_DIAMETER / 4 * cos(DEGRESS_TO_RADIAN(90 + (360.0 / PHIL_NUM) * i)));
chopEndX[i] = (int)((START_POINT)+DESK_DIAMETER / 2 + DESK_DIAMETER / 2 * sin(DEGRESS_TO_RADIAN(90 + (360.0 / PHIL_NUM) * i)));
chopEndY[i] = (int)((START_POINT)+DESK_DIAMETER / 2 + DESK_DIAMETER / 2 * cos(DEGRESS_TO_RADIAN(90 + (360.0 / PHIL_NUM) * i)));
philX[i] = (int)((START_POINT)+DESK_DIAMETER / 2 + (DESK_DIAMETER / 2 + PHIL_DIAMETER) * cos(DEGRESS_TO_RADIAN(-(360.0 / (PHIL_NUM * 2)) - (360.0 / PHIL_NUM) * i)) - PHIL_DIAMETER / 2);
philY[i] = (int)((START_POINT)+DESK_DIAMETER / 2 + (DESK_DIAMETER / 2 + PHIL_DIAMETER) * sin(DEGRESS_TO_RADIAN(-(360.0 / (PHIL_NUM * 2)) - (360.0 / PHIL_NUM) * i)) - PHIL_DIAMETER / 2);
chopSticks[i] = 0;
Finished[i] = 1;
}
break;
//绘图消息
case WM_PAINT:
hDC = BeginPaint(hWnd, &paintStruct);{ //获得视图设别环境
Ellipse(hDC, START_POINT, START_POINT, START_POINT + DESK_DIAMETER, START_POINT + DESK_DIAMETER);
//画圆桌
for (i = 0; i < PHIL_NUM; i++) {
MoveToEx(hDC, chopStartX[i], chopStartY[i], NULL);
LineTo(hDC, chopEndX[i], chopEndY[i]);
}
for (i = 0; i < PHIL_NUM; i++)
Ellipse(hDC, philX[i], philY[i], philX[i] + PHIL_DIAMETER, philY[i] + PHIL_DIAMETER);
for (i = 0; i < PHIL_NUM; i++) {
if (pParam[i].isEating == 1) {
MoveToEx(hDC, philX[i] + PHIL_DIAMETER / 2, philY[i] + PHIL_DIAMETER / 2, NULL);
LineTo(hDC, pParam[i].chopXY[2], pParam[i].chopXY[3]);
MoveToEx(hDC, philX[i] + PHIL_DIAMETER / 2, philY[i] + PHIL_DIAMETER / 2, NULL);
LineTo(hDC, pParam[i].chopXY[6], pParam[i].chopXY[7]);
}
else {
hPenWhite = CreatePen(PS_SOLID, 3, RGB(255, 255, 255));
hOldPen = (HPEN)SelectObject(hDC, hPenWhite);
MoveToEx(hDC, philX[i] + PHIL_DIAMETER / 2, philY[i] + PHIL_DIAMETER / 2, NULL);
LineTo(hDC, pParam[i].chopXY[2], pParam[i].chopXY[3]);
MoveToEx(hDC, philX[i] + PHIL_DIAMETER / 2, philY[i] + PHIL_DIAMETER / 2, NULL);
LineTo(hDC, pParam[i].chopXY[6], pParam[i].chopXY[7]);
SelectObject(hDC, hOldPen);
DeleteObject(hPenWhite);
}
}
TextOut(hDC, 20, 20, "press s", 7);
}
EndPaint(hWnd, &paintStruct);
break;
case WM_CHAR:
if (wParam == 's' || wParam == 'S') {
for (i = 0; i < PHIL_NUM; i++)
chopSticks[i] = FALSE;
ResetEvent(hEvent);
for (i = 0; i < PHIL_NUM; i++) {
pParam[i].Id = i;
pParam[i].hWnd = hWnd;
left = i;
right = (i + 1) % PHIL_NUM;
pParam[i].chopXY[0] = chopStartX[left];
pParam[i].chopXY[1] = chopStartY[left];
pParam[i].chopXY[2] = chopEndX[left];
pParam[i].chopXY[3] = chopEndY[left];
pParam[i].chopXY[4] = chopStartX[right];
pParam[i].chopXY[5] = chopStartY[right];
pParam[i].chopXY[6] = chopEndX[right];
pParam[i].chopXY[7] = chopEndY[right];
pParam[i].isEating = 0;
hThread[i] = (HANDLE)_beginthreadex(NULL, 0, DineMany, &pParam[i], 0, NULL);
}
InvalidateRect(hWnd, NULL, FALSE);
SetEvent(hEvent);
}
break;
case WM_CLOSE:
for (i = 0; i < PHIL_NUM; i++)
CloseHandle(hThread[i]);
DestroyWindow(hWnd);
break;
case WM_DESTROY:
PostQuitMessage(0);
break;
default:
return DefWindowProc(hWnd, uMsgId, wParam, lParam);
}
return 0;
}
unsigned int __stdcall DineMany(LPVOID pParam) {
PARAM* m_pParam = (PARAM*)pParam;
int left, right;
int TimeDelay = TIME, SleepTime;
BOOL canDine = FALSE;
WaitForSingleObject(hEvent, INFINITE);
left = m_pParam->Id;
right = (m_pParam->Id + 1) % PHIL_NUM;
while (TimeDelay > 0) {
WaitForSingleObject(hMutex, INFINITE);
if (FALSE == chopSticks[left] && FALSE == chopSticks[right]) {
chopSticks[left] = TRUE;
chopSticks[right] = TRUE;
canDine = TRUE;
}
ReleaseMutex(hMutex);
if (TRUE == canDine) {
m_pParam->isEating = 1;
InvalidateRect(m_pParam->hWnd, NULL, FALSE);
srand((unsigned)time(NULL));
SleepTime = ((rand() % 250 + 250) - (rand() % 250 - 250)) / 2;
Sleep(SleepTime);
TimeDelay = TimeDelay - SleepTime;
WaitForSingleObject(hMutex, INFINITE);
chopSticks[left] = FALSE;
chopSticks[right] = FALSE;
ReleaseMutex(hMutex);
canDine = FALSE;
m_pParam->isEating = 0;
SetEvent(hEvent);
}
if (FALSE == canDine) {
srand((unsigned)time(NULL));
SleepTime = ((rand() % 500 + 500) - (rand() % 500 - 500)) / 2;
Sleep(SleepTime);
TimeDelay = TimeDelay - SleepTime;
}
}
Finished[m_pParam->Id] = TRUE;
return 0L;
}
无法启动程序"D:AMy_Files\操作系统\操作系统实验\操作系统实验10\Project3DebuglProject3.exe".
1、尝试过重新建立项目
2、在网上搜了很多的相关解决方案,也不见效
3、最有可能的猜测就是代码有问题
有输出就行,其他的我应该就可以解决了