Answered step by step
Verified Expert Solution
Link Copied!

Question

1 Approved Answer

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

Step: 1

blur-text-image

Get Instant Access to Expert-Tailored Solutions

See step-by-step solutions with expert insights and AI powered tools for academic success

Step: 2

blur-text-image_2

Step: 3

blur-text-image_3

Ace Your Homework with AI

Get the answers you need in no time with our AI-driven, step-by-step assistance

Get Started

Recommended Textbook for

Advances In Databases And Information Systems 23rd European Conference Adbis 2019 Bled Slovenia September 8 11 2019 Proceedings Lncs 11695

Authors: Tatjana Welzer ,Johann Eder ,Vili Podgorelec ,Aida Kamisalic Latific

1st Edition

3030287297, 978-3030287290

More Books

Students also viewed these Databases questions

Question

explain the need for human resource strategies in organisations

Answered: 1 week ago

Question

describe the stages involved in human resource planning

Answered: 1 week ago