0% found this document useful (0 votes)
9 views2 pages

Compare System Call vs Function Call

The document describes an experiment to compare the performance of system calls and function calls in C++. It provides the name, student number, and date of the student who conducted the experiment. The aim is to write a program that measures and compares the average time taken for a system call (getpid()) and a function call. The code provided implements this by running getpid() and a simple return function in loops and calculating the average time for each using gettimeofday(). The output shows the function call takes less time on average than the system call.

Uploaded by

Harsh Malhotra
Copyright
© All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
9 views2 pages

Compare System Call vs Function Call

The document describes an experiment to compare the performance of system calls and function calls in C++. It provides the name, student number, and date of the student who conducted the experiment. The aim is to write a program that measures and compares the average time taken for a system call (getpid()) and a function call. The code provided implements this by running getpid() and a simple return function in loops and calculating the average time for each using gettimeofday(). The output shows the function call takes less time on average than the system call.

Uploaded by

Harsh Malhotra
Copyright
© All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd

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