sem信号量与死锁的边缘
1. 演示一个例子,出现死锁,用strace debug得到
#include<stdio.h>
#include<pthread.h>
#include<stdlib.h>
#include<string.h>
#include<semaphore.h> sem_t sem; typedef struct _cal_
{
int sum;
}CAL;
CAL cal; void *calculate(void*arg)
{
int i;
int sum;
for(i = 0; i < 100; i++)
{
cal.sum = cal.sum + i;
}
sem_post(&sem);
} void *sum(void *arg)
{
sem_wait(&sem);
printf("the sum is %d\n",cal.sum);
} void *times(void *arg)
{
sem_wait(&sem);
printf("the times is %d\n",cal.sum * cal.sum);
} int main(void)
{
int ret=-1;
pthread_t th[3];
sem_init(&sem,0,0);
ret = pthread_create(&th[0],NULL,calculate,NULL);
if (ret != 0)
{
printf("Pthread_create failed\n");
return -1;
} ret = pthread_create(&th[1],NULL,sum,NULL);
if (ret != 0)
{
printf("Pthread_create failed\n");
return -1;
} ret = pthread_create(&th[1],NULL,times,NULL);
if (ret != 0)
{
printf("Pthread_create failed\n");
return -1;
} pthread_join(th[0],NULL);
pthread_join(th[1],NULL);
pthread_join(th[2],NULL);
return 0;
}
mprotect(0xb5d46000, 4096, PROT_NONE) = 0
clone(child_stack=0xb6546464, flags=CLONE_VM|CLONE_FS|CLONE_FILES|CLONE_SIGHAND|CLONE_THREAD|CLONE_SYSVSEM|CLONE_SETTLS|CLONE_PARENT_SETTID|CLONE_CHILD_CLEARTID, parent_tidptr=0xb6546ba8, {entry_number:6, base_addr:0xb6546b40, limit:1048575, seg_32bit:1, contents:0, read_exec_only:0, limit_in_pages:1, seg_not_present:0, useable:1}, child_tidptr=0xb6546ba8) = 17181
futex(0xb6546ba8, FUTEX_WAIT, 17181, NULL
futex(0xb7533ba8, FUTEX_WAIT, 17204, NULLthe times is 4950
) = 0
--- SIGSEGV (Segmentation fault) @ 0 (0) ---
+++ killed by SIGSEGV (core dumped) +++
Segmentation fault (core dumped)
上述的log表明,该code出现死锁,FUTEX_WAIT....... 而且Core Dumped 虽然times is 9801结果出来。(一开始测试不出来)
2. 基于信号量的问题,需要进行优化和解决:
更新一版代码:
#include<stdio.h>
#include<pthread.h>
#include<stdlib.h>
#include<string.h>
#include<semaphore.h> sem_t sem_cal;
sem_t sem_times; typedef struct _cal_
{
int sum;
}CAL;
CAL cal; void *calculate(void*arg)
{
int i;
int sum;
for(i = 0; i < 100; i++)
{
cal.sum = cal.sum + i;
}
sem_post(&sem_cal);
} void *sum(void *arg)
{
sem_wait(&sem_cal);
printf("the sum is %d\n",cal.sum);
sem_post(&sem_times);
} void *times(void *arg)
{
sem_wait(&sem_times);
printf("the times is %d\n",cal.sum * cal.sum);
} int main(void)
{
int ret=-1;
pthread_t th[3];
sem_init(&sem_cal,0,0);
sem_init(&sem_times,0,0);
ret = pthread_create(&th[0],NULL,calculate,NULL);
if (ret != 0)
{
printf("Pthread_create failed\n");
return -1;
} ret = pthread_create(&th[1],NULL,sum,NULL);
if (ret != 0)
{
printf("Pthread_create failed\n");
return -1;
} ret = pthread_create(&th[1],NULL,times,NULL);
if (ret != 0)
{
printf("Pthread_create failed\n");
return -1;
} pthread_join(th[0],NULL);
pthread_join(th[1],NULL);
pthread_join(th[2],NULL);
return 0;
}
但是出现:
the sum is 4950
the times is 24502500
Segmentation fault (core dumped)
futex(0xb65afba8, FUTEX_WAIT, 17277, NULL) = -1 EAGAIN (Resource temporarily unavailable)
--- SIGSEGV (Segmentation fault) @ 0 (0) ---
+++ killed by SIGSEGV (core dumped) +++
Segmentation fault (core dumped)
此时出现段错误,但是结果都正常。
Resource temporarily unavailable 表明资源冲突
3. 跟着解决这个段错误的问题:
原来是 ret = pthread_create(&th[1],NULL,times,NULL);同一个线程ID。
(DUANG)
#include<stdio.h>
#include<pthread.h>
#include<stdlib.h>
#include<string.h>
#include<semaphore.h> sem_t sem_cal;
sem_t sem_times; typedef struct _cal_
{
int sum;
}CAL;
CAL cal; void *calculate(void*arg)
{
int i;
int sum;
for(i = 0; i < 100; i++)
{
cal.sum = cal.sum + i;
}
sem_post(&sem_cal);
} void *sum(void *arg)
{
sem_wait(&sem_cal);
printf("the sum is %d\n",cal.sum);
sem_post(&sem_times);
} void *times(void *arg)
{
sem_wait(&sem_times);
printf("the times is %d\n",cal.sum * cal.sum);
} int main(void)
{
int ret=-1;
pthread_t th[3];
sem_init(&sem_cal,0,0);
sem_init(&sem_times,0,0);
ret = pthread_create(&th[0],NULL,calculate,NULL);
if (ret != 0)
{
printf("Pthread_create failed\n");
return -1;
} ret = pthread_create(&th[1],NULL,sum,NULL);
if (ret != 0)
{
printf("Pthread_create failed\n");
return -1;
} ret = pthread_create(&th[2],NULL,times,NULL);
if (ret != 0)
{
printf("Pthread_create failed\n");
return -1;
} pthread_join(th[0],NULL);
pthread_join(th[1],NULL);
pthread_join(th[2],NULL);
return 0;
}
该博客的目的是:
1. 通过多个信号量去完成业务。
2. 信号量和互斥锁的区别下节分析。
sem信号量与死锁的边缘的更多相关文章
- [并发编程 - 多线程:信号量、死锁与递归锁、时间Event、定时器Timer、线程队列、GIL锁]
[并发编程 - 多线程:信号量.死锁与递归锁.时间Event.定时器Timer.线程队列.GIL锁] 信号量 信号量Semaphore:管理一个内置的计数器 每当调用acquire()时内置计数器-1 ...
- C++多线程框架-----Mutex互斥和Sem信号量
互斥和信号量是多线程编程的两个基础,其原理就不详细说了,大家去看看操作系统的书或者网上查查吧. 对于互斥的实现,无论什么操作系统都离不开三个步骤 1.初始化互斥锁 2.锁操作 3.解锁操 ...
- JAVA通过信号量避免死锁
死锁是这样一种情形:多个线程同时被阻塞,它们中的一个或者全部都在等待某个资源被释放.由于线程被无限期地阻塞,因此程序不可能正常终止. 导致死锁的根源在于不适当地运用"synchronized ...
- python基础--GIL全局解释器锁、Event事件、信号量、死锁、递归锁
ps:python解释器有很多种,最常见的就是C python解释器 GIL全局解释器锁: GIL本质上是一把互斥锁:将并发变成串行,牺牲效率保证了数据的安全 用来阻止同一个进程下的多个线程的同时执行 ...
- 利用多个sem信号量在线程通讯
直接上代码,主要用到sem_trywait & sem_post #include<stdio.h> #include<pthread.h> #include<s ...
- 信号量sem 的用法
#include <semaphore.h> sem_t sem; sem_init(&sem, 0, 0); sem_post(&sem); sem_wait(& ...
- linux 进程间信号量管理程序之sem_timedwait使用
在开发过程中,有三个独立执行的程序模块,三个模块都对sqlite数据库进行读写操作.sqlite在linux共享性较差,所以须要增加相互排斥信号量解决三个模块訪问数据库该问题. 另外,在增加信号量后, ...
- 计算机网络通信、线程、tcp、udp通信及信号量等读书笔记
一.计算机网络 1.什么是计算机网络:把分布在不同地理位置的计算机与专门的网络设备用通信线路互相连成一个规模大.功能强的系统,从而使众多计算机可以方便地互相传递信息.共享软件.硬件.数据信息等.简单来 ...
- linux io 学习笔记(01)---锁,信号量
1.采用信号量访问:当有段临界代码,需要保证排他的访问一个资源. 2.sudo dmesg -c 消除dmesg缓冲 3.互斥锁:代表的是一种锁资源,互斥锁的工作原理是:保证对共享资源操作的原子性 ...
随机推荐
- Solution Set - Border Theory
我发现写 Solution Set 就不用写每道题的题意了,岂不美哉? 首先是一些奇妙结论. 定理 1(弱周期定理) 对于字符串 \(S\),若 \(S[:p]\) 和 \(S[:q]\) ...
- jenkins pipeline构建项目
以前用的jenkins自由风格发布代码.界面丑陋,出现问题位置不够清晰.今天改进一下流程使用jenkins pipeline构建项目. 学习使我快乐 步骤一.安装pipeline插件 点击系统管理-& ...
- 关于Windows安装两个不同版本的MySQL详细步骤
关于Windows安装两个不同版本的MySQL详细步骤 安装两个不同版本的数据库原因 由于大部分教程所使用的数据库为5.7版本,而我之前安装的是8.0版本. 在一些特殊情况下,低版本数据库不能动,高版 ...
- Oracle之数据库的连接
Oracle 默认用户 数据库创建完毕后,需要设置数据库的默认用户.Oracle中为管理员预置了两个用户分别是SYS和SYSTEM.SYS的初始密码为Oracle安装时设置的数据库口令 admin,在 ...
- Oracle之PL/SQL Developer的下载与安装
PL/SQL是什么? PL/SQL Developer是一个集成开发环境(以下简称PL/SQL),专门开发面向Oracle数据库的应用.PL/SQL也是一种程序语言,叫做过程化SQL语言(Proced ...
- ssh静态代理模式讲解与使用
--作者:飞翔的小胖猪 --创建时间:2021年5月18日 --修改时间:2021年5月23日 一.说明 SSH 为建立在应用层基础上的安全协议.SSH 是较可靠专为远程登录会话和其他网络服务提供安全 ...
- 五、ES6之对象
一.对象和属性和方法 JavaScript中对象: var person={name:"Jack",age:20}; 或: var name = "jack"; ...
- LeetCode-086-分隔链表
分隔链表 题目描述:给你一个链表的头节点 head 和一个特定值 x ,请你对链表进行分隔,使得所有 小于 x 的节点都出现在 大于或等于 x 的节点之前. 你应当 保留 两个分区中每个节点的初始相对 ...
- 在矩池云上复现 CVPR 2018 LearningToCompare_FSL 环境
这是 CVPR 2018 的一篇少样本学习论文:Learning to Compare: Relation Network for Few-Shot Learning 源码地址:https://git ...
- 重磅 | 腾讯云服务网格开源项目 Aeraki Mesh 加入 CNCF 云原生全景图
作者 赵化冰,腾讯云工程师,Aeraki Mesh 创始人,Istio member,Envoy contributor,目前负责 Tencent Cloud Mesh 研发工作. 摘要 近日,腾讯云 ...