【发布时间】:2015-03-08 23:59:15
【问题描述】:
我编写了一个程序,该程序产生一个线程,该线程以阻塞方式从标准输入循环读取。我想让线程立即从阻塞读取中返回。我已经在读取线程中注册了我的信号处理程序(带有 sigaction 并且没有 SA_RESTART 标志),向它发送一个信号并期望读取以 EINTR 错误退出。但它不会发生。是 Cygwin 的问题或限制,还是我做错了什么? 代码如下:
#include <stdio.h>
#include <errno.h>
#include <pthread.h>
pthread_t thread;
volatile int run = 0;
void root_handler(int signum)
{
printf("%s ENTER (thread is %x)\n", __func__, pthread_self());
run = 0;
}
void* thr_func(void*arg)
{ int res;
char buffer[256];
printf("%s ENTER (thread is %x)\n", __func__, pthread_self());
struct sigaction act;
memset (&act, 0, sizeof(act));
act.sa_sigaction = &root_handler;
//act.sa_flags = SA_RESTART;
if (sigaction(SIGUSR1, &act, NULL) < 0) {
perror ("sigaction error");
return 1;
}
while(run)
{
res = read(0,buffer, sizeof(buffer));
if(res == -1)
{
if(errno == EINTR)
{
puts("read was interrupted by signal");
}
}
else
{
printf("got: %s", buffer);
}
}
printf("%s LEAVE (thread is %x)\n", __func__, pthread_self());
}
int main() {
run = 1;
printf("root thread: %x\n", pthread_self());
pthread_create(&thread, NULL, &thr_func, NULL);
printf("thread %x started\n", thread);
sleep(4);
pthread_kill(thread, SIGUSR1 );
//raise(SIGUSR1);
pthread_join(thread, NULL);
return 0;
}
我正在使用 Cygwin (1.7.32(0.274/5/3))。
【问题讨论】:
标签: linux unix io cygwin signals