当前位置:网站首页>Sleeping barber problem
Sleeping barber problem
2022-07-28 10:40:00 【interval_ package】
Sleeping barber problem
Sleeping barber problem
description:
The sleeping_barber Problem:
- A barbershop consists of a waiting room with n chairs and the barber room containing the barber chair.
- if there are no customers to be served, the barber goes to sleep.
- if a customer enters the barbershop and all chairs are occupied, then the customer leaves the shop.
- if the barber is busy but chairs are available,then the customer sits in a free chair.
- if the barber is asleep,the customer wakes up the barber.
Solution Method:
Semaphore:
| Semaphore name | Init numbers | Target |
|---|---|---|
| mutex | 1 | For mutually exclusive maintenance seat The value of the variable |
| barber_ready | 0 | Used to guarantee barber And customer Before and after |
| customer_ready | 0 | Used to guarantee barber And customer Before and after |
| seat(not semaphore) | n | The number of chairs left in the store |
| arrive | 0 | Judge whether the customer has gone to the barber |
Relation:
Because if there are no chairs in the store , Customers will no longer wait , So we don't set chairs as semaphores .
But chairs are also a resource , You need mutually exclusive allocations , So we used mutex To realize the mutually exclusive access of chairs .
Customer process:
- For customer process , We basically use the logic of temptation and inquiry .
- After entering the store , First try to explore to see if there are any remaining chairs , If you have one, go into the store and sit down , If not, then leave .
- After sitting in a chair , Just send it to the barber customer_ready The signal of , Show that you have been waiting .
- Wait for the barber to send ready The signal of , After receiving the signal, leave your position and go to the barber , Modify location information .
Barber process:
- For the barber process , We basically use to get - Logic of answer .
- The barber has been waiting for the customer to send ready The signal of , If you hear ready And nothing happens at this time , Just respond barber_ready The signal . That is to say, I heard someone say I'm ok , Just call someone over .
Update:
But there's a problem , If the barber cuts very, very fast , When the cutting process is over , If the process is still processing at this time seat The operation of , It will let the next process come directly , There is a problem .
So we introduced arrive Variable , Only after the customer arrives at the location , Will start cutting .
Source Code:
//
// Created by Zza on 2022/4/3.
//
#ifndef DATASTRUCTUREIMPLEMENTINGC_SLEEPING_BARBER_H
#define DATASTRUCTUREIMPLEMENTINGC_SLEEPING_BARBER_H
#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);
_Noreturn void *barber(void *param);
int seat_num = 5;
int cus_process_alive = CUSTOMER_NUMBER;
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 customer_ready;
sem_t barber_ready;
sem_t arrived;
sem_t mutex;
int main_barber_problem(int argc, char *argv[]) {
pthread_t barber_id;
pthread_t client_ids[CUSTOMER_NUMBER];
sem_init(&mutex,0,1);
sem_init(&barber_ready,0,0);
sem_init(&customer_ready,0,0);
sem_init(&arrived,0,0);
pthread_create(&barber_id, NULL, barber, NULL);
for (int i = 0; i < CUSTOMER_NUMBER; i++){
usleep(interval[i]*1000);
printf("%d: One customer comes, see %d seats left now\n",
time(NULL), seat_num);
pthread_create(&client_ids[i], NULL, customer, NULL);
}
while(cus_process_alive);
return 0;
}
int iter=0;
_Noreturn void *barber(void *param) {
int worktime = 500;
time_t t;
while(1) {
sem_wait(&customer_ready);
sem_post(&barber_ready);
sem_wait(&arrived);
iter++;
t = time(NULL);
printf("%d: Barber start cut the %d hair\n",t,iter);
usleep(worktime*1000);
t = time(NULL);
printf("%d: Barber end cut the %d hair\n",t,iter);
}
}
void *customer(void *param) {
sem_wait(&mutex);
if(seat_num > 0){
seat_num --;
printf("%d: One customer ready, make %d seats left now\n",
time(NULL), seat_num);
// Sit down and shout at once
sem_post(&customer_ready);
sem_post(&mutex);
sem_wait(&barber_ready);
// After hearing the news, I left my position
sem_wait(&mutex);
seat_num++;
printf("%d: One customer is to cut, make %d seats left now\n",
time(NULL), seat_num);
sem_post(&mutex);
sem_post(&arrived);
} else {
printf("%d: One customer leaves with no haircut\n", time(NULL));
sem_post(&mutex);
}
cus_process_alive--;
}
#endif //DATASTRUCTUREIMPLEMENTINGC_SLEEPING_BARBER_H
边栏推荐
- CentOS7下安装mysql5.7
- ACM寒假集训#7
- Hurun released the 2020 top 10 Chinese chip design private enterprises: Huawei Hisilicon did not appear on the list!
- IDEA创建我的第一个项目
- GKSpheresNoiseSource
- SQL Server 2016 learning records - single table query
- C language input string with spaces
- django-celery-redis异步发邮件
- 机器人技术(RoboCup 2D)实验五十题及主要函数含义
- 机器学习--手写英文字母1--分类流程
猜你喜欢
![Implement a queue with two stacks [C language]](/img/8a/679575bb0a562eff7e4317e64b4790.png)
Implement a queue with two stacks [C language]

SDUT Round 9 2020 Spring Festival campaign

How to play a ball game with RoboCup 2D

第一篇:UniAPP的小程序跨端开发-----创建uniapp项目

SQL Server 2016 learning records - set query

ACM winter vacation training 5

Machine learning -- handwritten English alphabet 2 -- importing and processing data

SQL Server 2016学习记录 --- 连接查询

markdown转成word或者pdf

GKVoronoiNoiseSource
随机推荐
markdown转成word或者pdf
Test question discovery ring of previous test questions
机器学习--手写英文字母1--分类流程
Inverse element & combinatorial number & fast power
5、Window端实现Mapreduce程序完成wordcount功能
GKNoiseMap
GKCylindersNoiseSource
14. Double pointer - the container that holds the most water
SemEval 2022 | 将知识引入NER系统,阿里达摩院获最佳论文奖
Install mysql5.7 under centos7
SuperMap iserver publishing management and calling map services
C语言 输入带空格的字符串
GKCircleObstacle
SQL Server 2016 学习记录 --- 数据定义
Codeforces Round #614 (Div. 2) A. ConneR and the A.R.C. Markland-N
Uni app project directory, file function introduction and development specification
16. String inversion
SQL Server 2016 learning records - View
机器人技术(RoboCup 2D)如何进行一场球赛
string matching