Program 7-2 sortMT: Merge-Sort with Multiple Threads in VS .NET

Creator Denso QR Bar Code in VS .NET Program 7-2 sortMT: Merge-Sort with Multiple Threads
Program 7-2 sortMT: Merge-Sort with Multiple Threads
QR Code ISO/IEC18004 Encoder In VS .NET
Using Barcode printer for .NET Control to generate, create QR Code image in .NET applications.
/* 7 SortMT File sorting with multiple threads (a work crew) sortMT [options] nt file */ #include "EvryThngh" #define DATALEN 56 /* Key: 8 bytes; Data: 56 bytes */ #define KEYLEN 8 typedef struct _RECORD { CHAR Key [KEYLEN]; TCHAR Data [DATALEN]; } RECORD; #define RECSIZE sizeof (RECORD) typedef RECORD * LPRECORD;
Decode QR Code JIS X 0510 In VS .NET
Using Barcode decoder for .NET framework Control to read, scan read, scan image in .NET framework applications.
typedef struct _THREADARG { DWORD iTh; LPRECORD LowRec; LPRECORD HighRec; } THREADARG, *PTHREADARG; static static static static
Print Barcode In VS .NET
Using Barcode creation for .NET framework Control to generate, create barcode image in .NET framework applications.
/* /* /* /*
Bar Code Scanner In Visual Studio .NET
Using Barcode recognizer for VS .NET Control to read, scan read, scan image in VS .NET applications.
Thread argument */ Thread number: 0, 1, 2, */ Low record */ High record */
Denso QR Bar Code Generation In C#
Using Barcode encoder for .NET Control to generate, create QR Code 2d barcode image in .NET framework applications.
int KeyCompare (LPCTSTR, LPCTSTR); DWORD WINAPI ThSort (PTHREADARG pThArg); DWORD nRec; /* Total number of records to be sorted */ HANDLE * ThreadHandle;
QR-Code Maker In .NET Framework
Using Barcode drawer for ASP.NET Control to generate, create QR Code ISO/IEC18004 image in ASP.NET applications.
int _tmain (int argc, LPTSTR argv []) { HANDLE hFile; LPRECORD pRecords = NULL; DWORD FsLow, nRead, LowRecNo, nRecTh, NPr, ThId, iTh; BOOL NoPrint; int iFF, iNP; PTHREADARG ThArg; LPTSTR StringEnd; iNP = iFF = NPr = hFile Options (argc, argv, _T ("n"), &NoPrint, NULL); iNP + 1; _ttoi (argv [iNP]); /* Number of threads */ = CreateFile (argv [iFF], GENERIC_READ | GENERIC_WRITE, 0, NULL, OPEN_EXISTING, 0, NULL); FsLow = GetFileSize (hFile, NULL); nRec = FsLow / RECSIZE; /* Total number of records */ nRecTh = nRec / NPr; /* Records per thread */ /* Allocate thread args and handle array and space for the file Read the complete file */ ThArg = malloc (NPr * sizeof (THREADARG)); /* Thread args */ ThreadHandle = malloc (NPr * sizeof (HANDLE)); pRecords = malloc (FsLow + sizeof (TCHAR)); ReadFile (hFile, pRecords, FsLow, &nRead, NULL); CloseHandle (hFile); LowRecNo = 0; /* Create the sorting threads */ for (iTh = 0; iTh < NPr; iTh++) { ThArg [iTh]iTh = iTh; ThArg [iTh]LowRec = pRecords + LowRecNo; ThArg [iTh]HighRec = pRecords + (LowRecNo + nRecTh); LowRecNo += nRecTh; ThreadHandle [iTh] = (HANDLE) _beginthreadex (NULL, 0, ThSort, &ThArg [iTh], CREATE_SUSPENDED, &ThId); } for (iTh = 0; iTh < NPr; iTh++) /* Run all sort threads */ ResumeThread (ThreadHandle [iTh]); WaitForSingleObject (ThreadHandle [0], INFINITE); for (iTh = 0; iTh < NPr; iTh++) CloseHandle (ThreadHandle [iTh]); StringEnd = (LPTSTR) pRecords + FsLow; *StringEnd = '\0'; if (!NoPrint) printf ("\n%s", (LPCTSTR) pRecords); free (pRecords); free (ThArg); free (ThreadHandle);
QR Code JIS X 0510 Generator In VB.NET
Using Barcode creator for VS .NET Control to generate, create QR-Code image in VS .NET applications.
return 0; /* End of _tmain */
Barcode Printer In .NET Framework
Using Barcode maker for Visual Studio .NET Control to generate, create barcode image in .NET applications.
static VOID MergeArrays (LPRECORD, LPRECORD); DWORD WINAPI ThSort (PTHREADARG pThArg) { DWORD GrpSize = 2, RecsInGrp, MyNumber, TwoToI = 1; LPRECORD First; MyNumber = pThArg->iTh; First = pThArg->LowRec; RecsInGrp = pThArg->HighRec - First; qsort (First, RecsInGrp, RECSIZE, KeyCompare); while ((MyNumber % GrpSize) == 0 && RecsInGrp < nRec) { /* Merge with the adjacent sorted array */ WaitForSingleObject ( ThreadHandle [MyNumber + TwoToI], INFINITE); MergeArrays (First, First + RecsInGrp); RecsInGrp *= 2; GrpSize *= 2; TwoToI *= 2; } _endthreadex (0); return 0; /* Suppress a warning message */ } static VOID MergeArrays (LPRECORD p1, LPRECORD p2) { DWORD iRec = 0, nRecs, i1 = 0, i2 = 0; LPRECORD pDest, p1Hold, pDestHold; nRecs = p2 - p1; pDest = pDestHold = malloc (2 * nRecs * RECSIZE); p1Hold = p1; while (i1 < nRecs && i2 < nRecs) { if (KeyCompare ((LPCTSTR) p1, (LPCTSTR) p2) <= 0) { memcpy (pDest, p1, RECSIZE); i1++; p1++; pDest++; } else { memcpy (pDest, p2, RECSIZE); i2++; p2++; pDest++; } } if (i1 >= nRecs) memcpy (pDest, p2, RECSIZE * (nRecs - i2)); else memcpy (pDest, p1, RECSIZE * (nRecs - i1)); memcpy (p1Hold, pDestHold, 2 * nRecs * RECSIZE); free (pDestHold); return; }
Bar Code Drawer In .NET
Using Barcode generator for .NET framework Control to generate, create bar code image in VS .NET applications.
Performance
UPC-A Supplement 2 Creation In .NET
Using Barcode printer for .NET Control to generate, create Universal Product Code version A image in .NET applications.
Appendix C includes the results of sorting large files of 64-byte records using one, two, and four threads SMP systems give significantly better results Divide and conquer is more than just a strategy
Painting Code 39 Extended In Visual Studio .NET
Using Barcode creation for .NET framework Control to generate, create Code 3/9 image in VS .NET applications.
for algorithm design; it can also be the key to exploiting threads and SMP The single-processor results can vary On a system with limited memory (that is, insufficient physical memory to hold the entire file, in addition to the OS and other active processes), the use of multiple threads increases the sort time because the threads contend for available physical memory On the other hand, multiple threads can improve performance with a single processor when there is sufficient memory The results are also heavily dependent on the initial data arrangement, as discussed in Appendix C
Making Code 2 Of 5 In .NET
Using Barcode drawer for VS .NET Control to generate, create 2 of 5 Industrial image in Visual Studio .NET applications.
Read Barcode In .NET Framework
Using Barcode decoder for .NET framework Control to read, scan read, scan image in .NET applications.
Painting Barcode In Java
Using Barcode maker for Java Control to generate, create barcode image in Java applications.
DataMatrix Creation In C#
Using Barcode creation for Visual Studio .NET Control to generate, create Data Matrix 2d barcode image in .NET applications.
DataMatrix Generator In VS .NET
Using Barcode creator for ASP.NET Control to generate, create Data Matrix image in ASP.NET applications.