Guest User

Основное приложение

a guest
Jun 6th, 2016
130
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 4.47 KB | None | 0 0
  1. #include "stdafx.h"
  2. #include <stdio.h>
  3. #include <conio.h>
  4. #include <stdlib.h>
  5. #include <time.h>
  6. #include <iostream>
  7. #include <Windows.h>
  8. #include <locale.h>
  9. using namespace std;
  10.  
  11. #define BUF_SIZE 256
  12. #define _CRT_SECURE_NO_WARNINGS
  13.  
  14.  
  15. TCHAR szName[] = TEXT("MyFileMappingObject");
  16. TCHAR szNameSecond[] = TEXT("MyFileMappingObjectSecond");
  17. TCHAR szMsg[] = TEXT("10");
  18.  
  19. void Mythread(PROCESS_INFORMATION myTh)
  20. {
  21. srand(time(NULL));
  22. double t = 1000 + rand() % 2000;
  23. ResumeThread(myTh.hThread);
  24. Sleep(t);
  25. SuspendThread(myTh.hThread);
  26. }
  27.  
  28.  
  29. DWORD WINAPI mainThread()
  30. {
  31.  
  32. // Zadanie peremennyh
  33. STARTUPINFO si; // struktura s informaciej o zapuske processa
  34. PROCESS_INFORMATION pi[2]; // struktura s informaciej o 2 processah
  35. ZeroMemory(&si, sizeof(si));
  36. si.cb = sizeof(si);
  37. ZeroMemory(&pi, sizeof(pi));
  38. HANDLE hMapFile; // mapping file handle
  39. LPCTSTR pBuf; // ukazatel' na nachal'nyj adres
  40. TCHAR *ProcessName = new TCHAR;
  41. bool flag = true, flag2 = true;
  42.  
  43.  
  44. // sozdanie, zapusk i unichtozhenie 2h processov
  45.  
  46. mbstowcs(ProcessName, "C:\\Users\\Username\\os2\\OsLab2FProc\\Debug\\OsLab2FProc.exe", 250);
  47. if (!CreateProcess(ProcessName,
  48. NULL,
  49. NULL,
  50. NULL,
  51. FALSE,
  52. CREATE_NEW_CONSOLE,
  53. NULL,
  54. NULL,
  55. &si,
  56. &pi[0])
  57. )
  58. printf("CreateProcess failed, error - %d.\n", GetLastError());
  59. SuspendThread(pi[0].hThread);
  60.  
  61. mbstowcs(ProcessName, "C:\\Users\\Username\\os2\\OsLab2SProc\\Debug\\OsLab2SProc.exe", 250);
  62. if (!CreateProcess(ProcessName, NULL, NULL, NULL, FALSE, CREATE_NEW_CONSOLE, NULL, NULL, &si, &pi[1]))
  63. printf("CreateProcess failed, error - %d.\n", GetLastError());
  64. SuspendThread(pi[1].hThread);
  65.  
  66. for (int i = 0; i < 3; i++) // zaderzhki mezhdu iteracijami u odnogo potoka net, ona voznikaet tol'ko pri zapuske vtorogo
  67. {
  68. // pervyj potok
  69. if (flag == true)
  70. {
  71. Mythread(pi[0]);
  72. hMapFile = OpenFileMapping(FILE_MAP_ALL_ACCESS, FALSE, szName);
  73. if (hMapFile == NULL)
  74. {
  75. printf("Can not open file mapping object (%d).\n", GetLastError());
  76. _getch();
  77. return 1;
  78. }
  79.  
  80. pBuf = (LPTSTR)MapViewOfFile(hMapFile, FILE_MAP_ALL_ACCESS, 0, 0, BUF_SIZE);
  81. if (pBuf == NULL)
  82. {
  83. printf("Presentation of the file can not be projected (%d).\n", GetLastError());
  84. _getch();
  85. CloseHandle(hMapFile);
  86. return 1;
  87. }
  88.  
  89. if (lstrcmp(pBuf, szMsg) == 0)
  90. {
  91. TerminateProcess(pi[0].hProcess, 0);
  92. UnmapViewOfFile(pBuf);
  93. CloseHandle(hMapFile);
  94. flag = false;
  95. }
  96. }
  97.  
  98.  
  99. // vtoroj potok
  100. if (flag2 == true)
  101. {
  102. Mythread(pi[1]);
  103. hMapFile = OpenFileMapping(FILE_MAP_ALL_ACCESS, FALSE, szNameSecond);
  104. if (hMapFile == NULL)
  105. {
  106. printf("Can not open file mapping object (%d).\n", GetLastError());
  107. _getch();
  108. return 1;
  109. }
  110.  
  111. pBuf = (LPTSTR)MapViewOfFile(hMapFile, FILE_MAP_ALL_ACCESS, 0, 0, BUF_SIZE);
  112. if (pBuf == NULL)
  113. {
  114. printf("Presentation of the file can not be projected (%d).\n", GetLastError());
  115. _getch();
  116. CloseHandle(hMapFile);
  117. return 1;
  118. }
  119.  
  120. if (lstrcmp(pBuf, szMsg) == 0)
  121. {
  122. TerminateProcess(pi[1].hProcess, 0);
  123. UnmapViewOfFile(pBuf);
  124. CloseHandle(hMapFile);
  125. flag2 = false;
  126. }
  127. }
  128. }
  129.  
  130. if (pi[0].hProcess != NULL) TerminateProcess(pi[0].hProcess, 0);
  131. if (pi[1].hProcess != NULL) TerminateProcess(pi[1].hProcess, 0);
  132.  
  133.  
  134. // zakrytie deskriptorov processov i potokov.
  135.  
  136. CloseHandle(pi[0].hProcess);
  137. CloseHandle(pi[0].hThread);
  138. CloseHandle(pi[1].hProcess);
  139. CloseHandle(pi[1].hThread);
  140. return 0;
  141. }
  142.  
  143.  
  144. int _tmain(int argc, _TCHAR* argv[])
  145. {
  146. HANDLE mT = CreateThread(NULL, 0, LPTHREAD_START_ROUTINE(&mainThread), 0, 0, 0);
  147. WaitForSingleObject(mT,INFINITE);
  148. CloseHandle(mT);
  149. return 0;
  150. }
Add Comment
Please, Sign In to add comment