Academic Integrity: tutoring, explanations, and feedback — we don’t complete graded work or submit on a student’s behalf.

c program. Problem 2 – measuring cost of system call Purpose: to understand mode

ID: 3746832 • Letter: C

Question

c program.

Problem 2 – measuring cost of system call

Purpose: to understand mode switch penalty and the cost difference among system calls.

Your task is to write a C program that measures the latencies of various system calls. In particular, you want to know 1) the cost of CPU mode switch by measuring a light-weight system call which does very little thing in the kernel, and 2) the cost of heavier system calls which triggers a lot of activities inside the kernel.

Your program should measure the latencies of three system calls: getpid(), open(), and read(). getpid() represents lightweight call, and open()/read() represent heavy system calls. For high-resolution latency measurements, you should use gettimeofday() system call. See the man page for details.

Because individual calls are too fast to accurately measure with the resolution provided with gettimeofday(), you need to measure the execution time of repetition of system calls. You then divide the time taken with how many times to arrive the individual system call latency.

Note that getpid() value may be cached by C runtime library – you may need to explicitly invoke system call. (see man page of getpid).

For open(), you need to use ‘/dev/null’ as the target. Beware that there is a limit in the number of open files, so you have to manage it carefully when repeating the calls. You need to close() the files in case you are nearing the limit, but you don’t want to include the cost of close() call in your measurement.

For read(), you need to use ‘/dev/zero’ special file as the source file.

Measurements should take a few seconds for each of the system calls. After measurement, your program must print out the result to standard output using following format:

Syscall repetitions time elapsed (micro sec) latency (nano sec)

---------------------------------------------------------------------------

getpid(): xxxxx xxxxxxxxx xxxxxx

open(): xxxxx xxxxxxxxx xxxxxx

read(): xxxxx xxxxxxxxx xxxxxx

N.B., the latency should be ‘time elapsed’ divided by ‘repetitions’.

What to turn in

- Your source code named prob2_.c

- Your binary named prob2_

- makefile

- A textfile named output_.txt which contains the execution result on cardiac.

Explanation / Answer

#include <stdio.h>
#include<fcntl.h>
#include <sys/time.h>
#include<time.h>
#define MAX 1000

int main()
{
int pid;
int i,fd ;
char c[12];
FILE *fp;
struct timeval start,end;
double time1,time2,time3;
//open file for writing
fp=fopen("output.txt","w");
  
if(!fp)
{
printf("Not able to open the file output.txt ");
return -1;
}
for(i = 0; i < MAX ; i++)
{
gettimeofday(&start,NULL);
//invoke getpid call
system(pid = getpid());
//printf("%d ",start.tv_usec);
}

gettimeofday(&end,NULL);
//printf("%d ",(end.tv_usec - start.tv_usec));
time1 = ((end.tv_sec - start.tv_sec) + (end.tv_usec - start.tv_usec) / 1000000.0)/MAX;
  
//wtite the time taken to execute getpid to
//to get micro second , divide multiply time by 1000000 , to get nano multiply time by 1000000000
printf("getpid(): %.10f %.10f ",time1*1000000,time1*1000000000);
fprintf(fp,"getpid():%.10f %.10f ",time1*1000000,time1*1000000000);
//in similar way execute other two commands ,open and read
  
for(i = 0; i < MAX ; i++)
{
gettimeofday(&start,NULL);
//invoke getpid call
system(open("/dev/null", O_RDONLY ));
//printf("%d ",start.tv_usec);
}
gettimeofday(&end,NULL);
//printf("%d ",(end.tv_usec - start.tv_usec));
time2 = ((end.tv_sec - start.tv_sec) + (end.tv_usec - start.tv_usec) / 1000000.0)/MAX;
  
//wtite the time taken to execute getpid to
printf("open(): %.10f %.10f ",time2*1000000,time2*1000000000);
fprintf(fp,"open():%.10f %.10f ",time2*1000000,time2*1000000000);
//in similar way execute other two commands ,open and read
fd = open("/dev/dev",O_RDONLY );
//printf("fd = %d ",fd);
for(i = 0; i < MAX ; i++)
{
gettimeofday(&start,NULL);
//invoke getpid call
system( read(fd,c,10));
//printf("%d ",start.tv_usec);
}
gettimeofday(&end,NULL);
//printf("%d ",(end.tv_usec - start.tv_usec));
time3 = ((end.tv_sec - start.tv_sec) + (end.tv_usec - start.tv_usec) / 1000000.0)/MAX;
  
//wtite the time taken to execute getpid to
printf("read(): %.10f %.10f ",time3*1000000,time3*1000000000);
fprintf(fp,"read(): %.10f %.10f ",time3*1000000,time3*1000000000);
}

----------------------------------------------------------

//output

//I have written output to standard output also , you can remove that

getpid(): 0.1690000000 169.0000000000   
open(): 0.1890000000 189.0000000000   
read(): 3.1300000000 3130.0000000000

------------------------------------------------------

//Makefile content

prob2.o : prob2.c   

          gcc -c  prob2.c                                                                                                                                      

prob2 : prob2.o                                                                                                                                                

        gcc -o prob2 prob2.o                                                                                                                                   

all   :                                                                                                                                                        

        gcc -o prob2 prob2.c                                                                                                                                   

clean:                                                                                                                                                         

        rm -rf prob2.o  

---------------------------------------

use

$make all

then execute as below

$./prob2

Hire Me For All Your Tutoring Needs
Integrity-first tutoring: clear explanations, guidance, and feedback.
Drop an Email at drjack9650@gmail.com
Chat Now And Get Quote