当我运行程序时,线程在 N 次发送后停止发送消息

时间:2021-06-01 10:04:39

标签: c multithreading posix shared-memory mutual-exclusion

源线程的 send() 方法向出租车线程发送一条消息,出租车线程通过 receive() 方法接收它。 这些方法在共享内存中进行了大量访问,我注意到共享内存中访问的次数越多,执行的发送和接收就越少

    #include "header.h"
//pthread_cond_wait e pthread_cond_signal per bloccare i processi e sbloccarli dopo il piazzamento
int main() {
    // Inizializza la maschera dei messaggi
    sa.sa_handler = handle_signal;
    sa.sa_flags = 0;
    sigemptyset(&my_mask);
    sa.sa_mask = my_mask;

    // Inizializzazione roba
    init();

    // Creazione dei segmenti di memoria condivisa
    shm_cells_id = shmget(IPC_PRIVATE, sizeof(*cells)*SO_WIDTH*SO_HEIGHT, IPC_CREAT | 0666);
    cells = shmat(shm_cells_id, 0, 0);

    shm_taxi_id = shmget(IPC_PRIVATE, sizeof(*taxis)*SO_TAXI, IPC_CREAT | 0666);
    taxis = shmat(shm_taxi_id, 0, 0);

    shm_sources_id = shmget(IPC_PRIVATE, sizeof(*sources)*SO_SOURCES, IPC_CREAT | 0666);
    sources = shmat(shm_sources_id, 0, 0);

    // Creazione della coda di messaggi
    msg1_id = msgget(IPC_PRIVATE, IPC_CREAT | IPC_EXCL | 0600);
    msg2_id = msgget(IPC_PRIVATE, IPC_CREAT | IPC_EXCL | 0600);

    // Crazione dei semafori
    //sem_init(&mutex, 0, 1);

    // Piazzamento delle celle vuote
    int n_holes = SO_HOLES;
    while (n_holes > 0) {
        int x = random_num(0, SO_WIDTH - 1);
        int y = random_num(0, SO_HEIGHT - 1);
        while (strcmp(cells[y*SO_WIDTH+x].content, "-H-") == 0 || is_around_hole(x,y)) {
            x = random_num(0, SO_WIDTH - 1);
            y = random_num(0, SO_HEIGHT - 1);
        }
        strcpy(cells[y*SO_WIDTH+x].content, "-H-");
        n_holes--;
    }

    // settaggio della griglia
    for (int i = 0; i < SO_HEIGHT; i++) {
        for (int j = 0; j < SO_WIDTH; j++) {
            if (strcmp(cells[i*SO_WIDTH+j].content, "-H-") != 0) {
                cells[i*SO_WIDTH+j].x = j;
                cells[i*SO_WIDTH+j].y = i;
                cells[i*SO_WIDTH+j].crossing_time = random_num(SO_TIMENSEC_MIN, SO_TIMENSEC_MAX);
                cells[i*SO_WIDTH+j].capacity = random_num(SO_CAP_MIN, SO_CAP_MAX);
                cells[i*SO_WIDTH+j].caught = false;
                strcpy(cells[i*SO_WIDTH+j].content, "---");
            }
        }
    }

    // creazione dei sources
    pthread_t sources_t[SO_SOURCES];
    int sources_t_args[SO_SOURCES];
    for (int i = 0; i < SO_SOURCES; i++) {
        sources_t[i] = i;
        sources_t_args[i] = i;
        pthread_create(&sources_t[i], NULL, make_sources, &sources_t_args[i]);
    }
    sleep(1);
    // creazione dei taxi
    pthread_t taxi_t[SO_TAXI];
    int taxi_t_args[SO_TAXI];
    for (int i = 0; i < SO_TAXI; i++) {
        taxi_t[i] = i;
        taxi_t_args[i] = i;
        pthread_create(&taxi_t[i], NULL, make_taxi, &taxi_t_args[i]);
    }
    sleep(1);
    pthread_cond_broadcast(&cond);

    /*for(int i = 0; i < SO_SOURCES; i++)
        pthread_join(sources_t[i], NULL);
    for(int i = 0; i < SO_TAXI; i++)
        pthread_join(taxi_t[i], NULL);*/

    // inizio simulazione
    endgame = false;
    start_timer(SO_DURATION);
    sleep(SO_DURATION);
    //sblocco taxi e sources

    //ciclo che genera la griglia ogni secondo

    // blocco il master fino alla fine della simulazione
}
void *make_sources(void *arg){
    int i =*((int *) arg);
    //printf("s_i: %d\n", i);
    pthread_mutex_lock(&lock);
    place_sources(i);
    pthread_mutex_unlock(&lock);

    pthread_mutex_lock(&lock2);
    pthread_cond_wait(&cond, &lock2);
    pthread_mutex_unlock(&lock2);

    while(!endgame){
        pthread_mutex_lock(&lock4);
        send_request(i);
        pthread_mutex_unlock(&lock4);
        sleep(1);
    }
}

// Fork per creare i processi taxi
void *make_taxi(void *arg){
    int i =*((int *) arg);
    //printf("t_i: %d\n", i);
    pthread_mutex_lock(&lock);
    place_taxi(i);
    pthread_mutex_unlock(&lock);

    pthread_mutex_lock(&lock2);
    pthread_cond_wait(&cond, &lock2);
    pthread_mutex_unlock(&lock2);

    while(!endgame) {
        pthread_mutex_lock(&lock5);
        receive_request(i);
        pthread_mutex_unlock(&lock5);
    }
}

int send_request(int i) {
    int d_x = random_num(0, SO_WIDTH - 1);
    int d_y = random_num(0, SO_HEIGHT - 1);
    int capacity = cells[d_y*SO_WIDTH+d_x].capacity;
    while (!is_capable(capacity, d_x, d_y) || cells[d_y*SO_WIDTH+d_x].content[1] == 'H' || cells[d_y*SO_WIDTH+d_x].caught == true) {
        d_x = random_num(0, SO_WIDTH - 1);
        d_y = random_num(0, SO_HEIGHT - 1);
    }
    cells[d_y*SO_WIDTH+d_x].caught = true;
    int x = sources->sources[i].x;
    int y = sources->sources[i].y;

我省略了一些方法,因为它们与问题无关。 我希望我说得很清楚。

0 个答案:

没有答案
相关问题