Untitled

 avatar
unknown
plain_text
9 months ago
3.4 kB
13
Indexable
#include <stdio.h>
#include <pthread.h>
#include <semaphore.h>

#define REPETICIONES 50

/* Estructura para pasar datos a los hilos sin usar variables globales */
typedef struct {
    sem_t *semA, *semB, *semC, *semD, *semE;
} sync_data_t;

void* hiloA(void* arg) {
    sync_data_t *data = (sync_data_t*)arg;
    
    for (int i = 0; i < REPETICIONES; i++) {
        sem_wait(data->semA);
        printf("A");
        fflush(stdout);
        sem_post(data->semC);
        
        sem_wait(data->semA);
        printf("A");
        fflush(stdout);
        sem_post(data->semC);
    }
    return NULL;
}

void* hiloB(void* arg) {
    sync_data_t *data = (sync_data_t*)arg;
    
    for (int i = 0; i < REPETICIONES; i++) {
        sem_wait(data->semB);
        printf("B");
        fflush(stdout);
        sem_post(data->semC);
        
        sem_wait(data->semB);
        printf("B");
        fflush(stdout);
        sem_post(data->semC);
    }
    return NULL;
}

void* hiloC(void* arg) {
    sync_data_t *data = (sync_data_t*)arg;
    
    for (int i = 0; i < REPETICIONES * 2; i++) {
        sem_wait(data->semC);
        printf("C");
        fflush(stdout);
        
        /* Alterna entre D y E */
        if (i % 2 == 0) {
            sem_post(data->semD);
        } else {
            sem_post(data->semE);
        }
    }
    return NULL;
}

void* hiloD(void* arg) {
    sync_data_t *data = (sync_data_t*)arg;
    
    for (int i = 0; i < REPETICIONES; i++) {
        sem_wait(data->semD);
        printf("D");
        fflush(stdout);
        
        /* Alterna entre A y B para la siguiente ronda */
        if (i % 2 == 0) {
            sem_post(data->semA);
        } else {
            sem_post(data->semB);
        }
    }
    return NULL;
}

void* hiloE(void* arg) {
    sync_data_t *data = (sync_data_t*)arg;
    
    for (int i = 0; i < REPETICIONES; i++) {
        sem_wait(data->semE);
        printf("E");
        fflush(stdout);
        
        /* Alterna entre A y B para la siguiente ronda */
        if (i % 2 == 0) {
            sem_post(data->semB);
        } else {
            sem_post(data->semA);
        }
    }
    return NULL;
}

int main() {
    pthread_t tA, tB, tC, tD, tE;
    sem_t semA, semB, semC, semD, semE;
    sync_data_t data;
    
    /* Inicialización de semáforos */
    sem_init(&semA, 0, 1);  /* A empieza primero */
    sem_init(&semB, 0, 0);  /* B espera */
    sem_init(&semC, 0, 0);
    sem_init(&semD, 0, 0);
    sem_init(&semE, 0, 0);
    
    /* Configurar estructura de datos compartida */
    data.semA = &semA;
    data.semB = &semB;
    data.semC = &semC;
    data.semD = &semD;
    data.semE = &semE;
    
    /* Crear hilos */
    pthread_create(&tA, NULL, hiloA, &data);
    pthread_create(&tB, NULL, hiloB, &data);
    pthread_create(&tC, NULL, hiloC, &data);
    pthread_create(&tD, NULL, hiloD, &data);
    pthread_create(&tE, NULL, hiloE, &data);
    
    /* Esperar finalización */
    pthread_join(tA, NULL);
    pthread_join(tB, NULL);
    pthread_join(tC, NULL);
    pthread_join(tD, NULL);
    pthread_join(tE, NULL);
    
    /* Destruir semáforos */
    sem_destroy(&semA);
    sem_destroy(&semB);
    sem_destroy(&semC);
    sem_destroy(&semD);
    sem_destroy(&semE);
    
    printf("\n");
    return 0;
}
Editor is loading...
Leave a Comment