哈工大操作系统实验 信号量的实现
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, (

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

被折叠的 条评论
为什么被折叠?



