#include <stdio.h> #include <stdlib.h> #include <pthread.h> #include <unistd.h>
ID: 3594410 • Letter: #
Question
#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
bridge_t br = new bridge_t;
bridge_destroy(){
br.dept_idx=NULL;
br.curr_dir=NULL;
br.num_car=NULL;
delete br;
return ;
}
int numV = 5; // add this in the beginning of the code
void ArriveBridge(int vid, int direc)
{
if (numV > 0)
{
fprintf(“Vehicle has arrived on the bridge”);
numV--;
}
else
while(numV <= 0);
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;
}
int departure_index =1;
void ExitBridge(int vid, int direc)
{
fprintf(stderr, "vid=%d dir=%d exit with departure idx=%d ",
vid, direc, br.dept_idx);
departure_index++; // increment departure_index
numV++; // increment numV so that another vehicle can come to bridge
return;
}
I have explained this code in detail in your previous question. This answer is in continuation with that so not adding minute details of other portion of the code here. If this helps please give a thumbs up. Good Luck!!
Related Questions
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.