任务描述:
0 推荐在openEuer上实现 1 描述操作系统中“读者-写者”问题,理解问题的本质,提交你理解或查找到的文本资料 2 利用多线程完成reader 和writer 3 在main中测试若干个reader 和writer的测试,提交截图说明代码的正确性
- 如果一个进程正在某个文件上写入内容,而另一个进程也同时开始在同一文件上写入内容,则系统将进入不一致状态。在特定的时间仅应允许一个进程更改文件中存在的数据的值。
- 另一个问题是,如果一个进程正在读取文件,而另一个进程同时在同一文件上写入,则可能会导致读取不干净,因为在该文件上写入的进程会更改文件的值,但是读取该文件的过程将读取文件中存在的旧值。因此,应避免这种情况。
1.问题描述
有两组并发进程:读者和写者,共享一个文件F,要求:
- 允许多个读者同时执行读操作
- 任一写者在完成写操作之前不允许其它读者或写者工作
- 写者执行写操作前,应让已有的写者和读者全部退出
2.问题分析
- 共享数据(文件、记录)由Reader和Writer是两组并发进程共享
- Reader和Writer两组并发进程
- 同组的Reader进程可同时对文件进行读,不用互斥
- 写-写互斥
- 读-写互斥
对于线程,有以下流程:定义--创建初始化-线程运行函数--线程退出
对于信号量,有以下流程:定义--创建初始化--信号量wait和signal--信号量销毁
代码实现
实现多读者(多线程)操作
#include <stdio.h> #include <stdlib.h> #include <pthread.h> #include <semaphore.h> #include <unistd.h> sem_t rmutex,wmutex; static void *readerThread(void *arg); static void *reader3Thread(void *arg); static void *reader2Thread(void *arg); static void *writerThread(void *arg); int readcount = 0; int n = 0; int nowLen = 1; char contentArticle[10][100]; int main(){ pthread_t readerTidp,writerTidp,reader3Tidp,reader2Tidp; void *retval; if(sem_init(&rmutex,0,1)==-1||sem_init(&wmutex,0,1)==-1){ printf("sem_init error\n"); return -1; }//init semaphore if(pthread_create(&readerTidp,NULL,readerThread,NULL) !=0||pthread_create(&writerTidp,NULL,writerThread,NULL) !=0||pthread_create(&reader3Tidp,NULL,reader3Thread,NULL) !=0||pthread_create(&reader2Tidp,NULL,reader2Thread,NULL) !=0){ printf("pthread_create error\n"); return -2; }//init pthread pthread_join(readerTidp,&retval); pthread_join(reader3Tidp,&retval); pthread_join(reader2Tidp,&retval); pthread_join(writerTidp,&retval); sem_destroy(&rmutex); sem_destroy(&wmutex); return 0; } static void *readerThread(void *arg){ for(int i = 0;i < 10;i++) { sem_wait(&rmutex); if(readcount == 0)sem_wait(&wmutex); readcount = readcount+1; sem_post(&rmutex); //read operatiom printf("\n\nI'm reader first Reader thread :...the global variable n equals to %d\n",n); for(int j = 0;j < nowLen-1;j++) { for(int k = 0;k < 26;k++) printf("%c",contentArticle[j][k]); printf("\n"); } printf("now the count 0f reader is %d\n",readcount); printf("now the length 0f content is %d\n",nowLen-1); sleep(5); sem_wait(&rmutex); readcount = readcount-1; if(readcount == 0)sem_post(&wmutex); sem_post(&rmutex); sleep(1); } } static void *reader3Thread(void *arg){ for(int i = 0;i < 10;i++) { sem_wait(&rmutex); if(readcount == 0)sem_wait(&wmutex); readcount = readcount+1; sem_post(&rmutex); //read operatiom printf("\n\nI'm reader third Reader thread :...the global variable n equals to %d\n",n); for(int j = 0;j < nowLen-1;j++) { for(int k = 0;k < 26;k++) printf("%c",contentArticle[j][k]); printf("\n"); } printf("now the count 0f reader is %d\n",readcount); printf("now the length 0f content is %d\n",nowLen-1); sleep(5); sem_wait(&rmutex); readcount = readcount-1; if(readcount == 0)sem_post(&wmutex); sem_post(&rmutex); sleep(8); } } static void *reader2Thread(void *arg){ for(int i = 0;i < 10;i++) { sem_wait(&rmutex); if(readcount == 0)sem_wait(&wmutex); readcount = readcount+1; sem_post(&rmutex); //read operatiom printf("\n\nI'm reader second Reader thread :...the global variable n equals to %d\n",n); for(int j = 0;j < nowLen-1;j++) { for(int k = 0;k < 26;k++) printf("%c",contentArticle[j][k]); printf("\n"); } printf("now the count 0f reader is %d\n",readcount); printf("now the length 0f content is %d\n",nowLen-1); sem_wait(&rmutex); readcount = readcount-1; if(readcount == 0)sem_post(&wmutex); sem_post(&rmutex); sleep(4); } } static void *writerThread(void *arg){ for(int i = 0;i < 10;i++) { sem_wait(&wmutex); //writer operation n = n+1; for(int k = 0;k < 26;k++) contentArticle[nowLen-1][k] = 'z'-k; nowLen++; printf("\n\nWriter thread :writing opration the global variable n equals to %d \n",n); sleep(2); sem_post(&wmutex); sleep(3); } }
代码运行截图如下:
标签:readcount,int,void,写者,线程,printf,rmutex,sem,多线程 From: https://www.cnblogs.com/syf0105/p/16872908.html