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之後),纔會刪除共享內存。

在這裏插入圖片描述

二 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;
}
發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章