操作系统中的 CalTrain 问题

问题描述 投票:0回答:0

我正在尝试解决 calTrain 问题。这是问题陈述(运行器代码在这里):

代码有时有效,有时无效。当它工作时,它应该给出这样的信息:看起来不错! .当它不起作用时,跑步者给我这个消息:错误,火车上的乘客太多。那么我的代码有什么问题?

void station_load_train(struct station *station, int count) //count: number of free seats
{
    pthread_mutex_lock(&station->mutex); //make sure that only one train is in the station
    station -> free_seats_num = count;
    if(station->free_seats_num > 0) station -> free_seats_stat = 1;
    station -> train_stat = 1;  //the train arrived
     while ((station->free_seats_stat == 1) && (station->pssngrs_stat == 1)){  //free seats are available and passengers are waiting
        pthread_cond_signal(&station->train_arrived);
        pthread_cond_signal(&station->free_seats_avail); //notify passengers that there are free seats
        pthread_cond_wait(&station->pssngr_boarded, &station->mutex); //wait for the passenger to board
    } 
    station -> train_stat = 0;  // the train left
    station -> free_seats_stat = 0;
    pthread_mutex_unlock(&station->mutex);
}

void station_wait_for_train(struct station *station)
{
    pthread_mutex_lock(&station->mutex); //enforce mutual exclusion
    station->pssngrs_stat = 1; // there are passengers in the station
    station->pssngrs_num++; // one more passenger arrived
    while((station->train_stat == 0) || (station->free_seats_stat == 0)){ //wait for the train or free seats
        if(station->train_stat == 0)
            pthread_cond_wait(&station->train_arrived, &station->mutex);
        if(station->free_seats_stat == 0)
            pthread_cond_wait(&station->free_seats_avail, &station->mutex);
    }
    pthread_mutex_unlock(&station->mutex);
}

void station_on_board(struct station *station)
{
    pthread_mutex_lock(&station->mutex);

    station->free_seats_num--; // a passenger took over one chair
    station->pssngrs_num--;
    if(station->pssngrs_num <= 0)
        station->pssngrs_stat = 0;
    if(station->free_seats_num <= 0)
        station->free_seats_stat = 0;
    pthread_cond_signal(&station->pssngr_boarded);  //passenger is on the train
    pthread_mutex_unlock(&station->mutex);
}
c operating-system synchronization
© www.soinside.com 2019 - 2024. All rights reserved.