linux 进程间通信系列7,使用pthread mutex

#include <stdio.h>
#include <stdlib.h>
#include <unistd.h>
#include <sys/shm.h>
#include <pthread.h>
#include <sys/wait.h> int main(){
pthread_mutex_t *m;
pthread_mutexattr_t mat;
int shmid;
pid_t pid; shmid = shmget(IPC_PRIVATE, sizeof(pthread_mutex_t), 0600);
if(shmid < 0){
perror("shmget");
return 1;
} m = (pthread_mutex_t*)shmat(shmid, NULL, 0); //准备设定mutex的attribute
pthread_mutexattr_init(&mat); //利用mutex进行进程间的通信
//底下这句没有的话,这个mutex只在本进程间有作用
if(pthread_mutexattr_setpshared(&mat, PTHREAD_PROCESS_SHARED) != 0){
perror("pthread_mutexattr_setpshared");
return 1;
}
pthread_mutex_init(m, &mat); pid = fork(); printf("[%s] before pthread_mutex_lock()\n",
pid == 0 ? "child" : "parent"); if(pthread_mutex_lock(m) != 0){
perror("pthread_mutex_lock");
return 1;
} printf("[%s] press enter\n", pid == 0 ? "child" : "parent");
getchar(); if(pthread_mutex_unlock(m) != 0){
perror("pthread_mutex_unlock");
return 1;
} printf("[%s] after pthread_mutex_lock()\n",
pid == 0 ? "child" : "parent"); shmdt(m); if(pid != 0){
wait(NULL);//wait child process to complete
printf("[%s] after wait()\n", pid == 0 ? "child" : "parent"); //delete shared memery
if(shmctl(shmid, IPC_RMID, NULL) != 0){
perror("shmctl");
return 1;
}
} return 0;
}

github源代码

编译方法:

g++ -g process-41-pthread-mutex.cpp -std=c++11 -pthread

运行结果:

[parent] before pthread_mutex_lock()
[parent] press enter
[child] before pthread_mutex_lock()
敲回车
[parent] after pthread_mutex_lock()
[child] press enter
敲回车
[child] after pthread_mutex_lock()
[parent] after wait()

c/c++ 学习互助QQ群:877684253

本人微信:xiaoshitou5854

c/c++ linux 进程间通信系列7,使用pthread mutex的更多相关文章

  1. c/c++ linux 进程间通信系列6,使用消息队列(message queue)

    linux 进程间通信系列6,使用消息队列(message queue) 概念:消息排队,先进先出(FIFO),消息一旦出队,就从队列里消失了. 1,创建消息队列(message queue) 2,写 ...

  2. c/c++ linux 进程间通信系列5,使用信号量

    linux 进程间通信系列5,使用信号量 信号量的工作原理: 由于信号量只能进行两种操作等待和发送信号,即P(sv)和V(sv),他们的行为是这样的: P(sv):如果sv的值大于零,就给它减1:如果 ...

  3. c/c++ linux 进程间通信系列4,使用共享内存

    linux 进程间通信系列4,使用共享内存 1,创建共享内存,用到的函数shmget, shmat, shmdt 函数名 功能描述 shmget 创建共享内存,返回pic key shmat 第一次创 ...

  4. c/c++ linux 进程间通信系列3,使用socketpair,pipe

    linux 进程间通信系列3,使用socketpair,pipe 1,使用socketpair,实现进程间通信,是双向的. 2,使用pipe,实现进程间通信 使用pipe关键点:fd[0]只能用于接收 ...

  5. c/c++ linux 进程间通信系列2,使用UNIX_SOCKET

    linux 进程间通信系列2,使用UNIX_SOCKET 1,使用stream,实现进程间通信 2,使用DGRAM,实现进程间通信 关键点:使用一个临时的文件,进行信息的互传. s_un.sun_fa ...

  6. c/c++ linux 进程间通信系列1,使用signal,kill

    linux 进程间通信系列1,使用signal,kill 信号基本概念:  软中断信号(signal,又简称为信号)用来通知进程发生了异步事件.进程之间可以互相通过系统调用kill发送软中断信号.内核 ...

  7. Linux 进程间通信系列之 信号

    信号(Signal) 信号是比较复杂的通信方式,用于通知接受进程有某种事件发生,除了用于进程间通信外,进程还可以发送信号给进程本身:Linux除了支持Unix早期信号语义函数sigal外,还支持语义符 ...

  8. 练习--LINUX进程间通信之消息队列MSG

    https://www.ibm.com/developerworks/cn/linux/l-ipc/part3/ 继续坚持,或许不能深刻理解,但至少要保证有印象. ~~~~~~~~~~~~~~ 消息队 ...

  9. 进程间通信系列 之 socket套接字及其实例

    进程间通信系列 之 概述与对比   http://blog.csdn.net/younger_china/article/details/15808685  进程间通信系列 之 共享内存及其实例   ...

随机推荐

  1. Python内置函数(56)——set

    英文文档: class set([iterable]) Return a new set object, optionally with elements taken from iterable. s ...

  2. PyCharm证书过期:Your license has expired

    报错“your evaluation license has expired, pycharm will now exit”1.解决步骤,点击‘Activation code’,授权激活pycharm ...

  3. 低延时的P2P HLS直播技术实践

    本文根据4月21日OSC源创会·武汉站的现场分享为蓝本,重新整理.以下是演讲内容: 近几年,随着直播.短视频等视频领域对带宽要求的提升以及CDN行业竞争的加剧,很多CDN公司开始往P2P-CDN方向发 ...

  4. 微信小程序代码构成

    一.小程序代码 app.json 是当前小程序的全局配置,包括了小程序的所有页面路径.界面表现.网络超时时间.底部tab等. { "pages":[ "pages/ind ...

  5. redis 系列22 复制Replication (下)

    一. 复制环境准备 1.1 主库环境(172.168.18.201) 环境 说明 操作系统版本 CentOS  7.4.1708  IP地址 172.168.18.201 网关Gateway 172. ...

  6. redis 系列7 数据结构之跳跃表

    一.概述 跳跃表(skiplist)是一种有序数据结构,它通过在每个节点中维持多个指向其他节点的指针,从而达到快速访问节点的目的.在大部分情况下,跳跃表的效率可以和平衡树(关系型数据库的索引就是平衡树 ...

  7. oracle数据库之plsql可视化操作建表

    首先登录PL/SQL developer.   点击工具栏中的第一个图标,选择“表”.     右边会弹出一个窗口,我们以可视化方式来创建一个Table. 如下图所示,在“一般”选项卡中,输入“名称” ...

  8. leetcode — minimum-depth-of-binary-tree

    /** * Source : https://oj.leetcode.com/problems/minimum-depth-of-binary-tree/ * * * Given a binary t ...

  9. Linux基础知识第七讲,用户权限以及用户操作命令

    目录 Linux基础知识第七讲,用户权限以及用户操作命令 一丶简介linux用户,用户权限,组的概念. 1.1 基本概念 1.2 组 1.3 ls命令查看权限. 二丶用户权限修改命令 1.chmod ...

  10. RDIFramework.NET ━ .NET快速信息化系统开发框架 V3.2-模块管理按子系统进行分类管理

    在RDIFramework.NET以往的框架中,模块管理界面展示了整个框架所管理的所有模块,如果系统过多,达几十个甚至上百个子系统时,管理起来就非常的麻烦,不光加载效率会很低,页面展示也会很不友好.框 ...