Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #include <stdio.h>
- #include <stdlib.h>
- #include <string.h>
- #include <windows.h>
- #define LINE "==============================================================================="
- #define PROCESSES_COUNT 3
- #define SOME_ARBITRARY_LARGE_NUMBER 256
- #define HELP_MESSAGE "Format: spo2 <type1> <search1> <type2> <search2> <type3> <search3>\n\
- <type> can be either 'ASC' or 'DSC'\n\
- <search> can be any natural number (1, 2, 3, ...)\n"
- int print_process_times(HANDLE process) {
- union {
- LONGLONG li;
- FILETIME ft;
- } raw_create_time, raw_end_time, raw_elapsed_time;
- FILETIME raw_cpu_time, raw_user_time;
- SYSTEMTIME elapsed_time, cpu_time, user_time;
- GetProcessTimes(process, &raw_create_time, &raw_end_time.ft, &raw_cpu_time, &raw_user_time);
- raw_elapsed_time.li = raw_end_time.li - raw_create_time.li;
- FileTimeToSystemTime(&raw_elapsed_time, &elapsed_time);
- FileTimeToSystemTime(&raw_cpu_time, &cpu_time);
- FileTimeToSystemTime(&raw_user_time, &user_time);
- printf("Elapsed time: %02i.%06i seconds\n", elapsed_time.wSecond, elapsed_time.wMilliseconds);
- printf("CPU time: %02i.%06i seconds\n", cpu_time.wSecond, cpu_time.wMilliseconds);
- printf("User time: %02i.%06i seconds\n", user_time.wSecond, user_time.wMilliseconds);
- }
- int check_input_failure(int argc, char ** argv) {
- if (argc != 7)
- return 1;
- for (char i = 1; i < 6; i += 2)
- if (strcmp(argv[i], "ASC") && strcmp(argv[i], "DSC"))
- return 2;
- for (char i = 2; i < 7; i += 2)
- if (atoi(argv[i]) == 0)
- return 3;
- return 0;
- }
- wchar_t ** parse_commandline_paramaters(int argc, char ** argv) {
- wchar_t ** result = malloc(sizeof(wchar_t *)*PROCESSES_COUNT);
- wchar_t * payload = malloc(sizeof(wchar_t)*SOME_ARBITRARY_LARGE_NUMBER*PROCESSES_COUNT);
- char * tmp = malloc(SOME_ARBITRARY_LARGE_NUMBER);
- for (int i = 0; i < PROCESSES_COUNT; i++) {
- *tmp = 0;
- tmp = strcat(tmp, "number_processor.exe ");
- tmp = strcat(tmp, argv[2 * i + 1]);
- tmp = strcat(tmp, " ");
- tmp = strcat(tmp, argv[2 * i + 2]);
- mbstowcs(payload + (SOME_ARBITRARY_LARGE_NUMBER*i), tmp, SOME_ARBITRARY_LARGE_NUMBER);
- result[i] = payload + (SOME_ARBITRARY_LARGE_NUMBER*i);
- }
- free(tmp);
- return result;
- }
- int main(int argc, char ** argv) {
- if (check_input_failure(argc, argv))
- return puts(HELP_MESSAGE);
- int open_descriptors = 3;
- STARTUPINFO sui;
- PROCESS_INFORMATION processes[PROCESSES_COUNT];
- TCHAR ** command_line = parse_commandline_paramaters(argc, argv);
- /*Create and Run Processes*/
- GetStartupInfo(&sui);
- for (int i = 0; i < PROCESSES_COUNT; i++) {
- if (!(CreateProcess(NULL, command_line[i], NULL, NULL, TRUE, 0, NULL, NULL, &sui, &(processes[i]))))
- return printf("Error while creating process #%i. Error: %i", i+1, GetLastError());
- printf("Process #%i: SUCCESS (ID: %i)\n", i+1, command_line[i], processes[i].dwProcessId);
- CloseHandle(processes[i].hThread);
- }
- puts(LINE);
- /*Terminate Processes and show their stats*/
- for (int i = 0; i < PROCESSES_COUNT; i++) {
- WaitForSingleObject(processes[i].hProcess, INFINITE);
- printf("Process #%i: END\n", i+1);
- print_process_times(processes[i].hProcess);
- puts("");
- CloseHandle(processes[i].hProcess);
- }
- puts(LINE);
- printf("Open Descriptors = %i\n", open_descriptors);
- getchar();
- return 0;
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement