Question

modify the code for timer_test_02.c to allow the time delay between events to be pseudo- random...

modify the code for timer_test_02.c to allow the time delay between events to be pseudo-


random exponential, with a mean time between arrivals of 0.1 second. Change the limit in the time_stamps() function from 5 time-stamps to 10, so that the mean run-time will be about 10*0.1 = 1.0 seconds. Once this is working, you should be able to generate 10 events with a pseudo-random exponential arrival process.



The code is:

#include <stdio.h>


#include <stdint.h>

#include <time.h>

#include <unistd.h>

#include <sys/types.h>

#include <unistd.h>

struct timespec start, end;

/* A user function to extract the number of nanoseconds that have elapsed*/

int64_t timespecDiff(struct timespec*, struct timespec*);

/* The time_stamps() process is encapsulated into a function */

int time_stamps(pid_t);

int main(void)

{

int r_val;

pid_t PID ;

PID = getpid();

r_val = time_stamps(PID);

return r_val;

}

int64_t timespecDiff(struct timespec *timeA_p, struct timespec *timeB_p)

{

return ((timeA_p->tv_sec * 1000000000) + timeA_p->tv_nsec) -

((timeB_p->tv_sec * 1000000000) + timeB_p->tv_nsec);

}

int time_stamps(pid_t PID){

/* A process that prints out a process ID, and time-stamps */

int k = 0 ;

double e_time_sec ;

/* start the clock*/

clock_gettime(CLOCK_MONOTONIC, &start);

/* Some code we are interested to measure time elapsed*/

for (k=0;k<5;k++)

{

usleep(100000);

clock_gettime(CLOCK_MONOTONIC, &end);

uint64_t timeElapsed = timespecDiff(&end, &start);

e_time_sec = ((double) timeElapsed) * 1.0E-9;

printf("PID: %d \t e_time: %E\n", PID, e_time_sec);

}

return 0;

}

0 0
Add a comment Improve this question Transcribed image text
Answer #1

#include <stdio.h>

#include <stdlib.h>

#include <stdint.h>

#include <time.h>

#include <unistd.h>

#include <sys/types.h>

#include <unistd.h>

#include <math.h>

struct timespec start, end;

/* A user function to extract the number of nanoseconds that have elapsed*/

int64_t timespecDiff(struct timespec*, struct timespec*);

/* The time_stamps() process is encapsulated into a function */

int time_stamps(pid_t);

double ran_expo(double lambda){

double u;

u = rand() / (RAND_MAX + 1.0);

return -log(1- u) / lambda;

}

int main(void)

{

int r_val;

pid_t PID ;

PID = getpid();

time_t t;

srand((unsigned) time(&t));

r_val = time_stamps(PID);

return r_val;

}

int64_t timespecDiff(struct timespec *timeA_p, struct timespec *timeB_p)

{

return ((timeA_p->tv_sec * 1000000000) + timeA_p->tv_nsec) -

((timeB_p->tv_sec * 1000000000) + timeB_p->tv_nsec);

}

int time_stamps(pid_t PID){

/* A process that prints out a process ID, and time-stamps */

int k = 0 ;

double e_time_sec ;

/* start the clock*/

clock_gettime(CLOCK_MONOTONIC, &start);

/* Some code we are interested to measure time elapsed*/

for (k=0;k<10;k++)

{

double r = ran_expo(0.1);

// printf("%lf\n", r);

usleep(10000 * r);

clock_gettime(CLOCK_MONOTONIC, &end);

uint64_t timeElapsed = timespecDiff(&end, &start);

e_time_sec = ((double)timeElapsed) * 1.0E-9;

printf("PID: %d \t e_time: %E\n", PID, e_time_sec);

}

return 0;

}

Add a comment
Know the answer?
Add Answer to:
modify the code for timer_test_02.c to allow the time delay between events to be pseudo- random...
Your Answer:

Post as a guest

Your Name:

What's your source?

Earn Coins

Coins can be redeemed for fabulous gifts.

Not the answer you're looking for? Ask your own homework help question. Our experts will answer your question WITHIN MINUTES for Free.
Similar Homework Help Questions
  • Source code to modify: #include <stdio.h> #include <unistd.h> #include <sys/types.h> int main() { pid_t pid; /*fork...

    Source code to modify: #include <stdio.h> #include <unistd.h> #include <sys/types.h> int main() { pid_t pid; /*fork a child process*/ pid = fork(); if (pid<0){ /*error occured*/ fprintf(stderr,"Fork failed\n"); return 1; } else if (pid == 0) { /*child process */ printf("I am the child %d\n",pid); execlp("/bin/ls","ls",NULL); } else { /*parent process */ /*parent will wait for the child to complete*/ printf("I am the parent %d\n",pid); wait(NULL); printf("Child Complete\n"); } return 0; } 1. Write program codes for 3.21 a and...

  • Please change this 2 thread program to a 4 thread program. /*compile as "gcc thread.c -pthread"...

    Please change this 2 thread program to a 4 thread program. /*compile as "gcc thread.c -pthread" */ #include <pthread.h> #include <stdio.h> #include <time.h> #define SIZE 100000000 //100 M //#define SIZE 10000 #define NUMBER_OF_TIMES 100 float a[SIZE]; typedef struct {        int start;        int end;        double sum;        int pid;    } range; void *thread_function(void *arg) //define a function that will be executed from //within a thread {    range *incoming = (range *) arg;...

  • IN UNIX, MODIFY CODE, PROVIDE SCREENSHOTS FOR GOOD RATING: T1. Modify Client.c program to accept two...

    IN UNIX, MODIFY CODE, PROVIDE SCREENSHOTS FOR GOOD RATING: T1. Modify Client.c program to accept two arguments (IP add & port no. of the concurrent Server with thread - conServThread.c). Similarly, modify the Server (conServThread.c) program to accept an argument which is the port number of the server to bind and listen to. Try these two updated programs (server and client) with a port number (e.g., hhmm6) with current time where hh is hours in 24-hour format and mm is...

  • I have the following code....from the previous lab....the above needs to be added to what is...

    I have the following code....from the previous lab....the above needs to be added to what is already existing. ALSO MODIFY SEMAPHORES TO USE pthreads instead of the pipe constructs P() & V() #include <stdio.h> #include <string.h> #include <sys/types.h> #include <unistd.h> #include <sys/wait.h> #include <stdlib.h> #include <sys/stat.h> void printStat(char *filename); //Main int main(int argc, char *argv[]) { //Process Id (storing)    pid_t pid;    int j;    //printf("Welcome to Project Three\n”);    // For loop*/    for (j = 1; j...

  • Can you help with this C programming question. I have provided the skeleton code below along...

    Can you help with this C programming question. I have provided the skeleton code below along with the Stack/Data/Process Class for you to see/reference. Along with the Stack/Data type definition.   **SKELTON CODE** #include #include #include Stack* concat_stack(Stack *s1, Stack *s2) { //your code here return NULL; } **STACK CLASS FOR YOU TO REFERENCE** #include #include #include #include Stack* create_stack(int stack_capacity) { Stack *s = (Stack*) malloc(sizeof(Stack)); if (stack_capacity < 1) { fprintf(stderr, "Error(create_stack): invalid capacity, set to 10\n"); s->capacity =...

  • c++ help Write a program that obtains the execution times for the following three code segments...

    c++ help Write a program that obtains the execution times for the following three code segments with the different n. code 1: for(int i = 0; i <= n; i++)   { x = i; } code 2: for (int i = 0; i <= n; i++) { for (int j = 0; j <= n; j++) { x = i + j; } } code 3: for (int i = 0; i <= n; i++) { for (int j =...

  • Please read the question carefully and in full and then answer it (using C language and...

    Please read the question carefully and in full and then answer it (using C language and any necessary libraries). (I am not sure what the expert's mean by when to move to using pipe. I guess if you need to, then you can use a random time or any exact time but make sure to put a comment next to it). you might use the below code to give you some idea: #include<stdio.h> #include<stdlib.h> #include<unistd.h> #include<sys/types.h> #include<string.h> #include<sys/wait.h> void main()...

  • C programming help! /* Your challenge is to format the following code in a readable manner, anwsering the questions in...

    C programming help! /* Your challenge is to format the following code in a readable manner, anwsering the questions in the comments. * */ #include <stdio.h> #include <stdlib.h> #include <sys/time.h> #include <time.h> #include <sys/socket.h> #include <arpa/inet.h> #include <sys/types.h> #include <sys/uio.h> #include <sys/stat.h> #include <fcntl.h> #include <unistd.h> #include <strings.h> #define BUFFERT 512 #define BACKLOG 1 int create_server_socket (int port); struct sockaddr_in sock_serv,sock_clt; int main(int argc, char** argv){ int sfd, fd; unsigned int length = sizeof(struct sockaddr_in); long int n, m, count...

  • modify sample code from example two to calculate the surface area and volume of a sphere...

    modify sample code from example two to calculate the surface area and volume of a sphere givin the radius int main() Example 2 Introduction to structures and passing the structure variables as arguments into functions. 1. A structure is created with circle related variables. 2. Two functions are created related to circle calculations. 3. This example passes structure variables from the main function to the message Circle function 4. The message Circle function converts the structure variables into regular double...

  • C please In this question, you will find the difference between two time durations. You are...

    C please In this question, you will find the difference between two time durations. You are given the following struct definitions: typedef struct _duration { int hours; int minutes; } Duration; Design a function to find the difference between two durations: Duration* subtract(Duration* duration1, Duration* duration2) { } Return a pointer to a new duration struct that contains the difference between the two. It may be useful to reduce the durations to a single unit (example: minutes), find the difference,...

ADVERTISEMENT
Free Homework Help App
Download From Google Play
Scan Your Homework
to Get Instant Free Answers
Need Online Homework Help?
Ask a Question
Get Answers For Free
Most questions answered within 3 hours.
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT