哈工大操作系统实验6 信号量的实现 pc.c 编译时报错 对‘sem_open‘未定义的引用

本文档记录了在Ubuntu环境下编写pc.c进行操作系统实验,使用信号量实现生产者-消费者问题的过程。在编译pc.c时遇到了对'sem_open'未定义的引用错误,通过在编译时添加-pthread选项成功解决了问题。

github 实验源码以及相关内容链接

https://github.com/Kevin-Kevin/hit-operatingSystem

实验内容

1. 编写 pc.c,在 ubuntu 上边运行

2. 在0.11 中实现信号量,用生产者—消费者程序检验之

pc.c 编写

思路

一开始想要用共享内存来做的,后来选择用文件的方式当缓冲区

代码

#include <stdlib.h>
#include <stdio.h>
#include <sys/types.h>
#include <sys/stat.h>
#include <fcntl.h>

#include <sys/ipc.h>
#include <sys/shm.h>

#include <fcntl.h>    /* For O_* constants */
#include <sys/stat.h> /* For mode constants */
#include <semaphore.h>

#include <sys/mman.h>
#include <sys/stat.h> /* For mode constants */
#include <fcntl.h>    /* For O_* constants */

#include <errno.h>
#include <string.h>

sem_t *empty, *full, *mutex;
int producer_input_queue(int fd)
{
   
   
    int i = 0;
    int end = 0;
    int ret = 0;
    int changedend = 0;
    int input = 0;
    for (i = 0; i <= 500; i++)
    {
   
   

        // 查看有没有空余资源,没有就睡觉
        sem_wait(empty);

        // 操作 buffer.c 之前上锁
        sem_wait(mutex);

        // 写入 buffer.c

        // 读取 end
        ret = lseek(fd, 9 * sizeof(int), SEEK_SET);

        ret = read(fd, &end, sizeof(int));

        //printf("get end = %d\n",end);

        if (end < 9)
        {
   
   
            end++;

            // 写入数字 i
            lseek(fd, (
评论
成就一亿技术人!
拼手气红包6.0元
还能输入1000个字符
 
红包 添加红包
表情包 插入表情
 条评论被折叠 查看
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值