文章

72

粉丝

0

获赞

5

访问

10.6k

头像
2014年计算机学科专业基础综合试题 - 第47题回答
操作系统
发布于2025年10月15日 19:34
阅读数 87

// 信号量
semaphore mutex = 1;        // 缓冲区互斥
semaphore empty = 1000;     // 空槽数
semaphore full = 0;         // 产品数
semaphore consumer_turn = 1;// 消费者连续取锁

// 全局变量
int count = 0;              // 当前消费者已连续取的产品数(在 mutex 保护下)

// 生产者进程
Producer() {
    while (true) {
        生产一件产品;
        P(empty);
        P(mutex);
        把产品放入缓冲区;
        V(mutex);
        V(full);
    }
}

// 消费者进程
Consumer() {
    while (true) {
        P(consumer_turn);   // 申请连续取的权限
        int c = 0;
        while (c < 10) {    // 连续取最多 10 件
            P(full);
            P(mutex);
         ...

登录查看完整内容


登录后发布评论

暂无评论,来抢沙发