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
Explica la importancia que tiene el plan de trabajo (cronograma) en los proyectos y porque se realiza antes de ejecutar la soluc
zysi [14]

Answer:

El plan de trabajo permite delimitar la necesidad a la cual se debe responder con una solución (<em>descripción del problema</em>), los hitos que se deben alcanzar (<em>objetivos general y específico</em>), los recursos requeridos para lograr desarrollar y aplicar la solución (<em>marco teórico</em>), los posibles beneficios derivados del proceso exitoso (<em>justificación del proyecto</em>) y una administración racional y realistas de los recursos a disposición en cuanto a disponibilidad, tiempo y personal mediante una secuenciación de tareas (i.e. <em>diagramas de Gantt</em>), todo reduce el componente iterativo inherente a diseño y producción de un producto, permite prever posibles imprevistos y mantener una perspectiva realista y una eficiencia aceptable en cuanto a presupuesto, metodologías y tecnologías a usar.

El plan de trabajo debe incluir principios de ingeniería concurrente para minimizar para manera mejor los posibles imprevistos, usualmente difíciles de manera con principios de ingeniería secuencial.

Explanation:

El plan de trabajo permite delimitar la necesidad a la cual se debe responder con una solución (<em>descripción del problema</em>), los hitos que se deben alcanzar (<em>objetivos general y específico</em>), los recursos requeridos para lograr desarrollar y aplicar la solución (<em>marco teórico</em>), los posibles beneficios derivados del proceso exitoso (<em>justificación</em>) y una administración racional y realistas de los recursos a disposición en cuanto a disponibilidad, tiempo y personal mediante una secuenciación de tareas (i.e. diagramas de Gantt), todo reduce el componente iterativo inherente a diseño y producción de un producto, permite prever posibles imprevistos y mantener una perspectiva realista y una eficiencia aceptable en cuanto a presupuesto, metodologías y tecnologías a usar.

El plan de trabajo debe incluir principios de ingeniería concurrente para minimizar para manera mejor los posibles imprevistos, usualmente difíciles de manera con principios de ingeniería secuencial.

5 0
3 years ago
If you want to change the name or file type of an existing word processing document, what command should you use?
mezya [45]
I think that the answer is "save as"
8 0
2 years ago
Read 2 more answers
Is anyone else excited for sephiroth (I think I spelt it wrong) in smash. And if you arn't are you excited for christmas
Fed [463]

Answer: idek what that is but okay ;v;

7 0
3 years ago
Write a Java test program, named TestGuitar, to create 3different Guitars representing each representing a unique test case and
alina1380 [7]

Answer:

Explanation:

//Guitar.java

import java.awt.Color;

import java.lang.reflect.Field;

import java.util.Random;

public class Guitar

     /**

     * these two fields are used to generate random note and duration

     */

     static char[] validNotes = 'A', 'B', 'C', 'D', 'E', 'F', 'G' ;

     static double[] validDuration = 0.25, 0.5, 1, 2, 4 ;

     /**

     * basic guitar attributes

     */

     private int numStrings;

     private double guitarLength;

     private String guitarManufacturer;

     private Color guitarColor;

     public Guitar()

           /**

           * default constructor

           */

           numStrings = 6;

           guitarLength = 28.2;

           guitarManufacturer = Gibson;

           guitarColor = Color.RED;

     

     public Guitar(int numStrings, double guitarLength,

                 String guitarManufacturer, Color guitarColor)

           /**

           * parameterized constructor

           */

           this.numStrings = numStrings;

           this.guitarLength = guitarLength;

           this.guitarManufacturer = guitarManufacturer;

           this.guitarColor = guitarColor;

     

     /**

     * required getters and setters

     */

     public static char[] getValidNotes()

           return validNotes;

     public static void setValidNotes(char[] validNotes)

           Guitar.validNotes = validNotes;      

     public static double[] getValidDuration()

           return validDuration;

     public

4 0
3 years ago
Which type of mountain is formed due to the collision of two different kinds of plates?
Lyrx [107]
D) Andean mountains are formed due to the collision of two different kinds of plates.
6 0
3 years ago
Read 2 more answers
Other questions:
  • What are factors that limit a technological design
    13·1 answer
  • Software obtained illegally is called open-source software. true / false
    15·1 answer
  • The grade of a metric bolt is designated by
    8·1 answer
  • To reload a picture taken with a digital camera means to copy the digital picture from the camera to your computer.
    5·2 answers
  • A system is composed of four parts, J, K, L, and M. All four must function for the system to function. The four component reliab
    6·1 answer
  • During a test, it is generally best to: A. answer all questions in order B answer the essay questions first C. answer the least
    7·2 answers
  • When is mail merge an effective productivity tool<br>​
    5·2 answers
  • Question 1 (1 point)
    13·1 answer
  • WHATS YALL FAVORITE GAME WHOEVER GIVES THE BEST ANSWER WINS 200
    13·2 answers
  • A CPU has just been powered on and it immediately executes a machine code instruction. What is the most likely type of instructi
    13·1 answer
Add answer
Login
Not registered? Fast signup
Signup
Login Signup
Ask question!