中断C / C ++与信号的readline [英] Interrupting c/c++ readline with signals
问题描述
我试图用信号(SIGUSR1)readline的中断,但很明显,如果信号没有被处理,程序退出,处理的时候,它的ReadLine收益,仿佛什么都没有发生。被readline的应该是能够使用的信号被中断。
I'm attempting to interrupt readline with signals (SIGUSR1), but obviously if the signal isn't handled, the program exits, when handling, it readline proceeds as though nothing has happened. Is readline supposed to be able to be interrupted using signals.
我是从这个问题其他的想法:从readline的力exit()函数
I got the idea from this other question: force exit from readline() function
#include <stdio.h>
#include <unistd.h>
#include <signal.h>
#include <readline/readline.h>
#include <pthread.h>
pthread_t main_id;
void *thread_main(void* arg)
{
sleep(10);
pthread_kill(main_id, SIGUSR1);
}
void signal_handler(int sig)
{
puts("got signal");
(void) sig;
}
int main(int argc, char** argv)
{
struct sigaction sa;
sa.sa_handler = signal_handler;
sa.sa_flags = 0;
sigemptyset(&sa.sa_mask);
sigaction(SIGUSR1, &sa, NULL);
main_id = pthread_self();
pthread_t id;
pthread_create(&id, NULL, thread_main, NULL);
char *input = readline("prompt> ");
puts("main thread done");
return 0;
}
输出:
$ ./test
prompt> got signal
enter something
main thread done
$
感谢。
推荐答案
约阿希姆Pileborg回答评论这个问题。
Joachim Pileborg answered this question in a comment.
最好的解决办法似乎是使用readline的备用接口。该文档是在 http://www.delorie.com/gnu/docs/readline /rlman_41.html
The best solution seems to be using the readline alternate interface. The docs are at http://www.delorie.com/gnu/docs/readline/rlman_41.html
此外,在 http://www.mcld.co一个非常基本的例子。英国/博客/ blog.php的?274 只是需要适应使用选择,而不是与睡眠投票。
Also an extremely basic example at http://www.mcld.co.uk/blog/blog.php?274 that just needs to be adapted to use select instead of polling with sleep.
比使用信号更好!
这篇关于中断C / C ++与信号的readline的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!