共享内存实现进程间通信

共享内存实现进程间通信
server.c
#include "sys/types.h"
#include 
#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 
#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;
}
  

你可能感兴趣的:(linux)