C语言trap 可重入和线程安全
来源:互联网 发布:python整段注释快捷键 编辑:程序博客网 时间:2024/06/06 03:32
#include <signal.h>#include <stdio.h>#include <time.h>#include <unistd.h>void handler(int signum){ char result[100]; time_t now;// struct tm time1; now = time(NULL);// localtime_r(&now, &time1);// ctime_r(&now,result); strcpy(result,ctime(&now));// strftime(result, 100, "%T", &time1); printf("At %s, user pressed Ctrl-C\n", result);}int main (void){ time_t now;// struct tm ltime; char result[100]; if (signal(SIGINT, handler) == SIG_IGN) signal(SIGINT, SIG_IGN); now = time(NULL); while(1) {#ifdef UNSAFE // localtime_r(&now, <ime); ctime(&now);#endif } return 0;}
对于信号处理函数,尽量做到简单,如果调用了不可重入函数,可能引发死锁问题。
#16 0x0000003257e7230f in _int_free () from /lib64/libc.so.6#17 0x0000003257e7276b in free () from /lib64/libc.so.6#18 0x0000003257e8c7ca in tzset_internal () from /lib64/libc.so.6#19 0x0000003257e8cf2e in __tz_convert () from /lib64/libc.so.6#20 0x0000003257e8b529 in ctime () from /lib64/libc.so.6
strace -p 1202Process 1202 attached - interrupt to quitfutex(0x3258154fc0, FUTEX_WAIT_PRIVATE, 5, NULL
gcc -D UNSAFE test1.c -o test_unsafe
gcc -D UNSAFE test1.c -o test
编译上面这个程序可以重现这个问题。
线程中也不要调用非线程安全系统调用,否则也会出现死锁。
关于futex的文章
http://www.cnblogs.com/yysblog/archive/2012/11/03/2752728.html
- C语言trap 可重入和线程安全
- 可重入和线程安全
- 线程安全和可重入
- 可重入和线程安全
- 线程安全和可重入 .
- 线程安全和可重入
- 线程安全和可重入
- 线程安全和可重入
- 可重入和线程安全
- 线程安全和可重入
- 线程安全 和可重入
- 可重入和线程安全
- 可重入和线程安全
- 可重入和线程安全
- 线程安全和可重入
- “可重入”和“线程安全”
- 自己实现C语言atoi函数和线程安全版的itoa函数
- 可重入、异步信号安全和线程安全
- Gerrit代码审核服务器搭建全过程
- opencv中cvSplit函数讲解
- 非递归中序遍历
- win8使用VMware装xp问题总结
- 巧夺天工的kfifo
- C语言trap 可重入和线程安全
- UART
- MSP430(f149)学习笔记——红外遥控发射
- 优秀网站设计人员要得到管理者赏识应该具备的心态
- 前言
- 来自极客标签10款最新设计素材-系列七
- SQL注入漏洞全接触--3.高级篇
- 【技术类】【ArcGIS对国产卫星的支持】篇1:Raster Types for Chinese Satellite Imagery插件介绍
- SAP SM56重置编号范围缓冲区