Question
Write a C program (time_pipe.c) that determines the amount of time necessary to run a command from the command line This program will be run
Write a C program (time_pipe.c) that determines the amount of time necessary to run a command from the command line
This program will be run as ./time and will report the amount of elapsed time to run the specified command
. This will involve using fork() and execvp() functions, as well as the gettimeofday() function to determine the elapsed time. It will also require the use of two different IPC mechanisms.
The general strategy is to fork a child process that will execute the specified command. However, before the child executes the command, it will record a timestamp of the current time (which we term starting time). The parent process will wait for the child process to terminate. Once the child terminates, the parent will record the current timestamp for the ending time. The difference between the starting and ending times represents the elapsed time to execute the command
The second, time_pipe.c, version will use a pipe. The child will write the starting time to the pipe, and the parent will read from it following the termination of the child process
You will use the gettimeofday() function to record the current timestamp. This function is passed a pointer to a struct timeval object, which contains two members: tv_sec and tv_usec. These represent the number of elapsed seconds and microseconds since January 1, 1970 (known as the UNIX EPOCH). The following code sample illustrates how this function can be used:
// Get current time timeval_t startTime; gettimeofday( &startTime, 0 );
// get the end time timeval_t end_time; gettimeofday( &end_time, 0 );
// calculate elapsed time timeval_t elapsed_time; timersub( &end_time, startTime, &elapsed_time );
// print elapsed time (microseconds right justified zero filled) printf( " Elapsed time: %d.%06d seconds ", elapsed_time.tv_sec, elapsed_time.tv_usec );
Step by Step Solution
There are 3 Steps involved in it
Step: 1
Get Instant Access to Expert-Tailored Solutions
See step-by-step solutions with expert insights and AI powered tools for academic success
Step: 2
Step: 3
Ace Your Homework with AI
Get the answers you need in no time with our AI-driven, step-by-step assistance
Get Started