Savelyev_Vyacheslav

OS_laba2_MasterL2

May 5th, 2021 (edited)
226
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 6.24 KB | None | 0 0
  1. // MasterL2.cpp : Этот файл содержит функцию "main". Здесь начинается и заканчивается выполнение программы.
  2. //
  3.  
  4. #define _CRT_SECURE_NO_WARNINGS
  5. #include <iostream>
  6. #include <thread>
  7. #include <ctime>
  8. #include <cstdlib>
  9.  
  10. #include <windows.h>
  11. #include <stdio.h>
  12. #include <string.h>
  13. using namespace std;
  14.  
  15.  
  16. class LiveTimer
  17. {
  18.  
  19. public:
  20.  
  21.     int iteration_;
  22.     int DurationMilliSEC_;
  23.     int fMIN_;
  24.     int fSEC_;
  25.     int fMilliSEC_;
  26.     void  Tstrt()
  27.     {
  28.         T_strt = std::chrono::high_resolution_clock::now();
  29.         chrono::duration<float> Tstrt = T_strt - T_strt;
  30.         cout << "Tstrt: " << Tstrt.count() << endl;
  31.     }
  32.     void  Tend()
  33.     {
  34.         T_end = std::chrono::high_resolution_clock::now();
  35.         chrono::duration<float> duration = (T_end - T_strt) * 1000;
  36.         LiveTimer::DurationMilliSEC_ = duration.count();
  37.         LiveTimer::fMIN_ = (duration.count() / 1000 / 60);
  38.         LiveTimer::fSEC_ = int(duration.count() / 1000) % 60;
  39.         LiveTimer::fMilliSEC_ = int(duration.count()) - fMIN_ * 60 * 1000 - fSEC_ * 1000;
  40.         chrono::duration<float> Tstrt = T_strt - T_strt;
  41.         cout << "Tend: " << duration.count() << endl;
  42.     }
  43.     void printT() {
  44.         cout << " MilliSEC/iteration: " << LiveTimer::DurationMilliSEC_ / iteration_ << endl;
  45.         cout << "MIN: " << LiveTimer::fMIN_ << " SEC: " << LiveTimer::fSEC_ << " MilliSEC: " << LiveTimer::fMilliSEC_ << endl;
  46.     }
  47. private:
  48.     std::chrono::time_point<std::chrono::steady_clock> T_strt, T_end;
  49.  
  50. };
  51.  
  52.  
  53.  
  54. LPWSTR winApiFormat(char text[], wchar_t wtext2[128]) {
  55.     mbstowcs(wtext2, text, strlen(text) + 1);//Plus null
  56.     LPWSTR ptr = wtext2;
  57.     return ptr;
  58. }
  59. void titleSet(char a[]) {
  60.     wchar_t wtext2[128];
  61.     LPWSTR ptr2 = winApiFormat(a, wtext2);
  62.     SetConsoleTitle(ptr2);
  63. }
  64.  
  65. void setIntArg(char* cmd, int pid) {
  66.     sprintf(cmd + strlen(cmd), " %d", pid);
  67. }
  68. void setCharArg(char* cmd, char* arg) {
  69.     sprintf(cmd + strlen(cmd), " %s", arg);
  70. }
  71.  
  72.  
  73.  
  74.  
  75. void pr(char cmd[128], char Process[128], char Prioritet[128]) {
  76.     /// ///////////////////////
  77.     char cmd2[128];
  78.     wchar_t wtext2[128];
  79.     strcpy(cmd2, cmd);
  80.     setCharArg(cmd2, Process);
  81.     setCharArg(cmd2, Prioritet);
  82.     cout << "Master: Start ___ " << cmd2 << " ___" << "\n";
  83.     LPWSTR cmdPtr = winApiFormat(cmd2, wtext2);
  84.  
  85.     STARTUPINFO info;
  86.     memset(&info, 0, sizeof(info));
  87.     info.cb = sizeof(info);
  88.     //HANDLE hEvent;
  89.     PROCESS_INFORMATION pinfo;
  90.    
  91.     if (Prioritet[0] == 'I') {
  92.         cout << "------------------------------------\n";
  93.         bool b = CreateProcess(NULL, cmdPtr, NULL, NULL, FALSE, IDLE_PRIORITY_CLASS | CREATE_NEW_CONSOLE,
  94.             NULL, NULL, &info, &pinfo);
  95.         if (!b) {
  96.             cout << "Master: Slave protsess ne zapushchen!!!!!!!!!!!!!\n";
  97.             cout << "Master: проверьте правильность указания имени процесса в коммандной строке";
  98.         }
  99.  
  100.     }
  101.     if (Prioritet[0] == 'N') {
  102.         cout << "++++++++++++++++++++++++++++++++++++++\n";
  103.         bool b = CreateProcess(NULL, cmdPtr, NULL, NULL, FALSE, NORMAL_PRIORITY_CLASS | CREATE_NEW_CONSOLE,
  104.             NULL, NULL, &info, &pinfo);
  105.         if (!b) {
  106.             cout << "Master: Slave protsess ne zapushchen!!!!!!!!!!!!!\n";
  107.             cout << "Master: проверьте правильность указания имени процесса в коммандной строке";
  108.         }
  109.    
  110.     }
  111.     if (Prioritet[0] == 'H') {
  112.         cout << "***************************************\n";
  113.         bool b = CreateProcess(NULL, cmdPtr, NULL, NULL, FALSE, HIGH_PRIORITY_CLASS | CREATE_NEW_CONSOLE,
  114.             NULL, NULL, &info, &pinfo);
  115.         if (!b) {
  116.             cout << "Master: Slave protsess ne zapushchen!!!!!!!!!!!!!\n";
  117.             cout << "Master: проверьте правильность указания имени процесса в коммандной строке";
  118.         }
  119.    
  120.     }
  121.     HANDLE process = OpenProcess(PROCESS_QUERY_INFORMATION | SYNCHRONIZE,
  122.             FALSE, pinfo.dwProcessId);
  123.         if (!process) cout << " Oshibka otkrytiay protsessa Sort\n";
  124.         if (WaitForSingleObject(process, INFINITE) == STATUS_WAIT_0)
  125.         {
  126.             cout << Process << " ------------zavershil rabotu------------\n";
  127.             //cin.get();
  128.         }
  129. }
  130.  
  131. //         MasterL2.exe
  132. int main(int argc, char* argv[]) {
  133.     char cmd[128];
  134.     if (argc != 1) strcpy(cmd, argv[1]);
  135.     else strcpy(cmd, "Sort.exe");
  136.  
  137.     char title[128] = "MasterL2";
  138.     titleSet(title);
  139.  
  140.     int pid = GetCurrentProcessId();////////////////////////////////////
  141.     char Process1[128] = "Process1";
  142.     char Process2[128] = "Process2";
  143.     char Prioritet1[128] = "IDLE_PRIORITY_CLASS";
  144.     char Prioritet2[128] = "NORMAL_PRIORITY_CLASS";
  145.     char Prioritet3[128] = "HIGH_PRIORITY_CLASS";
  146.     setIntArg(cmd, pid);
  147.  
  148.     LiveTimer timeProcess;
  149.     timeProcess.Tstrt();
  150.     pr(cmd, Process1, Prioritet1);
  151.     pr(cmd, Process2, Prioritet2);
  152.     pr(cmd, Process1, Prioritet1);
  153.     pr(cmd, Process2, Prioritet3);
  154.     pr(cmd, Process1, Prioritet2);
  155.     pr(cmd, Process2, Prioritet2);
  156.     timeProcess.Tend();
  157.  
  158.  
  159.     cout << "Master: Sleeping\n";
  160.     Sleep(3000);
  161.  
  162.     //exit(0);
  163.     cout << "Master: EXIT\n";
  164.     cin.get();
  165.     return 0;
  166. }
  167. // Запуск программы: CTRL+F5 или меню "Отладка" > "Запуск без отладки"
  168. // Отладка программы: F5 или меню "Отладка" > "Запустить отладку"
  169.  
  170. // Советы по началу работы
  171. //   1. В окне обозревателя решений можно добавлять файлы и управлять ими.
  172. //   2. В окне Team Explorer можно подключиться к системе управления версиями.
  173. //   3. В окне "Выходные данные" можно просматривать выходные данные сборки и другие сообщения.
  174. //   4. В окне "Список ошибок" можно просматривать ошибки.
  175. //   5. Последовательно выберите пункты меню "Проект" > "Добавить новый элемент", чтобы создать файлы кода, или "Проект" > "Добавить существующий элемент", чтобы добавить в проект существующие файлы кода.
  176. //   6. Чтобы снова открыть этот проект позже, выберите пункты меню "Файл" > "Открыть" > "Проект" и выберите SLN-файл.
  177.  
Add Comment
Please, Sign In to add comment