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.\n", 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.\n", 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.\n", 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.\n", 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.\n", 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.\n", 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.\n", 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.\n", tm.tm_hour, tm.tm_min, tm.tm_sec, pthread_self());
    sem_post(&wmutex);
}

原文地址:https://www.cnblogs.com/justsong/p/12219775.html

时间: 2024-09-30 17:21:29

IPC 经典问题:Reader & Writer Problem的相关文章

Stream,Reader/Writer,Buffered的区别(1)

Stream: 是字节流形式,exe文件,图片,视频等.支持8位的字符,用于 ASCII 字符和二进制数据. Reader/Writer: 是字符流,文本文件,XML,txt等,用于16位字符,也就是Unicode. Buffered:放到缓存区里为了读写提高效率,为read提供了高效的方法.InputStream的read会出现堵塞. InputStream: InputStream的子类: 1.ByteArrayInputStream: 就是因为InputStream 是不能被缓存,然后重复

IPC 经典问题:Sleeping Barber Problem

完整代码实现: #include <stdio.h> #include <unistd.h> #include <time.h> #include <stdlib.h> #include <pthread.h> #include <semaphore.h> #define CUSTOMER_NUMBER 20 void *customer(void *param); void *barber(void *param); int sea

02_IO操作的基本规律(InputStream,OutputStream,Reader,Writer,FileReader,FileWriter,BufferedReader,BufferedWri

 模拟BufferedInputStream,编写一个类 package toto.IO; import java.io.IOException; import java.io.InputStream; class MyBufferedInputStream{ private InputStream in; private byte[] buf = new byte[1024*4]; private int pos = 0,count = 0; MyBufferedInputStream(I

Stream,Reader/Writer,Buffered的区别(2)

Reader: Reader的子类: 1.BufferedReader: FileReader 没有提供读取文本行的功能,BufferedReader能够指定缓冲区大小,包装了read方法高效读取字符流.提供字符.数组和行的高效读取. 开发文档中的介绍: 从字符输入流中读取文本,缓冲各个字符,从而实现字符.数组和行的高效读取.可以指定缓冲区的大小,或者可使用默认的大小.大多数情况下,默认值就足够大了. 通常,Reader 所作的每个读取请求都会导致对底层字符或字节流进行相应的读取请求. 因此,建

multithreading - Reader/Writer Locks in C++

You Only Need To Note This: only 1 single thread can acquire an upgrade_lock at one time. others are very straightforward. 96 vote 1800 INFORMATION is more or less correct, but there are a few issues I wanted to correct. boost::shared_mutex _access;v

系统学习 Java IO (十五)----字符读写 Reader/Writer 其他子类

目录:系统学习 Java IO---- 目录,概览 跟踪行号的缓冲字符输入流 LineNumberReader LineNumberReader 类是一个 BufferedReader ,用于跟踪读取字符的行号.行号从 0 开始.每当 LineNumberReader 在包装的 Reader 返回的字符中遇到行终止符时,行号递增. 可以通过调用 getLineNumber() 方法获取当前行号,也可以通过调用 setLineNumber() 方法设置当前行号. 注意设置的行号不会改变实际的文件指

RFID Reader Writer Detector Emulator Sniffer

How does RFID tag technology works RFID is so called Radio Frequency Identification system which consists of two main parts: transmitter and receiver. The labels, access cards and even passports in some countries they have RFID transponders integrate

信号量解决写者优先&amp;读者优先&amp;公平竞争(reader writer)

先说问题: 这里的rand都是伪随机.解决也很简单,srand即可.内容懒得改了~~ 描述及思路: 代码: 运行结果: 读者优先: 效果图: 公平竞争: 只有读者写者线程做了一些修改.增加的信号量que.sem_init(&que,0,1). 效果: 原文地址:https://www.cnblogs.com/lqerio/p/11117657.html

经典线程同步问题

生产者消费者问题 读者作家问题 哲学家吃饭问题 生产者消费者问题 http://en.wikipedia.org/wiki/Producer%E2%80%93consumer_problem 分别用锁.信号量.同步监视器模拟的例子. package thread; import java.util.Random; import java.util.concurrent.Semaphore; import java.util.concurrent.locks.Condition; import j