Traffic Control Problem

NS-cars.c and EW-cars.c are two programs that simulate traffic control. When north-south cars pass the crossroads, they should ensure that road from north to south is available, if north-south road is not available, they keep waiting. This is the same to east-west cars. Note that when north-south road is available, east-west road is not available and vice versa.

Please add semaphore synchronization in these two programs so that north-south cars and east-west cars can pass the crossroads alternately and there will not be car accident.

/*NS-cars.c*/
#include <stdio.h>
#include <stdlib.h>
#include <unistd.h>
int main(int argc, char *argv[]) {
    int i, car = 0, loop = 3;
    for (i = 0; i < loop; i++) {
        printf("Semaphore: The road from north to south is open\n");
        sleep(1);
        printf("NS-Car: car %d passed\n", car++);
    }
    printf("NS: Time is up. %d cars passed.\n", car);
    return 0;
}
/*EW-cars.c*/
#include <stdio.h>
#include <stdlib.h>
#include <unistd.h>
int main(int argc, char *argv[]) {
    int i, car = 0, loop = 3;
    for (i = 0; i < loop; i++) {
        printf("Semaphore: The road from east to west is open\n");
        sleep(rand() % 2 + 1);
        printf("EW-Car: car %d passed\n", car++);
    }
    printf("EW: Time is up. %d cars passed.\n", car);
    return 0;
}

Last updated