signals

Calling pthread_cond_signal without locking mutex

左心房为你撑大大i 提交于 2019-11-26 11:31:26
I read somewhere that we should lock the mutex before calling pthread_cond_signal and unlock the mutext after calling it: The pthread_cond_signal() routine is used to signal (or wake up) another thread which is waiting on the condition variable. It should be called after mutex is locked, and must unlock mutex in order for pthread_cond_wait() routine to complete. My question is: isn't it OK to call pthread_cond_signal or pthread_cond_broadcast methods without locking the mutex? If you do not lock the mutex in the codepath that changes the condition and signals, you can lose wakeups. Consider

Python: Catch Ctrl-C command. Prompt “really want to quit (y/n)”, resume execution if no

孤者浪人 提交于 2019-11-26 11:03:04
问题 I have a program that may have a lengthy execution. In the main module I have the following: import signal def run_program() ...time consuming execution... def Exit_gracefully(signal, frame): ... log exiting information ... ... close any open files ... sys.exit(0) if __name__ == \'__main__\': signal.signal(signal.SIGINT, Exit_gracefully) run_program() This works fine, but I\'d like the possibility to pause execution upon catching SIGINT, prompting the user if they would really like to quit,

Is it possible to capture a Ctrl+C signal and run a cleanup function, in a “defer” fashion?

自古美人都是妖i 提交于 2019-11-26 10:06:40
问题 I want to capture the Ctrl+C ( SIGINT ) signal sent from the console and print out some partial run totals. Is this possible in Golang? Note: When I first posted the question I was confused about Ctrl+C being SIGTERM instead of SIGINT . 回答1: You can use the os/signal package to handle incoming signals. ^C is SIGINT, so you can use this to trap os.Interrupt . c := make(chan os.Signal, 1) signal.Notify(c, os.Interrupt) go func(){ for sig := range c { // sig is a ^C, handle it } }() The manner

Signal handling using “TERM”

天涯浪子 提交于 2019-11-26 09:09:45
问题 I have a standalone application in which I have to prompt the user with an confirm dialog box to save the changes made by him when he tries to shutdown the system by start-->shutdown . I came to know that by using signalhandlers we can do it. Can some one help me how to use signal handlers 回答1: Update May 2012 (2 and half years later) Trejkaz comments: On current versions of Java this signal handling code fails because the " INT " signal is "reserved by the VM or the OS". Additionally, none

Ctrl + C interrupt event handling in Linux

独自空忆成欢 提交于 2019-11-26 09:08:55
问题 I am developing an application that uses C++ and compiles using Linux GNU C Compiler. However, I want to invoke a function as the user interrupts the script using Ctrl C keys. What should I do? Any answers would be much appreciated. 回答1: When you press Ctr + C , the operating system sends a signal to the process. There are many signals and one of them is SIGINT. The SIGINT ("program interrupt") is one of the Termination Signals. There are a few more kinds of Termination Signals, but the

python: windows equivalent of SIGALRM

不打扰是莪最后的温柔 提交于 2019-11-26 08:32:02
问题 I have this decorator: def timed_out(timeout): def decorate(f): if not hasattr(signal, \"SIGALRM\"): return f def handler(signum, frame): raise TimedOutExc() @functools.wraps(f) def new_f(*args, **kwargs): old = signal.signal(signal.SIGALRM, handler) signal.alarm(timeout) try: result = f(*args, **kwargs) finally: signal.signal(signal.SIGALRM, old) signal.alarm(0) return result new_f.func_name = f.func_name return new_f return decorate The code only does anything on linux, though, as on

Print int from signal handler using write or async-safe functions

笑着哭i 提交于 2019-11-26 08:29:27
问题 I want to print a number into log or to a terminal using write (or any async-safe function) inside a signal handler. I would prefer not to use buffered I/O. Is there an easy and recommended way to do that ? For example in place of printf , below I would prefer write (or any asyn safe function). void signal_handler(int sig) { pid_t pid; int stat; int old_errno = errno; while((pid = waitpid(-1, &stat, WNOHANG)) > 0) printf(\"child %d terminated\\n\", pid); errno = old_errno; return; } Printing

Handling multiple SIGCHLD

。_饼干妹妹 提交于 2019-11-26 08:13:16
问题 In a system running Linux 2.6.35+ my program creates many child processes and monitors them. If a child process dies I do some clean-up and spawn the process again. I use signalfd() to get the SIGCHLD signal in my process. signalfd is used asynchronously using libevent . When using signal handlers for non-real time signals, while the signal handler is running for a particular signal further occurrence of the same signal has to be blocked to avoid getting into recursive handlers. If multiple

Segmentation fault handling

独自空忆成欢 提交于 2019-11-26 07:36:39
问题 I have an application which I use to catch any segmentation fault or ctrl-c. Using the below code, I am able to catch the segmentation fault but the handler is being called again and again. How can I stop them. For your information, I don\'t want to exit my application. I just can take care to free all the corrupted buffers. Is it possible? void SignalInit(void ) { struct sigaction sigIntHandler; sigIntHandler.sa_handler = mysighandler; sigemptyset(&sigIntHandler.sa_mask); sigIntHandler.sa

Signal handling with multiple threads in Linux

孤街浪徒 提交于 2019-11-26 06:57:05
问题 In Linux, what happens when a program (that possibly has multiple threads) receives a signal, like SIGTERM or SIGHUP? Which thread intercepts the signal? Can multiple threads get the same signal? Is there a special thread dedicated entirely to handling signals? If not, what happens inside the thread that is to handle the signal? How does the execution resume after the signal handler routine finishes? 回答1: This is slightly nuanced, based on which version of the Linux kernel you are using.