chap3
This commit is contained in:
29
bac2/os/chap3/ex5.c
Normal file
29
bac2/os/chap3/ex5.c
Normal file
@ -0,0 +1,29 @@
|
||||
#include <sys/time.h>
|
||||
#include <stdio.h>
|
||||
#include <time.h>
|
||||
#include <unistd.h>
|
||||
|
||||
#define printvi(X) printf("%s = %d\n", #X, X);
|
||||
#define printvf(X) printf("%s = %lld\n", #X, X);
|
||||
|
||||
typedef struct timespec timespec_t;
|
||||
|
||||
timespec_t start, end, s = {0, 1};
|
||||
|
||||
long long int dtime(timespec_t *start, timespec_t *end){
|
||||
return (end->tv_sec - start->tv_sec) * 1e9 + end->tv_nsec - start->tv_nsec;
|
||||
}
|
||||
|
||||
int main(void)
|
||||
{
|
||||
FILE* fd = fopen("plot", "w");
|
||||
long long int time, time_sum;
|
||||
for (int i = 0; i < 10000; ++i) {
|
||||
clock_gettime(CLOCK_REALTIME, &start);
|
||||
nanosleep(&s, NULL);
|
||||
clock_gettime(CLOCK_REALTIME, &end);
|
||||
time = dtime(&start, &end);
|
||||
time_sum += time;
|
||||
fprintf(fd, "%d %lld\n", i, time);
|
||||
}
|
||||
}
|
Reference in New Issue
Block a user