Below is the frame work for this. Please help me to fill the method bridge_desto
ID: 3594537 • Letter: B
Question
Below is the frame work for this. Please help me to fill the method bridge_destory() and ArriveBridge() existBridge() crossBridge and *Oneviechile(). Appreciate it! I NEED Concurrent Programming method for this!!!!! Thank you so much in advanced!
#include <stdio.h>
#include <stdlib.h>
#include <pthread.h>
#include <unistd.h>
#define MAX_BR_CAP 5
#define CROSS_TIME 4
#define DIREC_PROB 0.7
#define handle_err(s) do{perror(s); exit(EXIT_FAILURE);}while(0)
typedef struct _thread_argv
{
int vid;
int direc;
int time_to_cross;
} thread_argv;
typedef struct _bridge {
int dept_idx;
int num_car;
int curr_dir;
} bridge_t;
void bridge_init();
void bridge_destroy();
void dispatch(int n);
void *OneVehicle(void *argv);
void ArriveBridge(int vid, int direc);
void CrossBridge(int vid, int direc, int time_to_cross);
void ExitBridge(int vid, int direc);
pthread_t *threads = NULL; /* Array to hold thread structs */
thread_argv *args = NULL; /* Array to hold thread arguments */
int num_v = 30; /* Total number of vehicles to be created */
bridge_t br; /* Bridge struct shared by the vehicle threads*/
int main(int argc, char *argv[])
{
int sched_opt;
int i;
if(argc < 2)
{
printf("Usage: %s SCHED_OPT [SEED] ", argv[0]);
exit(EXIT_SUCCESS);
}
/* Process Arguments */
sched_opt = atoi(argv[1]);
if(argc == 3)
srand((unsigned int)atoi(argv[2]));
else
srand((unsigned int)time(NULL));
/* Allocate memory for thread structs and arguments */
if((threads = (pthread_t *)malloc(sizeof(pthread_t) * num_v)) == NULL)
handle_err("malloc() Failed for threads");
if((args = (thread_argv *)malloc(sizeof(thread_argv) * num_v)) == NULL)
handle_err("malloc() Failed for args");
/* Init bridge struct */
bridge_init();
/* Create vehicle threads */
switch(sched_opt)
{
case 1 : dispatch(5); break;
case 2 : dispatch(10); break;
case 3 : dispatch(30); break;
default:
fprintf(stderr, "Bad Schedule Option %d ", sched_opt);
exit(EXIT_FAILURE);
}
/* Join all the threads */
for(i = 0; i < num_v; i++)
pthread_join(threads[i], NULL);
/* Clean up and exit */
bridge_destroy();
exit(EXIT_SUCCESS);
}
/**
* Create n vehicle threads for every 10 seconds until the total
* number of vehicles reaches num_v
* Each thread handle is stored in the shared array - threads
*/
void dispatch(int n)
{
int k, i;
for(k = 0; k < num_v; k += n)
{
printf("Dispatching %d vehicles ", n);
for( i = k; i < k + n && i < num_v; i++)
{
/* The probability of direction 0 is DIREC_PROB */
int direc = rand() % 1000 > DIREC_PROB * 1000 ? 0 : 1;
args[i] = (thread_argv){i, direc, CROSS_TIME};
if(pthread_create(threads + i, NULL, &OneVehicle, args + i) != 0)
handle_err("pthread_create Failed");
}
printf("Sleep 10 seconds "); sleep(10);
}
}
void *OneVehicle(void *argv)
{
thread_argv *args = (thread_argv *)argv;
ArriveBridge(args->vid, args->direc);
CrossBridge(args->vid, args->direc, args->time_to_cross);
ExitBridge(args->vid, args->direc);
pthread_exit(0);
}
void bridge_init()
{
br.dept_idx = 0;
br.curr_dir = 0;
br.num_car = 0;
return;
}
void bridge_destroy()
{
br.dept_idx=NULL;
br.curr_dir=NULL;
br.num_car=NULL;
return;
}
void ArriveBridge(int vid, int direc)
{
return;
}
void CrossBridge(int vid, int direc, int time_to_cross)
{
fprintf(stderr, "vid=%d dir=%d starts crossing. Bridge num_car=%d curr_dir=%d ",
vid, direc, br.num_car, br.curr_dir);
sleep(time_to_cross);
return;
}
void ExitBridge(int vid, int direc)
{
fprintf(stderr, "vid=%d dir=%d exit with departure idx=%d ",
vid, direc, br.dept_idx);
return;
}
Explanation / Answer
#include <stdio.h>
#include <stdlib.h>
int menu();
void max(int traficValues[]);
int smallest(int traficValues[]);
float average(int traficValues[]);
int main(void)
{
int traficValues[7]={23, 55, 64, 52, 79, 66, 30};//pre-load
int option;
int Min;
float avg;
option = menu();
while(option!=4)
{
switch(option)
{
case 1: max(traficValues);/*could you either call a function or just write everything in the case*/
option = menu();// Ask user what they want to do next
break;
case 2: Min = smallest(traficValues);
printf("Minimum trafic values: %d", Min);
option = menu();
break;
case 3: avg = average(traficValues);
printf("Average trafic values of the week: %.1f", avg);
option = menu();
break;
case 4: printf("exit");
exit(101);
break;
default: printf("Incorrect Option. Please re-enter. ");
option = menu();
break;
}
}
return 0;
}
int menu(void)
{
int option;
printf(" What would you like to do ");
printf("1. View maxium trafic values ");
printf("2. View minimum trafic values ");
printf("3. Average trafic values ");
printf("4. exit ");
scanf("%d", &option);
return option;
}
void max(int traficValues[])
{
int i;
int biggest;
biggest = traficValues[0];
for(i=0; i<7; i++)
{
if(traficValues[i]> biggest)
biggest = traficValues[i];
}
printf("Maxium trafic Values: %d", biggest);
return;
}
int smallest(int traficValues[])
{
int i;
int smallest;
smallest = traficValues[0];
for(i=0; i<7; i++)
if(traficValues[i]< smallest)
smallest = traficValues[i];
return smallest;
}
float average(int traficValues[])
{
int i;
float sum = 0.0;
float avg = 0.0;
for(i=0;i<7; i++)
{
sum = sum+ traficValues[i];
}
avg = sum/7;
return avg;
}
Related Questions
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.