zoukankan      html  css  js  c++  java
  • 进程间通讯之共享内存

    共享内存是被多个时程共享的一部份物理内存,共享内存是进程间共享数据的一种最快的方法。

    头文件:#include<sys/types.h>         #include<sys/shm.h>

    A:创建共享内存。

    int shmget(key_t key,int size,int shmflg)

    1、key:键值,由ftok函数获得。

    2、size:创建共享内存的大小。

    3、shmflg:标识位。

    成功返回共享内存标识符,失败返回-1。

    B:映射共享内存。

    int shmat(int shmid,char *shmaddr,int flag)

    1、shmid:shmget函数返回的共享内存标识符。

    2、flag:决定以什么方式来确定映射的地址(通常为NULL)。

    成功返回共享内存映射到进程中的地址,失败返回-1。

    C:删除映射

    int shmdt(char *shmddr)

    D:清除共享内存

    int shmctl(int shmid,int cmd,struct shmid_ds *buf)

    例子:两个进程利用共享内存相互通信

    server.c

     1 /*************************************************************************
     2     > File Name: server1.c
     3     > Author: xu
     4     > Mail: eeexu123@163.com 
     5     > Created Time: 2016年10月09日 星期日 14时38分18秒
     6  ************************************************************************/
     7 
     8 #include<stdio.h>
     9 #include<stdlib.h>
    10 #include<signal.h>
    11 #include<sys/stat.h>
    12 #include<sys/types.h>
    13 #include<fcntl.h>
    14 #include<unistd.h>
    15 #include<sys/shm.h>
    16 #include<sys/ipc.h>
    17 #define BUFFER_SIZE 128
    18 
    19 void myfun()
    20 {
    21     return;
    22 }
    23 
    24 int main()
    25 {
    26     struct mybuf
    27     {
    28         int pid;
    29         char buf[BUFFER_SIZE];
    30     };
    31     
    32     int key,shmid;
    33     int pid;
    34     struct mybuf *p;
    35     //注册SIGUSR1到函数中
    36     signal(SIGUSR2,myfun);
    37     key = ftok("./e.c",'a');
    38     if(key < 0)
    39     {
    40         printf("creat two process error
    ");
    41         exit(1);
    42     }
    43     //创建共享内存
    44     shmid = shmget(key,BUFFER_SIZE,IPC_CREAT|S_IRWXU);
    45     if(shmid < 0)
    46     {
    47         perror("shmget");
    48         exit(1);
    49     }
    50     printf("create share memory success
    ");
    51     //映射共享内存
    52     p = (struct mybuf *)shmat(shmid,NULL,0);
    53     if(p == NULL)
    54     {
    55         perror("shmat");
    56         exit(1);
    57     }
    58     printf("map the share memory success
    ");
    59     
    60     p->pid = getpid();    //把服务器PID写入共享内存中
    61 
    62     pause();    //等待客户端
    63 
    64     pid = p->pid;    //读取客户端PID
    65 
    66     while(1)
    67     {
    68         printf("parent process start write share memory
    ");
    69         fgets(p->buf,124,stdin);
    70         kill(pid,SIGUSR1);
    71         pause();
    72     }
    73     shmdt(p);
    74     shmctl(shmid,IPC_RMID,NULL);
    75     
    76     return 0;
    77 }

    client.c

    /*************************************************************************
        > File Name: client1.c
        > Author: xu
        > Mail: eeexu123@163.com 
        > Created Time: 2016年10月09日 星期日 14时38分30秒
     ************************************************************************/
    
    #include<stdio.h>
    #include<stdlib.h>
    #include<signal.h>
    #include<sys/stat.h>
    #include<sys/types.h>
    #include<fcntl.h>
    #include<unistd.h>
    #include<sys/shm.h>
    #include<sys/ipc.h>
    #define BUFFER_SIZE 128
    
    void my_fun()
    {
        return;
    }
    
    int main()
    {
        struct my_buf
        {
            int pid;
            char buf[BUFFER_SIZE];
        };
        
        int key,shmid;
        int pid;
        struct my_buf *p;
        //注册SIGUSR2到函数中
        signal(SIGUSR1,my_fun);
        key = ftok("./e.c",'a');
        if(key < 0)
        {
            perror("fotk");
            exit(1);
        }
        //创建共享内存
        shmid = shmget(key,BUFFER_SIZE,IPC_CREAT|S_IRWXU);
        if(shmid < 0)
        {
            perror("shmget");
            exit(1);
        }
        //映射共享内存
        p = (struct my_buf *)shmat(shmid,NULL,0);
        if(p == NULL)
        {
            perror("shmat");
            exit(1);
        }
        printf("map the share memory success
    ");
        
         pid = p->pid;    //读取服务器PID
    
        p->pid = getpid();    //将客户端PID写入共享内存中
    
        kill(pid,SIGUSR2);    //给服务器发信号
    
        while(1)
        {
            pause();
            printf("client process recave data form share memory:%s
    ",p->buf);
            kill(pid,SIGUSR2);
        }
        
        shmdt(p);
        shmctl(shmid,IPC_RMID,NULL);
        
        return 0;
    }
  • 相关阅读:
    (转)CMD指令大全
    [转]测试人员要像医生一样把要测试的程序当自己的病人一样看待一样检测!
    robotFramework学习笔记
    mysql数据库转换成数据字典的方法(整理)
    【转】PHP SQL防注入的一些经验
    性能测试基础知识(概念)
    iOS--MJRefresh的使用 上拉刷新和下拉加载
    IOS----UIScrollerView的使用
    iOS -- UILabel的高度自适应
    第二章 图像形成
  • 原文地址:https://www.cnblogs.com/eeexu123/p/5949606.html
Copyright © 2011-2022 走看看