共享内存实现进程间通信

共享内存实现进程间通信
server.c
#include "sys/types.h"
#include <signal.h>
#include "unistd.h"
#include "stdio.h"
#include "stdlib.h"
#include "sys/shm.h"
#include "string.h"

struct mybuf{
        int pid;
        char buf[128];
};
void fun(int sigunm){

        return;

}
int main(){

        int shmid;
        struct mybuf * p;
        int pid;
        int key;
         key=ftok("./a.c",'a');
        if(key<0){
                printf("create key failure\n");
                return -1;
        }
        printf("create key success");

        shmid=shmget(key,128,IPC_CREAT|0777);//创建共享内存

        if(shmid<0){
                printf("create share memory failure\n");
                return -1;
        }
        printf("create share memory success shime=%d\n",shmid);

        signal(SIGUSR1,fun);//处理接收到的SIGUSER2
        p=(struct mybuf * )shmat(shmid,NULL,0);//把共享内存映射到用户空间
        if(p==NULL){
                printf("shmat function failure\n");
                return -3;
        }
        //获取本进程pid
        p->pid=getpid();
        pause();//等待 client read server pid;
        pid=p->pid;

        while(1){
                printf("parent process start write share memory:\n");
                fgets(p->buf,128,stdin);//键盘输入到共享内存
                kill(pid,SIGUSR2);//发送SIGUSER1信号内容给client pid
                printf("send SIGUSR2 pid=%d, buf=%s",pid,p->buf);
                pause();//等待进程读
        }


        shmdt(p);
        shmctl(shmid,IPC_RMID,NULL);
        system("ipcs -m ");
        return 0;
}

client.c

#include "sys/types.h"
#include <signal.h>
#include "unistd.h"
#include "stdio.h"
#include "stdlib.h"
#include "sys/shm.h"
#include "string.h"

struct mybuf{
        int pid;
        char buf[128];
};
void fun(int sigunm){

        return;

}
int main(){

        int shmid;
        struct mybuf * p;
        int pid;
        int key;
        key=ftok("./a.c",'a');
        if(key<0){
                printf("create key failure\n");
                return -1;
        }
        printf("create key success");

        shmid=shmget(key,128,IPC_CREAT|0777);//创建共享内存

        if(shmid<0){
                printf("create share memory failure\n");
                return -1;
        }
        printf("create share memory success shime=%d\n",shmid);

        signal(SIGUSR2,fun);//处理接收到的SIGUSER2
        p=(struct mybuf * )shmat(shmid,NULL,0);//把共享内存映射到用户空间
        if(p==NULL){
                printf("shmat function failure\n");
                return -3;
        }
        //获取server进程pid
        pid=p->pid;//read memory
        //write client pid to share memory
        p->pid=getpid();
        //kill signal
        kill(pid,SIGUSR1);
        printf("server pid=%d lient pid=%d",pid,p->pid);
        //clitent start read data from memery
        while(1){
                pause();//waite server write data
                printf("client process receve data:%s\n send to server pid=%d",p->buf,pid);
                kill(pid,SIGUSR1);//发送SIGUSER1信号内容给server write memory
        }


        shmdt(p);
        shmctl(shmid,IPC_RMID,NULL);
        system("ipcs -m ");
        return 0;
}
  

  • 0
    点赞
  • 3
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值