SCR/SCR3.1/TP2/my_cp_map.c

71 lines
1.1 KiB
C
Raw Normal View History

2023-09-15 16:17:36 +02:00
#include <stdio.h>
#include <string.h>
#include <stdlib.h>
#include <unistd.h>
#include <fcntl.h>
#include <assert.h>
#include <time.h>
#include <sys/mman.h>
#define BUFSIZE 1024
static inline double tstamp(void)
{
struct timespec tv;
clock_gettime(CLOCK_REALTIME, &tv);
return tv.tv_sec + tv.tv_nsec * 1.0e-9;
}
int main(int argc, char *argv[])
{
//char buf[BUFSIZE];
char * bufin = NULL,
* bufout = NULL;
int fin,
fout;
double start,
end;
size_t filesize = 0;
assert(argc == 3);
start = tstamp();
fin = open(argv[1],O_RDONLY);
assert(fin >=0);
fout = open(argv[2],O_RDWR|O_CREAT|O_TRUNC,0600);
assert(fout >=0);
filesize = lseek(fin,0,SEEK_END);
ftruncate (fout,filesize);
bufin = mmap(NULL,filesize,PROT_READ,MAP_PRIVATE,fin,0);
assert(bufin != (void*)-1);
bufout = mmap(NULL,filesize,PROT_WRITE,MAP_SHARED,fout,0);
assert(bufout != (void*)-1);
memcpy(bufout,bufin,filesize);
//munmap(bufin,filesize);
//munmap(bufout,filesize);
//ssize_t nb_read = read(fin,buf,sizeof(buf));
close(fin);
close(fout);
end = tstamp();
printf("time = %.3lf\n",end - start);
return 0;
}