Answered step by step
Verified Expert Solution
Link Copied!

Question

1 Approved Answer

C Programming in Unix (using PuTTY) Problem: I already copied the file, now I need to get the elapsed time and CPU time for the

C Programming in Unix (using PuTTY)

Problem: I already copied the file, now I need to get the elapsed time and CPU time for the copy process.

---------------------------------------------------------------------------------

Here is the full instruction of my lab, noted that the bolded part are the one that I have not done yet:

Write a C program to copy a file (source file) to another file (target file). The program will copy text files a character at a time. It will use the C runtime library functions fopen() and fclose() to open and close the files. It will use the functions getc() and putc() to read and write characters. The function getc() reads one character and the function putc() writes one character.

Google fopen(), fclose(), getc() and putc() to find out the syntax of these functions. The program must check to see if the files opened successfully before calling getc() and putc().

The program will call getc() to read the character and write the character to the target by calling putc(). Heres what the loop looks like:

while(( ch = fgetc(source)) != EOF )

fputc(ch, target);

The program will measure the elapsed time as well as the CPU time for copy operation. To measure the elapsed time, the program will call the gettimeofday() function, once before and once after the code we are measuring. Google gettimeofday() to get information about the system call.

The CPU time is measured using the times() system call. The CPU time includes User CPU time as well as the System CPU time. Display both elapsed time and cpu time

After the program exits the loop, it should close both files by calling the function fclose() as shown below.

fclose(source);

fclose(target);

where source and target are file descriptors returned by successful fopen() calls.

----------------------------------------------------------

The following are hints and instructions for time API:

ELAPSED TIME

The gettimeofday() system call returns the calendar time in the buffer pointed

to by "tv".

#include

int gettimeofday(struct timeval *tv, struct timezone *tz);

Returns 0 on success, or -1 on error

struct timeval {

time_t tv_sec; /* seconds since 00:00:00, 1 Jan 1970 UTC */

long tv_usec; /* Additional microseconds (long int) */

};

The tz argument a historical artifact. Should always be specified as NULL.

USAGE

struct timeval t0, t1;

gettimeofday(&t0, 0); //Takes pointer to timeval structure as argument.

/*... YOUR CODE YOU ARE MEASURING.... */

gettimeofday(&t1, 0); //Second call to gettimeofday()

long elapsed = (t1.tv_sec - t0.tv_sec) + (t1.tv_usec - t0.tv_usec)/1000000.0;

PROCESS TIME

Process time is the amount of CPU time used by a process since it was created.

The kernel separates the CPU time into two components:

1. User CPU time: is the amount of time spent executing in user mode.

2. System CPU time: is the amount of time spent executing in kernel mode.

This is the time that the kernel spends executing system calls or performing

other tasks on behalf of the program.

The Process time is also referred to as the CPU time consumed by the process.

API

#include

clock_t times(struct tms *);

Returns number of clock ticks since "arbitrary" time in past on success or

(clock_t)-1 on error

The number of clock ticks per second is system dependent. To find this value for

your system, you call the system function sysconf(_SC_CLK_TCK), as in

clk_tcks = sysconf(_SC_CLK_TCK);

The value of _SC_CLK_TCK is 10000000.

The tms structure has the following FIELDS

struct tms {

clock_t tms_utime; /* user CPU time used by caller */

clock_t tms_stime; /* System CPU time used by caller */

clock_t tms_cutime; /* user CPU time of all children */

clock_t tms_cstime; /* System CPU time of all children */

};

The clock_t data type is an integer type that measures time in units called

"clock ticks".

Dividing a clock_t value by _SC_CLK_TCK converts it to seconds.

---------------------------------------

The following are my current C source code, please help me to expand on it.

#include #include

int main(int argc, char* argv[]) {

int ch; // File descriptors. FILE *source, *target;

if (argc < 3) { printf("Usage: fcopy "); exit(EXIT_FAILURE); }

// Open source file for read. source = fopen(argv[1], "r"); if (source == NULL) // Could not open file { printf("1. Press any key to exit.. "); exit(EXIT_FAILURE); }

// Open target file for write. target = fopen(argv[2], "w"); if (target == NULL) // Could not open file { fclose(source); printf("2. Press any key to exit... "); exit(EXIT_FAILURE); }

// Read character at a time for source file until we reach end of file (EOF), // and write it to the target file.

while ((ch = fgetc(source)) != EOF) fputc(ch, target);

printf("File copied sucessfully./n");

// Must close both the files. fclose(source); fclose(target);

return 0;

}

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_step_2

Step: 3

blur-text-image_step3

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

Knowledge Discovery In Databases

Authors: Gregory Piatetsky-Shapiro, William Frawley

1st Edition

ISBN: 0262660709, 978-0262660709

More Books

Students also viewed these Databases questions

Question

=+4 How did it affect HR?

Answered: 1 week ago