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. Spring Boot 最核心的 25 个注解,都是干货!

    学习和应用 Spring Boot 有一些时间了,你们对 Spring Boot 注解了解有多少呢?今天栈长我给大家整理了 Spring Boot 最核心的 25 个注解,都是干货! 你所需具备的基础 ...

  2. Redis Windows下安装方法

    一.安装 首先在网上下载Redis,下载地址:https://github.com/MicrosoftArchive/redis/releases 根据电脑系统的实际情况选择32位还是64位,在这里我 ...

  3. Python—day18 dandom、shutil、shelve、系统标准流、logging

    一.dandom模块 (0, 1) 小数:random.random() [1, 10] 整数:random.randint(1, 10) [1, 10) 整数:random.randrange(1, ...

  4. Java Runtime.exec()的使用

    Sun的doc里其实说明还有其他的用法: exec(String[] cmdarray, String[] envp, File dir) Executes the specified command ...

  5. Python内置函数(63)——super

    英文文档: super([type[, object-or-type]]) Return a proxy object that delegates method calls to a parent ...

  6. CompletableFuture基本用法

    异步计算 所谓异步调用其实就是实现一个可无需等待被调用函数的返回值而让操作继续运行的方法.在 Java 语言中,简单的讲就是另启一个线程来完成调用中的部分计算,使调用继续运行或返回,而不需要等待计算结 ...

  7. Python读写文件你真的了解吗?

    内容概述 Python文件操作 针对大文件如何操作 为什么不能修改文件? 你需要知道的基本知识 1. Python文件操作 这一部分内容不是重点,因为很简单网上很多,主要看看文件操作的步骤就可以了. ...

  8. Asp.Net SignalR Hub类中的操作详解

    Hub类中的操作 在服务端我们要通过Hub类做一系列操作,下面就说说我们都可以做什么操作 客户端的发送消息操作 调用所有的客户端的helloClient方法 Clients.All.helloClie ...

  9. Linux命令收集

    文件处理命令:ls 功能描述:显示目录文件 命令英文原意:list 命令所在路径:/bin/ls 执行权限:所有用户 语法:  ls  选项[-ald]  [文件或目录] -a    显示所有文件,包 ...

  10. docker 常用命令和使用

    首先安装Docker CE 在ubantu上,参照https://docs.docker.com/install/linux/docker-ce/ubuntu/#set-up-the-reposito ...