【发布时间】:2014-04-26 07:22:49
【问题描述】:
当下面的C程序执行,SIGUSR1被反复发送给正在运行的进程时,pclose()调用有时会返回13。13对应我系统上的SIGPIPE。
为什么会这样?
我正在使用while true; do kill -SIGUSR1 <process-id>; done 向程序发送SIGUSR1。该程序在 Ubuntu 14.04 上执行。
#include <pthread.h>
#include <signal.h>
#include <unistd.h>
#include <stdio.h>
void handler(int i) {}
void* task(void*)
{
FILE *s;
char b [BUFSIZ];
while (1) {
if ((s = popen("echo hello", "r")) == NULL) {
printf("popen() failed\n");
}
while (fgets(b, BUFSIZ, s) != NULL) ;
if (int r = pclose(s)) {
printf("pclose() failed (%d)\n", r);
}
}
return 0;
}
int main(int argc, char **argv)
{
struct sigaction action;
action.sa_handler = handler;
sigemptyset(&action.sa_mask);
action.sa_flags = 0;
sigaction(SIGUSR1, &action, NULL);
pthread_t tid;
pthread_create(&tid, 0, task, NULL);
pthread_join(tid, NULL);
}
【问题讨论】:
标签: linux multithreading signals posix popen