Question: I'm working on an assignment that requires us to solve the one way bridge problem I would appreciate any tips or hints on the code

I'm working on an assignment that requires us to solve the one way bridge problem I would appreciate any tips or hints on the code on where we should change or what we should change:

In Gallap Park at Ann Arbor, there is one-lane bridge. Vehicles coming from the south and the north arrive at this one-lane bridge. Vehicles heading in the same direction can cross the bridge at the same time, but vehicles heading in opposite directions cannot. And, if there are ever more than three vehicles on the bridge at one time, the bridge will collapse under their weight.

If a vehicle arrives at the bridge, the number of vehicles on the bridge is less than 3 and they all head in the same directions, this new vehicle should be allowed to cross the bridge without waiting even if there are vehicles waiting in the opposite direction.

In the main thread, you need to create one thread for each vehicle. The arrival pattern in the simulator

is described as follows:

id arrival_time direction

0 0.0 N

1 1.0 N

2 2.0 S

3 3.0 S

4 4.0 S

5 5.0 N

6 6.0 N

7 7.0 S

8 8.0 S

9 9.0 N

10 10.0 N

11 11.0 N

12 12.0 N

Wanted Output

Time 0.0: Vehicle 0 (N) crossing

Time 1.0: Vehicle 1 (N) crossing

Time 5.0: Vehicle 5 (N) crossing

Time 6.0: Vehicle 6 (N) crossing

Time 9.0: Vehicle 9 (N) crossing

Time 10.0: Vehicle 10 (N) crossing

Time 11.0: Vehicle 11 (N) crossing

Time 14.0: Vehicle 12 (N) crossing

Time 19.0: Vehicle 2 (S) crossing

Time 19.0: Vehicle 3 (S) crossing

Time 19.0: Vehicle 4 (S) crossing

Time 24.0: Vehicle 7 (S) crossing

Time 24.0: Vehicle 8 (S) crossing

We have the following code done but it keeps missing a few of the cars because it never gets to a certain time and it doesn't have the cars passing at the right intervals.

#include

#include

#include

#include

sem_t lock_reader; // lock for reader

sem_t lock_writer; // lock for writer

int bridge_count = 0;

int prev_dir = 0;

double t0, t;

double GetTime()

{

struct timeval t;

int rc = gettimeofday(&t, NULL);

return (double)t.tv_sec + (double)t.tv_usec/1e6;

}

typedef struct _thread_info {

int id;

int dir;

double inter_arrival_t;

} vehicle_info;

void put(int value){

prev_dir = value;

}

void* Reader(void* arg) {

vehicle_info *ti = (vehicle_info *) arg;

//printf("Time %2.1f: Reader %d arriving ", GetTime()-t0, ti->id);

sem_wait(&lock_reader); //lock bridge

bridge_count++;

printf("bridge count %d ", bridge_count);

// printf("bridge count %d ", bridge_count);

if(bridge_count==2){

printf("//sleep ");

usleep(1000000);

}

if (bridge_count <2) { //if vehicles on bridge is less than 2

if(ti->dir != prev_dir){ //if vehicle is same direction as previous, let cross

sem_wait(&lock_writer); //lock vehicle, and cross

printf("Time %2.1f: Reader %d crossing ", GetTime()-t0, ti->id);

usleep(5000000); //crossing

printf("Time %2.1f: Reader %d CROSSED ", GetTime()-t0, ti->id);

}

else if(ti->dir==prev_dir){

sem_wait(&lock_writer);

printf("Time %2.1f: Reader %d crossing ", GetTime()-t0, ti->id);

usleep(5000000); //crossing

printf("Time %2.1f: Reader %d CROSSED ", GetTime()-t0, ti->id);

}

}

sem_post(&lock_reader); //unlock bridge

//crossing

// printf("Time %2.1f: Reader %d reading ", GetTime()-t0, ti->id);

sem_wait(&lock_reader); //lock bridge

bridge_count--; //one vehicle has left bridge

if (bridge_count <=2) { //as long as there is 1 or less vehicles on bridge

sem_post(&lock_writer); //unlock vehicle (letting it exit)

printf("vehicle left bridge ");

}

sem_post(&lock_reader); //unlock bridge

//printf("Time %2.1f: Reader %d exiting ", GetTime()-t0, ti->id);

return 0;

}

void* Writer(void* arg) {

vehicle_info *ti = (vehicle_info *) arg;

sem_wait(&lock_writer); //locking vehicle

//crossing

printf("Time %2.1f: Writer %d vehicle arrived at bridge ", GetTime()-t0, ti->id);

put(ti->dir);

// usleep(2000000);

sem_post(&lock_writer);

//printf("Time %2.1f: Writer %d exiting ", GetTime()-t0, ti->id);

return 0;

}

int main(int argc, char *argv[])

{

int i;

int NN = 13;

vehicle_info tis[NN];

pthread_t vehicle[NN];

sem_init(&lock_reader, 0, 1);

sem_init(&lock_writer, 0, 1);

for (i=0; i

tis[i].id = i;

tis[i].dir = 0;

tis[i].inter_arrival_t = 1.0;

}

//0 = north

//1 = south

tis[0].dir = 0;

tis[1].dir = 0;

tis[2].dir = 1;

tis[3].dir = 1;

tis[4].dir = 1;

tis[5].dir = 0;

tis[6].dir = 0;

tis[7].dir = 1;

tis[8].dir = 1;

tis[9].dir = 0;

tis[10].dir = 0;

tis[11].dir = 0;

tis[12].dir = 0;

t0 = GetTime();

for (i=0; i

usleep((int)(tis[i].inter_arrival_t*1000000));

pthread_create(&vehicle[i], NULL, Writer, (void*)&tis[i]);

pthread_create(&vehicle[i], NULL, Reader, (void*)&tis[i]);

}

for (i=0; i

pthread_join(vehicle[i], NULL);

}

}

Step by Step Solution

There are 3 Steps involved in it

1 Expert Approved Answer
Step: 1 Unlock blur-text-image
Question Has Been Solved by an Expert!

Get step-by-step solutions from verified subject matter experts

Step: 2 Unlock
Step: 3 Unlock

Students Have Also Explored These Related Databases Questions!