Untitled

 avatar
unknown
plain_text
a year ago
3.9 kB
3
Indexable
##include <stdio.h>
#include <stdlib.h>
#include <pthread.h>
#include <semaphore.h>
#include <unistd.h>

// Global variables
int num_students;
int num_glasses;
int glasses_available = 0;
int students_waiting = 0;
int students_finished = 0;

pthread_mutex_t mutex;
sem_t student_sem;
sem_t coordinator_sem;

// Function prototypes
void* student_thread(void* arg);
void* coordinator_thread(void* arg);

int main(int argc, char* argv[]) {
    if (argc != 3) {
        fprintf(stderr, "Usage: %s <total_students> <total_glasses>\n", argv[0]);
        return 1;
    }

    num_students = atoi(argv[1]);
    num_glasses = atoi(argv[2]);

    pthread_mutex_init(&mutex, NULL);
    sem_init(&student_sem, 0, 0);
    sem_init(&coordinator_sem, 0, 1);

    pthread_t coordinator;
    pthread_t* student_threads = (pthread_t*)malloc(num_students * sizeof(pthread_t));

    // Create coordinator thread
    pthread_create(&coordinator, NULL, coordinator_thread, NULL);

    // Create student threads
    for (int i = 0; i < num_students; i++) {
        pthread_create(&student_threads[i], NULL, student_thread, (void*)(long)i);
    }

    // Join coordinator thread
    pthread_join(coordinator, NULL);

    // Join student threads
    for (int i = 0; i < num_students; i++) {
        pthread_join(student_threads[i], NULL);
    }

    // Clean up resources
    free(student_threads);
    pthread_mutex_destroy(&mutex);
    sem_destroy(&student_sem);
    sem_destroy(&coordinator_sem);

    printf("All students have finished watching the movie.\n");

    return 0;
}

void* student_thread(void* arg) {
    int student_id = (int)(long)arg;

    printf("Student %d is waiting for 3D glasses.\n", student_id);

    sem_wait(&coordinator_sem); // Request permission from the coordinator
    pthread_mutex_lock(&mutex); // Lock the mutex

    if (glasses_available > 0) {
        // If 3D glasses are available, take one
        glasses_available--;
        printf("Student %d is watching the 3D movie with glasses. Glasses available: %d\n", student_id, glasses_available);
        pthread_mutex_unlock(&mutex); // Unlock the mutex
    } else {
        // If no 3D glasses are available, wait
        students_waiting++;
        pthread_mutex_unlock(&mutex); // Unlock the mutex
        sem_post(&student_sem); // Signal the coordinator that a student is waiting
        sem_wait(&coordinator_sem); // Wait for the coordinator's signal

        // Student has been signaled by the coordinator, indicating glasses are available
        pthread_mutex_lock(&mutex); // Lock the mutex
        students_waiting--;
        glasses_available--;
        printf("Student %d is watching the 3D movie with glasses. Glasses available: %d\n", student_id, glasses_available);
        pthread_mutex_unlock(&mutex); // Unlock the mutex
    }

    // Simulate watching the movie (you can replace this with actual movie-watching code)
    sleep(5);

    pthread_mutex_lock(&mutex); // Lock the mutex
    glasses_available++; // Return the 3D glasses
    printf("Student %d returned the 3D glasses. Glasses available: %d\n", student_id, glasses_available);
    students_finished++;
    pthread_mutex_unlock(&mutex); // Unlock the mutex

    if (students_finished == num_students) {
        sem_post(&student_sem); // Signal coordinator that all students have finished
    }

    pthread_exit(NULL);
}

void* coordinator_thread(void* arg) {
    while (1) {
        sem_wait(&student_sem); // Wait for a student to signal
        pthread_mutex_lock(&mutex); // Lock the mutex

        if (students_waiting > 0) {
            // If there are waiting students, signal one of them
            sem_post(&coordinator_sem);
        }

        pthread_mutex_unlock(&mutex); // Unlock the mutex

        if (students_finished == num_students) {
            // All students have finished, terminate coordinator
            pthread_exit(NULL);
        }
    }
}
Editor is loading...
Leave a Comment