2

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://eli.thegreenplace.net/2018/measuring-context-switching-and-memory-overheads-for-linux-threads/

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);
}
Charles Julien
  • 365
  • 3
  • 16
  • 1
    The "test question" is using incorrect terminology. The switch between kernel mode and user mode isn't technically called a context switch. It's called a mode switch. You can measure the overhead of a mode switch by benchmarking very minimal system calls. See this question: https://stackoverflow.com/questions/23599074/system-calls-overhead/41783404 – Petr Skocik Feb 07 '19 at 12:02
  • What if your loop gets interrupted by another unrelated process? You risk taking into account the time spent into this other process, don't you? – Yugo Amaryl Jul 13 '19 at 08:48

2 Answers2

2

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.

Alex Hoppus
  • 3,821
  • 4
  • 28
  • 47
0

Amazingly enough, the answer I provided above was accepted as correct in this test.

However to optimize the accuracy, we should rule out the "printf" method and go for either a syscall with invalid parameter as mentioned by Alex Hoppus or an empty method.

At the end, result should divided by 2 as mentioned by Alex Hoppus.

Charles Julien
  • 365
  • 3
  • 16