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.

Respuesta :

Answer and Explanation:

#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\n");

return -1;

}

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

{

gettimeofday(&start,NULL);

//invoke getpid call

system(pid = getpid());

//printf("%d\n",start.tv_usec);

}

gettimeofday(&end,NULL);

//printf("%d\n",(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\n",time1*1000000,time1*1000000000);

fprintf(fp,"getpid():%.10f %.10f\n",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\n",start.tv_usec);

}

gettimeofday(&end,NULL);

//printf("%d\n",(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\n",time2*1000000,time2*1000000000);

fprintf(fp,"open():%.10f %.10f\n",time2*1000000,time2*1000000000);

//in similar way execute other two commands ,open and read

fd = open("/dev/dev",O_RDONLY );

//printf("fd = %d\n",fd);

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

{

gettimeofday(&start,NULL);

//invoke getpid call

system( read(fd,c,10));

//printf("%d\n",start.tv_usec);

}

gettimeofday(&end,NULL);

//printf("%d\n",(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\n",time3*1000000,time3*1000000000);

fprintf(fp,"read(): %.10f %.10f\n",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