linux理发师多线程问题

上传人:第*** 文档编号:32688764 上传时间:2018-02-12 格式:DOC 页数:5 大小:197.84KB
返回 下载 相关 举报
linux理发师多线程问题_第1页
第1页 / 共5页
linux理发师多线程问题_第2页
第2页 / 共5页
linux理发师多线程问题_第3页
第3页 / 共5页
linux理发师多线程问题_第4页
第4页 / 共5页
linux理发师多线程问题_第5页
第5页 / 共5页
亲,该文档总共5页,全部预览完了,如果喜欢就下载吧!
资源描述

《linux理发师多线程问题》由会员分享,可在线阅读,更多相关《linux理发师多线程问题(5页珍藏版)》请在金锄头文库上搜索。

1、用多线程同步方法解决睡眠理发师问题( Sleeping-Barber Problem)1 操作系统: Linux2 程序设计语言:C语言3 设有1个理发师5把椅子(另外还有一把理发椅),几把椅子可用连续存储单元.1技术要求:1)为每个理发师顾客产生一个线程,设计正确的同步算法2)每个顾客进入理发室后,即时显示“ Entered” 及其线程自定义标识,还同时显示理发室共有几名顾客及其所坐的位置。3)至少有10 个顾客,每人理发至少3 秒钟。4)多个顾客须共享操作函数代码。提示: (1) 连续存储区可用数组实现。(2) 编译命令可用:gcc -lpthread -o 目标文件名 源文件名(3) 多

2、线程编程方法参见附件。)详细代码#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( sem_wait( count-; printf (the

3、 barber is cutting hair, count is : %dn, count); sem_post( sem_post( sleep(3); void customer (void *arg) int i ,id= 0, num=0; while (time (NULL) end_time) sem_wait( 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, (ch

4、ar *)arg, num, count); sem_post( sem_post( sem_wait( else sem_post( 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 ( ret = sem_init ( for (i =0;i5;i+) chair i = -1; if (0!= ret) per

5、ror(sem init); ret= pthread_create ( if (0!= ret) perror(create barber); ret = pthread_create ( if (0!= ret) perror(create customers); ret = pthread_create( if (0!=ret) perror(create customers); ret = pthread_create( if (0!=ret) perror(create customers); ret = pthread_create( if (0!=ret) perror(crea

6、te customers); ret = pthread_create( if(0!=ret) perror(create custmers); ret = pthread_create( if (0!= ret) perror(create customers); ret = pthread_create ( if (0!= ret) perror(create customers); ret = pthread_create( if (0!=ret) perror(create customers); ret = pthread_create( if (0!=ret) perror(cre

7、ate customers); ret = pthread_create( 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_join(id7, NULL); pthread_join(id8, NULL); pthread_join(id9, NULL); pthread_join(id10, NULL); pthread_join(id11, NULL); exit(0); 运行结果如下

展开阅读全文
相关资源
相关搜索

当前位置:首页 > 中学教育 > 职业教育

电脑版 |金锄头文库版权所有
经营许可证:蜀ICP备13022795号 | 川公网安备 51140202000112号