Download as pdf or txt
Download as pdf or txt
You are on page 1of 2

Name: Harsh Malhotra

Reg No: 19BLC1035


Date: 02-04-2021

Operating Systems Lab (CSE2005)


Experiment - 8
System Call VS Function Call

Aim:
To Write a Program to Compare System Call and Function Call.

Procedure:
1. Open a C/C++ Compiler.
2. Write the required program and code for the algorithm.
3. Fill in values for all the parameters.
4. Observe and Verify the Outputs.
5. Compare the Values of Average Time for System Call and Average Time for Function
Call.

Code:
#include <sys/time.h>
#include <unistd.h>
#include <assert.h>
#include<stdio.h>

int ret10()
{
return(10);
}

long nanosec(struct timeval t)


{
return((t.tv_sec*1000000+t.tv_usec)*1000);
}

void main()
{
int i,j,res;
long iterations=1000000;
float avgTimeSysCall, avgTimeFuncCall;
struct timeval t1, t2;
res=gettimeofday(&t1,NULL);
assert(res==0);

for (i=0;i<iterations; i++)


{
j=getpid();
}

res=gettimeofday(&t2,NULL);
assert(res==0);
avgTimeSysCall = (nanosec(t2) - nanosec(t1))/(iterations*1.0);
res=gettimeofday(&t1,NULL); assert(res==0);

for (i=0;i<iterations; i++)


{
j=ret10();
}

res=gettimeofday(&t2,NULL);
assert(res==0);
avgTimeFuncCall = (nanosec(t2) - nanosec(t1))/(iterations*1.0);
printf("Average time for System call getpid : %f\n",avgTimeSysCall);
printf("Average time for Function call : %f\n",avgTimeFuncCall);
}

Output:

Results:
Thus, We have successfully written a program to find the Average time for System Call and
Average time for Function Call. It can be observed that for the Average time for Function Call
is less than the Average time for System Call in the above case.

END

You might also like