iis服务器助手广告
返回顶部
首页 > 资讯 > 操作系统 >IPC之System V vs POSIX
  • 852
分享到

IPC之System V vs POSIX

linux 2023-08-30 15:08:24 852人浏览 泡泡鱼
摘要

文章目录 IPC示例共享内存POSIX shmSystem V shm IPC 当谈到IPC(Inter-Process Communication,进程间通信)时,它是指不同进程之间进行数据交换和通信的机制。 它允许

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 #include         #include            #include #include #include #include int main(int arGC, char **argv){int ret;int fd;    char *m;    int *t;fd = shm_open("/somename", O_CREAT | O_RDWR, DEFFILEMODE);      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 #include         #include            #include #include 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 #include         #include            #include #include #include #include #include #include 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 #include         #include            #include #include #include #include #include #include int main(int argc, char **argv){    int shmid;    key_t key = 0x1234;         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;}

来源地址:https://blog.csdn.net/todo_cct/article/details/132496486

--结束END--

本文标题: IPC之System V vs POSIX

本文链接: https://www.lsjlt.com/news/383235.html(转载时请注明来源链接)

有问题或投稿请发送至: 邮箱/279061341@qq.com    QQ/279061341

本篇文章演示代码以及资料文档资料下载

下载Word文档到电脑,方便收藏和打印~

下载Word文档
猜你喜欢
  • IPC之System V vs POSIX
    文章目录 IPC示例共享内存POSIX shmSystem V shm IPC 当谈到IPC(Inter-Process Communication,进程间通信)时,它是指不同进程之间进行数据交换和通信的机制。 它允许...
    99+
    2023-08-30
    linux
  • 浅谈并发处理PHP进程间通信之System V IPC
    目录前言Unix System V IPC信号量共享内存消息队列函数介绍ftoksemaphore函数shared_memory函数代码实现小结前言 它的安装和使用非常简单,在编译 ...
    99+
    2024-04-02
软考高级职称资格查询
编程网,编程工程师的家园,是目前国内优秀的开源技术社区之一,形成了由开源软件库、代码分享、资讯、协作翻译、讨论区和博客等几大频道内容,为IT开发者提供了一个发现、使用、并交流开源技术的平台。
  • 官方手机版

  • 微信公众号

  • 商务合作