c - Why I can't receive SIGPOLL signal from ioctl function? -
i got strange problem can't solve. code.
#include <stdio.h> #include <stropts.h> #include <signal.h> #include <sys/types.h> void handle_signal(int s) { char c = getchar(); printf("got char '%c'\n"); if(c == 'q') { exit(0); } } int main(int argc, char** argv) { sigset(sigpoll, handle_signal); ioctl(0, i_setsig, s_rdnorm); printf("type q exit"); while(1); return 0; }
when run program, type character in terminal did not work!!! can not receive sigpoll signal. have can give me advice? way, operating system ubuntu 12.04.
on linux needs set o_async
flag , f_setown
property on file descriptor sigio
signal (a synonym of sigpoll
). , signal handler can call async-signal safe functions:
#include <stdio.h> #include <signal.h> #include <unistd.h> #include <stdlib.h> #include <fcntl.h> #include <ctype.h> void handle_signal(int) { // can use async-signal safe functions here. char msg[] = "got char c\n"; char* c = msg + (sizeof msg - 3); if(1 != read(stdin_fileno, c, 1) || !isprint(*c)) return; write(stdout_fileno, msg, sizeof msg - 1); if(*c == 'q') exit(exit_success); } int main() { printf("type q exit\n"); signal(sigio, handle_signal); fcntl(stdin_fileno, f_setfl, o_async | fcntl(stdin_fileno, f_getfl)); fcntl(stdin_fileno, f_setown, getpid()); sigset_t mask; sigemptyset(&mask); for(;;) sigsuspend(&mask); return exit_success; }
you may have @ f_setsig
allows receiving signal of choosing , information signal handler.
Comments
Post a Comment