Advertisement
Guest User

Untitled

a guest
Feb 18th, 2018
58
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C 4.60 KB | None | 0 0
  1. #include "windows.h"
  2.  
  3. #include "stdio.h"
  4. #include "stdint.h"
  5.  
  6. #define FILE_SIZE (32 * 1024)
  7.  
  8. char FileBuff[FILE_SIZE];
  9. const char FileName[] = "in.txt";
  10.  
  11. struct ThreadParams
  12. {
  13.     HANDLE hMutex;
  14.     const int fileSize;
  15.     const char fileName[64];
  16.     char* fileBuff;
  17. };
  18.  
  19. char generateRandomSymbol()
  20. {
  21.     return ((char)((rand() % (126 + 1 - 33)) + 33));
  22. }
  23.  
  24. void printErrorAndExit(char* functionName)
  25. {
  26.     printf("Error %i while calling %s", GetLastError(), functionName);
  27.     exit(EXIT_SUCCESS);
  28. }
  29.  
  30. void createFileAndFillRandomChars(const char* const filename, const int filesize)
  31. {
  32.     HANDLE file = CreateFile(filename, GENERIC_READ | GENERIC_WRITE, 0, NULL, CREATE_ALWAYS, FILE_ATTRIBUTE_NORMAL, NULL);
  33.     if(INVALID_HANDLE_VALUE == file)
  34.     {
  35.         printErrorAndExit("CreateFile");
  36.     }
  37.  
  38.     int result = 0;
  39.     int buff = 0;
  40.     for(uint64_t i = 0; i < filesize; i++)
  41.     {
  42.         buff = generateRandomSymbol();
  43.         WriteFile(file, &buff, 1, &result, 0);
  44.         if(0 == result)
  45.         {
  46.             printErrorAndExit("WriteFile");
  47.         }
  48.     }
  49.  
  50.     result = CloseHandle(file);
  51.     if(0 == result)
  52.     {
  53.         printErrorAndExit("CloseHandle");
  54.     }
  55. }
  56.  
  57. void mapFileToRam(const char* const filename, const int filesize, char* filebuff)
  58. {
  59.     int result = 0;
  60.     OFSTRUCT buff = {0};
  61.  
  62.     HANDLE file = OpenFile(filename, &buff, OF_READ);
  63.     if(INVALID_HANDLE_VALUE == file)
  64.     {
  65.         printErrorAndExit("OpenFile");
  66.     }
  67.  
  68.     ReadFile(file, filebuff, filesize, &result, NULL);
  69.     if(0 == result)
  70.     {
  71.         printErrorAndExit("ReadFile");
  72.     }
  73.  
  74.     result = CloseHandle(file);
  75.     if(0 == result)
  76.     {
  77.         printErrorAndExit("CloseHandle");
  78.     }
  79. }
  80.  
  81. DWORD firstThreadFunction(LPVOID par)
  82. {
  83.     int bytesWritten = 0;
  84.     int result = WaitForSingleObject(((struct ThreadParams*)par)->hMutex, INFINITE);
  85.     if(WAIT_OBJECT_0 != result)
  86.     {
  87.         printErrorAndExit("WaitForSingleObject");
  88.     }
  89.  
  90.     HANDLE file = CreateFile(((struct ThreadParams*)par)->fileName, GENERIC_READ | GENERIC_WRITE, 0, 0, CREATE_ALWAYS, FILE_ATTRIBUTE_NORMAL, 0);
  91.     if(INVALID_HANDLE_VALUE == file)
  92.     {
  93.         printErrorAndExit("CreateFile");
  94.     }
  95.  
  96.     int a[10] = {0};
  97.     WriteFile(file, ((struct ThreadParams*)par)->fileBuff, ((struct ThreadParams*)par)->fileSize, &bytesWritten, a);
  98.     if(0 == bytesWritten)
  99.     {
  100.         printErrorAndExit("WriteFile");
  101.     }
  102.  
  103.     result = CloseHandle(file);
  104.     if(0 == result)
  105.     {
  106.         printErrorAndExit("CloseHandle");
  107.     }
  108.  
  109.     result = ReleaseMutex(((struct ThreadParams*)par)->hMutex);
  110.     if(0 == result)
  111.     {
  112.         printErrorAndExit("ReleaseMutex");
  113.     }
  114. }
  115.  
  116. DWORD secondThreadFunction(LPVOID par)
  117. {
  118.     int result = WaitForSingleObject(((struct ThreadParams*)par)->hMutex, INFINITE);
  119.     if(WAIT_OBJECT_0 != result)
  120.     {
  121.         printErrorAndExit("WaitForSingleObject");
  122.     }
  123.  
  124.     for(uint64_t i = 0; i < ((struct ThreadParams*)par)->fileSize; i+= 2)
  125.     {
  126.         ((struct ThreadParams*)par)->fileBuff[i] = '*';
  127.     }
  128.  
  129.     result = ReleaseMutex(((struct ThreadParams*)par)->hMutex);
  130.     if(0 == result)
  131.     {
  132.         printErrorAndExit("ReleaseMutex");
  133.     }
  134. }
  135.  
  136. DWORD thirdThreadFunction(LPVOID par)
  137. {
  138.     int result = WaitForSingleObject(((struct ThreadParams*)par)->hMutex, INFINITE);
  139.     if(WAIT_OBJECT_0 != result)
  140.     {
  141.         printErrorAndExit("WaitForSingleObject");
  142.     }
  143.  
  144.     for(uint64_t i = 1; i < ((struct ThreadParams*)par)->fileSize; i+= 2)
  145.     {
  146.         ((struct ThreadParams*)par)->fileBuff[i] = '@';
  147.     }
  148.  
  149.     result = ReleaseMutex(((struct ThreadParams*)par)->hMutex);
  150.     if(0 == result)
  151.     {
  152.         printErrorAndExit("ReleaseMutex");
  153.     }
  154. }
  155.  
  156. int main()
  157. {
  158.     HANDLE fileMutex = CreateMutex(NULL, FALSE, NULL);
  159.     if(0 == fileMutex)
  160.     {
  161.         printErrorAndExit("CreateMutex");
  162.     }
  163.  
  164.     struct ThreadParams structIn = {fileMutex, FILE_SIZE, "out.txt", FileBuff};
  165.  
  166.     createFileAndFillRandomChars(FileName, FILE_SIZE);
  167.     mapFileToRam(FileName, FILE_SIZE, FileBuff);
  168.  
  169.     int handles[3];
  170.  
  171.     handles[0] = CreateThread(0, 0, firstThreadFunction, &structIn, 0, 0);
  172.     if(0 == handles[0])
  173.     {
  174.         printErrorAndExit("CreateThread");
  175.     }
  176.  
  177.     handles[1] = CreateThread(0, 0, secondThreadFunction, &structIn, 0, 0);
  178.     if(0 == handles[1])
  179.     {
  180.         printErrorAndExit("CreateThread");
  181.     }
  182.  
  183.     handles[2] = CreateThread(0, 0, thirdThreadFunction, &structIn, 0, 0);
  184.     if(0 == handles[2])
  185.     {
  186.         printErrorAndExit("CreateThread");
  187.     }
  188.  
  189.     WaitForMultipleObjects(3, handles, 1, INFINITE);
  190. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement