(API) Windows. , Win API Borland Delphi, C++ Builder Visual Studio.
Sort, :
1. ;
2. : m:s:ms;
3. t1;
3.1. i 1 100 :
3.2. 0-10000;
3.3. ;
4. t2;
5. : (t2 - t1) / 100;
6. ( ms);
7. ;
8. : m:s:ms.
Master, :
1. i 1 3 :
1.1. CreateProcess, Sort , . , : ": NP; : PP", NP (1 2), PP ( STARTUPINFO);
1.2. ( WaitForSingleObject);
2. , .
Threads, , N/50 printArray. :
1. , N/50 ;
2. CreateThread, sort printArray ;
3. , SetThreadPriority.
4. , ResumeThread;
5. .
Sort.cpp
#include <iostream>
#include <windows.h>
#include <locale>
#include <conio.h>
#define QUANTITY 1000
#define N 3500
using namespace std;
void sort(int * arr, int n)
{
int max;
for (int i = 0; i < n; i++)
{
max = i;
for (int j = i + 1; j < n; j++)
if (arr[j] < arr[max])
max = j;
swap(arr[i], arr[max]);
}
}
int getTimeInMS(SYSTEMTIME time)
{
return time.wDay * 24 * 60 * 60 * 1000 +
time.wHour * 60 * 60 * 1000 +
time.wMinute * 60 * 1000 +
time.wSecond * 1000 +
time.wMilliseconds;
}
int main(int argc, char * argv[])
|
|
{
setlocale(LC_ALL, "");
SYSTEMTIME timeStart, timeEnd, t1, t2;
GetSystemTime(&timeStart);
cout << " : " << timeStart.wMinute << ":" << timeStart.wSecond << ":" << timeStart.wMilliseconds << endl;
int arr[N];
GetSystemTime(&t1);
srand(time(NULL));
for (int i = 0; i < QUANTITY; i++)
{
for (int j = 0; j < N; j++)
arr[j] = rand() % 10000;
sort(arr, N);
}
GetSystemTime(&t2);
cout << " : " << ((getTimeInMS(t2) - getTimeInMS(t1)) / QUANTITY) << " ms" << endl;
GetSystemTime(&timeEnd);
cout << " : " << timeEnd.wMinute << ":" << timeEnd.wSecond << ":" << timeEnd.wMilliseconds << endl;
getch();
return 0;
}
Master.cpp
#include <iostream>
#include <windows.h>
#include <cstdio>
#include <clocale>
using namespace std;
char * prioritiesString[] = {
"IDLE_PRIORITY_CLASS",
"HIGH_PRIORITY_CLASS",
"IDLE_PRIORITY_CLASS",
"IDLE_PRIORITY_CLASS",
"NORMAL_PRIORITY_CLASS",
"IDLE_PRIORITY_CLASS"
};
WORD prioritiesWORD[] = {
IDLE_PRIORITY_CLASS,
HIGH_PRIORITY_CLASS,
IDLE_PRIORITY_CLASS,
IDLE_PRIORITY_CLASS,
NORMAL_PRIORITY_CLASS,
IDLE_PRIORITY_CLASS
};
int main(int argc, char * argv[])
{
setlocale(LC_ALL, "");
char stringFirst[32];
char stringSecond[32];
for (int i = 1, j = 0; i <= 3; i++, j += 2)
{
STARTUPINFO consoleInfo1, consoleInfo2;
PROCESS_INFORMATION processInfo1, processInfo2;
ZeroMemory(&consoleInfo1, sizeof(consoleInfo1));
ZeroMemory(&consoleInfo2, sizeof(consoleInfo2));
sprintf(stringFirst, "Ïðîöåññ: 1; Ïðèîðèòåò: %s", prioritiesString[j]);
consoleInfo1.lpTitle = stringFirst;
consoleInfo1.cb = sizeof(consoleInfo1);
CreateProcess(NULL,
"Sort.exe",
NULL,
NULL,
FALSE,
CREATE_NEW_CONSOLE | prioritiesWORD[j],
NULL,
NULL,
&consoleInfo1,
&processInfo1);
sprintf(stringSecond, "Ïðîöåññ: 2; Ïðèîðèòåò: %s", prioritiesString[j + 1]);
consoleInfo2.lpTitle = stringSecond;
consoleInfo2.cb = sizeof(consoleInfo2);
CreateProcess(NULL,
"Sort.exe",
NULL,
NULL,
FALSE,
CREATE_NEW_CONSOLE | prioritiesWORD[j + 1],
NULL,
NULL,
&consoleInfo2,
&processInfo2);
WaitForSingleObject(processInfo1.hProcess, INFINITE);
WaitForSingleObject(processInfo2.hProcess, INFINITE);
}
return 0;
}
Threads.cpp
#include <iostream>
#include <clocale>
#include <ctime>
#include <windows.h>
#define N (3500/50)
using namespace std;
int arr[N];
void printArray()
{
for (int i = 0; i < N; i++)
|
|
cout << arr[i] << " ";
cout << endl;
}
void sort()
{
int max;
for (int i = 0; i < N; i++)
{
max = i;
for (int j = i + 1; j < N; j++)
if (arr[j] < arr[max])
max = j;
swap(arr[i], arr[max]);
}
}
int main()
{
setlocale(LC_ALL, "");
HANDLE threadSort, threadPrint;
DWORD idSort, idPrint;
srand(time(NULL));
for (int i = 0; i < N; i++)
arr[i] = rand() % 10000;
threadSort = CreateThread(NULL,
0,
(LPTHREAD_START_ROUTINE)&sort,
NULL,
CREATE_SUSPENDED,
&idSort
);
threadPrint = CreateThread(NULL,
0,
(LPTHREAD_START_ROUTINE)&printArray,
NULL,
CREATE_SUSPENDED,
&idPrint
);
SetThreadPriority(threadSort, THREAD_PRIORITY_NORMAL);
SetThreadPriority(threadPrint, THREAD_PRIORITY_NORMAL);
ResumeThread(threadSort);
ResumeThread(threadPrint);
system("pause");
}
1 Threads.exe
threadSort, THREAD_PRIORITY_HIGHEST
threadPrint, THREAD_PRIORITY_IDLE
2 Threads.exe
threadSort, THREAD_PRIORITY_IDLE
threadPrint, THREAD_PRIORITY_HIGHEST
3 Threads.exe
threadSort, THREAD_PRIORITY_NORMAL
threadPrint, THREAD_PRIORITY_ NORMAL
4 Master.exe ( 2 )
5 Master.exe ( 2 )
6 Master.exe ( 2 )
(API) Windows. , Win API ++ Builder. . .