pthread_kill的用法
#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");
}
相關文章
- Qdrant用法;Qdrant在langchain裡的用法LangChain
- SQL AS 的用法SQL
- SUBMIT 的用法MIT
- indexOf()的用法Index
- Promise的用法Promise
- PHPdefine()的用法PHP
- SqlParameter的用法SQL
- Tcpdump 的用法TCP
- Javascript 的 this 用法JavaScript
- top的用法
- iptables的用法
- tie的用法
- SqlServer的with(nolock)的用法SQLServer
- Oracle的Cast的用法OracleAST
- jQuery的:checked的用法jQuery
- PHP Session的用法PHPSession
- meta元素的用法
- React Hooks 的用法ReactHook
- MongoDB的基本用法MongoDB
- React ref的用法React
- webpack的基本用法Web
- rematch的基本用法REM
- Pythonyield的用法Python
- Vue slot的用法Vue
- Mysql LIMIT的用法MySqlMIT
- Promise的基本用法Promise
- js中的this用法JS
- Seajs的用法JS
- *html的CSSHacks用法HTMLCSS
- java Properties的用法Java
- golang range的用法Golang
- 【SqlServer系列】AS的用法SQLServer
- Oracle Hints的用法Oracle
- oracle job的用法Oracle
- OpenCV的SVM用法OpenCV
- Oracle keep的用法Oracle
- SQL 中With as 的用法SQL
- WITH的簡單用法