我有以下代码:如何在从“killall”或“kill -p pid”接收到kill信号时退出程序之前执行处理函数?
#include <stdio.h>
#include <stdlib.h>
#include <pthread.h>
pthread_t test_thread;
void *thread_test_run (void *v)
{
int i=1;
while(1)
{
printf("into thread %d\r\n",i);
i++;
sleep(1);
}
return NULL
}
int main()
{
pthread_create(&test_thread, NULL, &thread_test_run, NULL);
sleep (20);
pthread_cancel(test_thread);
sleep(100);
// In this period (before the finish of myprogram),
// I execute killall to kill myprogram
// I want to add a signal handle function to
// execute pthread_exit() before the program quit
}
我想通过将信号处理函数执行了pthread_exit()程序退出之前完成我的代码。
怎么办?
见'人signal'和'男人sigaction'。给自己一脚,尝试一下,如果你不能向我们展示我们在哪里以及如何向我们展示,就回来。 – alk