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
open(): read():
--------------------------------------------------------------------------getpid():
repetitions xxxxx
xxxxx xxxxx
time elapsed (micro sec) latency (nano sec) xxxxxxxxx
xxxxxxxxx xxxxxxxxx
N.B., the latency should be ‘time elapsed’ divided by
‘repetitions’.
• Make sure that you check for error condition for open() and
read(). open() or read() may return very quickly when kernel
discovers obvious error condition early on.
• The number of open files is limited per process. So when you
repeat open(), you have to close() file descriptors so that you
don't go over the limit. Otherwise, subsequent open() calls will
return error. When you close, make sure you exclude the time taken
for close() calls.
• As specified above, you have to use '/dev/null' for open()
measurements, and '/dev/zero' for read() measurements. This means
that for read() measurement, you
need to open '/dev/zero' file once, and then use the resulting file
descriptor for subsequent read() operations you want to measure
time.
What to turn in: - Your source code named prob2_<myname>.c -
Your binary named prob2_<myname>
- Makefile
#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<myname>.o : prob2<myname>.c
gcc -c prob2<myname>.c
prob2<myname> : prob2<myname>.o
gcc -o prob2<myname> prob2<myname>.o
all :
gcc -o prob2<myname> prob2<myname>.c
clean:
rm -rf prob2_<myname>.o
---------------------------------------
use
$make all
then execute as below
$./prob2_<myname>
Than you!!!!
Purpose: to understand mode switch penalty and the cost difference among system calls. Your task is...
C++ code and also provide comments explaining everything Credit Card Debt The True Cost of Paying Minimum Payment Write a C++ program to output the monthly payment schedule for a credit card debt, when each month nothing more is charged to the account but only the minimum payment is paid. The output stops when the balance is fully paid - remaining balance = 0. Input: Data input must be done in a separate function. Input the following: credit card balance,...
Please read the article and answer about questions. You and the Law Business and law are inseparable. For B-Money, the two predictably merged when he was negotiat- ing a deal for his tracks. At other times, the merger is unpredictable, like when your business faces an unexpected auto accident, product recall, or government regulation change. In either type of situation, when business owners know the law, they can better protect themselves and sometimes even avoid the problems completely. This chapter...