• IPC 经典问题:Reader & Writer Problem


    完整代码实现:

    #include <stdio.h>
    #include <unistd.h>
    #include <time.h>
    #include <stdlib.h>
    #include <pthread.h>
    #include <semaphore.h>
    #define TOTAL_NUMBER 20
    void *writer(void *param);
    void *reader(void *param);
    
    int reader_num = 0;
    int writer_num = 0;
    int reader_mutex = 0;
    int unit[TOTAL_NUMBER] = {0, 1, 0, 0, 0, 1, 0, 1, 0, 1, 0, 0, 0, 1, 1, 1, 0, 0, 1, 0};
    
    sem_t wmutex;
    sem_t mutex;
    
    int main(int argc, char *argv[]) {
        sem_init(&mutex,0,1);
        sem_init(&wmutex,0,1);
    
        for (int i = 0; i < TOTAL_NUMBER; i++){
            sleep(1);
            time_t t = time(NULL);
            struct tm tm = *localtime(&t);
            if(unit[i] == 0){
                pthread_t thread_id; 
                pthread_create(&thread_id, NULL, reader, NULL);
                reader_num ++;
                printf("%d:%d:%d: Creating %dth reader.
    ", tm.tm_hour, tm.tm_min, tm.tm_sec, reader_num);
            }else{
                pthread_t thread_id; 
                pthread_create(&thread_id, NULL, writer, NULL);
                writer_num ++;
                printf("%d:%d:%d: Creating %dth writer.
    ", tm.tm_hour, tm.tm_min, tm.tm_sec, writer_num);
            }
        }
    }
    
    void *reader(void *param) {
        time_t t = time(NULL);
        struct tm tm = *localtime(&t);
        printf("%d:%d:%d: NO.%u reader requires reading.
    ", tm.tm_hour, tm.tm_min, tm.tm_sec, pthread_self());
    
        sem_wait(&mutex);
            reader_mutex ++;
            if(reader_mutex == 1){
                sem_wait(&wmutex);
            }
        sem_post(&mutex);
        // Read data
        t = time(NULL);
        tm = *localtime(&t);
        printf("%d:%d:%d: NO.%u reader begins to read.
    ", tm.tm_hour, tm.tm_min, tm.tm_sec, pthread_self());
        sleep(1);
        t = time(NULL);
        tm = *localtime(&t);
        printf("%d:%d:%d: End of NO.%u reader for reading.
    ", tm.tm_hour, tm.tm_min, tm.tm_sec, pthread_self());
        sem_wait(&mutex);
            reader_mutex --;
            if(reader_mutex == 0){
                sem_post(&wmutex);
            }
        sem_post(&mutex);
    }
    
    void *writer(void *param) {
        time_t t = time(NULL);
        struct tm tm = *localtime(&t);
        printf("%d:%d:%d: NO.%u writer requires writing.
    ", tm.tm_hour, tm.tm_min, tm.tm_sec, pthread_self());
    
        sem_wait(&wmutex);
        // Write data
        t = time(NULL);
        tm = *localtime(&t);
        printf("%d:%d:%d: NO.%u writer begins to write.
    ", tm.tm_hour, tm.tm_min, tm.tm_sec, pthread_self());
        sleep(6);
        t = time(NULL);
        tm = *localtime(&t);
        printf("%d:%d:%d: End of NO.%u writer for writing.
    ", tm.tm_hour, tm.tm_min, tm.tm_sec, pthread_self());
        sem_post(&wmutex);
    }
    
  • 相关阅读:
    Golang (Go语言) Mac OS X下环境搭建 环境变量配置 开发工具配置 Sublime Text 2
    网站状态保存方法
    学习MVC第一课:初识MVC
    ASP.NET MVC 中动态从路由中获取URL
    ASP.NET MVC2程序开发入门到精通系列课程01
    OpenCV 里的sigma 是多少
    日期大小比较
    安装完ODAC,出现ORA12560:TNS:协议适配器错误
    Spring+IBATIS+Struts2开发流程
    【转】SSH标准配置
  • 原文地址:https://www.cnblogs.com/justsong/p/12219775.html
Copyright © 2020-2023  润新知