天天看點

linux程序間通信(五)----IPC篇----共享記憶體實作程序間通信

先給自己打個廣告,本人的微信公衆号正式上線了,搜尋:張笑生的地盤,主要關注嵌入式軟體開發,股票基金定投,足球等等,希望大家多多關注,有問題可以直接留言給我,一定盡心盡力回答大家的問題

linux程式間通信(五)----IPC篇----共享記憶體實作程式間通信

一 what

在《linux程序間通信(四)----IPC篇----共享記憶體初識篇》文章中,我們知道了共享記憶體是什麼,通過幾個常用的函數shmget、ftok、shmat、shmdt、shmctl,了解了如何建立共享記憶體,但是建立好的共享記憶體之間,如何實作程序間通信呢?

共享記憶體對象是存在于核心中的,兩個程序間如何通路這個共享記憶體對象呢?這篇文章介紹兩種通路方式,分别是有親緣關系的父子程序通路同一個共享記憶體,以及非親緣關系之間通路同一個共享記憶體。

共享記憶體使用步驟

1. 建立/打開共享記憶體
2. 映射共享記憶體,即把指定的共享記憶體映射到程序的位址空間用于通路
3. 讀寫共享記憶體
4. 撤銷共享記憶體映射
5. 删除共享記憶體對象
           

使用共享記憶體時的一些注意點或是限制條件

6. 共享記憶體的數量是有限制的,通過ipcs -l指令檢視,當然如果我們具有管理者權限,可以通過 cat /proc/sys/kernel/shmmax來檢視
7. 共享記憶體删除的時間點,shmctl添加删除标記,隻有當所有進車都取消共享記憶體映射時(即所有程序調用shmdt之後),才會删除共享記憶體。
           
linux程式間通信(五)----IPC篇----共享記憶體實作程式間通信

二 how

2.1 親緣關系的程序通路同一個共享記憶體

要想讓共享記憶體能夠在親緣關系的程序中通路,fork函數必須在shmget函數之後

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

void myfun(int signum)
{
    return;
}

int main(int argc, char *argv[])
{
    int shmid;
    int key;
    char *p;
    int pid;

    // shmget函數必須在fork函數之前,先建立一個共享記憶體
    shmid = shmget(IPC_PRIVATE, 128, IPC_CREAT | 0777);
    if (shmid < 0) {
        printf("create shared memory fail\n");
        return -1;
    }
    printf("create shared memory sucess, shmid = %d\n", shmid);
    pid = fork();
    if (pid > 0) {    // parent process
        signal(SIGUSR2, myfun);
        p = (char *)shmat(shmid, NULL, 0);
        if (p == NULL) {
            printf("shmat fail\n");
            return -1;
        }
        printf("parent process shmat sucess\n");
        while (1) {
            //write share memory
            printf("parent process begin to write memory data\n");
            fgets(p, 128, stdin);
            kill(pid, SIGUSR1);   // tell child process to read data
            pause();              // wait child process read
        }
    }
    if (pid == 0) { // child process to read
        signal(SIGUSR1, myfun);
        p = (char *)shmat(shmid, NULL, 0);
        if (p == NULL) {
            printf("shmat fail\n");
            return -1;
        }
        printf("child process shmat sucess\n");
        while (1) {
            pause(); // wait parent process write
            //start read share memory
            printf("child process read share memory data:%s\n", p);
            kill(getppid(), SIGUSR2);
        }
    }

    //在使用者空間删除共享記憶體的位址
    shmdt(p);
    
    //memcpy(p, "abcd", 4);  //執行這個語句會出現segment fault
    
    shmctl(shmid, IPC_RMID, NULL);
    system("ipcs -m");
    return 0;
}
           

2.2 非親緣關系的程序通路同一個共享記憶體

程序server

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

struct mybuf
{
    int pid;
    char buf[124];
};

void myfun(int signum)
{
    return;
}

int main(int argc, char *argv[])
{
    int shmid;
    int key;
    struct mybuf *p;
    int pid;
    
    key = ftok("./a.c", 'a');
    if (key , 0) {
        printf("create key fail\n");
        return -1;
    }
    printf("create key sucess\n");

    shmid = shmget(key, 128, IPC_CREAT | 0777);
    if (shmid < 0) {
        printf("create shared memory fail\n");
        return -1;
    }
    printf("create shared memory sucess, shmid = %d\n", shmid);

    signal(SIGUSR2, myfun);
    p = (struct mybuf *)shmat(shmid, NULL, 0);
    if (p == NULL) {
        printf("shmat fail\n");
        return -1;
    }
    printf("parent process shmat sucess\n");
    
    // get client pid
    p->pid = getpid(); //write server pid to share memory
    pause();  // wait client to read server pid
    pid=p->pid;
    
    while (1) {
        //write share memory
        printf("parent process begin to write memory data\n");
        fgets(p->buf, 124, stdin);
        kill(pid, SIGUSR1);   // tell client process to read data
        pause();              // wait client process read
    }

    //在使用者空間删除共享記憶體的位址
    shmdt(p);
    
    //memcpy(p, "abcd", 4);  //執行這個語句會出現segment fault
    
    shmctl(shmid, IPC_RMID, NULL);
    system("ipcs -m");
    return 0;
}
           

程序client

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

struct mybuf
{
    int pid;
    char buf[124];
};

void myfun(int signum)
{
    return;
}

int main(int argc, char *argv[])
{
    int shmid;
    int key;
    struct mybuf *p;
    int pid;
    
    key = ftok("./a.c", 'a');
    if (key , 0) {
        printf("create key fail\n");
        return -1;
    }
    printf("create key sucess\n");

    shmid = shmget(key, 128, IPC_CREAT | 0777);
    if (shmid < 0) {
        printf("create shared memory fail\n");
        return -1;
    }
    printf("create shared memory sucess, shmid = %d\n", shmid);

    signal(SIGUSR1, myfun);
    p = (struct mybuf *)shmat(shmid, NULL, 0);
    if (p == NULL) {
        printf("shmat fail\n");
        return -1;
    }
    printf("client process shmat sucess\n");
    
    // get server pid
    //read share memory
    pid = p->pid;
    // write client pid to share memory
    p->pid = getpid();
    kill(pid, SIGUSR2);   // tell client process to read data
    
    //client start to read share memory

    while (1) {
        pause();              // wait server process write share memory
        printf("client process read data:%s\n", p->buf); // read data
        kill(pid, SIGUSR2);   // server can  write share memory
    }

    //在使用者空間删除共享記憶體的位址
    shmdt(p);
    
    //memcpy(p, "abcd", 4);  //執行這個語句會出現segment fault
    
    shmctl(shmid, IPC_RMID, NULL);
    system("ipcs -m");
    return 0;
}
           

繼續閱讀