• 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
    ", 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
    ", 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
    ", 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
    ", 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
    ", 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
    ", tm.tm_hour, tm.tm_min, tm.tm_sec);
            sem_post(&mutex);
        }
    }
    
  • 相关阅读:
    数组的应用:一。冒泡排序二。折半查找!二维数组的学习。
    break与continue,while 循环和一维数组的学习及作业
    for循环的应用:迭代法和穷举法
    循环
    称体重
    js js弹出框、对话框、提示框、弹窗总结
    windows 服务器开设端口
    SQL Server 数据库分离与附加(图文教程)
    ASP.NET MVC5 PagedList分页示例
    mvc 连接数据库但单复值得问题
  • 原文地址:https://www.cnblogs.com/justsong/p/12219769.html
Copyright © 2020-2023  润新知