Savelyev_Vyacheslav

OS_MasterL3

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