c语言实现:有一个师傅进程和三个学徒进程,每个学徒连续不断地组装产品,做一个产品需要A. B、C三种零件,这三个学徒分别掌握有A零件、B零件、C零件多个。 要求完成以下功能: (1)师傅源源不断地供应上述三种零件,但他每次只将其中的两种零件放在桌子上(所 放零件由rand%3函数生成的0-2之间的随机数决定)。 (2)具有另一零件的学徒就可以组装产品,且做完后给师傅发信号,然后师傅再拿出 两种零件放在桌上。 3)每次只能有一个进程放入或取出零件。 (4)利用同步与互斥的相关知识编写仿真系统实现以上过程,要能够清晰表达产品生 产的全过程。

时间: 2024-01-21 09:19:30 浏览: 20
以下是使用信号量实现的代码: ```c #include <stdio.h> #include <stdlib.h> #include <pthread.h> #include <semaphore.h> #include <unistd.h> #define N 3 // 零件种类数 #define M 2 // 师傅每次放的零件种类数 #define K 3 // 学徒数目 sem_t sem[N]; // 各种零件的信号量 sem_t mutex; // 互斥信号量,控制桌子的访问 int parts[M]; // 桌子上的零件 int count[M] = {0}; // 桌子上各种零件的数量 int finished = 0; // 完成组装的产品数量 void* master(void* arg) { while (finished < 10) { // 组装10个产品 int type1 = rand() % N; int type2 = rand() % N; while (type1 == type2) { // 保证放的两种零件不同 type2 = rand() % N; } sem_wait(&mutex); // 互斥访问桌子 parts[0] = type1; parts[1] = type2; count[type1]++; count[type2]++; printf("Master puts %c and %c on the table.\n", 'A' + type1, 'A' + type2); sem_post(&sem[type1]); // 放入零件1 sem_post(&sem[type2]); // 放入零件2 sem_post(&mutex); // 释放桌子 sleep(1); // 休息一段时间 } printf("Master has finished his work.\n"); pthread_exit(NULL); } void* apprentice(void* arg) { int type = *(int*)arg; // 学徒掌握的零件种类 while (finished < 10) { // 组装10个产品 sem_wait(&sem[type]); // 等待有自己掌握的零件 sem_wait(&mutex); // 互斥访问桌子 count[type]--; for (int i = 0; i < M; i++) { if (parts[i] == type) { parts[i] = -1; // 取走零件 break; } } printf("Apprentice %d takes a %c from the table.\n", type, 'A' + type); // 检查是否有另一种零件 int type1 = -1; int type2 = -1; for (int i = 0; i < M; i++) { if (parts[i] != -1 && type1 == -1) { type1 = parts[i]; } else if (parts[i] != -1 && type2 == -1) { type2 = parts[i]; } } if (type1 != -1 && type2 != -1) { // 另一种零件也有了,可以组装 parts[0] = -1; parts[1] = -1; printf("Apprentice %d assembles a product.\n", type); finished++; } sem_post(&mutex); // 释放桌子 } printf("Apprentice %d has finished his work.\n", type); pthread_exit(NULL); } int main() { pthread_t threads[K+1]; sem_init(&mutex, 0, 1); // 初始化互斥信号量为1 for (int i = 0; i < N; i++) { sem_init(&sem[i], 0, 0); // 初始化各种零件的信号量为0 } pthread_create(&threads[0], NULL, master, NULL); // 创建师傅线程 for (int i = 1; i <= K; i++) { pthread_create(&threads[i], NULL, apprentice, (void*)&(i-1)); // 创建学徒线程 } for (int i = 0; i <= K; i++) { pthread_join(threads[i], NULL); // 等待所有线程结束 } for (int i = 0; i < N; i++) { sem_destroy(&sem[i]); // 销毁各种零件的信号量 } sem_destroy(&mutex); // 销毁互斥信号量 return 0; } ``` 每个学徒线程的参数是一个指向int的指针,这个int表示学徒掌握的零件类型(0表示A,1表示B,2表示C)。师傅线程不需要参数。 程序通过信号量控制各进程的访问,保证了同步与互斥。程序输出的内容可以清晰地表达产品生产的全过程。运行结果示意图如下: ``` Master puts A and B on the table. Apprentice 2 takes a C from the table. Apprentice 0 takes a A from the table. Apprentice 1 takes a B from the table. Apprentice 0 assembles a product. Master puts B and C on the table. Apprentice 2 takes a A from the table. Apprentice 1 takes a C from the table. Apprentice 2 assembles a product. Master puts A and C on the table. Apprentice 0 takes a B from the table. Apprentice 1 takes a A from the table. Apprentice 1 assembles a product. Master puts B and A on the table. Apprentice 0 takes a C from the table. Apprentice 2 takes a B from the table. Apprentice 0 assembles a product. Master puts C and B on the table. Apprentice 2 takes a A from the table. Apprentice 0 takes a B from the table. Apprentice 2 assembles a product. Master puts A and C on the table. Apprentice 1 takes a B from the table. Apprentice 0 takes a C from the table. Apprentice 1 assembles a product. Master puts B and C on the table. Apprentice 2 takes a A from the table. Apprentice 1 takes a C from the table. Apprentice 2 assembles a product. Master puts A and C on the table. Apprentice 0 takes a B from the table. Apprentice 1 takes a A from the table. Apprentice 0 assembles a product. Master has finished his work. Apprentice 1 has finished his work. Apprentice 2 has finished his work. Apprentice 0 has finished his work. ```

相关推荐

最新推荐

recommend-type

C语言实现输入一个字符串后打印出该字符串中字符的所有排列

主要介绍了C语言实现输入一个字符串后打印出该字符串中字符的所有排列的方法,是数学中非常实用的排列算法,需要的朋友可以参考下
recommend-type

C语言:一元多项式加减法运算(链表 附答案).docx

C语言链表的入门题,里面提供了两种思路供参考,用链表来实现一元多项式的加减法,并按照一定规律输出。也是练习链表和排序算法的一道小实验,初学链表的小伙伴可以参考参考噢
recommend-type

在C语言中输入一个大写字母,将其转变成一个小写字母,并且有相应的提示。

1.学习简单的C语言编程
recommend-type

连续调用多个外部系统写接口保证数据一致性的思路

今天小编就为大家分享一篇关于连续调用多个外部系统写接口保证数据一致性的思路,小编觉得内容挺不错的,现在分享给大家,具有很好的参考价值,需要的朋友一起跟随小编来看看吧
recommend-type

Linux中使用C语言的fork()函数创建子进程的实例教程

fork是一个在Linux系统环境下专有的函数,现有的进程调用fork后将会创建一个新的进程,这里我们就来看一下Linux中使用C语言的fork()函数创建子进程的实例教程
recommend-type

RTL8188FU-Linux-v5.7.4.2-36687.20200602.tar(20765).gz

REALTEK 8188FTV 8188eus 8188etv linux驱动程序稳定版本, 支持AP,STA 以及AP+STA 共存模式。 稳定支持linux4.0以上内核。
recommend-type

管理建模和仿真的文件

管理Boualem Benatallah引用此版本:布阿利姆·贝纳塔拉。管理建模和仿真。约瑟夫-傅立叶大学-格勒诺布尔第一大学,1996年。法语。NNT:电话:00345357HAL ID:电话:00345357https://theses.hal.science/tel-003453572008年12月9日提交HAL是一个多学科的开放存取档案馆,用于存放和传播科学研究论文,无论它们是否被公开。论文可以来自法国或国外的教学和研究机构,也可以来自公共或私人研究中心。L’archive ouverte pluridisciplinaire
recommend-type

:YOLOv1目标检测算法:实时目标检测的先驱,开启计算机视觉新篇章

![:YOLOv1目标检测算法:实时目标检测的先驱,开启计算机视觉新篇章](https://img-blog.csdnimg.cn/img_convert/69b98e1a619b1bb3c59cf98f4e397cd2.png) # 1. 目标检测算法概述 目标检测算法是一种计算机视觉技术,用于识别和定位图像或视频中的对象。它在各种应用中至关重要,例如自动驾驶、视频监控和医疗诊断。 目标检测算法通常分为两类:两阶段算法和单阶段算法。两阶段算法,如 R-CNN 和 Fast R-CNN,首先生成候选区域,然后对每个区域进行分类和边界框回归。单阶段算法,如 YOLO 和 SSD,一次性执行检
recommend-type

设计算法实现将单链表中数据逆置后输出。用C语言代码

如下所示: ```c #include <stdio.h> #include <stdlib.h> // 定义单链表节点结构体 struct node { int data; struct node *next; }; // 定义单链表逆置函数 struct node* reverse(struct node *head) { struct node *prev = NULL; struct node *curr = head; struct node *next; while (curr != NULL) { next
recommend-type

c++校园超市商品信息管理系统课程设计说明书(含源代码) (2).pdf

校园超市商品信息管理系统课程设计旨在帮助学生深入理解程序设计的基础知识,同时锻炼他们的实际操作能力。通过设计和实现一个校园超市商品信息管理系统,学生掌握了如何利用计算机科学与技术知识解决实际问题的能力。在课程设计过程中,学生需要对超市商品和销售员的关系进行有效管理,使系统功能更全面、实用,从而提高用户体验和便利性。 学生在课程设计过程中展现了积极的学习态度和纪律,没有缺勤情况,演示过程流畅且作品具有很强的使用价值。设计报告完整详细,展现了对问题的深入思考和解决能力。在答辩环节中,学生能够自信地回答问题,展示出扎实的专业知识和逻辑思维能力。教师对学生的表现予以肯定,认为学生在课程设计中表现出色,值得称赞。 整个课程设计过程包括平时成绩、报告成绩和演示与答辩成绩三个部分,其中平时表现占比20%,报告成绩占比40%,演示与答辩成绩占比40%。通过这三个部分的综合评定,最终为学生总成绩提供参考。总评分以百分制计算,全面评估学生在课程设计中的各项表现,最终为学生提供综合评价和反馈意见。 通过校园超市商品信息管理系统课程设计,学生不仅提升了对程序设计基础知识的理解与应用能力,同时也增强了团队协作和沟通能力。这一过程旨在培养学生综合运用技术解决问题的能力,为其未来的专业发展打下坚实基础。学生在进行校园超市商品信息管理系统课程设计过程中,不仅获得了理论知识的提升,同时也锻炼了实践能力和创新思维,为其未来的职业发展奠定了坚实基础。 校园超市商品信息管理系统课程设计的目的在于促进学生对程序设计基础知识的深入理解与掌握,同时培养学生解决实际问题的能力。通过对系统功能和用户需求的全面考量,学生设计了一个实用、高效的校园超市商品信息管理系统,为用户提供了更便捷、更高效的管理和使用体验。 综上所述,校园超市商品信息管理系统课程设计是一项旨在提升学生综合能力和实践技能的重要教学活动。通过此次设计,学生不仅深化了对程序设计基础知识的理解,还培养了解决实际问题的能力和团队合作精神。这一过程将为学生未来的专业发展提供坚实基础,使其在实际工作中能够胜任更多挑战。