1.互斥量数据结构

D:\005-代码\001-开源项目源码\005-gnuc\glibc-2.31.tar\glibc-2.31\glibc-2.31\sysdeps\nptl\bits\pthreadtypes.h
typedef union
{struct __pthread_mutex_s __data;char __size[__SIZEOF_PTHREAD_MUTEX_T];long int __align;
} pthread_mutex_t;
D:\005-代码\001-开源项目源码\005-gnuc\glibc-2.31.tar\glibc-2.31\glibc-2.31\sysdeps\x86\nptl\bits\struct_mutex.h
struct __pthread_mutex_s
{int __lock;unsigned int __count;int __owner;
#if _MIPS_SIM == _ABI64unsigned int __nusers;
#endif/* KIND must stay at this position in the structure to maintainbinary compatibility with static initializers.  */int __kind;
#if _MIPS_SIM == _ABI64int __spins;__pthread_list_t __list;
# define __PTHREAD_MUTEX_HAVE_PREV      1
#elseunsigned int __nusers;__extension__ union{int __spins;__pthread_slist_t __list;};
# define __PTHREAD_MUTEX_HAVE_PREV      0
#endif
};
问题:源码sysdeps中的htl和nptl目录之间有什么区别?两个目录中的同名文件都对它做了定义,但定义的不一样。

2.互斥量属性数据结构

D:\005-代码\001-开源项目源码\005-gnuc\glibc-2.31.tar\glibc-2.31\glibc-2.31\sysdeps\htl\bits\pthreadtypes.h
#include <bits/types/struct___pthread_mutexattr.h>
typedef struct __pthread_mutexattr pthread_mutexattr_t;
D:\005-代码\001-开源项目源码\005-gnuc\glibc-2.31.tar\glibc-2.31\glibc-2.31\sysdeps\htl\bits\types\struct___pthread_mutexattr.h
struct __pthread_mutexattr
{int __prioceiling;enum __pthread_mutex_protocol __protocol;enum __pthread_process_shared __pshared;enum __pthread_mutex_type __mutex_type;
};

3.互斥量属性设置的相关函数

头文件:
#include <pthread.h>
函数:
int pthread_mutexattr_destroy(pthread_mutexattr_t *attr);
int pthread_mutexattr_init(pthread_mutexattr_t *attr);
返回值:Upon successful completion, pthread_mutexattr_destroy() and pthread_mutexattr_init() shall return zero; otherwise, an error number shall be returned to indicate the error.
ERRORSThe pthread_mutexattr_destroy() function may fail if:EINVAL The value specified by attr is invalid.The pthread_mutexattr_init() function shall fail if:ENOMEM Insufficient memory exists to initialize the mutex attributes object.These functions shall not return an error code of [EINTR].The following sections are informative.头文件:
#include <pthread.h>
函数:int pthread_mutexattr_getpshared(const pthread_mutexattr_t *restrict attr, int *restrict pshared);int pthread_mutexattr_setpshared(pthread_mutexattr_t *attr,int pshared);这里pshared的取值是1.线程锁PTHREAD_PROCESS_PRIVARE(mutex的默认属性就是线程锁)2.进程锁PTHREAD_PROCESS_SHARED
返回值:Upon successful completion, pthread_mutexattr_setpshared() shall return zero; otherwise, an error number shall be returned to indicate the error.Upon successful completion, pthread_mutexattr_getpshared() shall return zero and store the value of the process-shared attribute of attr into the object referenced by the pshared  parameter.  Other‐wise, an error number shall be returned to indicate the error.ERRORSThe pthread_mutexattr_getpshared() and pthread_mutexattr_setpshared() functions may fail if:EINVAL The value specified by attr is invalid.The pthread_mutexattr_setpshared() function may fail if:EINVAL The new value specified for the attribute is outside the range of legal values for that attribute.These functions shall not return an error code of [EINTR].The following sections are informative.

4.利用互斥量实现进程间同步的示例代码

/*
作者:Muten
编码时间:20201017
编码目的:演示借助互斥锁和共享内存实现进程间同步问题
代码功能:借助互斥锁和共享内存实现进程间同步问题
测试环境:Linux localhost.localdomain 3.10.0-957.el7.x86_64 #1 SMP Thu Nov 8 23:39:32 UTC 2018 x86_64 x86_64 x86_64 GNU/Linux
版本信息:VER1.0
说明:如果line40,43,49,52行被注释掉,测试会出现达不到我们想要计算的300000
*/#include <stdio.h>
#include <unistd.h>
#include <fcntl.h>
#include <stdlib.h>
#include <string.h>
#include <pthread.h>
#include <sys/mman.h>
#include <sys/wait.h>struct mt{int num ;pthread_mutex_t mutex;pthread_mutexattr_t mutexattr;
};
int main()
{int i ;struct mt *mm;pid_t pid;mm = mmap(NULL,sizeof(*mm),PROT_READ|PROT_WRITE,MAP_SHARED|MAP_ANON,-1,0);// mm接受的是mmap的返回值,这个变量存在于映射区内,父子进程都可以访问和改变这块区域memset(mm,0,sizeof(*mm));// 防止垃圾数据pthread_mutexattr_init(&mm->mutexattr);// 初始化互斥锁属性pthread_mutexattr_setpshared(&mm->mutexattr,PTHREAD_PROCESS_SHARED); // 设置互斥锁的属性pthread_mutex_init(&mm->mutex,&mm->mutexattr); // 初始化互斥锁pid = fork();if(pid == 0){for(i = 0;i < 100000;i++){pthread_mutex_lock(&mm->mutex);(mm->num)++;printf("---------------------------------child-----num++ %d.\n",mm->num);pthread_mutex_unlock(&mm->mutex);}}else if(pid > 0){for(i = 0;i< 100000;i++){    pthread_mutex_lock(&mm->mutex);mm->num += 2;printf("--parent-----num++ %d.\n",mm->num);pthread_mutex_unlock(&mm->mutex);}wait(NULL);}pthread_mutexattr_destroy(&mm->mutexattr);pthread_mutex_destroy(&mm->mutex);munmap(mm,sizeof(*mm));return 0;
}

利用互斥量实现进程间同步相关推荐

  1. linux操作系统之信号量、互斥量在进程间的同步、文件锁

    (1)信号量:进化版的互斥量 多个线程间对某个对象的部分数据进行共享,使用互斥锁是没有办法实现的,只能将整个数据对象锁住.这样虽然达到了多线程操作数据共享的目的,却导致线程并发性下降. 信号量:相对折 ...

  2. 进程间同步(互斥量、信号量)

    进程间同步可以使用互斥量mutex(互斥锁).信号量和文件锁. 进程间同步使用信号量: int sem_init(sem_t *sem, int pshared, unsigned int value ...

  3. Visual C++利用互斥量同步线程实现文件读取进度条

    忘了原文的位置了. 一.前言 文件读取进度条的实现可以有很多种方法,常用的是在读取文件的过程中隔一定时间向对话框发送消息以控制进度条的位置,但是这种方法很难确定隔多少时问发送一个消息,因为文件的大小是 ...

  4. 利用管道实现进程间同步

    进程间同步是指进程的运行有先后顺序,如A进程等待B进程执行完某个动作A进程才能继续往下运行.进程间通信的方法都可以用来同步,只是操作是否方便的一个问题.复习了UNIX高级编程,觉得POSIX的信号量是 ...

  5. linux 进程间读写锁,Linux系统编程—进程间同步

    我们知道,线程间同步有多种方式,比如:信号量.互斥量.读写锁,等等.那进程间如何实现同步呢?本文介绍两种方式:互斥量和文件锁. ##互斥量mutex 我们已经知道了互斥量可以用于在线程间同步,但实际上 ...

  6. 进程间同步的几种方法

    什么是临界区? 每个进程中访问临界资源的那段程序称为临界区(临界资源是一次仅允许一个进程使用的共享资源).每次只准许一个进程进入临界区,进入后不允许其他进程进入. 进程间同步 把异步环境下的一组并发进 ...

  7. 如何保证进程间同步工作_系统设计硬核知识(2)——操作系统的进程管理

    操作系统基本原理包含以下 5 大管理. 我们先来说说进程管理. 因为处理机是计算机系统的核心资源,所以整个操作系统的重心是处理机管理. 处理机管理中最基本的.最重要的概念是进程.进程是系统并发执行的体 ...

  8. android p获取通话记录_Android 底层的进程间同步机制

    作者:Android面试官 进程间通信的经典实现 进程间通信(Inter-process communication,IPC)指运行在不同进程中的若干线程间的数据交换,可发生在一台机器上,也可通过网络 ...

  9. (28)System Verilog进程间同步(事件event)

    (28)System Verilog进程间同步(事件event) 1.1 目录 1)目录 2)FPGA简介 3)System Verilog简介 4)System Verilog进程间同步(事件eve ...

  10. (30)System Verilog进程间同步(邮箱mailbox)

    (30)System Verilog进程间同步(邮箱mailbox) 1.1 目录 1)目录 2)FPGA简介 3)System Verilog简介 4)System Verilog进程间同步(邮箱m ...

最新文章

  1. mnist学习实例(2)
  2. 黑鲨科学计算机,科学技术篇:玩家注意黑鲨一口气发布两款重磅新品
  3. 前端之JQuery(一)
  4. Python股票分析系列——系列介绍和获取股票数据.p1
  5. 【辨异】limit, limitation
  6. 关于WPF的Binding 的 ConverterParameter 参数的动态设置
  7. 凸优化第七章统计估计 7.5实验设计
  8. 2021年5月软考网络工程师上午真题(带答案解析)上
  9. 最新版网站推广完全手册
  10. Sematic Web 学习笔记---现代逻辑导论
  11. jackson-databind反序列化漏洞
  12. 岭南师范学院计算机考试考场,广东专插本考场安排在哪?附:2018年考场详细安排表~...
  13. win10下 oracle安装(11g)
  14. Google.cn刚上不去了
  15. 谨以此篇文章开启我的博客生涯
  16. 感量越大抑制频率约低_电子产品:开关电源系统EMI传导快速设计理论(讲义部分)...
  17. jsp网站服务器配置
  18. 8支团队正在努力构建下一代以太坊Ethereum 2.0
  19. java爬虫---Jsoup
  20. 四层电梯西门子S7-200PLC梯形图程序

热门文章

  1. win10企业版2016长期服务激活教程
  2. JAVA 小易爱回文
  3. 如何在为知笔记(Wiz)和印象笔记(Evernote)之间相互迁移笔记?
  4. python算法之lowb排序三人组(冒泡排序,插入排序,选择排序)
  5. 什么是嵌入式人工智能
  6. 数字调制解调技术:第7章-COSATAS环载波频率参数设计问题
  7. 恭主驾到:最新的汽车年审新规,都了解了吗?
  8. Win7怎么设置自动关机?Win7设置自动关机的方法
  9. 《修C传》——初始C语言 <凝气篇>
  10. php dth网络节点,基于 DHT 网络的磁力链接和BT种子的搜索引擎架构