Advertisement
Guest User

Untitled

a guest
Sep 18th, 2019
138
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 1.37 KB | None | 0 0
  1. #include <unistd.h>
  2. #include <sys/wait.h>
  3. #include "stdio.h"
  4. #include <stdlib.h>
  5. #include <time.h>
  6. #include "format.h"
  7. #include <string.h>
  8.  
  9. /**
  10. * Utilities Unleashed
  11. * CS 241 - Fall 2019
  12. */
  13.  
  14. int main(int argc, char *argv[]) {
  15. pid_t child = fork();
  16. if (child == -1) {
  17. return -1;
  18. }
  19. struct timespec* timeSpec = malloc(sizeof(struct timespec));
  20. if (child != 0) { // Parent
  21. int firstRead = clock_gettime(CLOCK_MONOTONIC, timeSpec);
  22. double firstTimeSec = 0.00;
  23. double nextTimeSec = 0.00;
  24. double firstTimeNanoSec = 0.00;
  25. double nextTimeNanoSec = 0.00;
  26. if (firstRead != -1) {
  27. firstTimeSec = timeSpec->tv_sec;
  28. firstTimeNanoSec = (double) timeSpec->tv_nsec;
  29. }
  30. waitpid(child,NULL,0);
  31. int nextRead = clock_gettime(CLOCK_MONOTONIC, timeSpec);
  32. if (nextRead != -1) {
  33. nextTimeSec = timeSpec->tv_sec;
  34. nextTimeNanoSec = (double) timeSpec->tv_nsec;
  35. }
  36. double duration = (nextTimeSec - firstTimeSec) + ((nextTimeNanoSec-firstTimeNanoSec) / 1000000000.00);
  37. display_results(argv, duration);
  38. return (int) duration;
  39. }
  40. else {
  41. int execCode = execvp(argv[1], &argv[1]);
  42. if (execCode == -1) print_exec_failed();
  43. }
  44. return 0; // return seconds it took to complete
  45. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement