У нас: 141825 рефератів
Щойно додані Реферати Тор 100
Скористайтеся пошуком, наприклад Реферат        Грубий пошук Точний пошук
Вхід в абонемент


HINSTANCE hPrevInstance, LPSTR lpCmdLine, int nCmdShow)

{

//create semaphore

hSemaphore = CreateSemaphore(NULL, 1, 1, "JustASemaphore");

if(hSemaphore==NULL)

{

MessageBox(NULL, "Failed to create semaphore.", "Kursova", 0);

return 0;

}

DialogBoxParam(hInstance, (LPSTR)IDD_DIALOG1, NULL, (DLGPROC)WindowProc, 0);

return 0;

}

//*****

LRESULT CALLBACK WindowProc(HWND hWnd,UINT message,WPARAM wParam,LPARAM lParam)

{

switch(message)

{

case WM_COMMAND:

if(LOWORD(wParam) == IDC_QUIT)

SendMessage(hWnd, WM_CLOSE, 0, 0);

case WM_INITDIALOG:

//create clients generators

hClient1Thread = CreateThread(0, 0, (LPTHREAD_START_ROUTINE)Client1Thread,

hWnd, 0, &dwTread1ID);

hClient2Thread = CreateThread(0, 0, (LPTHREAD_START_ROUTINE)Client2Thread,

hWnd, 0, &dwTread2ID);

if(hClient1Thread==NULL || hClient2Thread==NULL)

{

MessageBox(NULL, "Can't create client threads", "Kursova", 0);

PostMessage(hWnd, WM_CLOSE, 0, 0);

}

//create server thread

hServerThread = CreateThread(0, 0, (LPTHREAD_START_ROUTINE)ServerThread,

hWnd, 0, &dwServerID);

if(hServerThread == NULL)

{

MessageBox(NULL, "Can't create server thread", "Kursova", 0);

PostMessage(hWnd, WM_CLOSE, 0, 0);

}

//create timer

uTimerID = SetTimer(0, 0, 1000, TimerProc);

if(uTimerID==NULL)

{

MessageBox(NULL, "Can't create timer", "Kursova", 0);

PostMessage(hWnd, WM_CLOSE, 0, 0);

}

hMainWindow = hWnd;

break;

case WM_CLOSE:

TerminateThread(hServerThread, 0);

TerminateThread(hClient1Thread, 0);

TerminateThread(hClient2Thread, 0);

CloseHandle(hSemaphore);

KillTimer(0, uTimerID);

EndDialog(hWnd, 0);

break;

}

return FALSE;

}

/// main thread, wich is serv clients ///////////////////////////////////////////////

DWORD WINAPI ServerThread(HWND hWnd)

{

UINT uServing1 = 0;

UINT uServing2 = 0;

UINT uCount;

bool bWait;

CLIENT Client;

UINT ClientType;

UINT uServingTime;

char cBuff[100]; //!!!!!!!! 100 --> 10

UINT TurnLength;

ofstream File;

File.open("Results.txt");

for(uCount=0; uCount<=1000; uCount++)

{

do

{

WaitForSingleObject(hSemaphore, INFINITE);

if(uServing1 >= uAmount1)

{

if(uServing2 >= uAmount2) bWait = TRUE;

else

{

Client = Turn2[uServing2];

ClientType = 2;

uServing2++;

bWait = FALSE;

}

}

else

{

Client = Turn1[uServing1];

ClientType = 1;

uServing1++;

bWait = FALSE;

}

ReleaseSemaphore(hSemaphore, 1, NULL);

Sleep(0);

}

while(bWait==TRUE);

//update main window

if(ClientType == 1)

{

uTurn1--;

IntToStr(cBuff, uTurn1);

SendDlgItemMessage(hWnd, IDC_TURN1, WM_SETTEXT, 0, (long)cBuff);

}

else

{

uTurn2--;

IntToStr(cBuff, uTurn2);

SendDlgItemMessage(hWnd, IDC_TURN2, WM_SETTEXT, 0, (long)cBuff);

}

uTurn--;

IntToStr(cBuff, uTurn);

SendDlgItemMessage(hWnd, IDC_TURN, WM_SETTEXT, 0, (long)cBuff);

//serving client

//generate serving time and sleep for this time

uServingTime = (unsigned)(Generator() * TIME_RATIO);

if(ClientType==1) uServingTime += (unsigned)(CLIENT1_SERVTIME * TIME_RATIO);

else uServingTime += (unsigned)(CLIENT2_SERVTIME * TIME_RATIO);

Serving(&Client, ClientType, uServingTime);

//save results in Results.txt

File << "Клієнт номер: " << uCount << endl;

File << " тип: " << ClientType << endl;

File << " порядковий номер у групі: " << uCount << endl;

File << " поступив у чергу: " << Client.uIn << endl;

File << " обслугований: " << Client.uOut << endl;

File << " час роботи системи: " << uWorkingTime << endl;

File << " черга клієнтів 1 : " << uAmount1 << endl;

File << " черга клієнтів 2 : " << uAmount2 << endl;

//update main window

if(ClientType == 1)

{

IntToStr(cBuff, uServing1);

SendDlgItemMessage(hWnd, IDC_SERVED1, WM_SETTEXT, 0, (long)cBuff);

}

else

{

IntToStr(cBuff, uServing2);

SendDlgItemMessage(hWnd, IDC_SERVED2, WM_SETTEXT, 0, (long)cBuff);

}

IntToStr(cBuff, uServing1+uServing2);

SendDlgItemMessage(hWnd, IDC_SERVED, WM_SETTEXT, 0, (long)cBuff);

} // while(TRUE) Sleep(10000);

SendMessage(hWnd, WM_CLOSE, 0, 0);

File.close();

return 0;

}

/////////////////////////////////////////////////////////////////////////////////////

/// client treads realization ///////////////////////////////////////////////////////

DWORD WINAPI Client1Thread(HWND hWnd)

{

UINT uCount;

UINT uTime;

char cBuff[10];

UINT TurnLength;

for(uCount=1; uCount<=5000; uCount++)

{

//generate to next client uTime --- in miliseconds

//uTime = CLIENT1_COMETIME + Generator();

uTime = (unsigned)(CLIENT1_COMETIME * TIME_RATIO);

uTime += (unsigned)(Generator() * TIME_RATIO);

Sleep(uTime);

//is CMO working

if(bCMOWork==FALSE) return 0;

//add client1 to the first turn

WaitForSingleObject(hSemaphore, INFINITE);

Turn1[uCount].uNumber = uCount;

Turn1[uCount].uIn = GetTickCount();

Turn1[uCount].uOut = 0;

uAmount1++;

//update main window

uTurn1++;

IntToStr(cBuff, uTurn1);

SendDlgItemMessage(hWnd, IDC_TURN1, WM_SETTEXT, 0, (long)cBuff);

uTurn++;

IntToStr(cBuff, uTurn);

SendDlgItemMessage(hWnd, IDC_TURN, WM_SETTEXT, 0, (long)cBuff);

ReleaseSemaphore(hSemaphore, 1, NULL);

}

return 0;

}

DWORD WINAPI Client2Thread(HWND hWnd)

{

UINT uCount;

UINT uTime;

char cBuff[10];

UINT TurnLength;

for(uCount=1; uCount<=5000; uCount++)

{

//generate to next client uTime --- in miliseconds

//uTime = CLIENT2_COMETIME + Generator();

uTime = (unsigned)(CLIENT2_COMETIME * TIME_RATIO);

uTime += (unsigned)(Generator() * TIME_RATIO);

Sleep(uTime);

//is CMO working

if(bCMOWork==FALSE) return 0;

//add client2 to the first turn

WaitForSingleObject(hSemaphore, INFINITE);

Turn2[uCount].uNumber = uCount;

Turn2[uCount].uIn = GetTickCount();

uAmount2++;

//update main window

uTurn2++;

IntToStr(cBuff, uTurn2);

SendDlgItemMessage(hWnd, IDC_TURN2, WM_SETTEXT, 0, (long)cBuff);

uTurn++;

IntToStr(cBuff, uTurn);

SendDlgItemMessage(hWnd, IDC_TURN, WM_SETTEXT, 0, (long)cBuff);

ReleaseSemaphore(hSemaphore, 1, NULL);

}

return 0;

}

/////////////////////////////////////////////////////////////////////////////////////

////////// timer procedure //////////////////////////////////////////////////////////

void Serving(CLIENT *lpClient, UINT uClientType, UINT uServingTime)

{

char cBuff[10];

UINT uQuitTime;

UINT uStart;

//calculate quit time

uStart = GetTickCount();

uQuitTime = GetTickCount() + uServingTime;

//update main window

IntToStr(cBuff, uClientType);

SendDlgItemMessage(hMainWindow, IDC_CLIENTTYPE, WM_SETTEXT, 0, (long)cBuff);

do

{

IntToStr(cBuff, GetTickCount() - uStart);

SendDlgItemMessage(hMainWindow, IDC_PASSED, WM_SETTEXT, 0, (long)cBuff);

}while(uQuitTime >= GetTickCount());

lpClient->uOut = GetTickCount();

return;

}

/////////////////////////////////////////////////////////////////////////////////////

////////// timer procedure //////////////////////////////////////////////////////////

VOID CALLBACK TimerProc(HWND hwnd, UINT uMsg, UINT idEvent, DWORD dwTime)

{

char cBuff[10];

uWorkingTime++;

IntToStr(cBuff, uWorkingTime);

SendDlgItemMessage(hMainWindow, IDC_WORKINGTIME, WM_SETTEXT, 0, (long)cBuff);

return;

}


Сторінки: 1 2 3 4 5 6