Advertisement
Guest User

Untitled

a guest
May 5th, 2016
377
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. #include <stdio.h>
  2. #include <stdlib.h>
  3. #include <string.h>
  4. #include <windows.h>
  5.  
  6. #define LINE "==============================================================================="
  7. #define PROCESSES_COUNT 3
  8. #define SOME_ARBITRARY_LARGE_NUMBER 256
  9. #define HELP_MESSAGE "Format: spo2 <type1> <search1> <type2> <search2> <type3> <search3>\n\
  10. <type> can be either 'ASC' or 'DSC'\n\
  11. <search> can be any natural number (1, 2, 3, ...)\n"
  12.  
  13. int print_process_times(HANDLE process) {
  14.     union {
  15.         LONGLONG li;
  16.         FILETIME ft;
  17.     } raw_create_time, raw_end_time, raw_elapsed_time;
  18.     FILETIME raw_cpu_time, raw_user_time;
  19.     SYSTEMTIME elapsed_time, cpu_time, user_time;
  20.  
  21.     GetProcessTimes(process, &raw_create_time, &raw_end_time.ft, &raw_cpu_time, &raw_user_time);
  22.     raw_elapsed_time.li = raw_end_time.li - raw_create_time.li;
  23.     FileTimeToSystemTime(&raw_elapsed_time, &elapsed_time);
  24.     FileTimeToSystemTime(&raw_cpu_time, &cpu_time);
  25.     FileTimeToSystemTime(&raw_user_time, &user_time);
  26.  
  27.     printf("Elapsed time: %02i.%06i seconds\n", elapsed_time.wSecond, elapsed_time.wMilliseconds);
  28.     printf("CPU time: %02i.%06i seconds\n", cpu_time.wSecond, cpu_time.wMilliseconds);
  29.     printf("User time: %02i.%06i seconds\n", user_time.wSecond, user_time.wMilliseconds);
  30. }
  31.  
  32. int check_input_failure(int argc, char ** argv) {
  33.     if (argc != 7)
  34.         return 1;
  35.     for (char i = 1; i < 6; i += 2)
  36.         if (strcmp(argv[i], "ASC") && strcmp(argv[i], "DSC"))
  37.             return 2;
  38.     for (char i = 2; i < 7; i += 2)
  39.         if (atoi(argv[i]) == 0)
  40.             return 3;
  41.  
  42.     return 0;
  43. }
  44. wchar_t ** parse_commandline_paramaters(int argc, char ** argv) {
  45.     wchar_t ** result = malloc(sizeof(wchar_t *)*PROCESSES_COUNT);
  46.     wchar_t * payload = malloc(sizeof(wchar_t)*SOME_ARBITRARY_LARGE_NUMBER*PROCESSES_COUNT);
  47.     char * tmp = malloc(SOME_ARBITRARY_LARGE_NUMBER);
  48.  
  49.     for (int i = 0; i < PROCESSES_COUNT; i++) {
  50.         *tmp = 0;
  51.         tmp = strcat(tmp, "number_processor.exe ");
  52.         tmp = strcat(tmp, argv[2 * i + 1]);
  53.         tmp = strcat(tmp, " ");
  54.         tmp = strcat(tmp, argv[2 * i + 2]);
  55.         mbstowcs(payload + (SOME_ARBITRARY_LARGE_NUMBER*i), tmp, SOME_ARBITRARY_LARGE_NUMBER);
  56.         result[i] = payload + (SOME_ARBITRARY_LARGE_NUMBER*i);
  57.     }
  58.     free(tmp);
  59.     return result;
  60. }
  61.  
  62. int main(int argc, char ** argv) {
  63.     if (check_input_failure(argc, argv))
  64.         return puts(HELP_MESSAGE);
  65.    
  66.     int open_descriptors = 3;
  67.     STARTUPINFO sui;
  68.     PROCESS_INFORMATION processes[PROCESSES_COUNT];
  69.     TCHAR ** command_line = parse_commandline_paramaters(argc, argv);
  70.     /*Create and Run Processes*/
  71.     GetStartupInfo(&sui);
  72.     for (int i = 0; i < PROCESSES_COUNT; i++) {
  73.         if (!(CreateProcess(NULL, command_line[i], NULL, NULL, TRUE, 0, NULL, NULL, &sui, &(processes[i]))))
  74.             return printf("Error while creating process #%i. Error: %i", i+1, GetLastError());
  75.         printf("Process #%i: SUCCESS (ID: %i)\n", i+1, command_line[i], processes[i].dwProcessId);
  76.         CloseHandle(processes[i].hThread);
  77.     }
  78.     puts(LINE);
  79.     /*Terminate Processes and show their stats*/
  80.     for (int i = 0; i < PROCESSES_COUNT; i++) {
  81.         WaitForSingleObject(processes[i].hProcess, INFINITE);
  82.         printf("Process #%i: END\n", i+1);
  83.         print_process_times(processes[i].hProcess);
  84.         puts("");
  85.         CloseHandle(processes[i].hProcess);
  86.     }
  87.     puts(LINE);
  88.     printf("Open Descriptors = %i\n", open_descriptors);
  89.    
  90.     getchar();
  91.     return 0;
  92. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement