



免费预览已结束,剩余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年中国化工用有机硅产品行业市场分析及投资价值评估前景预测报告
- 2025年中国护理和孕妇文胸行业市场分析及投资价值评估前景预测报告
- 2025年河北科技工程职业技术大学选聘工作人员52名考前自测高频考点模拟试题及答案详解1套
- 2025年江西省省直事业单位招聘工作人员笔试违纪违规处理意见模拟试卷及1套完整答案详解
- 2025江苏苏州市吴江区引进教育重点紧缺人才12人模拟试卷及完整答案详解一套
- 2025年南平政和县卫健系统事业单位公开招聘紧缺急需专业工作人员模拟试卷完整答案详解
- 2025年南平市供电服务有限公司招聘52人模拟试卷完整参考答案详解
- 2025福建省福州第十八中学招聘工作人员1人考前自测高频考点模拟试题及参考答案详解一套
- 2025湖南邵阳市洞口县黄桥镇中心卫生院面向社会公开招聘编外合同制影像(医师)技师考前自测高频考点模拟试题有答案详解
- 2025第二季度重庆医科大学附属大学城医院临床医技科室人员招聘考前自测高频考点模拟试题带答案详解
- 2025年10月“江南十校”2026届新高三第一次综合素质检测 化学试卷(含答案详解)
- 桩基冲击钻机施工方案
- 2025年七年级上学期语文第一个月月考试卷含答案
- 屠宰兽医检验考试题及答案
- 月考试卷讲评课课件
- 读书分享读书分享哈利波特
- 游戏:看表情符号猜成语PPT
- 影视鉴赏-第一章-影视鉴赏的基本概念
- 电厂安全生产运行管理培训课件
- 输液室运用PDCA降低静脉输液患者外渗的发生率品管圈(QCC)活动成果
- 数星星的孩子习题精选及答案
评论
0/150
提交评论