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 ...
随机推荐
- AGC043 B题题解
有的时候,碰到一道题,要给自己先设立部分分,再去想如何把部分分推广到一般情况.这题就是绝佳的例子. 不妨将\(a_i\)用\(a_i - 1\)替代,这样就变成了\(a_i \in \{ 0, 1, ...
- hive的调优策略
hive有时执行速度很慢,若hive on spark 的话,在sparkUI上可以清楚看到是否数据倾斜 优化方法: 1.增加reduce数目 hive.exec.reducers.bytes.per ...
- sql server的bcp指令
有时需要允许bcp指令 -- 允许配置高级选项EXEC sp_configure 'show advanced options', 1GO-- 重新配置RECONFIGUREGO-- 启用xp_cmd ...
- 世界上最快的排序算法——Timsort
前言 经过60多年的发展,科学家和工程师们发明了很多排序算法,有基本的插入算法,也有相对高效的归并排序算法等,他们各有各的特点,比如归并排序性能稳定.堆排序空间消耗小等等.但是这些算法也有自己的局限性 ...
- STL——容器(List)list 的赋值操作
list.assign(beg, end); //将[beg, end)区间中的数据拷贝赋值给本身 1 #include <iostream> 2 #include <list> ...
- Consul的使用
Consul的使用 生产部署中,Consul安装在要注册服务的每个节点上.Consul有两种运行模式:客户端和服务器端,每个Consul数据中心必须至少有一个服务器,负责维护Consul状态,为了 ...
- flink1.11报错No ExecutorFactory found to execute the application
使用flink1.11版本时,报错:No ExecutorFactory found to execute the application 查找maven下载的依赖,发现没有下载flink-clien ...
- CSS3全览_动画+滤镜
CSS3全览_动画+滤镜 目录 CSS3全览_动画+滤镜 1. 列表和生成的内容 2. 变形 3. 过渡 4. 动画 5. 滤镜, 混合, 裁剪和遮罩 6. 针对特定媒体的样式 作者: https:/ ...
- 什么是java的深浅拷贝?
什么是java的深浅拷贝? 浅拷贝 就是很肤浅的拷贝,只拷贝了别人的地址.没有拷贝地址里面的值.地址里面的值改变后,就都改变了. 深拷贝 就是很深入的拷贝,深入到核心的拷贝,拷贝了别人地址里面的值,别 ...
- 1-解决java Scanner出现 java.util.NoSuchElementException
起因:在函数中新建scanner对象,然后多次调用此方法出现上述异常 原因:Scanner(system.in)在Scanner中接受的是键盘 输入,当调用close()方法时 Scanner的关闭会 ...