System calls vs. procedure calls: How much more expensive is a system call than
ID: 3789837 • Letter: S
Question
System calls vs. procedure calls: How much more expensive is a system call than a procedure call? Write a simple test program to compare the cost of a simple procedure call to a simple system call (getpid0 is a good candidate on UNIX, see the man page) To prevent the optimizing compiler from optimizing out" your procedure calls, do not compile with optimization on. You should use a system call such as the UNIX gettimeofday0 for time measurements. Design your code so that measurement overhead is negligible. Also, be aware that timer values in some systems have limited resolution (e.g., millisecond resolution). Explain the difference (if any) between the time required by your simple procedure call and simple system call by discussing what work each call must do. You can create a simple procedure call that just calculates the sum of two integers. int Simple_Add(int a int b): You can have two loops calling the simple procedure call and the system call 1,000,000 times. Then add two gettimeofday0 calls around each loop. For each loop, the time difference between the two time points will be the total time for the loop. A simple division by 1,000,000 will then give the time for the procedure call or the system call. Your program should report the difference between the two calls, in microseconds (10^-6 sec). Your program should be named timing_syscall.c.Explanation / Answer
#include<stdio.h>
#include <time.h>
int Simple_Add(int a,int b)
{
return a+b;
}
int main(int argc, char const *argv[])
{
int i;
clock_t t;
t = clock();
for ( i = 0; i < 1000000; ++i)
{
Simple_Add(6,6);
}
t = clock() - t;
double time_taken = (((double)t)/CLOCKS_PER_SEC); // in seconds
printf("Time taken by procedure call %lf ",time_taken );
t = clock();
for ( i = 0; i < 1000000; ++i)
{
getpid();
}
t = clock() - t;
double time_taken1 = (((double)t)/CLOCKS_PER_SEC); // in seconds
printf("Time taken by system call %lf seconds ",time_taken1);
double diff=(time_taken1-time_taken)/1000000;
printf("Difference between calls is %0.15lf micro seconds ",diff);
}
=======================================================================
akshay@akshay-Inspiron-3537:~/Chegg$ gcc timing_syscall.c
akshay@akshay-Inspiron-3537:~/Chegg$ ./a.out
Time taken by procedure call 0.003477
Time taken by system call 0.003623 seconds
Difference between calls is 0.000000000146000 micro seconds
Related Questions
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.