操作系统 进程间通信实验题之共享内存

/*实验题目:参考题目“用pthread线程+sem+mutex模拟“生产者-消费者”问题”,
用“共享内存”的方法实现父、子进程之间的生产者消费者问题,信号量操作采用“sem_init/sem_wait/sem_post”模式*/

同时参考以下网页:
(a)Linux下利用信号量函数和共享内存函数和C语言实现生产者消费者问题
https://blog.csdn.net/qq_31490151/article/details/78984593
(b)Linux进程通信之共享内存实现生产者/消费者模式
https://www.cnblogs.com/cjvae/p/9179559.html

/*程序文件使用的头文件和结构体*/
#include <stdio.h>
#include <string.h>
#include <stdlib.h>
#include <errno.h>
#include <fcntl.h>
#include <sys/ipc.h>
#include <sys/shm.h>
#include <sys/sem.h>
#include <sys/types.h>
#include <semaphore.h>//信号量sem_t函数头文件
#include <unistd.h>

#define BUFFER_SIZE 5
#define MSG_KEY (key_t)1235
#define WORKTIMES 5

typedef int buffer_item;

//缓冲区结构体
struct buffer
{
    buffer_item buffer[BUFFER_SIZE];  //全局数组作为线程间共享的数据
    int in_pthread;                   //in_pthread为放入指针
    int out_pthread;                  //out_pthread为取出指针
    int count_pthread;                //count_pthread为缓冲区已放入指针计数量
    sem_t s;                          //访问共享内存的信号量
};

int shmID;//共享内存标识符
pid_t parent;//生产者
pid_t child;//消费者
/*producer_consumer.c*/
  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值