-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy path3.c
More file actions
84 lines (77 loc) · 2.17 KB
/
Copy path3.c
File metadata and controls
84 lines (77 loc) · 2.17 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
/*
Name: Teagan Haddawy
Date: Tuesday Jan 6, 2026
Title: Lab 1 - Part 5
Description: Compares the time of the library functions vs system calls in order to copy
*/
#include <stdio.h>
#include <stdlib.h>
#include <fcntl.h>
#include <unistd.h>
#include <time.h>
#define BUFFER_SIZE 2048
void func_copy(const char* src_file, const char* dst_file) //runs the same as 1.c
{
FILE *src = fopen(src_file, "rb");
if (src == NULL)
{
return;
}
FILE *dst = fopen(dst_file, "wb");
if (dst == NULL)
{
return;
}
char *buf = malloc(BUFFER_SIZE);
size_t bytes_read;
while ((bytes_read = fread(buf, 1, BUFFER_SIZE, src)) > 0)
{
fwrite(buf, 1, bytes_read, dst);
}
free(buf);
fclose(src);
fclose(dst);
}
void syscall_copy(const char* src_file, const char* dst_file) //runs the same as 2.c
{
int src_fd = open(src_file, O_RDONLY);
if (src_fd < 0)
{
return;
}
int dst_fd = open(dst_file, O_WRONLY | O_CREAT | O_TRUNC, 0644);
if (dst_fd < 0)
{
return;
}
char *buf = malloc(BUFFER_SIZE);
ssize_t bytes_read;
while ((bytes_read = read(src_fd, buf, BUFFER_SIZE)) > 0)
{
write(dst_fd, buf, bytes_read);
}
free(buf);
close(src_fd);
close(dst_fd);
}
int main(int argc, char *argv[])
{
if (argc != 3) //checks for 3 arguments needed
{
fprintf(stderr, "Usage: %s <src_filename> <dst_filename>", argv[0]);
exit(1);
}
clock_t start, end; //creates a clock start and end
double func_time, syscall_time;
start = clock(); //starts the clock
func_copy(argv[1], argv[2]); //completes a copy using function calls
end = clock(); //end the clock when its finished
func_time = (double)(end - start) / CLOCKS_PER_SEC; //calculate time for first method
start = clock(); //start clock for system call method
syscall_copy(argv[1], argv[2]); // completes the copy using system calls
end = clock(); //ends clock for system call method
syscall_time = (double)(end - start) / CLOCKS_PER_SEC; //calculate time
printf("Function time: %f\n", func_time);
printf("System Call time: %f\n", syscall_time);
return 0;
}