Beispiel #1
0
static void sail_test() {
    check_expect_i(sail(AMERICA, EAST), EUROPE);
    check_expect_i(sail(EUROPE, WEST), AMERICA);
    check_expect_i(sail(EUROPE, SOUTH), AFRICA);
    check_expect_i(sail(AFRICA, NORTH), EUROPE);
    check_expect_i(sail(AMERICA, SOUTH), NOWHERE);
    check_expect_i(sail(AFRICA, EAST), NOWHERE);
    check_expect_i(sail(NOWHERE, NORTH), NOWHERE);
    check_expect_i(sail(NOWHERE, WEST), NOWHERE);
}
Beispiel #2
0
/**
 * Boat thread.
 */
void *thr_boat(void *ptr)
{
    boat_t *boat = (boat_t*) ptr;
    int i;
    
    while (!dock_is_closed()) {
        // if no available spots at bay, sail for a bit
        if (sem_trywait(&sem_dock) == 0) {
            printf("[B%02d] entered bay.\n", boat->id);
            
            // look for an available anchorage
            for (i = 0; !dock_is_closed(); i = (i+1) % ANCHORAGES) {
                if (enter_anchorage(i, boat)) {
                    
                    // wait until crane is available for moving cargo
                    wait_for_crane(i, boat);
                    
                    // trigger crane to move cargo
                    printf("[B%02d] will start unloading (%d)/loading in [A%d].\n", boat->id, boat->cargo, i);
                    sem_post(&sem_start_cargo[i]);
                    
                    // at this point, the crane is being occupied,
                    // leaving the waiting spot vacant
                    sem_post(&sem_enter_anchorage[i]);
                    
                    // wait until moving cargo is finished and leave
                    sem_wait(&sem_end_cargo[i]);
                    
                    // now the crane is avaible for the next boat
                    leave_crane(i, boat->id);
                    
                    printf("[B%02d] left [A%d].\n", boat->id, i);
                    
                    break;
                }
                sleep(1); // wait 1 sec to avoid some race conditions
            }
            // boat leaving dock, so the spot is vacant
            sem_post(&sem_dock);
        }
        sail(boat->id);
    }
    
    printf(">>>>> The dock is closed. [B%02d] is on its way.\n", boat->id);
    
    return NULL;
}