对于多线程编程时候,对于signal传递的信号处理函数对于各个线程是共享的,虽然各个线程可以设置自己的屏蔽字。如下面所示代码。两个线程中,每个线程有一个signal接受信号,但是实际上执行时每个线程会执行两个signal信号处理函数。

也可以用  pthread_sigmask(SIG_SETMASK, &set, NULL);设置自己线程的屏蔽字,不接收某个信号。注意::如果没有设置屏蔽字,并且也没有设置signal函数接收SIGUSR1与

SIGUSR12时,用kill发送信号会导致进程直接退出。

#include #include

#include #include #include

void thread_handler_first();

void thread_handler_second();

void sign_handler(int sign);

int main()

{

pthread_t thread_first, thread_second;

if(pthread_create(&thread_first, NULL, (void *)*thread_handler_first, NULL) != 0)

{

printf("create child thread fail\n"); exit(EXIT_FAILURE);

}

else

printf("create first child thread success\n");

if(pthread_create(&thread_second, NULL, (void *)*thread_handler_second, NULL) != 0)

{

printf("create child thread fail\n"); exit(EXIT_FAILURE);

}

else

printf("create second child thread success\n");

sleep(2);

printf("Now the main thread prepare to send SIGUSR1 and SIGUSR2 to first child thread\n");

if(pthread_kill(thread_first, SIGUSR1) || pthread_kill(thread_first, SIGUSR2))

{

printf("send SIGUSR1 and SIGUSR2 to first child thread fail\n"); exit(EXIT_FAILURE);

}

else

printf("send SIGUSR1 and SIGUSR2 to first child thread success\n");

sleep(1);

printf("Now the main thread prepare to send SIGUSR1 and SIGUSR2 to second child thread\n");

if(pthread_kill(thread_second, SIGUSR1) || pthread_kill(thread_second, SIGUSR2))

{

printf("send SIGUSR1 and SIGUSR2 to second child thread fail\n"); exit(EXIT_FAILURE);

}

else

printf("send SIGUSR1 and SIGUSR2 to second child thread success\n");

sleep(1);

printf("Now the main thread prepare to send SIGKILL to both first and second child thread\n");

if(pthread_kill(thread_first, SIGKILL) != 0)

{

printf("send SIGKILL to first child thread fail\n"); exit(EXIT_FAILURE);

}

else

printf("send SIGKILL to first child thread success\n");

if(pthread_kill(thread_first, SIGKILL) != 0)

{

printf("send SIGKILL to second child thread fail\n"); exit(EXIT_FAILURE);

}

else

printf("send SIGKILL to second child thread success\n");

sleep(1);

pthread_join(thread_first, NULL);

pthread_join(thread_second, NULL);

exit(EXIT_SUCCESS);

}

void thread_handler_first()

{

int i;

__sigset_t set;

signal(SIGUSR1, sign_handler);

sigfillset(&set);

sigdelset(&set, SIGUSR1);

sigdelset(&set, SIGUSR2);

pthread_sigmask(SIG_SETMASK, &set, NULL);

for(i = 0; i < 5; i++)

{

printf("this is first thread:%u...\n", pthread_self()); pause();

}

}

void thread_handler_second()

{

int i;

signal(SIGUSR2, sign_handler);

//    signal(SIGUSR1, sign_handler);

for(i = 0; i < 5; i++)

{

printf("This is second thread:%u...\n",pthread_self()); pause();

}

}

void sign_handler(int sign)

{

printf("\nthread:%u, receive signal:%u---\n", pthread_self(), sign);

}

执行结果如下:

create first child thread success

create second child thread success

This is second thread:3069864816...

this is first thread:3078257520...

Now the main thread prepare to send SIGUSR1 and SIGUSR2 to first child thread

send SIGUSR1 and SIGUSR2 to first child thread success

thread:3078257520, receive signal:12---

thread:3078257520, receive signal:10---

this is first thread:3078257520...

Now the main thread prepare to send SIGUSR1 and SIGUSR2 to second child thread

send SIGUSR1 and SIGUSR2 to second child thread success

thread:3069864816, receive signal:12---

thread:3069864816, receive signal:10---

This is second thread:3069864816...

Now the main thread prepare to send SIGKILL to both first and second child thread

Killed

c语言线程唤醒signal,多线程编程与signal信号处理相关推荐

  1. linux线程 ppt,Linux多线程编程多核编程.ppt

    <Linux多线程编程多核编程.ppt>由会员分享,可在线阅读,更多相关<Linux多线程编程多核编程.ppt(28页珍藏版)>请在装配图网上搜索. 1.Linux多线程编程, ...

  2. python 线程锁_python多线程编程(3): 使用互斥锁同步线程

    问题的提出 上一节的例子中,每个线程互相独立,相互之间没有任何关系.现在假设这样一个例子:有一个全局的计数num,每个线程获取这个全局的计数,根据num进行一些处理,然后将num加1.很容易写出这样的 ...

  3. python创建线程函数_Python多线程编程(三):threading.Thread类的重要函数和方法...

    这篇文章主要介绍threading模块中的主类Thread的一些主要方法,实例代码如下: 复制代码 代码如下: ''' Created on 2012-9-7 @author:  walfred @m ...

  4. python线程唤醒_python 多线程

    python 多线程 真正的多线程吗? 对于多核处理器,在同一时间确实可以多个线程独立运行,但在Python中确不是这样的了.原因在于,python虚拟机中引入了GIL这一概念.GIL(Global ...

  5. linux某个线程信号唤醒,linux多线程编程--信号量和条件变量 唤醒丢失事件

    PriorityQueue有一个特征需要特别注意,即:对于那些通过排序方法判定为"相等"的元素,在通过poll方法依次取出它们时,它们的顺序是不确定的,特别是不会维持插入的顺序.举 ...

  6. python线程唤醒_Python 并发编程(一)之线程

    常用用法 t.is_alive() Python中线程会在一个单独的系统级别线程中执行(比如一个POSIX线程或者一个Windows线程) 这些线程将由操作系统来全权管理.线程一旦启动,将独立执行直到 ...

  7. ios跨线程通知_iOS多线程编程指南(三)Run Loop

    Run loops是线程相关的的基础框架的一部分.一个run loop就是一个事件处理的循环,用来不停的调度工作以及处理输入事件.使用run loop的目的是让你的线程在有工作的时候忙于工作,而没工作 ...

  8. linux条件变量唤醒丢失,多线程编程精髓(三)

    本篇主要讲Linux环境下的多线程同步内核对象. (1)linux线程同步之互斥体:linux互斥体的用法与windows的临界区对象类似,使用数据结构 pthread_mutex_t表示互斥体对象( ...

  9. 线程间通信 - 多线程编程(一)

    线程间的通信目的主要是用于线程同步,所以线程没有像进程通信中的用于数据交换的通信机制. Linux系统中的线程间通信方式主要以下几种: 锁机制:包括互斥锁.条件变量.读写锁.自旋锁         互 ...

最新文章

  1. 通过web sql实现增删查改
  2. 清除浏览器缓存之后为什么还是显示旧的html页面_H5缓存机制浅析-移动端Web加载性能优化...
  3. 爬墙技术哪家强,师范找锡伟
  4. filesystemwatch java_C#方法的委托和java中的回调
  5. 华为Mate 40E预约页面突然上线:或搭载麒麟990E芯片
  6. Unity2020.1中如何安装DOTS的Entities包?
  7. AVR Studio 5 使用初体验及完整版下载地址
  8. 开源中国 4 周年, 三个平台客户端全面开源
  9. 编译DCNv2网络:error: command ‘C:\\Program Files\\NVIDIAGPUComputingToolkit\\CUDA\\v10.0\\bin\\nvcc.exe‘
  10. 动态规划系列-连续的子数组和(leetcode523)
  11. 区块链与程序员:赚钱还是创业
  12. 固态硬盘装win7系统怎么安装教程
  13. Pipeline快速入门
  14. 系统编程之实战小项目-利用LVGL 与 mplayer制作音频播放器
  15. python数据分析实训报告总结_Python实训周总结(精品文档)
  16. Java(1):Java SE疯狂复习基本数据类型、OOP
  17. 求职面试中怎样谈自己的缺点
  18. 不一样的【青椒炒蛋】—36道超人气家常菜
  19. 华为路由器旁挂引流实验(使用流策略)
  20. java 引用其他类_java如何调用其他类中的方法?

热门文章

  1. 初学Java心得体会
  2. 学计算机的能考什么职称,我是学计算机专业的,本科,请问我能考那些类别的职称...
  3. python关于uwsgi
  4. 前台页面HTML5的Audio音频标签学习使用
  5. 6.5Python面向对象(5):多继承
  6. 大驼峰命名法和小驼峰命名法
  7. Git是什么?Git常用的命令是什么?
  8. js 各种数值类型正则匹配检测
  9. 使用Markdown编辑器写博客
  10. 计算机是否将取代人类英语作文,机器人取代人类工作英语作文 [圣诞节英语作文大全:圣诞节将会取代春节吗]...