码农知识堂 - 1000bd
  •   Python
  •   PHP
  •   JS/TS
  •   JAVA
  •   C/C++
  •   C#
  •   GO
  •   Kotlin
  •   Swift
  • Linux进程间通信


    在这里插入图片描述


    文章目录

    • 一、Linux 进程间通信
    • 二、Linux 进程间同步


    一、Linux 进程间通信

        进程间通信(IPC,InterProcess Communication)是指在不同进程之间传播或交换信息。IPC的方式通常有管道(包括无名管道和命名管道)、消息队列、信号量、共享内存、Socket(套接字)等。其中 Socket和支持不同主机上的两个进程IPC。

    1. 管道
    #include 
    int pipe(int fd[2]);  // 返回值:若成功返回0,失败返回-1;
    
    • 1
    • 2
    1. FIFO,也称为命名管道
    #include 
    // 返回值:成功返回0, 出错返回-1
    int mkfifo(const char* pathname, mode_t mode);
    
    • 1
    • 2
    • 3
    1. 消息队列
    #include 
    // 创建或打开消息队列:成功返回队列ID,失败返回-1
    int msgget(key_t key, int flag);
    // 添加消息:成功返回0,失败返回-1
    int msgsnd(int msqid, const void ptr, size_t size, int flag);
    // 读取消息:成功返回消息数据的长度,失败返回-1
    int msgrcv(int msqid, void* ptr, size_t size, long type, int flag);
    // 控制消息队列:成功返回0, 失败返回-1
    int msgctl(int msqid, int cmd, struct msqid_ds * buf);
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    1. 信号量
    #include 
    // 创建或获取一个信号量组:若成功返回信号量集ID,失败返回-1
    int semget(key_t key, int num_sems, int sem_flags);
    // 对信号量组进行操作,改变信号量的值:成功返回0,失败返回-1
    int semop(int semid, struct sembuf semoparray[], size_t numops);  
    // 控制信号量的相关信息
    int semctl(int semid, int sem_num, int cmd, ...);
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    1. 共享内存
    #include 
    // 创建或获取一个共享内存:成功返回共享内存ID,失败返回-1
    int shmget(key_t key, size_t size, int flag);
    // 连接共享内存到当前进程的地址空间:成功返回指向共享内存的指针,失败返回-1
    void shmat(int shm_id, const void *addr, int flag);
    // 断开与共享内存的连接:成功返回0,失败返回-1
    int shmdt(void addr); 
    // 控制共享内存的相关信息:成功返回0,失败返回-1
    int shmctl(int shm_id, int cmd, struct shmid_ds *buf);
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9

    6种Linux进程间的通信方式
    Linux进程间通信详解(最全)
    Linux系统-进程间通信

    二、Linux 进程间同步

       常用的同步方式有:互斥锁、条件变量、读写锁、记录锁(文件锁)和信号灯,以下为互斥锁同步和条件变量同步示例。

    #include 
    #include 
    #include 
    #include 
    #include 
    #include 
    #include 
     
    #define SHM_SIZE 4096
    #define SHM_NAME "/myshm"
    #define SEM_NAME "/mysem"
     
    typedef struct {
        pthread_mutex_t mutex;
        pthread_cond_t cond;
        char buffer[SHM_SIZE];
    } shm_t;
     
    int main(int argc, char *argv[]) {
        int fd, pid;
        shm_t *shm;
        pthread_mutexattr_t mutex_attr;
        pthread_condattr_t cond_attr;
     
        // 创建共享内存区域
        fd = shm_open(SHM_NAME, O_CREAT | O_RDWR, S_IRUSR | S_IWUSR);
        if (fd < 0) {
            perror("shm_open");
            exit(1);
        }
     
        // 设置共享内存大小
        if (ftruncate(fd, sizeof(shm_t)) < 0) {
            perror("ftruncate");
            exit(1);
        }
     
        // 将共享内存映射到进程地址空间
        shm = mmap(NULL, sizeof(shm_t), PROT_READ | PROT_WRITE, MAP_SHARED, fd, 0);
        if (shm == MAP_FAILED) {
            perror("mmap");
            exit(1);
        }
     
        // 初始化互斥量属性和条件变量属性
        pthread_mutexattr_init(&mutex_attr);
        pthread_condattr_init(&cond_attr);
        pthread_mutexattr_setpshared(&mutex_attr, PTHREAD_PROCESS_SHARED);
        pthread_condattr_setpshared(&cond_attr, PTHREAD_PROCESS_SHARED);
     
        // 初始化互斥量和条件变量
        pthread_mutex_init(&shm->mutex, &mutex_attr);
        pthread_cond_init(&shm->cond, &cond_attr);
     
        // 创建子进程
        pid = fork();
        if (pid < 0) {
            perror("fork");
            exit(1);
        }
     
        if (pid == 0) {
            // 子进程写入共享内存
            sleep(1);
            pthread_mutex_lock(&shm->mutex);
            sprintf(shm->buffer, "Hello, world!");
            pthread_cond_signal(&shm->cond);
            pthread_mutex_unlock(&shm->mutex);
            exit(0);
        } else {
            // 父进程读取共享内存
            pthread_mutex_lock(&shm->mutex);
            pthread_cond_wait(&shm->cond, &shm->mutex);
            printf("Received message: %s\n", shm->buffer);
            pthread_mutex_unlock(&shm->mutex);
        }
     
        // 删除共享内存
        if (shm_unlink(SHM_NAME) < 0) {
            perror("shm_unlink");
            exit(1);
        }
     
        return 0;
    }
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11
    • 12
    • 13
    • 14
    • 15
    • 16
    • 17
    • 18
    • 19
    • 20
    • 21
    • 22
    • 23
    • 24
    • 25
    • 26
    • 27
    • 28
    • 29
    • 30
    • 31
    • 32
    • 33
    • 34
    • 35
    • 36
    • 37
    • 38
    • 39
    • 40
    • 41
    • 42
    • 43
    • 44
    • 45
    • 46
    • 47
    • 48
    • 49
    • 50
    • 51
    • 52
    • 53
    • 54
    • 55
    • 56
    • 57
    • 58
    • 59
    • 60
    • 61
    • 62
    • 63
    • 64
    • 65
    • 66
    • 67
    • 68
    • 69
    • 70
    • 71
    • 72
    • 73
    • 74
    • 75
    • 76
    • 77
    • 78
    • 79
    • 80
    • 81
    • 82
    • 83
    • 84
    • 85

    【Linux线程同步专题】一、什么是线程同步、互斥量与死锁
    【Linux线程同步专题】二、读写锁
    【Linux线程同步专题】三、条件变量
    【Linux线程同步专题】四、信号量
    【Linux线程同步专题】五、进程间同步

    Linux进程间共享内存通信时如何同步?(附源码)
    linux进程同步机制,五个常用的
    Linux操作系统进程同步的几种方式及基本原理

       

  • 相关阅读:
    Java的HTTPClient工具类(附代码实现)
    Win10下基于VS2015编译SQLite3源码
    声网3D在线互动场景空间音频的实时渲染——如何把“声临其境”推向极致
    分布式文件服务器——Windows环境MinIO的三种部署模式
    用DIV+CSS技术设计的餐饮美食网页与实现制作(web前端网页制作课作业)HTML+CSS+JavaScript美食汇响应式美食菜谱网站模板
    基于JavaSwing开发远程控制系统 课程设计 大作业源码 毕业设计
    iwebsec靶场 SQL注入漏洞通关笔记5- updatexml注入(报错型盲注)
    CCF-CSP真题《202209-3—防疫大数据》思路+python题解
    新手选MT4老手选MT5,有道理吗?anzo capital昂首资本这样分析
    STC15单片机-通过PWM调整灯亮度
  • 原文地址:https://blog.csdn.net/Liuqz2009/article/details/133786839
  • 最新文章
  • 攻防演习之三天拿下官网站群
    数据安全治理学习——前期安全规划和安全管理体系建设
    企业安全 | 企业内一次钓鱼演练准备过程
    内网渗透测试 | Kerberos协议及其部分攻击手法
    0day的产生 | 不懂代码的"代码审计"
    安装scrcpy-client模块av模块异常,环境问题解决方案
    leetcode hot100【LeetCode 279. 完全平方数】java实现
    OpenWrt下安装Mosquitto
    AnatoMask论文汇总
    【AI日记】24.11.01 LangChain、openai api和github copilot
  • 热门文章
  • 十款代码表白小特效 一个比一个浪漫 赶紧收藏起来吧!!!
    奉劝各位学弟学妹们,该打造你的技术影响力了!
    五年了,我在 CSDN 的两个一百万。
    Java俄罗斯方块,老程序员花了一个周末,连接中学年代!
    面试官都震惊,你这网络基础可以啊!
    你真的会用百度吗?我不信 — 那些不为人知的搜索引擎语法
    心情不好的时候,用 Python 画棵樱花树送给自己吧
    通宵一晚做出来的一款类似CS的第一人称射击游戏Demo!原来做游戏也不是很难,连憨憨学妹都学会了!
    13 万字 C 语言从入门到精通保姆级教程2021 年版
    10行代码集2000张美女图,Python爬虫120例,再上征途
Copyright © 2022 侵权请联系2656653265@qq.com    京ICP备2022015340号-1
正则表达式工具 cron表达式工具 密码生成工具

京公网安备 11010502049817号