操作系统实验 windows编程多线程 生产者消费者问题 画圆画方(内置bug版)

时间:2022-10-14 04:45:44

操作系统实验 windows编程多线程 生产者消费者问题 画圆画方(内置bug版)

实验3:随便写的

 #include <windows.h>
#include <string>
#include <stdio.h>
#pragma warning(disable:4996) CRITICAL_SECTION cs; int cnt; DWORD WINAPI Thread( LPVOID lpParam )
{
int n = (int)lpParam; if ( n == )
for ( int i = ; i <= ; i++ )
{
EnterCriticalSection(&cs);
printf( "第%d个线程输出%d\n", n, i );
LeaveCriticalSection(&cs);
Sleep();
}
if ( n == )
for ( int i = ; i >= ; i-- )
{
EnterCriticalSection(&cs);
printf( "第%d个线程输出%d\n", n, i );
LeaveCriticalSection(&cs);
Sleep();
} return ;
} int main()
{
cnt = ; DWORD ThreadID;
HANDLE hThread[];
InitializeCriticalSection(&cs);
for ( int i = ; i < ; i++ )
{
hThread[i] = CreateThread( NULL, , Thread, (LPVOID)(i+), , &ThreadID );
printf( "Thread %d has been created\n", i );
}
WaitForMultipleObjects( , hThread, TRUE, INFINITE ); DeleteCriticalSection(&cs);
return ;
}

实验4:https://blog.csdn.net/morewindows/article/details/7577591

修改了部分代码并添加了注释

 #include <stdio.h>
#include <process.h>
#include <windows.h> const int THREADNUM = ;
const int END_PRODUCE_NUMBER = ; //要生产的产品个数
const int BUFFER_SIZE = ; //缓冲区个数
int g_Buffer[BUFFER_SIZE]; //缓冲池
int g_i, g_j; //信号量与关键段 CRITICAL_SECTION g_cs;
HANDLE g_hSemaphoreBufferEmpty, g_hSemaphoreBufferFull; int data=; //生产者线程函数
unsigned int __stdcall ProducerThreadFun(PVOID pM)
{
while( data < END_PRODUCE_NUMBER )
{
//等待有空的缓冲区出现
WaitForSingleObject( g_hSemaphoreBufferEmpty, INFINITE ); //互斥的访问缓冲区
EnterCriticalSection(&g_cs); //如果投放了足够多的数据,就直接离开临界区
if( data == END_PRODUCE_NUMBER )
{
LeaveCriticalSection(&g_cs);
break;
} //否则投放数据
data++;
g_Buffer[g_i] = data; printf("%d号生产者在缓冲池中第%d个缓冲区中投放数据%d\n\n", GetCurrentThreadId(), g_i, g_Buffer[g_i]); //循环投放数据
g_i = ( g_i + ) % BUFFER_SIZE;
LeaveCriticalSection(&g_cs); Sleep(); //将Full信号量+1,表示生产了一个数据可以取了
ReleaseSemaphore(g_hSemaphoreBufferFull, , NULL);
} printf("生产者完成任务,线程结束运行\n\n");
return ;
} //消费者线程函数
unsigned int __stdcall ConsumerThreadFun(PVOID pM)
{
while (true)
{
//等待非空的缓冲区出现
WaitForSingleObject( g_hSemaphoreBufferFull, INFINITE ); //互斥的访问缓冲区
EnterCriticalSection(&g_cs); printf("%d号消费者从缓冲池第%d个缓冲区取出数据%d\n\n",GetCurrentThreadId(), g_j, g_Buffer[g_j]); if ( g_Buffer[g_j] == END_PRODUCE_NUMBER )
{
LeaveCriticalSection(&g_cs);
break;
} g_j = ( g_j + ) % BUFFER_SIZE;
LeaveCriticalSection(&g_cs); Sleep(); //释放一个数据使empty信号量+1,表示多了一个空位可以生产了
ReleaseSemaphore(g_hSemaphoreBufferEmpty, , NULL);
} printf("%d号消费者收到通知,线程结束运行\n\n", GetCurrentThreadId()); return ;
} int main()
{
printf(" 生产者消费者问题 3生产者 1消费者 4缓冲区\n\n"); //初始化临界区
InitializeCriticalSection(&g_cs); //初始化信号量,一个记录有产品的缓冲区个数,另一个记录空缓冲区个数.
g_hSemaphoreBufferEmpty = CreateSemaphore(NULL, , , NULL); //开始信号量为4个,代表可以生产4个数据
g_hSemaphoreBufferFull = CreateSemaphore(NULL, , , NULL); //开始信号量为0个,代表可以取走0个数据
g_i = ;
g_j = ;
memset( g_Buffer, , sizeof(g_Buffer) ); HANDLE hThread[THREADNUM];
//生产者线程 3个
hThread[] = (HANDLE)_beginthreadex(NULL, , ProducerThreadFun, NULL, , NULL);
hThread[] = (HANDLE)_beginthreadex(NULL, , ProducerThreadFun, NULL, , NULL);
hThread[] = (HANDLE)_beginthreadex(NULL, , ProducerThreadFun, NULL, , NULL);
//消费者线程 1个
hThread[] = (HANDLE)_beginthreadex(NULL, , ConsumerThreadFun, NULL, , NULL); //等待线程全部结束后关闭信号量
WaitForMultipleObjects(THREADNUM, hThread, TRUE, INFINITE);
for (int i = ; i < THREADNUM; i++)
CloseHandle(hThread[i]); //销毁信号量和关键段
CloseHandle(g_hSemaphoreBufferEmpty);
CloseHandle(g_hSemaphoreBufferFull);
DeleteCriticalSection(&g_cs);
return ;
}

实验5:写的崩溃,内置一神秘bug,绘制点数如果太多比如超过500绘制一段时间就会未响应,而且绘制期间鼠标变成小圆圈

猜测是消息循环的问题,因为这是综合了多家代码并进行大量经验主义的修修补补才写成的(实际上完全不懂自己写的是什么)求大神解答为什么会有这种bug

 #include <windows.h>
#include <math.h>
#define PI 3.14159265
#define POINTNUM 400 CRITICAL_SECTION cs;
static TCHAR szAppName[] = TEXT("画圆画方bug版");
static LRESULT CALLBACK WndProc (HWND, UINT, WPARAM, LPARAM); int WINAPI WinMain(HINSTANCE hInstance, HINSTANCE hPrevInstance, PSTR szCmdLine, int iCmdShow)
{
HWND hWnd;
MSG msg;
WNDCLASS wndclass; wndclass.style = CS_HREDRAW | CS_VREDRAW;
wndclass.lpfnWndProc = WndProc;
wndclass.cbClsExtra = ;
wndclass.cbWndExtra = ;
wndclass.hInstance = hInstance;
wndclass.hIcon = LoadIcon(NULL, IDI_APPLICATION);
wndclass.hCursor = LoadCursor(NULL, IDC_ARROW);
wndclass.hbrBackground = (HBRUSH) GetStockObject(WHITE_BRUSH);
wndclass.lpszMenuName = NULL;
wndclass.lpszClassName = szAppName; if (!RegisterClass(&wndclass))
{
MessageBox (NULL, TEXT("This program requires Windows NT!"), szAppName, MB_ICONERROR);
return ;
} hWnd = CreateWindow(szAppName, // window class name
szAppName, // window caption
WS_OVERLAPPEDWINDOW, // window style
CW_USEDEFAULT, // initial x position
CW_USEDEFAULT, // initial y position
, // initial x size
, // initial y size
NULL, // parent window handle
NULL, // window menu handle
hInstance, // program instance handle
NULL); // creation parameters ShowWindow(hWnd, iCmdShow);
UpdateWindow(hWnd); while (GetMessage(&msg, NULL, , ))
{
TranslateMessage(&msg);
DispatchMessage(&msg);
} return msg.wParam;
} DWORD WINAPI DrawCircle( LPVOID lpParama )
{
HDC hDC = HDC(lpParama);
int x = , y = , r = ;
COLORREF color = RGB(,,); int xr, yr;
for (int i=; i <= POINTNUM; i++)
{
EnterCriticalSection(&cs); double rec = ((double)i/POINTNUM*)*PI/;
xr = r*cos(rec);
yr = r*sin(rec); SetPixel( hDC, x+xr, y+yr, color ); LeaveCriticalSection(&cs);
Sleep();
}
return ;
} DWORD WINAPI DrawRect( LPVOID lpParama )
{
HDC hDC = HDC(lpParama); int halfl = , halfh =;
int x = , y = ;
COLORREF color = RGB(,,); int average = POINTNUM/; int xr, yr;
int adx, ady; xr = x - halfl; yr = y - halfh;
ady = ;
for ( int i = ; i <= average; i++ )
{
EnterCriticalSection(&cs); adx = (*halfl)*i/average;
SetPixel( hDC, x+xr+adx, y+yr+ady, color ); LeaveCriticalSection(&cs);
Sleep();
} Sleep();
xr = x + halfl; yr = y - halfh;
adx = ;
for ( int i = ; i <= average; i++ )
{
EnterCriticalSection(&cs); ady = (*halfh)*i/average;
SetPixel( hDC, x+xr+adx, y+yr+ady, color ); LeaveCriticalSection(&cs);
Sleep();
} Sleep();
xr = x + halfl; yr = y + halfh;
ady = ;
for ( int i = ; i <= average; i++ )
{
EnterCriticalSection(&cs); adx = -(*halfl)*i/average;
SetPixel( hDC, x+xr+adx, y+yr+ady, color ); LeaveCriticalSection(&cs);
Sleep();
} Sleep();
xr = x -halfl; yr = y + halfh;
adx = ;
for ( int i = ; i <= average; i++ )
{
EnterCriticalSection(&cs); ady = -(*halfh)*i/average;
SetPixel( hDC, x+xr+adx, y+yr+ady, color ); LeaveCriticalSection(&cs);
Sleep();
}
return ;
} static LRESULT CALLBACK WndProc(HWND hWnd, UINT message, WPARAM wParam, LPARAM lParam)
{
HDC hDC;
PAINTSTRUCT ps; switch (message)
{
case WM_CREATE:
return ; case WM_PAINT:
{
hDC = BeginPaint(hWnd, &ps); DWORD ThreadID;
HANDLE hThread[];
InitializeCriticalSection(&cs); hThread[] = CreateThread( NULL, , DrawCircle, (LPVOID)(hDC), , &ThreadID );
hThread[] = CreateThread( NULL, , DrawRect, (LPVOID)(hDC), , &ThreadID ); WaitForMultipleObjects( , hThread, TRUE, INFINITE );
DeleteCriticalSection(&cs); TextOut(hDC, , , TEXT("绘制完毕!"), );
}
EndPaint(hWnd, &ps);
return ; case WM_DESTROY:
PostQuitMessage();
return ;
} return DefWindowProc (hWnd, message, wParam, lParam);
}