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;
}
#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;
}
modify the code for timer_test_02.c to allow the time delay between events to be pseudo- random...
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...
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 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 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 =...
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 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...
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,...
Remove srand(time(NULL)); from this C++ code so that it still finds random numbers correctly. Then, Write a program that adds the following to the fixed code. • Add a function that will use the BubbleSort method to put the numbers in ascending order. – Send the function the array. – Send the function the size of the array. – The sorted array will be sent back through the parameter list, so the data type of the function will be void....
Hello, how can i compile this C code using option -std=c99 or -std=gnu99 #include <stdio.h> #include <stdlib.h> #include <string.h> #include <ctype.h> #include <unistd.h> #include <sys/types.h> #include <sys/wait.h> #include <sys/stat.h> #include <fcntl.h> static char* args[512]; pid_t pid; int command_pipe[2]; static void waiting(int n); static int command(int input, int first, int last) { int fd[2]; int flag=0; pipe( fd ); pid = fork(); if (pid == 0) { for(int i=0;args[i]!=0;i++) { if(args[i][0]=='>') { fd[1]=open(args[i+1], O_CREAT|O_TRUNC|O_WRONLY, 0644); flag=1; } if(args[i]=='>>' ) { fd[1]=open(args[i+1],...
Doubly Linked List The assignment is to modify the below code in any way (like changing the method of a function). Time complexity is omitted. Any methods/functions below could be changed into something different. I was thinking of changing the method of getting size of list and maybe change from numbers to letters for nodes. import java.util.Scanner; /* Class Node */ class Node { protected int data; protected Node next, prev; /* Constructor */ public Node() { next = null;...