用共享内存实现生产者和消费者

1、shm_com.h

#include<sys/ipc.h>
#include<sys/shm.h>
#define SHM_BUF_SIZE 2048

struct shm_buf
{
    int num;
    char buf[SHM_BUF_SIZE];
};


2、sem_com.h

#include<sys/types.h>
#include<sys/ipc.h>
#include<sys/sem.h>
#include<stdio.h>
#include<stdlib.h>

union semnum
{
    int pid;
    struct semid_ds *buf;
    unsigned short *arr;
};

3、sem_com.c

#include"sem_com.h"


int sem_init(int sem_id,int init_pid)
{
    union semnum sem_num;
    sem_num.pid = init_pid;


    if(semctl(sem_id,0,SETVAL,sem_num) == -1)
    {
        perror("Fail");
return -1;
    }
    return 0;
}


int sem_del(int sem_id)
{
    union semnum sem_num;


    if(semctl(sem_id,0,IPC_RMID,sem_num) == -1)
    {
        perror("Faile to delete");
return -1;
    }
    return 0;
}


int p_sem(int sem_id)
{
    struct sembuf sem_a;
    sem_a.sem_num = 0;
    sem_a.sem_op = -1;
    sem_a.sem_flg = SEM_UNDO;


    if(semop(sem_id,&sem_a,1) == -1)
    {
        perror("Fail to semop");
return -1;
    }
    return 0;
}


int v_sem(int sem_id)
{
    struct sembuf sem_a;
    sem_a.sem_num = 0;
    sem_a.sem_op = 1;
    sem_a.sem_flg = SEM_UNDO;


    if(semop(sem_id,&sem_a,1) == -1)
    {
        perror("Fail to semop");
return -1;
    }
    return 0;
}


4、producer

#include "shm_com.h"
#include "sem_com.h"
#include <signal.h>


int signal_init()
{
    signal(SIGINT,SIG_IGN);
    signal(SIGSTOP,SIG_IGN);
    signal(SIGQUIT,SIG_IGN);
    return 0;
}


int main()
{
    char *shared_memory;
    struct shm_buf *shm_inst;
    char buffer[BUFSIZ];
    int shmid;
    int semid;


    signal_init();


    semid = semget(ftok(".",'a'),1,0666|IPC_CREAT);
    sem_init(semid,1);
    
    shmid = shmget(ftok(".",'c'),sizeof(struct shm_buf),0666|IPC_CREAT);
    if(shmid == -1)
    {
        perror("Fail to shmget");
sem_del(semid);
exit(1);
    }
    shared_memory=shmat(shmid,0,0);
    if(shared_memory==(void*)-1)
    {
perror("Fail to shmat");
sem_del(semid);
exit(1);
    }


    printf("Shared memory is %p\n",shared_memory);


    shm_inst = (struct shm_buf *)shared_memory;


    do
    {
        p_sem(semid);
printf("input something to memory:");


if(fgets(shm_inst->buf,SHM_BUF_SIZE,stdin) == NULL)
{
   perror("Fail to fgets");
   v_sem(semid);
   break;
}
shm_inst->num = getpid();
v_sem(semid);
    }while((strncmp(shm_inst->buf),"end",3) != 0);


    sem_del(semid);


    if(shmdt(shared_memory) == -1)
    {
        perror("Fail to share");
exit(1);
    }
    exit(0);


}

5、customer

#include "shm_com.h"
#include "sem_com.h"
#include <string.h>


int main()
{
    char *shared_memory = NULL;
    struct shm_buf *shm_inst;
    int shmid;
    int semid;


    semid = semget(ftok(".",'a'),1,0666);


    if(semid == -1)
    {
        perror("producer don not exist!\n");
exit(1);
    }


    shmid = shmget(ftok(".",'c'),sizeof(struct shm_buf),0666|IPC_CREAT);


    if(shmid == -1)
    {
        perror("Fail to shmget!");
exit(1);
    }


    shared_memory = shmat(shmid,0,0);


    if(shared_memory == (void *)-1)
    {
        perror("Fail to shmat!");
exit(1);
    }
    printf("Shared memory is %p\n",shared_memory);


    shm_inst = (struct shm_buf *)shared_memory;


    do
    {
        p_sem(semid);


printf("process write memory %d:%s",shm_inst->num,shm_inst->buf);


if(strncmp(shm_inst->buf,"end",3) == 0)
{
   perror("Fail to compare!");
   exit(1);
}


shm_inst->num = 0;
memset(shm_inst->buf,0,SHM_BUF_SIZE);
v_sem(semid);
    }while(1);


    if(shmdt(shared_memory) == -1)
    {
        perror("Fail to shmdt");
exit(1);
    }


    if(shmctl(shmid,IPC_RMID,NULL) == -1)
    {
        perror("Fail to shmctl");
exit(1);
    }
    exit(0);
}

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值