Linux进程间通信之《共享内存》入门

简介: 共享内存是Linux系统进程间通信常用的方式,通常用于数据量较大的情况,如果只是用于不同的进程间消息通知,那不如用消息队列或者socket。之前做的项目中,使用共享内存的其实只有一种情况:视频数据的共享。设备类似于DVR,视频采集编码在一个独立的程序中,另一个程序负责协议通信。共享内存要想好用,共享的那段内存,需要用数据结构和队列组织起来,加上读写索引和数据有效标志(已读和未读、可读)。下面的这个示例代码是我初学时的,适合入门和了解使用流程。

 目录

简述

代码

写端代码

读取端代码

编译

运行




代码

写端代码

#include <stdio.h>
#include <signal.h>
#include <stdlib.h>
#include <errno.h>
#include <sys/ipc.h>
#include <sys/types.h>
#include <sys/shm.h>
#include <string.h>
#include <unistd.h>
#define N 1024
typedef struct
{
    pid_t pid;
    char text[N];
}SHMBUF;
void handler(int signo) {printf("signo=%d\n", signo);}
int main()
{
  int shmid;
    SHMBUF *shmadd;
    key_t key;
    pid_t peerpid;
    if ((key = ftok(".", 'a')) == -1)
    {
        perror("ftok");
        exit(-1);
    }
    signal(SIGUSR1, handler);
  if ((shmid = shmget(key, sizeof(SHMBUF), 0666 | IPC_CREAT | IPC_EXCL)) == -1)
  {
        if (errno == EEXIST)
        {
            shmid = shmget(key, sizeof(SHMBUF), 0666);
          if ((shmadd = (SHMBUF *)shmat(shmid, NULL, 0)) == (SHMBUF *)-1)
          {
            perror("shmat");
            exit(-1);
          }
            peerpid = shmadd->pid;
            shmadd->pid = getpid();
            kill(peerpid, SIGUSR1);
        }
        else
        {
          perror("shmget");
        exit(-1);
        }
  }
    else    //first process
    {
      if ((shmadd = (SHMBUF *)shmat(shmid, NULL, 0)) == (SHMBUF *)-1)
      {
        perror("shmat");
        exit(-1);
      }
        shmadd->pid = getpid();
        //sprintf(shmadd, "%d", getpid());
        pause();
        peerpid = shmadd->pid;
    }
    printf(">");
    while (1)
    {
        fgets(shmadd->text, N, stdin);
        shmadd->text[strlen(shmadd->text)-1] = '\0';
        kill(peerpid, SIGUSR1);
        if (strncmp(shmadd->text, "quit", 4) == 0)
        {
            sleep(1);
          if (shmdt(shmadd) == -1)
          {
            perror("shmdt");
          }
          if (shmctl(shmid, IPC_RMID, NULL) == -1)
          {
            perror("RM");
            exit(-1);
          }
            exit(0);
        }
        pause();
        printf(">");
    }
  return 0;
}

image.gif

读取端代码

#include <stdio.h>
#include <signal.h>
#include <stdlib.h>
#include <errno.h>
#include <sys/ipc.h>
#include <sys/types.h>
#include <sys/shm.h>
#include <string.h>
#include <unistd.h>
#define N 1024
typedef struct
{
    pid_t pid;
    char text[N];
}SHMBUF;
void handler(int signo) {printf("signo=%d\n", signo);}
int main()
{
  int shmid;
    SHMBUF *shmadd;
    key_t key;
    pid_t peerpid;
    if ((key = ftok(".", 'a')) == -1)
    {
        perror("ftok");
        exit(-1);
    }
    signal(SIGUSR1, handler);
  if ((shmid = shmget(key, sizeof(SHMBUF), 0666 | IPC_CREAT | IPC_EXCL)) == -1)
  {
        if (errno == EEXIST)
        {
            shmid = shmget(key, sizeof(SHMBUF), 0666);
          if ((shmadd = (SHMBUF *)shmat(shmid, NULL, 0)) == (SHMBUF *)-1)
          {
            perror("shmat");
            exit(-1);
          }
            peerpid = shmadd->pid;
            shmadd->pid = getpid();
            kill(peerpid, SIGUSR1);
        }
        else
        {
          perror("shmget");
        exit(-1);
        }
  }
    else    //first process
    {
      if ((shmadd = (SHMBUF *)shmat(shmid, NULL, 0)) == (SHMBUF *)-1)
      {
        perror("shmat");
        exit(-1);
      }
        shmadd->pid = getpid();
        pause();
        peerpid = shmadd->pid;
    }
    while (1)
    {
        pause();
        printf("read %s\n", shmadd->text);
        if (strncmp(shmadd->text, "quit", 4) == 0)
        {
          if (shmdt(shmadd) == -1)
          {
            perror("shmdt");
          }
            exit(0);
        }
       // sleep(1);
       usleep(100000);
       kill(peerpid, SIGUSR1);
    }
    exit(0);
}

image.gif

编译

gcc reader.c -o reader
gcc writer.c -o writer

image.gif

运行

image.gif编辑


目录
相关文章
|
3天前
|
消息中间件 算法 Linux
【Linux】详解如何利用共享内存实现进程间通信
【Linux】详解如何利用共享内存实现进程间通信
|
2天前
|
Linux 数据库
linux守护进程介绍 | Linux的热拔插UDEV机制
linux守护进程介绍 | Linux的热拔插UDEV机制
linux守护进程介绍 | Linux的热拔插UDEV机制
|
2天前
|
Unix Linux 调度
linux线程与进程的区别及线程的优势
linux线程与进程的区别及线程的优势
|
2天前
|
Linux 调度 C语言
|
2天前
|
Java Linux Arthas
linux上如何排查JVM内存过高?
linux上如何排查JVM内存过高?
16 0
|
3天前
|
存储 安全 Linux
【Linux】详解进程通信中信号量的本质&&同步和互斥的概念&&临界资源和临界区的概念
【Linux】详解进程通信中信号量的本质&&同步和互斥的概念&&临界资源和临界区的概念
|
1月前
|
Linux Shell 调度
【Linux】7. 进程概念
【Linux】7. 进程概念
43 3
|
4天前
|
存储 Linux Shell
Linux:进程概念
Linux:进程概念
19 8