Can we write a c program to find out time spent in context switch in Linux? Could you please share code if you have one? Thanks
Why not just this one as rough estimation?
#include
#include
#include
#include
int main(int argc, char **argv) {
struct timeval tv, tvt;
int diff;
gettimeofday(&tv, 0);
diff = tvt.tv_usec - tv.tv_usec;
if (fork() != 0) {
gettimeofday(&tvt, 0);
diff = tvt.tv_usec - tv.tv_usec;
printf("%d\n", diff);
}
return 0;
}
Note: Actually we shouldn't put null as the second argument, check man gettimeofday. Also, we should check if tvt.tv_usec > tv.tv_usec! Just a draft.