Question: Write a C program called time.c that determines the amount of time necessary to run a command from the command line. This program will be

Write a C program called time.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 exec() 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 example output below reports the amount of time to run the command ls:

./time ls

time.c

time

Elapsed time: 0.25422

As the parent and child are separate processes, they will need to arrange how the starting time will be shared between them.

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 t_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:

struct timeval current;

gettimeofday(¤t,NULL);

// current.tv_sec represents seconds

// current.tv_usec represents microseconds

For IPC between the child and parent processes, the contents of the shared memory pointer can be assigned the struct timeval representing the starting time. When pipes are used, a pointer to a struct timeval can be written toand read fromthe pipe.

Your output must show the following:

Name of command

Output of command

Child: starting time

Parent: starting time (as received from pipe)

Parent: ending time

Parent: elapsed time

Deliverables:

1) Thoroughly documented code (explain what the code is doing, not the obvious C/C++ language syntax) in one source code file.

2) Instructions to compile and generate the executable file.

3) A discussion on how you got the code to work and issues you encountered along the way.

4) These 5 test commands and their output (as described above):

ls

ps

whoami

hostname

date

Step by Step Solution

There are 3 Steps involved in it

1 Expert Approved Answer
Step: 1 Unlock blur-text-image
Question Has Been Solved by an Expert!

Get step-by-step solutions from verified subject matter experts

Step: 2 Unlock
Step: 3 Unlock

Students Have Also Explored These Related Databases Questions!