IPC 经典问题:Sleeping Barber Problem
完整代码实现:
#include <stdio.h>
#include <unistd.h>
#include <time.h>
#include <stdlib.h>
#include <pthread.h>
#include <semaphore.h>
#define CUSTOMER_NUMBER 20
void *customer(void *param);
void *barber(void *param);
int seat_num = 5;
int interval[CUSTOMER_NUMBER] = {100, 100, 200, 100, 250, 400, 200, 100, 200, 700, 100, 200, 600, 100, 250, 400, 200, 100, 200, 700};
sem_t cust_ready;
sem_t barber_ready;
sem_t mutex;
int main(int argc, char *argv[]) {
pthread_t barberid;
pthread_t clientids[CUSTOMER_NUMBER];
sem_init(&mutex,0,1);
sem_init(&barber_ready,0,0);
sem_init(&cust_ready,0,0);
pthread_create(&barberid, NULL, barber, NULL);
for (int i = 0; i < CUSTOMER_NUMBER; i++){
usleep(interval[i]*1000);
time_t t = time(NULL);
struct tm tm = *localtime(&t);
pthread_create(&clientids[i], NULL, customer, NULL);
printf("%d:%d:%d: One customer comes, now there are %d seats left now\n", tm.tm_hour, tm.tm_min, tm.tm_sec, seat_num);
}
}
void *barber(void *param) {
int worktime = 500;
while(1) {
sem_wait(&cust_ready);
sem_wait(&mutex);
seat_num += 1;
time_t t = time(NULL);
struct tm tm = *localtime(&t);
printf("%d:%d:%d: Barber works, there are %d seats left now\n", tm.tm_hour, tm.tm_min, tm.tm_sec, seat_num);
usleep(worktime*1000);
t = time(NULL);
tm = *localtime(&t);
printf("%d:%d:%d: Barber has cut hair, there are %d seats left now\n", tm.tm_hour, tm.tm_min, tm.tm_sec, seat_num);
sem_post(&barber_ready);
sem_post(&mutex);
}
}
void *customer(void *param) {
sem_wait(&mutex);
if(seat_num > 0){
seat_num --;
time_t t = time(NULL);
struct tm tm = *localtime(&t);
printf("%d:%d:%d: One customer comes, now there are %d seats left now\n", tm.tm_hour, tm.tm_min, tm.tm_sec, seat_num);
sem_post(&cust_ready);
sem_post(&mutex);
sem_wait(&barber_ready);
t = time(NULL);
tm = *localtime(&t);
printf("%d:%d:%d: One customer leaves with haircut, now there are %d seats left now\n", tm.tm_hour, tm.tm_min, tm.tm_sec, seat_num);
} else {
time_t t = time(NULL);
struct tm tm = *localtime(&t);
printf("%d:%d:%d: One customer leaves with no haircut\n", tm.tm_hour, tm.tm_min, tm.tm_sec);
sem_post(&mutex);
}
}
IPC 经典问题:Sleeping Barber Problem的更多相关文章
- IPC 经典问题:Reader & Writer Problem
完整代码实现: #include <stdio.h> #include <unistd.h> #include <time.h> #include <stdl ...
- Classic IPC Problems 经典的进程间通信问题
The Producer-Consumer Problem Presenter Notes: 生产者消费者问题(英语:Producer-consumer problem),也称有限缓冲问题(英语:Bo ...
- EOF
Process to end of file 就是处理到文件的结束 以经典的 A + B Problem 为例 把每一行的两个数字加起来,然后打印出来,直到文件末尾 c 语言表示:
- 一次完整的从webshell到域控的探索之路
前言 内网渗透测试资料基本上都是很多大牛的文章告诉我们思路如何,但是对于我等小菜一直是云里雾里. 于是使用什么样的工具才内网才能畅通无阻,成了大家一直以来的渴求. 今天小菜我本着所有师傅们无私分享的精 ...
- Metasploit域渗透测试全程实录(终结篇)
本文作者:i春秋签约作家——shuteer 前言 内网渗透测试资料基本上都是很多大牛的文章告诉我们思路如何,但是对于我等小菜一直是云里雾里.于是使用什么样的工具才内网才能畅通无阻,成了大家一直以来的渴 ...
- 从hello world 说程序运行机制
转自:http://www.cnblogs.com/yanlingyin/archive/2012/03/05/2379199.html 开篇 学习任何一门编程语言,都会从hello world 开始 ...
- poj3311 TSP经典状压dp(Traveling Saleman Problem)
题目链接:http://poj.org/problem?id=3311 题意:一个人到一些地方送披萨,要求找到一条路径能够遍历每一个城市后返回出发点,并且路径距离最短.最后输出最短距离即可.注意:每一 ...
- HDU 1016 Prime Ring Problem(经典DFS+回溯)
Prime Ring Problem Time Limit: 4000/2000 MS (Java/Others) Memory Limit: 65536/32768 K (Java/Other ...
- Hdu 1016 Prime Ring Problem (素数环经典dfs)
Prime Ring Problem Time Limit: 4000/2000 MS (Java/Others) Memory Limit: 65536/32768 K (Java/Other ...
随机推荐
- 膜 zhouakngyang 宝典
持续更新! 注意:本文无 F12. about 周老师:怎么这么强! ZAKY 打 CF 大图:zaky cgr rk1 大图:zaky 传奇 \(1\) ZAKY 打 ATC ZAKY 切题
- Spring Cloud Alibaba (一): SpringCloud与SpringBoot版本选型
前言 近年SpringCloud与SpringBoot更新迭代非常频繁,导致我们对两者的版本选型非常的困难.若是版本选择有问题, 会导致开发中调试兼容性占用非常多的得必要时间. SpringClo ...
- 记:create-react-app暴露配置报错
上面主要是说 webpack 版本冲突 不是create-react-app本身的问题,需要手动解决. 解决办法: npm run eject // 显示所有的依赖项 如果运行出现类似这样的报错 Ar ...
- IDEA的基本操作——导入导出jar包
在使用Jmeter工具测试时,有时也需要导出jar包,测试对应功能,或者自己二次开发Jmeter工具,也是需要导出jar包的.既然经常用,所以就总结了下导入导出jar包的方法. 导入jar包 先打开i ...
- openstack高可用集群17-openstack集成Ceph准备
Openstack集成Ceph准备 Openstack环境中,数据存储可分为临时性存储与永久性存储. 临时性存储:主要由本地文件系统提供,并主要用于nova虚拟机的本地系统与临时数据盘,以及存储gla ...
- SQLServer多事务——事务嵌套
在ERP中,偶尔会有存储过程里面继续调用存储过程的情况 其中更有一些特殊的存储过程分别都使用了存储过程,大致可以分为下面几种情况: 1.平行事务,在多个事务中,任意一个成功则提交数据库,失败则各自RO ...
- Java 8 新特性 - Lambda表达式
Lambda表达式 vs 匿名类既然lambda表达式即将正式取代Java代码中的匿名内部类,那么有必要对二者做一个比较分析.一个关键的不同点就是关键字 this.匿名类的 this 关键字指向匿名类 ...
- Error:java: JDK isn't specified for module 'xxx'异常的解决方法
问题描述 博主启动的项目的时候出现了一个这样的异常 解决方法 打开左上角这个Project Structure
- java instanceof 判断是否是String
if(formbean.getBean().get("RZZGMCM") instanceof String){ formbean.getBean().put("RZZG ...
- 发送微信通知 java
//实现类@Service public class WeChatServiceImpl implements IWeChatService { @Override public WeChatSend ...