0% found this document useful (0 votes)
24 views3 pages

Struct Bridge

The document defines a structure for managing a bridge with variables to track the number of vehicles waiting and crossing from both the north and south directions. It includes functions to initialize the bridge and manage the arrival and departure of vehicles, ensuring that traffic rules are followed to prevent deadlock and maintain order. The implementation uses locks and condition variables to synchronize access to the bridge.

Uploaded by

nirob
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
24 views3 pages

Struct Bridge

The document defines a structure for managing a bridge with variables to track the number of vehicles waiting and crossing from both the north and south directions. It includes functions to initialize the bridge and manage the arrival and departure of vehicles, ensuring that traffic rules are followed to prevent deadlock and maintain order. The implementation uses locks and condition variables to synchronize access to the bridge.

Uploaded by

nirob
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
You are on page 1/ 3

struct bridge {

int north_waiting;

int north_crossing;

int north_consecutive;

int south_waiting;

int south_crossing;

int south_consecutive;

struct lock lock;

struct condition northbound_done;

struct condition southbound_done;

void bridge_init(struct bridge *b)

b->north_waiting = 0;

b->north_crossing = 0;;

b->north_consecutive = 0;

b->south_waiting = 0;

b->south_crossing = 0;

b->south_consecutive = 0;

lock_init(&b->lock);

cond_init(&b->northbound_done);

cond_init(&b->southbound_done);

int bridge_arrive_north(struct bridge *b)

lock_acquire(&b->lock);

b->north_waiting++;

while ((b->south_crossing > 0) ||

((b->south_waiting > 0) && (b->northConsecutive >= 5)) {


cond_wait(&b->southbound_done;

b->north_waiting--;

b->north_crossing++;

b->northConsecutive++;

b->southConsecutive = 0;

lock_release(&b->lock);

int bridge_leave_north(struct bridge *b)

lock_acquire(&b->lock);

b->north_crossing--;

if (b->north_crossing == 0) {

cond_broadcast(b->northbound_done);

lock_release(&b->lock);

Additional working space for Problem , if needed.

int bridge_arrive_south(struct bridge *b)

lock_acquire(&b->lock);

b->south_waiting++;

while ((b->north_crossing > 0) ||

((b->north_waiting > 0) && (b->southConsecutive >= 5)) {

cond_wait(&b->northbound_done;

b->south_waiting--;

b->south_crossing++;

b->southConsecutive++;
b->northConsecutive = 0;

lock_release(&b->lock);

int bridge_leave_south(struct bridge *b)

lock_acquire(&b->lock);

b->south_crossing--;

if (b->south_crossing == 0) {

cond_broadcast(b->southbound_done);

lock_release(&b->lock);

You might also like