IPC之System V vs POSIX

IPC

当谈到IPC(Inter-Process Communication,进程间通信)时,它是指不同进程之间进行数据交换和通信的机制。
它允许在操作系统中运行的不同进程之间传输数据,这些进程可以是在同一台计算机上运行的不同应用程序,也
可以是在不同计算机上运行的不同应用程序。

IPC有多种实现方式,包括管道、消息队列、共享内存、信号量、套接字等。每种方式都有自己的特点和适用场景。

Linux 提供有SystemV 和 POSIX 两种接口:
SYSTEM V的接口使用时间比较久,应用广泛,很多旧的产品功能采用;
POSIX的接口设计较好,学习使用都比较容易。

个人觉得如果是新的代码还是采用POSIX接口比较好。

system V 的IPC (消息队列、信号量、共享内存)
https://man7.org/linux/man-pages/man7/sysvipc.7.html

POSIX IPC 的是各种IPC分开说明的
https://man7.org/linux/man-pages/man7/mq_overview.7.html 消息队列
https://man7.org/linux/man-pages/man7/sem_overview.7.html 信号量
https://man7.org/linux/man-pages/man7/shm_overview.7.html 共享内存

看以上的文档基本上就够了。

附上POSIX的标准
https://pubs.opengroup.org/onlinepubs/9699919799/

示例

共享内存

通过以下示例,可以了解一下POSIX与SystemV 的接口区别。

POSIX shm

// 一个主进程,负责往共享内存中写数据
#include <sys/mman.h>
#include <sys/stat.h>        /* For mode constants */
#include <fcntl.h>           /* For O_* constants */
#include <stdio.h>
#include <unistd.h>
#include <errno.h>
#include <string.h>

int main(int argc, char **argv)
{
	int ret;
	int fd;
    char *m;
    int *t;

	fd = shm_open("/somename", O_CREAT | O_RDWR, DEFFILEMODE);  /* 这里有特别注意mode的取值,如果是0 的话,则可能导致其他进程无权限获取共享内存 */
    if (fd < 0) {
        printf("shm open fail. %s\n", strerror(errno));
        return -1;
    }

    if (ftruncate(fd, 4) < 0) {
        printf("ftruncate fail.\n");
        goto error;
    }

    m = mmap(NULL, 4, PROT_READ | PROT_WRITE, MAP_SHARED, fd, 0);
    if (m == MAP_FAILED) {
        printf("mmap error");
        goto error;
    }
    t = (int *)m;

    *t = 0;
    while (1) {
        (*t)++;
        sleep(1);
    }

    munmap(m, 4);

error:
    close(fd);
    shm_unlink("/somename");

	return 0;
}

// 另一个进程,读取共享内存的数据
#include <sys/mman.h>
#include <sys/stat.h>        /* For mode constants */
#include <fcntl.h>           /* For O_* constants */
#include <unistd.h>
#include <stdio.h>

int main(int argc, char **argv)
{
	int ret;
	int fd;
    char *m;
    int *t;

	fd = shm_open("/somename", O_RDWR, 0);
    if (fd < 0) {
        printf("shm open fail.\n");
        return -1;
    }

    m = mmap(NULL, 4, PROT_READ | PROT_WRITE, MAP_SHARED, fd, 0);
    if (m == MAP_FAILED) {
        printf("mmap error");
        goto error;
    }
    t = (int *)m;

    while (1) {
        printf("read %d\n", *t);
        sleep(2);
    }

    munmap(m, 4);

error:
    close(fd);
    shm_unlink("/somename");

	return 0;
}

System V shm

// 进程1 写入共享内存数据
#include <sys/mman.h>
#include <sys/stat.h>        /* For mode constants */
#include <fcntl.h>           /* For O_* constants */
#include <stdio.h>
#include <unistd.h>
#include <errno.h>
#include <string.h>

#include <sys/ipc.h>
#include <sys/shm.h>

int main(int argc, char **argv)
{
    int shmid;
    key_t key = 0x1234;
    char *addr;
    int *val;

    shmid = shmget(key, 4, IPC_CREAT | DEFFILEMODE);
    if (shmid < 0) {
        printf("shmget fail\n");
        return -1;
    }

    printf("get id %d\n", shmid);

    addr = shmat(shmid, NULL, 0);
    if (addr == (void *)-1) {
        printf("shmat fail. %s\n", strerror(errno));
        goto error;
    }

    val = (int *)addr;

    *val = 0;
    while (1) {
        (*val)++;
        sleep(1);
    }

error:
    shmctl(shmid, IPC_RMID, NULL);
    return -1;
}


// 读取共享内存数据
#include <sys/mman.h>
#include <sys/stat.h>        /* For mode constants */
#include <fcntl.h>           /* For O_* constants */
#include <stdio.h>
#include <unistd.h>
#include <errno.h>
#include <string.h>

#include <sys/ipc.h>
#include <sys/shm.h>

int main(int argc, char **argv)
{
    int shmid;
    key_t key = 0x1234;     /* 还可以用ftok 来生成一个与文件有关的key值,这里写死简化逻辑 */
    char *addr;
    int *val;

    shmid = shmget(key, 4, 0);
    if (shmid < 0) {
        printf("shmget fail\n");
        return -1;
    }

    printf("read id %d\n", shmid);

    addr = shmat(shmid, NULL, 0);
    if (addr == (void *)-1) {
        printf("shmat fail.\n");
        goto error;
    }

    val = (int *)addr;

    while (1) {
        printf("read %d\n", *val);
        sleep(2);
    }

error:
    shmctl(shmid, IPC_RMID, NULL);
    return -1;
}

  • 0
    点赞
  • 3
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值