pthread_kill pthread_cancel

pthread_kill函数的功能是向指定线程发送信号,信号为0时用于检查此线程ID的线程是否存活。

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

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

 
 
  1. #define _MULTI_THREADED 
  2. #include <pthread.h> 
  3. #include <stdio.h> 
  4. #include "check.h" 
  5.  
  6. void cleanupHandler(void *parm) { 
  7.   printf("Inside cancellation cleanup handler/n"); 
  8.  
  9. void *threadfunc(void *parm) 
  10.   unsigned int  i=0; 
  11.   int           rc=0, oldState=0; 
  12.   printf("Entered secondary thread/n"); 
  13.   pthread_cleanup_push(cleanupHandler, NULL); 
  14.   rc = pthread_setcancelstate(PTHREAD_CANCEL_DISABLE, &oldState); 
  15.   checkResults("pthread_setcancelstate()/n", rc); 
  16.   /* Allow cancel to be pending on this thread */ 
  17.   sleep(2); 
  18.   while (1) { 
  19.     printf("Secondary thread is now looping/n"); 
  20.     ++i; 
  21.     sleep(1); 
  22.     /* pthread_testcancel() has no effect until cancelability is enabled.*/ 
  23.     /* At that time, a call to pthread_testcancel() should result in the */ 
  24.     /* pending cancel being acted upon                                   */ 
  25.     pthread_testcancel(); 
  26.     if (i == 5) { 
  27.       printf("Cancel state set to ENABLE/n"); 
  28.       rc = pthread_setcancelstate(PTHREAD_CANCEL_ENABLE,&oldState); 
  29.       checkResults("pthread_setcancelstate(2)/n", rc); 
  30.       /* Now, cancellation points will allow pending cancels 
  31.          to get through to this thread */ 
  32.     } 
  33.   } /* infinite */ 
  34.   pthread_cleanup_pop(0); 
  35.   return NULL; 
  36.  
  37. int main(int argc, char **argv) 
  38.   pthread_t             thread
  39.   int                   rc=0; 
  40.   void                 *status=NULL; 
  41.  
  42.   printf("Enter Testcase - %s/n", argv[0]); 
  43.  
  44.   /* Create a thread using default attributes */ 
  45.   printf("Create thread using the NULL attributes/n"); 
  46.   rc = pthread_create(&thread, NULL, threadfunc, NULL); 
  47.   checkResults("pthread_create(NULL)/n", rc); 
  48.  
  49.   sleep(1); 
  50.   printf("Cancel the thread/n"); 
  51.   rc = pthread_cancel(thread); 
  52.   checkResults("pthread_cancel()/n", rc); 
  53.  
  54.   rc = pthread_join(thread, &status); 
  55.   if (status != PTHREAD_CANCELED) { 
  56.     printf("Thread returned unexpected result!/n"); 
  57.     exit(1); 
  58.   } 
  59.   printf("Main completed/n"); 
  60.   return 0; 
  61. }

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);
}

  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值