Skip to content
TheChatArchiveThe Chat Archive
gpt-4-0314· openaiHugging Face Suzhen/CodeChat

编写一个写者优先解决读者写者问题的程序,其中读者和写者均是多个进程,用信号量作为同步互斥机制。使用C语言

  1. User

    编写一个写者优先解决读者写者问题的程序,其中读者和写者均是多个进程,用信号量作为同步互斥机制。使用C语言

  2. Assistant
    #include <stdio.h>
    #include <stdlib.h>
    #include <pthread.h>
    #include <semaphore.h>
    #include <unistd.h>
    
    // number of readers and writers
    #define NUM_READERS 5
    #define NUM_WRITERS 2
    
    // shared data
    int data = 0;
    
    // semaphores
    sem_t mutex, read_count_access, write_count_access, write;
    int read_count = 0, write_count = 0;
    
    void *reader(void *rno)
    {
        int r_no = *((int *)rno);
    
        // Start Reading
        sem_wait(&read_count_access);
        read_count++;
        if (read_count == 1)
            sem_wait(&write);
        sem_post(&read_count_access);
    
        printf("Reader %d: reads data %d\n", r_no, data);
    
        // Finish Reading
        sem_wait(&read_count_access);
        read_count--;
        if (read_count == 0)
            sem_post(&write);
        sem_post(&read_count_access);
    
        pthread_exit(NULL);
    }
    
    void *writer(void *wno)
    {
        int w_no = *((int *)wno);
    
        sem_wait(&write_count_access);
        write_count++;
        if (write_count == 1)
            sem_wait(&mutex);
        sem_post(&write_count_access);
    
        // start writing
        sem_wait(&write);
        data = rand() % 100; // generate random data to write
        printf("Writer %d: writes data %d\n", w_no, data);
        sem_post(&write);
    
        sem_wait(&write_count_access);
        write_count--;
        if (write_count == 0)
            sem_post(&mutex);
        sem_post(&write_count_access);
    
        pthread_exit(NULL);
    }
    
    int main()
    {
        srand(time(NULL));
        pthread_t readers[NUM_READERS], writers[NUM_WRITERS];
        int reader_id[NUM_READERS], writer_id[NUM_WRITERS];
    
        sem_init(&mutex, 0, 1);
        sem_init(&write, 0, 1);
        sem_init(&read_count_access, 0, 1);
        sem_init(&write_count_access, 0, 1);
    
        // creating readers and writers
        for (int i = 0; i < NUM_READERS; i++)
        {
            reader_id[i] = i + 1;
            pthread_create(&readers[i], NULL, (void *)reader, &reader_id[i]);
        }
        for (int i = 0; i < NUM_WRITERS; i++)
        {
            writer_id[i] = i + 1;
            pthread_create(&writers[i], NULL, (void *)writer, &writer_id[i]);
        }
    
        // waiting for threads to finish
        for (int i = 0; i < NUM_READERS; i++)
            pthread_join(readers[i], NULL);
        for (int i = 0; i < NUM_WRITERS; i++)
            pthread_join(writers[i], NULL);
    
        // destroy semaphores
        sem_destroy(&mutex);
        sem_destroy(&write);
        sem_destroy(&read_count_access);
        sem_destroy(&write_count_access);
    
        return 0;
    }
    

    这个程序使用4个信号量 mutex,write,read_count_access 和 write_count_access作为同步互斥机制。其中:

    • mutex用于保护write_count,以便正确地实现写者优先
    • write用于保证读者和写者之间的互斥访问共享数据
    • read_count_access用于保护read_count
    • write_count_access用于保护write_count

    每个读者线程都在一个循环中,首先申请read_count信号量,开始读数据,然后发布read_count信号量并处理读到的数据。 当读者数量从0变为1时会申请write信号量,以阻止写者访问共享数据。 当读者数量从1降至0时,释放write信号量,允许写者访问共享数据。

    每个写者线程都在一个循环中,首先申请write_count信号量,然后开始写数据,并发布write_count 信号量。 当写者数量从0变为1时,会申请mutex信号量,以阻止新的读者进入。 当写者数量从1降至0时,释放mutex信号量,允许读者访问共享数据。

  3. User

    用线程实现睡觉的理发师问题,(同步互斥方式采用信号量或mutex方式均可) 理发师问题的描述:一个理发店接待室有n张椅子,工作室有1张椅子;没有顾客时,理发师睡觉;第一个顾客来到时,必须将理发师唤醒;顾客来时如果还有空座的话,他就坐在一个座位上等待;如果顾客来时没有空座位了,他就离开,不理发了;当理发师处理完所有顾客,而又没有新顾客来时,他又开始睡觉。 使用c语言

  4. Assistant
    #include <stdio.h>
    #include <unistd.h>
    #include <pthread.h>
    #include <semaphore.h>
    #include <time.h>
    
    // number of chairs in waiting room
    #define NUM_CHAIRS 3
    
    // shared data
    int waiting = 0; // number of customers waiting
    int is_sleeping = 1;
    
    // semaphores
    sem_t barber_ready;
    pthread_mutex_t waiting_mutex;
    
    // function prototypes
    void *barber(void *_);
    void *customer(void *_);
    
    int main()
    {
        srand(time(NULL));
    
        // initialize semaphores
        sem_init(&barber_ready, 0, 0);
        pthread_mutex_init(&waiting_mutex, NULL);
    
        // create barber thread
        pthread_t b_thread;
        pthread_create(&b_thread, NULL, barber, NULL);
    
        // create customer threads
        while (1)
        {
            // simulate a random delay before new customer arrives
            sleep(rand() % 5 + 1);
    
            pthread_t c_thread;
            pthread_create(&c_thread, NULL, customer, NULL);
        }
    
        return 0;
    }
    
    void *barber(void *_)
    {
        while (1)
        {
            pthread_mutex_lock(&waiting_mutex);
            int customers_waiting = waiting;
            pthread_mutex_unlock(&waiting_mutex);
    
            if (customers_waiting > 0)
            {
                // Customer is waiting, so wake up the barber
                is_sleeping = 0;
                sem_post(&barber_ready);
    
                // Simulate hair cutting
                printf("Barber: cutting hair...\n");
                sleep(rand() % 3 + 1);
                printf("Barber: finished cutting hair, ready for another customer.\n");
    
                pthread_mutex_lock(&waiting_mutex);
                waiting--; // decrease the number of customers waiting
                pthread_mutex_unlock(&waiting_mutex);
            }
            else
            {
                printf("Barber: no customers, going to sleep...\n");
                is_sleeping = 1;
                sleep(1);
            }
        }
    
        pthread_exit(NULL);
    }
    
    void *customer(void *_)
    {
        // Create a unique ID for this customer
        unsigned long id = pthread_self();
    
        // Enter the waiting room
        pthread_mutex_lock(&waiting_mutex);
    
        if (waiting < NUM_CHAIRS)
        {
            printf("Customer %lu: arrived and waiting.\n", id);
            waiting++; // increase the number of customers waiting
            pthread_mutex_unlock(&waiting_mutex);
    
            if (!is_sleeping)
            {
                printf("Customer %lu: waking up the barber.\n", id);
            }
    
            // Wait for the barber to be ready
            sem_wait(&barber_ready);
    
            // Get the hair cut
            printf("Customer %lu: getting a haircut.\n", id);
        }
        else
        {
            printf("Customer %lu: no vacant chairs, leaving...\n", id);
            pthread_mutex_unlock(&waiting_mutex);
        }
    
        pthread_exit(NULL);
    }
    

    这个程序使用信号量和互斥量来解决睡觉的理发师问题。代码中,等待室有NUM_CHAIRS=3张空余椅子。当没有顾客时,理发师进入睡眠状态;当有一个顾客来时,理发师唤醒。如果有空椅子,顾客在椅子上等待;否则,顾客离开。

    有两个线程:barber线程表示理发师,customer线程表示顾客。主线程不断生成新的customer线程,每次在1到5秒内随机等待。

    barber线程循环检查是否有等待的顾客。如果有,理发师唤醒,并准备好为顾客理发。然后进行模拟理发过程,之后理发师将等待顾客计数减一。如果没有顾客等待,理发师就开始睡觉。

    customer线程首先尝试进入等待室。为此,它必须获取等待互斥锁。如果有空置的椅子,它将等待理发师准备好,然后处理理发过程。如果没有空置的椅子,它将释放锁并退出。

    需要注意的是,理发师线程和顾客线程之间的同步是通过barber_ready信号量完成的,而访问等待顾客数量的互斥是通过一个互斥量waiting_mutex完成的。我们选择互斥量,因为它更适合在这种情况下实现基本的互斥。

Responses were produced in different, uncontrolled contexts (system prompts, settings, prior turns). This is not a benchmark.