pthread_kill的用法
来源:互联网 发布:焉足以知是且非邪 编辑:程序博客网 时间:2024/06/06 11:46
#include <pthread.h>
#include <unistd.h>
#include <stdio.h>
#include <string.h>
#include <signal.h>
void sighand(int signo){
fprintf(stdout, "Thread %u in signal handler\n", (unsigned int )pthread_self());
}
void* func(void* param){
unsigned int uid = (unsigned int)pthread_self();
printf("enter thread %u\n", uid);
int rc = sleep(30);
if(rc != 0){
fprintf(stderr, "thread %u fail\n", uid);
return NULL;
}
fprintf(stdout, "thread %u success\n", uid);
return NULL;
}
void* maskedFunc(void* param){
unsigned int uid = (unsigned int)pthread_self();
printf("enter masked thread %u\n", uid);
sigset_t mask;
int rc;
sigfillset(&mask);
rc = pthread_sigmask(SIG_BLOCK, &mask, NULL);
if(rc != 0){
fprintf(stderr, "error occured in thread %u", uid);
return NULL;
}
rc = sleep(30);
if(rc != 0){
fprintf(stderr, "masked thread %u fail\n", uid);
return NULL;
}
fprintf(stdout, "masked thread %u success\n", uid);
return NULL;
}
int main(int argc, char **argv){
struct sigaction actions;
memset(&actions, 0, sizeof(actions));
sigemptyset(&actions.sa_mask);
actions.sa_flags = 0;
actions.sa_handler = sighand;
sigaction(SIGALRM, &actions, NULL);
int rc;
pthread_t pid1, pid2;
rc = pthread_create(&pid1, NULL, func, NULL);
if(rc != 0){
fprintf(stderr, "error:%s\n", strerror(rc));
}
rc = pthread_create(&pid2, NULL, maskedFunc, NULL);
if(rc != 0){
fprintf(stderr, "error:%s\n", strerror(rc));
}
sleep(3);
//send msg
pthread_kill(pid1, SIGALRM);
pthread_kill(pid2, SIGALRM);
//wait for threads to complete
pthread_join(pid1, NULL);
pthread_join(pid2, NULL);
fprintf(stdout, "main thread complete\n");
}
#include <unistd.h>
#include <stdio.h>
#include <string.h>
#include <signal.h>
void sighand(int signo){
fprintf(stdout, "Thread %u in signal handler\n", (unsigned int )pthread_self());
}
void* func(void* param){
unsigned int uid = (unsigned int)pthread_self();
printf("enter thread %u\n", uid);
int rc = sleep(30);
if(rc != 0){
fprintf(stderr, "thread %u fail\n", uid);
return NULL;
}
fprintf(stdout, "thread %u success\n", uid);
return NULL;
}
void* maskedFunc(void* param){
unsigned int uid = (unsigned int)pthread_self();
printf("enter masked thread %u\n", uid);
sigset_t mask;
int rc;
sigfillset(&mask);
rc = pthread_sigmask(SIG_BLOCK, &mask, NULL);
if(rc != 0){
fprintf(stderr, "error occured in thread %u", uid);
return NULL;
}
rc = sleep(30);
if(rc != 0){
fprintf(stderr, "masked thread %u fail\n", uid);
return NULL;
}
fprintf(stdout, "masked thread %u success\n", uid);
return NULL;
}
int main(int argc, char **argv){
struct sigaction actions;
memset(&actions, 0, sizeof(actions));
sigemptyset(&actions.sa_mask);
actions.sa_flags = 0;
actions.sa_handler = sighand;
sigaction(SIGALRM, &actions, NULL);
int rc;
pthread_t pid1, pid2;
rc = pthread_create(&pid1, NULL, func, NULL);
if(rc != 0){
fprintf(stderr, "error:%s\n", strerror(rc));
}
rc = pthread_create(&pid2, NULL, maskedFunc, NULL);
if(rc != 0){
fprintf(stderr, "error:%s\n", strerror(rc));
}
sleep(3);
//send msg
pthread_kill(pid1, SIGALRM);
pthread_kill(pid2, SIGALRM);
//wait for threads to complete
pthread_join(pid1, NULL);
pthread_join(pid2, NULL);
fprintf(stdout, "main thread complete\n");
}
- pthread_kill的用法
- pthread_kill()和pthread_cancel()的用法
- pthread_kill
- pthread_cancel 和pthread_kill的区别
- HPUX对于pthread_kill的处理
- pthread_kill函数检测一个线程是否还活着的程序
- 线程的信号pthread_kill()函数(线程四)
- Linux Pthread_kill
- Linux Pthread_kill
- pthread_kill pthread_cancel
- Linux Pthread_kill
- pthread_kill 使用方法
- signal pthread_kill
- linux中使用pthread_kill函数测试线程是否存活的例子
- LINUX中使用PTHREAD_KILL函数测试线程是否存活的例子
- linux中使用pthread_kill函数测试线程是否存活的例子
- 对已经detach的线程采用pthread_kill检测线程是否存活时会有问题
- linux中使用pthread_kill函数测试线程是否存活的例子
- Ruby中的Mixin (混入)
- 4738 Caocao's Bridges (Tarjan求割边)
- 过滤器与拦截器
- 深入理解Linux系统中的用户和组账号
- 关于图片下载后保存背景透明变黑色的问题
- pthread_kill的用法
- 句柄类帮助容器实现对因继承而相关联对象的存储
- InnerException 消息是“反序列化对象 属于类型 *** 时出现错误。读取 XML 数据时,超出最大字符串内容长度配额 (8192)。(注意细节)
- OSSemCreate初值为0和1的区别
- err:Module is not specified
- java 用jws发布一个简单的webservice
- C++心得体会
- CATransition类动画
- Container With Most Water (隔板如何蓄水) 【leetcode】