I would like to share a test question I met recently and discuss about it:
Write a C program that roughly measures the overhead of a context switch >between the kernel and userspace on a UNIX/Linux system (without using the >pthread API). Please indicate the assumptions your rough estimation is based >on.
I would like to discuss with more advanced C programmers the different options of this problematic.
I have limited knowledge in C programming and I documented myself to provide an acceptable answer:
https://www.researchgate.net/post/How_can_I_measure_thread_creation_and_destruction
https://github.com/eliben/code-for-blog/blob/master/2018/threadoverhead/thread-pipe-msgpersec.c
I realized quickly, despite of my limited knowledge, the ambiguity of the question. Indeed, the question doesn't stipulate if an answer should be given in unit of time or memory.
I personally chose to develop my reasoning measuring time with the library time.h and a very simple snippet. Result should be divided by 1 000 000.
Does my answer make sense or am I completely missing the point ?
#include<time.h>
#include<stdio.h>
int main(){
clock_t begin=clock();
int i;
for(i=0;i<1000000;i++){
printf("%d",i);
}
clock_t end=clock();
printf("Time taken:%lf",(double)(end-begin)/CLOCKS_PER_SEC);
}
You should try different approach.
As it was stated, you are trying to measure
overhead of a context switch >between the kernel and userspace
Context switch from user to kernel is done via syscall
. For sure printf underneath uses write
syscall, but this syscall is too heavy to get reliable estimation. To improve this estimation you should answer to the question - what is the fastest syscall in linux? And the answer is - syscall with invalid parameter.
P.S. Don't forget about measurement accuracy. Also you should divide your result by 2 because syscall is a round-trip.