signals

Python - Handle CTRL+D with 'import signal'

不羁岁月 提交于 2019-12-11 02:07:58
问题 I can currently handle CTRL + C via: def hand_inter(signum, frame): print 'hey, nice job.' signal.signal(signal.SIGINT, hand_inter) However I am required to also handle CTRL + D yet cannot find the appropriate "signal.CTRL+D" call for signum. 回答1: Ctrl + D is not a signal, it's end of file. If you have an interactive program, you will be most probably reading STDIN and Ctrl + D is way how user says that the input is over. Outside this context it does not have any special meaning. The code

Why my Linux signal handler has no reaction to the signal?

拜拜、爱过 提交于 2019-12-11 01:54:55
问题 #include <stdio.h> #include <signal.h> #include <stdlib.h> static void pr_mask(const char * string) { sigset_t procmask; sigprocmask(SIG_SETMASK, NULL, &procmask); printf("%s: ", string); if(sigismember(&procmask, SIGINT)) printf("SIGINT "); if(sigismember(&procmask, SIGUSR1)) printf("SIGUSR1 "); if(sigismember(&procmask, SIGUSR2)) printf("SIGUSR2 "); if(sigismember(&procmask, SIGTERM)) printf("SIGTERM "); if(sigismember(&procmask, SIGQUIT)) printf("SIGQUIT "); printf("/n"); } static void

Interrupting syscalls in threads on linux

跟風遠走 提交于 2019-12-11 01:38:57
问题 I have a pthread that runs in a loop, calling accept() in a blocking manner. Is there any way to interrupt that call from another thread? Everything points to sending the thread a signal, but apparently you can only send a process a signal. I can't just kill the thread because then it leaves the socket open. And that's not very clean anyway. Is there really no way to do this? 回答1: You can signal a thread using pthread_kill(3). The pthread_kill() function sends the signal sig to thread,

Shell script: How to prevent SIGINT from interrupting current task

孤街醉人 提交于 2019-12-11 01:15:47
问题 I have a quite long shell script and I'm trying to add signal handling to it. The main task of the script is to run various programs and then clean up their temporary files. I want to trap SIGINT. When the signal is caught, the script should wait for the current program to finish execution, then do the cleanup and exit. Here is an MCVE: #!/bin/sh stop_this=0 trap 'stop_this=1' 2 while true ; do result="$(sleep 2 ; echo success)" # run some program echo "result: '$result'" echo "Cleaning up...

sigprocmask during signal's execution

↘锁芯ラ 提交于 2019-12-11 01:09:36
问题 I'm currently researching using sigprocmask to block certain signals (in this case, SIGALRM and SIGCHLD ) when a critical segment of code is executing. Both of the signal handlers associated with these signals will access and modify a central data structure, so it is crucial that I prevent them from accessing it while the main process is working on it. At the moment, my plan would be to simply disable these signals at the start of the critical section of code, and then re-enable them at the

Fixing race condition when sending signal to interrupt system call

折月煮酒 提交于 2019-12-11 00:43:15
问题 I have a thread that read() s from a socket, and I want to be able to stop the thread asynchronously. The thread pseudocode looks like: int needs_quit = 0; void *thread_read(void *arg) { while(1) { if(needs_quit) { close(sock_fd); return NULL; } int ret = read(sock_fd ...); if(ret == EINTR) //we received an interrupt signal to stop { close(sock_fd); return NULL; } //do stuff with read data } } The signal handler simply sets needs_quit to 1. Most of the time, this code will work. But, if a

How to handle OS signal in a python program?

梦想的初衷 提交于 2019-12-11 00:33:17
问题 I am writing a python program which reads from a queue through a infinite while loop. How can I handle signal sent by OS / Keyboard interrupt(CTRL+C) to break from the while loop and close active connections and files and exit the program gracefully instead of killing the process. while True: read_from_file_and_do_something() ## Handle a signal of shutdown here. ## Send email before exiting. This program will run as a daemon. Thus would require a signal to be sent. 回答1: I think "signal"

SIGSTOP/SIGCONT POSIX behavior

女生的网名这么多〃 提交于 2019-12-11 00:28:01
问题 I'm playing around with signals: SIGSTOP and SIGCONT in particular. Here is a test program I wrote. The idea is to create a chain of N + 1 processes (including the main process). Each one has to wait for its child to stop, then stop itself. The main process has to wake up its child when the latter has stopped. To do so, the f function recursively create the process chain. Each of the process uses sigsuspend on the SIGCHLD signal apart from the last child who stops itself directly. When its

Program OK outside debugger, SIGILL under debugger when stepping?

此生再无相见时 提交于 2019-12-10 23:35:48
问题 I'm trying to debug a program on a BeagleBone Black. Outside the debugger it produces an incorrect result but no SIGILL . It also runs OK under the debugger without a breakpoint. However it produces a SIGILL with a breakpoint set when stepping. The program and library does not use SIGILL -based cpu feature probes. However, I don't know what GDB is doing. Under the debugger I am seeing: (gdb) b main Breakpoint 1 at 0x26f20: file test.cxx, line 22. (gdb) r Starting program: /home/cryptopp/test

Multithreaded server, signal handling. POSIX

可紊 提交于 2019-12-10 23:35:47
问题 I have trouble dealing with signal handling in my multithreaded server. I create one thread per connection but I want to have an option to terminate the server with SIGINT. However, things get nasty when one of the threads catches the signal. Is there a way I could block the threads from getting the signal except for the main thread? 回答1: A thread inherits its signal mask from the thread creating it. Assuming the creating thread is the "main" thread, you might like to block all signals in