zoukankan      html  css  js  c++  java
  • 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);
    }
    
  • 相关阅读:
    windows下使用mingw编译出ffplay(简化版)
    Linux中查看GNOME版本号
    Linux操作系统入门学习总结(2015.10)
    c++11并发机制
    CentOS 7修改管理员密码
    windows下批量杀死进程
    ffmpeg抓屏输出的设置
    User-Defined-Literal自定义字面量
    GitHub支持的Markdown语法 GitHub Flavored Markdown
    c++11支持类数据成员的初始化
  • 原文地址:https://www.cnblogs.com/justsong/p/12219775.html
Copyright © 2011-2022 走看看