/*
Questo programma e' solo un esempio dal quale
prendere qualche spunto per il timing
delle system call lseek/read.
Manca tutta la gestione degli errori,
la stima dell'overhead della gettimeofday ed
altro ancora.
*/
#include <sys/types.h>
#include <sys/stat.h>
#include <fcntl.h>
#include <stdio.h>
#include <errno.h>
#include <unistd.h>
#include <stdlib.h>
#include <sys/time.h>
/* resources not allocated yet */
int fd_in = -1;
long *t2_t1 = NULL ,*t3_t2 = NULL;
void myexit(int);
long deltausecs(struct timeval *,struct timeval *);
int main(int argc,char *argv[])
{
char string[256];
long i,numlet;
off_t offset = 0,position;
struct timeval t1,t2,t3;
char buffer[1];
if (argc != 4) {
fprintf(stderr,"Uso: %s <dist. tra le lett.> <num. di lett.> <file da leggere>\n",argv[0]);
myexit(5);
}
sscanf(argv[1],"%ld",&offset);
sscanf(argv[2],"%ld",&numlet);
t2_t1 = calloc(numlet, sizeof(long));
t3_t2 = calloc(numlet, sizeof(long));
printf("offset %ld numlet %ld\n",offset,numlet);
if ( (fd_in = open(argv[3], O_RDONLY )) < 0 ) {
sprintf(string,"Apertura di %1.200s fallita",argv[2]);
perror(string); myexit(5);
}
position = lseek(fd_in,0L ,SEEK_SET);
read(fd_in, buffer , sizeof(buffer));
for(i = 0 ; i < numlet ; i++) {
(void) gettimeofday( &t1 , (struct timezone *) 0);
position = lseek(fd_in,offset,SEEK_CUR);
(void) gettimeofday( &t2 , (struct timezone *) 0);
read(fd_in, buffer , sizeof(buffer));
(void) gettimeofday( &t3 , (struct timezone *) 0);
t2_t1[i] = deltausecs( &t2 , &t1 );
t3_t2[i] = deltausecs( &t3 , &t2 );
}
for(i = 0 ; i < numlet ; i++) {
printf("Elapsed Time: lseek %-3ld read %-3ld\n",t2_t1[i],t3_t2[i]);
}
myexit(0);
}
long deltausecs(struct timeval *last,struct timeval *first)
{
return (last->tv_sec - first->tv_sec) * 1000000L +
last->tv_usec - first->tv_usec;
}
/* free the (eventually) allocated resources */
void myexit(int ret)
{
if (fd_in >= 0) close(fd_in);
if (t2_t1 != 0) free(t2_t1);
if (t3_t2 != 0) free(t3_t2);
exit(ret);
}
--
AntonioValletta - 16 Nov 2001