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. 程序员如何面试才能拿到offer

    一.概述 面试,难还是不难?取决于面试者的底蕴(气场+技能).心态和认知及沟通技巧.面试其实可以理解为一场聊天和谈判,在这过程中有心理.思想上的碰撞和博弈.其实你只需要搞清楚一个逻辑:“面试官为什么会 ...

  2. 如何为ASP.NET Core的强类型配置对象添加验证

    原文: Adding validation to strongly typed configuration objects in ASP.NET Core 作者: Andrew Lock 译文: La ...

  3. Node.js 中的 stream

    什么是 stream Stream 借鉴自 Unix 编程哲学中的 pipe. Unix shell 命令中,管道式的操作 | 将上一个命令的输出作为下一个命令的输入.Node.js stream 中 ...

  4. SpringBoot入门教程(十五)集成Druid

    Druid是阿里巴巴开源平台上一个数据库连接池实现,它结合了C3P0.DBCP.PROXOOL等DB池的优点,同时加入了日志监控,可以很好的监控DB池连接和SQL的执行情况,可以说是针对监控而生的DB ...

  5. Django中的templates(你的HTML页面放哪里)

    本文回答Django里面的HTML文件应该怎么放,以及Django是如何查找模板文件的. 到目前为止我们没有使用HTML页面,在之前的说明中所有内容都是写死在程序里的,如果你想改变内容就要修改代码.但 ...

  6. C# 反射,通过类名、方法名调用方法

    在 C# 代码中,有些时候只知道方法的名字(string),需要调用该方法,那么就需要用到 C# 的反射机制.下面是一个简单的 demo. using System; using System.Ref ...

  7. Nacos 发布 v0.8.0 Pre-GA版本,安全稳定上生产?

    服务注册和服务配置开源项目 Nacos 本周发布了 v0.8.0 Pre-GA 版本,作为开源项目生命周期中的里程碑版本之一,v0.8.0 Pre-GA版本支持登录.命名空间.Metrics监控(对接 ...

  8. Java设计模式总结

    什么是设计模式   设计模式(Design pattern)是一套被反复使用.多数人知晓的.经过分类编目的.代码设计经验的总结.通过对这些设计模式的合理使用能够是我们的系统更加的健壮. 六大设计原则 ...

  9. InnoSetup 使用

    目录 简介 示例脚本 相关参考 在进行 WPF 程序打包发布的时候如果对程序打包没有特别高的要求,InnoSetup 足以胜任普通的程序打包发布需求,它支持安装包加密,安装包升级安装,注册表操作等常规 ...

  10. pl/sql to_date

    to_date 函数:TO_DATE( string1 [, format_mask] [, nls_language] ) 后面两个函数为可选 ,意思将字符串类型转换为时间类型 , 可以自定义时间格 ...