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

/*实验题目:参考题目“用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*/

猜你喜欢

转载自blog.csdn.net/JxufeCarol/article/details/90273928
今日推荐