利用互斥量实现进程间同步

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 == _ABI64
  unsigned int __nusers;
#endif
  /* KIND must stay at this position in the structure to maintain
     binary compatibility with static initializers.  */
  int __kind;
#if _MIPS_SIM == _ABI64
  int __spins;
  __pthread_list_t __list;
# define __PTHREAD_MUTEX_HAVE_PREV      1
#else
  unsigned 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.
ERRORS
       The 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.

ERRORS
       The 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;
}

 

 

 

  • 0
    点赞
  • 3
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值