Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #include <unistd.h>
- #include <sys/wait.h>
- #include "stdio.h"
- #include <stdlib.h>
- #include <time.h>
- #include "format.h"
- #include <string.h>
- /**
- * Utilities Unleashed
- * CS 241 - Fall 2019
- */
- int main(int argc, char *argv[]) {
- pid_t child = fork();
- if (child == -1) {
- return -1;
- }
- struct timespec* timeSpec = malloc(sizeof(struct timespec));
- if (child != 0) { // Parent
- int firstRead = clock_gettime(CLOCK_MONOTONIC, timeSpec);
- double firstTimeSec = 0.00;
- double nextTimeSec = 0.00;
- double firstTimeNanoSec = 0.00;
- double nextTimeNanoSec = 0.00;
- if (firstRead != -1) {
- firstTimeSec = timeSpec->tv_sec;
- firstTimeNanoSec = (double) timeSpec->tv_nsec;
- }
- waitpid(child,NULL,0);
- int nextRead = clock_gettime(CLOCK_MONOTONIC, timeSpec);
- if (nextRead != -1) {
- nextTimeSec = timeSpec->tv_sec;
- nextTimeNanoSec = (double) timeSpec->tv_nsec;
- }
- double duration = (nextTimeSec - firstTimeSec) + ((nextTimeNanoSec-firstTimeNanoSec) / 1000000000.00);
- display_results(argv, duration);
- return (int) duration;
- }
- else {
- int execCode = execvp(argv[1], &argv[1]);
- if (execCode == -1) print_exec_failed();
- }
- return 0; // return seconds it took to complete
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement