免费预览已结束,剩余1页可下载查看
下载本文档
版权说明:本文档由用户提供并上传,收益归属内容提供方,若内容存在侵权,请进行举报或认领
文档简介
用多线程同步方法解决睡眠理发师问题( Sleeping-Barber Problem)1 操作系统: Linux2 程序设计语言:C语言3 设有1个理发师5把椅子(另外还有一把理发椅),几把椅子可用连续存储单元.1技术要求:1)为每个理发师顾客产生一个线程,设计正确的同步算法2)每个顾客进入理发室后,即时显示“ Entered” 及其线程自定义标识,还同时显示理发室共有几名顾客及其所坐的位置。3)至少有10 个顾客,每人理发至少3秒钟。4)多个顾客须共享操作函数代码。提示: (1) 连续存储区可用数组实现。(2) 编译命令可用:gcc -lpthread -o 目标文件名源文件名(3) 多线程编程方法参见附件。)详细代码#include #include #include #include #include #include #include #include #define n 5 time_t end_time; sem_t mutex, customers, barbers; int count = 0; int chair 5 = -1, -1, -1, -1, -1 ; void barber(void *arg) while (time (NULL) 0) while (count 0) sem_wait(&customers); sem_wait(&mutex); count-; printf (the barber is cutting hair, count is : %dn, count); sem_post(&mutex); sem_post(&barbers); sleep(3); void customer (void *arg) int i ,id= 0, num=0; while (time (NULL) end_time) sem_wait(&mutex); if (count n) count+; num= count % 5; num+; printf(customer entered:the customer %s comes in and sits at %d the chair count is: %dn, (char *)arg, num, count); sem_post(&mutex); sem_post(&customers); sem_wait(&barbers); else sem_post(&mutex); sleep(2); int main (int argc, char *argv) pthread_t id1, id2, id3, id4, id5, id6, id7, id8, id9, id10, id11; int ret= 0; int i; end_time = time (NULL) + 30; sem_init (&mutex, 0, 1); ret = sem_init (&barbers, 0, 1); for (i =0;i5;i+) chair i = -1; if (0!= ret) perror(sem init); ret= pthread_create (&id11, NULL, (void*)barber,id11); if (0!= ret) perror(create barber); ret = pthread_create ( &id1,NULL,(void*)customer, id1); if (0!= ret) perror(create customers); ret = pthread_create(&id2, NULL, (void*)customer, id2); if (0!=ret) perror(create customers); ret = pthread_create(&id3, NULL, (void*)customer, id3); if (0!=ret) perror(create customers); ret = pthread_create(&id4, NULL, (void*)customer, id4); if (0!=ret) perror(create customers); ret = pthread_create(&id5, NULL, (void*)customer, id5); if(0!=ret) perror(create custmers); ret = pthread_create(&id6, NULL, (void*)customer, id6); if (0!= ret) perror(create customers); ret = pthread_create ( &id7,NULL,(void*)customer, id7); if (0!= ret) perror(create customers); ret = pthread_create(&id8, NULL, (void*)customer, id8); if (0!=ret) perror(create customers); ret = pthread_create(&id9, NULL, (void*)customer, id9); if (0!=ret) perror(create customers); ret = pthread_create(&id10, NULL, (void*)customer, id10); if (0!=ret) perror(create customers); pthread_join(id1, NULL); pthread_join(id2, NULL); pthread_join(id3, NULL); pthread_join(id4, NULL); pthread_join(id5, NULL); pthread_join(id6, NULL); pthread_jo
温馨提示
- 1. 本站所有资源如无特殊说明,都需要本地电脑安装OFFICE2007和PDF阅读器。图纸软件为CAD,CAXA,PROE,UG,SolidWorks等.压缩文件请下载最新的WinRAR软件解压。
- 2. 本站的文档不包含任何第三方提供的附件图纸等,如果需要附件,请联系上传者。文件的所有权益归上传用户所有。
- 3. 本站RAR压缩包中若带图纸,网页内容里面会有图纸预览,若没有图纸预览就没有图纸。
- 4. 未经权益所有人同意不得将文件中的内容挪作商业或盈利用途。
- 5. 人人文库网仅提供信息存储空间,仅对用户上传内容的表现方式做保护处理,对用户上传分享的文档内容本身不做任何修改或编辑,并不能对任何下载内容负责。
- 6. 下载文件中如有侵权或不适当内容,请与我们联系,我们立即纠正。
- 7. 本站不保证下载资源的准确性、安全性和完整性, 同时也不承担用户因使用这些下载资源对自己和他人造成任何形式的伤害或损失。
最新文档
- 初中地理教材考试真题及答案
- 拖拉机涂装加工生产线操作调整工岗前操作能力考核试卷含答案
- 加气混凝土大板拼装工安全意识水平考核试卷含答案
- 2025重庆巫山县人力社保局公益性岗位招聘2人笔试考试备考试题及答案解析
- 有色液固分离工安全生产意识水平考核试卷含答案
- 2025华电集团直属抽蓄工程管理公司招聘20人考试笔试模拟试题及答案解析
- 水面保洁员创新应用考核试卷含答案
- 2025贵州遵义市赤水市恒迅建筑工程有限公司项目管理人员聘任招聘笔试历年参考题库附带答案详解
- 锅炉运行值班员操作安全水平考核试卷含答案
- 2025江西长旅数智科技有限公司拟人员笔试历年参考题库附带答案详解
- 中医诊断四诊合参
- 武汉万科商品房交付标准化工作手册2.0版
- 食品安全考试试题及答案2021
- 郦道元《水经注·序》原文翻译注释与鉴赏
- 数独题目中级90题(后附答案)
- 西门子s71500系列系统手册
- 腹直肌分离康复(产后康复课件PPT)
- 携手共育 静待花开 家长会课件
- 酒驾处罚书格式(标准版)
- 灭火器每月定期检查及记录(卡)表
- 土地整理平整工程外观质量评定项目表
评论
0/150
提交评论