Untitled

 avatar
unknown
plain_text
a year ago
3.2 kB
5
Indexable
#include <stdio.h>
#include <stdlib.h>
#include <pthread.h>
#include <semaphore.h>

#define ARRAY_SIZE 10
#define NUM_PRODUCERS 3
#define NUM_CONSUMERS 3
#define TOTAL_PRODUCTIONS 10000

int shared_array[ARRAY_SIZE];
sem_t mutex, full_sem, empty_sem;

FILE *producers_activity_file, *consumers_activity_file;

void *producer(void *arg) {
    int producer_id = *((int *)arg);

    for (int production_number = 1; production_number <= TOTAL_PRODUCTIONS; ++production_number) {
        int value = rand() % 10001;

        sem_wait(&empty_sem);
        sem_wait(&mutex);

        for (int i = 0; i < ARRAY_SIZE; ++i) {
            if (shared_array[i] == -1) {
                shared_array[i] = value;
                fprintf(producers_activity_file, "Producer %d -- produced item %d with the value %d\n", producer_id, production_number, value);
                break;
            }
        }

        sem_post(&mutex);
        sem_post(&full_sem);
    }

    return NULL;
}

void *consumer(void *arg) {
    int consumer_id = *((int *)arg);

    while (1) {
        sem_wait(&full_sem);
        sem_wait(&mutex);

        int index = -1;
        for (int i = 0; i < ARRAY_SIZE; ++i) {
            if (shared_array[i] != -1) {
                index = i;
                break;
            }
        }

        if (index == -1) {
            sem_post(&mutex);
            sem_post(&full_sem);
            break;
        }

        int value = shared_array[index];
        shared_array[index] = -1;

        printf("Consumer %d -- consumed item %d with the value %d\n", consumer_id, index + 1, value);
        fprintf(consumers_activity_file, "Consumer %d -- consumed item %d with the value %d\n", consumer_id, index + 1, value);

        sem_post(&mutex);
        sem_post(&empty_sem);
    }

    return NULL;
}

int main() {
    sem_init(&mutex, 0, 1);
    sem_init(&full_sem, 0, 0);
    sem_init(&empty_sem, 0, ARRAY_SIZE);

    for (int i = 0; i < ARRAY_SIZE; ++i) {
        shared_array[i] = -1;
    }

    producers_activity_file = fopen("Producers_activity.txt", "w");
    consumers_activity_file = fopen("Consumers_activity.txt", "w");

    pthread_t producer_threads[NUM_PRODUCERS];
    int producer_ids[NUM_PRODUCERS];
    for (int i = 0; i < NUM_PRODUCERS; ++i) {
        producer_ids[i] = i + 1;
        pthread_create(&producer_threads[i], NULL, producer, &producer_ids[i]);
    }

    pthread_t consumer_threads[NUM_CONSUMERS];
    int consumer_ids[NUM_CONSUMERS];
    for (int i = 0; i < NUM_CONSUMERS; ++i) {
        consumer_ids[i] = i + 1;
        pthread_create(&consumer_threads[i], NULL, consumer, &consumer_ids[i]);
    }

    for (int i = 0; i < NUM_PRODUCERS; ++i) {
        pthread_join(producer_threads[i], NULL);
    }

    for (int i = 0; i < NUM_CONSUMERS; ++i) {
        sem_post(&full_sem);
    }

    for (int i = 0; i < NUM_CONSUMERS; ++i) {
        pthread_join(consumer_threads[i], NULL);
    }

    fclose(producers_activity_file);
    fclose(consumers_activity_file);

    sem_destroy(&mutex);
    sem_destroy(&full_sem);
    sem_destroy(&empty_sem);

    return 0;
}
Editor is loading...
Leave a Comment