C Programming: Run time on Files Programming Software: PuTTY, Ubuntu, other C pr
ID: 3843980 • Letter: C
Question
C Programming: Run time on Files
Programming Software: PuTTY, Ubuntu, other C programming tools, etc.
----------------------
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(). Here’s 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.
Compile and test the program. Post the source file and the screenshot of the test.
----------------
Source Codes that can help:
---------------
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.
Explanation / Answer
#include<stdio.h>
#include<conio.h>
int main()
{
FILE *f1,*f2;
char ch;
f1=fopen("abc.c","r");
if(f1==NULL)
{
printf("File does not exist");
}
f2=fopen("newFile.c","w");
if(f2==NULL)
{
printf("File does not create");
}
ch=fgetc(f1);
while(ch!=EOF)
{
fputc(ch,f2);
ch=fgetc(f1);
}
fclose(f1);
fclose(f2);
return 0;
}
Related Questions
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.