您的位置:首页 > 其它

pthread_kill pthread_cancel

2013-01-09 11:14 573 查看
pthread_kill函数的功能是向指定线程发送信号,信号为0时用于检查此线程ID的线程是否存活。

pthread_cancel函数的功能是给线程发送取消信号,使线程从取消点退出。(http://baike.baidu.com/view/8517720.htm)

请在创建的线程中使用signal(SIGKILL,sig_handler)处理信号,如果你给一个线程发送了SIGQUIT,但线程却没有实现signal处理函数,则整个进程退出。

#define _MULTI_THREADED
#include <pthread.h>
#include <stdio.h>
#include "check.h"

void cleanupHandler(void *parm) {
printf("Inside cancellation cleanup handler/n");
}

void *threadfunc(void *parm)
{
unsigned int  i=0;
int           rc=0, oldState=0;
printf("Entered secondary thread/n");
pthread_cleanup_push(cleanupHandler, NULL);
rc = pthread_setcancelstate(PTHREAD_CANCEL_DISABLE, &oldState);
checkResults("pthread_setcancelstate()/n", rc);
/* Allow cancel to be pending on this thread */
sleep(2);
while (1) {
printf("Secondary thread is now looping/n");
++i;
sleep(1);
/* pthread_testcancel() has no effect until cancelability is enabled.*/
/* At that time, a call to pthread_testcancel() should result in the */
/* pending cancel being acted upon                                   */
pthread_testcancel();
if (i == 5) {
printf("Cancel state set to ENABLE/n");
rc = pthread_setcancelstate(PTHREAD_CANCEL_ENABLE,&oldState);
checkResults("pthread_setcancelstate(2)/n", rc);
/* Now, cancellation points will allow pending cancels
to get through to this thread */
}
} /* infinite */
pthread_cleanup_pop(0);
return NULL;
}

int main(int argc, char **argv)
{
pthread_t             thread;
int                   rc=0;
void                 *status=NULL;

printf("Enter Testcase - %s/n", argv[0]);

/* Create a thread using default attributes */
printf("Create thread using the NULL attributes/n");
rc = pthread_create(&thread, NULL, threadfunc, NULL);
checkResults("pthread_create(NULL)/n", rc);

sleep(1);
printf("Cancel the thread/n");
rc = pthread_cancel(thread);
checkResults("pthread_cancel()/n", rc);

rc = pthread_join(thread, &status);
if (status != PTHREAD_CANCELED) {
printf("Thread returned unexpected result!/n");
exit(1);
}
printf("Main completed/n");
return 0;
}

Output:

Enter Testcase - QP0WTEST/TPTESTC0

Create thread using the NULL attributes

Entered secondary thread

Cancel the thread

Secondary thread is now looping

Secondary thread is now looping

Secondary thread is now looping

Secondary thread is now looping

Secondary thread is now looping

Cancel state set to ENABLE

Secondary thread is now looping

Inside cancellation cleanup handler

Main completed

(stevenliyong)

进程之间发送信号:

#include <stdio.h>
#include <stdlib.h>
#include <sys/types.h>
#include <signal.h>

void send_signal(int pid,int signal)
{
if(kill(pid,signal) == 0)
printf("send signal success.\n");
else
printf("send signal failed.\n");
}

void received_signal(int signal)
{
if(signal == SIGUSR1)
printf("received signal SIGUSR1\n");
}
int main()
{
pid_t pid;
int status = 0;

if((pid = fork()) == 0) {
signal(SIGUSR1,received_signal);
printf("i am child process pid = %d.\n",getpid());
while (1);
} else {
sleep(1);
printf("pid = %d\n",(pid_t)pid);
send_signal(pid,SIGUSR1);
printf("i am parent process.pid = %d\n",getpid());
while (1);
wait(&status);
}
exit(0);
}
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: