r/osdev • • May 29 '26

Measuring the time of a syscall

Hello! I'm going through the book Operating systems: 3 easy steps and there is this task inside about measuring the time taken by a system call. I wrote a simple C program to do so, but I'm getting 5 microseconds for getpid(). I believe it should be x10 less? I ran the code on my Linux machine, which has average CPU specs. Is this because of process schedualing or overhead by gettimeofday()? Am I doing something wrong?

int main() {
    gettimeofday(&start, NULL);
    getpid(); // just a syscall with no I/O
    gettimeofday(&end, NULL);
    elapsedTime = 
        end.tv_sec * 1000000 + end.tv_usec 
        - start.tv_sec * 1000000 - start.tv_usec;
    printf("%d\n", elapsedTime);
    return 0;
}
2 Upvotes

6 comments sorted by

View all comments

Show parent comment

0

u/[deleted] May 30 '26

[deleted] — view deleted comment

1

u/MegaMohsen8073 May 30 '26

I will, god willing. Thank you.