4,436
社区成员
发帖
与我相关
我的任务
分享
//共享内存机制
#include<iostream>
#include<windows.h>
#include<fstream>
#include<time.h>
#include<queue>
#include<TCHAR.h>
using namespace std;
#define semaphore int
#define N 1000000
int n = 1;
int* DB;
int SortedNum = 0;
bool END = false; //标识排序工作的完成
TCHAR filename[256];
HANDLE mutex1 = CreateMutex(NULL, FALSE, NULL);
HANDLE finished = CreateSemaphore(NULL, 0, 1, NULL);
void CreateRandNum() {
FILE *file = fopen("input.txt", "w+");//读写打开一个文本文件
srand((unsigned int)time(NULL));
int* data = (int*)malloc(sizeof(int)*N);
for (int i = 0; i < N; i++) {
data[i] = rand();
fprintf(file, "%d ", data[i]);
}
fclose(file);
}
class Param {
public:
int f; //front
int t; //tail
Param(int a, int b) {
f = a;
t = b;
}
};
//对包含元素大于1000的序列进行再分割,快速排序
int Partition(int *p, int left, int right) {
cout << n++ << endl;
int f = left;
int t = right;
int key = *(p + f);
while (f < t) {
while (f < t &&key <= *(p + t)) t--;
p[f] = p[t];
while (f < t && key >= *(p + f)) f++;
p[t] = p[f];
}
p[f] = key;
return f;
}
//对包含元素小于1000的序列不再分割,进行冒泡排序,其实也可以递归调用快速排序,只不过不再创建新的进程
void Sort(int*p, int f, int t) {
int num = t - f + 1;
for (int i = 0; i < num - 1; i++)
for (int j = 0; j < num - 1 - i; j++) {
if (*(p + j + 1) < *(p + j)) {
int temp = *(p + j);
*(p + j) = *(p + j + 1);
*(p + j + 1) = temp;
}
}
}
/*快速排序线程*/
DWORD WINAPI QuickSortThread(PVOID pM) {
Param *pBuf = (Param*)pM;
while (!END) {
if (pBuf->t - pBuf->f < 1000) {
WaitForSingleObject(mutex1, INFINITE);
Sort(DB, pBuf->f, pBuf->t);
SortedNum += pBuf->t - pBuf->f + 1;
ReleaseMutex(mutex1);
if (SortedNum == N)
ReleaseSemaphore(finished, 1, NULL);
}
else {
WaitForSingleObject(mutex1, INFINITE);
int s = Partition(DB, pBuf->f, pBuf->t);
SortedNum++;
ReleaseMutex(mutex1);
Param *para1 = new Param(pBuf->f, s - 1);
Param *para2 = new Param(s + 1, pBuf->t);
HANDLE Thread1 = CreateThread(NULL, 0, QuickSortThread, para1, 0, NULL);
WaitForSingleObject(Thread1, INFINITE);
CloseHandle(Thread1);
HANDLE Thread2 = CreateThread(NULL, 0, QuickSortThread, para2, 0, NULL);
WaitForSingleObject(Thread2, INFINITE);
CloseHandle(Thread2);
}
}
return 0;
}
int main() {
CreateRandNum();
printf("输入生成文件名:\n");
scanf("%ws", filename);
//Create file mapping
HANDLE hFile = CreateFile(filename, (GENERIC_READ | GENERIC_WRITE),
FILE_SHARE_READ, NULL, OPEN_EXISTING, FILE_ATTRIBUTE_NORMAL, NULL);
//if (hFile == INVALID_HANDLE_VALUE) printf("Can't open file!");
HANDLE MapSrc = CreateFileMapping(hFile, NULL, PAGE_READWRITE, 0, 0, _T("input_map"));
DB = (int*)MapViewOfFile(MapSrc, FILE_MAP_ALL_ACCESS, 0, 0, 0);
Param *p = new Param(0, N - 1);
HANDLE Thread = CreateThread(NULL, 0, QuickSortThread, p, 0, NULL);
WaitForSingleObject(finished, INFINITE);
FlushViewOfFile(DB, 4 * N);
CloseHandle(hFile);
CloseHandle(MapSrc);
CloseHandle(Thread);
return 0;
}
void CreateRandNum() {
FILE *file = fopen("input.txt", "w+");//读写打开一个文本文件
srand((unsigned int)time(NULL));
int* data = (int*)malloc(sizeof(int)*N);
for (int i = 0; i < N; i++) {
data[i] = rand();
//fprintf(file, "%d ", data[i]);
fwrite(&data[i], 4, 1, file); //assume int takes 4Bytes
}
fclose(file);
}
//对包含元素大于1000的序列进行再分割,快速排序
int Partition(int *p, int left, int right) {
cout << n++ << endl;
int f = left;
int t = right;
int key = p[f]; //the way "p+f" is not correct, "f" here is the index but not offset
while (f < t) {
while (f < t &&key <= p[t]) t--;
p[f] = p[t];
while (f < t && key >= p[f]) f++;
p[t] = p[f];
}
p[f] = key;
return f;
}
//对包含元素小于1000的序列不再分割,进行冒泡排序,其实也可以递归调用快速排序,只不过不再创建新的进程
void Sort(int*p, int f, int t) {
int num = t - f + 1;
for (int i = 0; i < num - 1; i++)
for (int j = 0; j < num - 1 - i; j++) {
if (p[ j + 1] < p[j]) {
int temp = p[j];
p[j] = p[j+1];
p[j+1] = p[j];
}
}
}
//共享内存机制
#include<iostream>
#include<windows.h>
#include<fstream>
#include<time.h>
#include<queue>
#include<TCHAR.h>
using namespace std;
#define semaphore int
#define N 100000
int n = 1;
int* DB;
int SortedNum = 0;
bool END = false; //标识排序工作的完成
TCHAR filename[256];
HANDLE mutex1 = CreateMutex(NULL, FALSE, NULL);
HANDLE finished = CreateSemaphore(NULL, 0, 1, NULL);
void CreateRandNum() {
FILE *file = fopen("input.txt", "wb+");//读写打开一个文本文件
srand((unsigned int)time(NULL));
int* data = (int*)malloc(sizeof(int)*N);
for (int i = 0; i < N; i++) {
data[i] = rand();
//fprintf(file, "%d ", data[i]);
fwrite(&data[i], 4, 1, file);
}
fclose(file);
}
class Param {
public:
int f; //front
int t; //tail
Param(int a, int b) {
f = a;
t = b;
}
};
//对包含元素大于1000的序列进行再分割,快速排序
int Partition(int *p, int left, int right) {
cout << n++ << endl;
int f = left;
int t = right;
int key = p[f];
while (f < t) {
while (f < t &&key <= p[t]) t--;
p[f] = p[t];
while (f < t && key >= p[f]) f++;
p[t] = p[f];
}
p[f] = key;
return f;
}
//对包含元素小于1000的序列不再分割,进行冒泡排序,其实也可以递归调用快速排序,只不过不再创建新的进程
void Sort(int*p, int f, int t) {
int num = t - f + 1;
for (int i = 0; i < num-1; i++)
for (int j = 0; j < num-1 -i; j++) {
if (p[f+j + 1] < p[f+j]) {
int temp = p[f+j];
p[f+j] = p[f+j + 1];
p[f+j + 1] = temp;
}
}
}
/*快速排序线程*/
DWORD WINAPI QuickSortThread(PVOID pM) {
Param *pBuf = (Param*)pM;
while (!END) {
if (pBuf->t - pBuf->f < 1000) {
WaitForSingleObject(mutex1, INFINITE);
Sort(DB, pBuf->f, pBuf->t);
SortedNum += pBuf->t - pBuf->f + 1;
ReleaseMutex(mutex1);
if (SortedNum >= N)
ReleaseSemaphore(finished, 1, NULL);
}
else {
WaitForSingleObject(mutex1, INFINITE);
int s = Partition(DB, pBuf->f, pBuf->t);
SortedNum++;
ReleaseMutex(mutex1);
if (pBuf->f != s) {
Param *para1 = new Param(pBuf->f, s - 1);
HANDLE Thread1 = CreateThread(NULL, 0, QuickSortThread, para1, 0, NULL);
WaitForSingleObject(Thread1, INFINITE);
CloseHandle(Thread1);
}
if (s != pBuf->t) {
Param *para2 = new Param(s + 1, pBuf->t);
HANDLE Thread2 = CreateThread(NULL, 0, QuickSortThread, para2, 0, NULL);
WaitForSingleObject(Thread2, INFINITE);
CloseHandle(Thread2);
}
}
break; //Well, actuall we don't need while loop as we will "wait" until the recursive thread been finished.
}
return 0;
}
int main() {
CreateRandNum();
printf("输入生成文件名:\n");
scanf("%ws", filename);
//Create file mapping
HANDLE hFile = CreateFile(filename, (GENERIC_READ | GENERIC_WRITE),
FILE_SHARE_READ, NULL, OPEN_EXISTING, FILE_ATTRIBUTE_NORMAL, NULL);
//if (hFile == INVALID_HANDLE_VALUE) printf("Can't open file!");
HANDLE MapSrc = CreateFileMapping(hFile, NULL, PAGE_READWRITE, 0, 0, _T("input_map"));
DB = (int*)MapViewOfFile(MapSrc, FILE_MAP_ALL_ACCESS, 0, 0, 0);
Param *p = new Param(0, N - 1);
HANDLE Thread = CreateThread(NULL, 0, QuickSortThread, p, 0, NULL);
WaitForSingleObject(finished, INFINITE);
FlushViewOfFile(DB, 4 * N);
CloseHandle(hFile);
CloseHandle(MapSrc);
CloseHandle(Thread);
return 0;
}