c/c++ linux 进程间通信系列7,使用pthread mutex
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;
}
编译方法:
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的更多相关文章
- c/c++ linux 进程间通信系列6,使用消息队列(message queue)
linux 进程间通信系列6,使用消息队列(message queue) 概念:消息排队,先进先出(FIFO),消息一旦出队,就从队列里消失了. 1,创建消息队列(message queue) 2,写 ...
- c/c++ linux 进程间通信系列5,使用信号量
linux 进程间通信系列5,使用信号量 信号量的工作原理: 由于信号量只能进行两种操作等待和发送信号,即P(sv)和V(sv),他们的行为是这样的: P(sv):如果sv的值大于零,就给它减1:如果 ...
- c/c++ linux 进程间通信系列4,使用共享内存
linux 进程间通信系列4,使用共享内存 1,创建共享内存,用到的函数shmget, shmat, shmdt 函数名 功能描述 shmget 创建共享内存,返回pic key shmat 第一次创 ...
- c/c++ linux 进程间通信系列3,使用socketpair,pipe
linux 进程间通信系列3,使用socketpair,pipe 1,使用socketpair,实现进程间通信,是双向的. 2,使用pipe,实现进程间通信 使用pipe关键点:fd[0]只能用于接收 ...
- c/c++ linux 进程间通信系列2,使用UNIX_SOCKET
linux 进程间通信系列2,使用UNIX_SOCKET 1,使用stream,实现进程间通信 2,使用DGRAM,实现进程间通信 关键点:使用一个临时的文件,进行信息的互传. s_un.sun_fa ...
- c/c++ linux 进程间通信系列1,使用signal,kill
linux 进程间通信系列1,使用signal,kill 信号基本概念: 软中断信号(signal,又简称为信号)用来通知进程发生了异步事件.进程之间可以互相通过系统调用kill发送软中断信号.内核 ...
- Linux 进程间通信系列之 信号
信号(Signal) 信号是比较复杂的通信方式,用于通知接受进程有某种事件发生,除了用于进程间通信外,进程还可以发送信号给进程本身:Linux除了支持Unix早期信号语义函数sigal外,还支持语义符 ...
- 练习--LINUX进程间通信之消息队列MSG
https://www.ibm.com/developerworks/cn/linux/l-ipc/part3/ 继续坚持,或许不能深刻理解,但至少要保证有印象. ~~~~~~~~~~~~~~ 消息队 ...
- 进程间通信系列 之 socket套接字及其实例
进程间通信系列 之 概述与对比 http://blog.csdn.net/younger_china/article/details/15808685 进程间通信系列 之 共享内存及其实例 ...
随机推荐
- 洛谷P1036选数(素数+组合数)
题目链接:https://www.luogu.org/problemnew/show/P1036 主要考两个知识点:判断一个数是否为素数.从n个数中选出m个数的组合 判断一个数是否为素数: 素数一定是 ...
- Python——day11 函数(对象、名称空间、作用域、嵌套、闭包)
一.函数对象 函数名就是存放了函数的内存地址,存放了内存地址的变量都是对象,即 函数名 就是 函数对象 函数对象的应用 1. 可以直接被引用 fn = cp_fn 2 .可以当作函数参数传递 c ...
- 微信小程序实战--集阅读与电影于一体的小程序项目(一)
1.首页欢迎界面 项目目录结构 新建项目ReaderMovie,然后新建文件,结构如下 welcome.wxml <view class='container'> <image cl ...
- 实现网站页面的QQ临时会话,分享到空间微博等按钮.
一 qq临时会话 要实现qq临时会话首先要到qq在线状态官网开通qq在线状态,其中临时对话也分为加密和未加密. 1.1:加密模式 <a target="_blank" hre ...
- Lottie 动画里有图片怎么办?设计师小姐姐也能帮你减少开发量!
一.序 Hi,大家好,我是承香墨影! Lottie 是 Airbnb 开源的一套跨平台的完整解决方案,设计师只需要使用 After Effectes (之后简称 AE)设计出动画之后,使用 Lotti ...
- 从0打卡leetcode之day 5 ---两个排序数组的中位数
前言 我靠,才坚持了四天,就差点不想坚持了.不行啊,我得把leetcode上的题给刷完,不然怕是不好进入bat的大门. 题目描述 给定两个大小为 m 和 n 的有序数组 nums1 和 nums2 . ...
- ansible学习系列2-ansible常用模块使用
1. 查看支持的模块 [root@localhost ~]# ansible-doc -l 这里我们看下ansible的支持的模块个数 [root@localhost ~]# ansible-doc ...
- cache2go源码最后一讲 - examples
先看一下我们讲到哪里了: cache2go的源码前面我们已经讲完了cacheitem和cachetable的实现,今天cahce和examples会一起讲完~ 1.cache.go源码 ...
- iOS 字典实现原理
在目前的开发中,NSDictionary是经常被使用,不过很少人会研究字典NSDictionary底层的实现,下面我们来一起看一下NSDictionary的实现原理. 一.字典原理 字典通过使用- ( ...
- C#组件系列——又一款日志组件:Elmah的学习和分享
前言:好久没动笔了,都有点生疏,12月都要接近尾声,可是这月连一篇的产出都没有,不能坏了“规矩”,今天还是来写一篇.最近个把月确实很忙,不过每天早上还是会抽空来园子里逛逛.一如既往,园子里每年这个时候 ...