1answer.
Ask question
Login Signup
Ask question
All categories
  • English
  • Mathematics
  • Social Studies
  • Business
  • History
  • Health
  • Geography
  • Biology
  • Physics
  • Chemistry
  • Computers and Technology
  • Arts
  • World Languages
  • Spanish
  • French
  • German
  • Advanced Placement (AP)
  • SAT
  • Medicine
  • Law
  • Engineering
svetlana [45]
3 years ago
5

Your task is to write a C program that measures the latencies of various system calls. In particular, you want to know 1) the co

st 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.
Computers and Technology
1 answer:
tensa zangetsu [6.8K]3 years ago
3 0

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

You might be interested in
URGENT!
lutik1710 [3]

<u>Explanation:</u>

Hey there! you need not to panic about it ,your program didn't have Driver program i.e main program! the correct & working code is given below:

// C++ program to count  even digits  in a given number .

#include <iostream>  

using namespace std;  

// Function to count digits

int countEven(int n)  

{  

int even_count = 0;    

while (n > 0)  

{  

 int rem = n % 10;  

 if (rem % 2 == 0)  

  even_count++;  

 n = n / 10;  

}  

cout << "Even count : "

 << even_count;  

if (even_count % 2 == 0 )  

 return 1;  

else

 return 0;  

}  

// Driver Code  

int main()  

{  

int n;  

std::cin >>n;

int t = countEven(n);  

return 0;  

}  

6 0
2 years ago
Why would Network Systems employees be employed by the government?
Ghella [55]
Well knowing how the government is with security id go with A. as it makes a lot of sense unlike B and D and if they were looking for software the question would mention it
8 0
3 years ago
Read 2 more answers
Who would like to join a team devoted to decreasing spam and bullying?
Tema [17]
Yes i would love to help the cause <span />
6 0
3 years ago
Which symbol would be used in a flowchart to represent a connection to another part of the flowchart on the same page
AlekseyPX

Answer:

The answer is "Connectors".

Explanation:

Please find the Connectors image in the attached file.

In this question, the connectors symbol is usually small throughout the flowcharts. It is used as a connection to display and changes throughout the flux. It was usually marked by major letters that indicate jump points. This diagram visually shows the sequence of events in a system and who is accountable.

7 0
3 years ago
An accountant initially records adjusting entries in a(n) _____.
Alexus [3.1K]
Journal!
Adjusting entries are changes to journal entries you've already recorded.
4 0
3 years ago
Other questions:
  • Two electronics technicians are looking at the testing instrument in the figure above. Technician A says that this instrument is
    7·1 answer
  • Match the technology with the appropriate task.
    7·1 answer
  • ​A(n) ________ database makes it possible to store information across millions of machines in hundreds of data centers around th
    14·1 answer
  • What is the relationship between ionic bonds and cleavage
    13·1 answer
  • Which of the following people choose a career path for the right reasons
    12·2 answers
  • Select the best word or phrase to complete each sentence Jobs that require you to use your v are typically rewarding Individuals
    12·1 answer
  • Your computer has a single hard disk w/ a single volume used by the C:\ drive. You have previously upgraded the disk to a dynami
    6·1 answer
  • You are the IT Administrator for a small corporate network. Until now, the network has consisted only of workstations accessing
    6·1 answer
  • Suppose that a laptop identifies its processor as follows: AMD’s A84500M. What does the AMD represent?
    5·1 answer
  • Synapse is not working and is crashing every time you attach it. What should you do?
    13·1 answer
Add answer
Login
Not registered? Fast signup
Signup
Login Signup
Ask question!