signals

In C++ when interrupted with ctrl-c call a function with arguments (other than signal number) before dying

前提是你 提交于 2019-12-03 08:59:23
I want to write a few extra lines to a file when interrupted with ctrl-c before the program dies. However the location of the file is not hard coded so I need something more than normal interrupt handling as explained here . What is the best way to do this? Motivation: I'm doing time dependent finite element simulations. Sometimes I forget or mis-estimate a reasonable tmax and the simulation would take a very long time to complete, so I interrupt it with ctrl-c. However I still want to use the data generated up to that point. In particular there's an xml file which needs a few lines appended

How to catch CTRL+C in Clojure?

喜夏-厌秋 提交于 2019-12-03 08:56:27
问题 I have a simple single threaded Clojure program that creates a temp file for swapping data. When the program exits normally this file is deleted, however when the program is exited via Ctrl + C , Ctrl + D , or Ctrl + Z that bit of code never executes. I need it to execute regardles sof how the program exits. I know that I need to catch that signal (I've done this before in other languages), but I can't seem to figure out how to do it in Clojure. 回答1: I don't know if Clojure has wrapped method

Asynchronous signals with asyncio

岁酱吖の 提交于 2019-12-03 08:16:50
My model post processing is using the post_save signal: from django.core.signals import request_finished from django.dispatch import receiver from models import MyModel from pipeline import this_takes_forever @receiver(post_save, sender=MyModel) def my_callback(sender, **kwargs): this_takes_forever(sender) The this_takes_forever routine does IO so I want to defer it to avoid blocking the request too much. I thought this was a great use case for the new asyncio module. But I have a hard time getting my mind around the whole process. I think I should be able to adapt the signal receiver like

python 2.6.x theading / signals /atexit fail on some versions?

妖精的绣舞 提交于 2019-12-03 08:03:47
I've seen a lot of questions related to this... but my code works on python 2.6.2 and fails to work on python 2.6.5. Am I wrong in thinking that the whole atexit "functions registered via this module are not called when the program is killed by a signal" thing shouldn't count here because I'm catching the signal and then exiting cleanly? What's going on here? Whats the proper way to do this? import atexit, sys, signal, time, threading terminate = False threads = [] def test_loop(): while True: if terminate: print('stopping thread') break else: print('looping') time.sleep(1) @atexit.register

Linux: system() + SIGCHLD handling + multithreading

こ雲淡風輕ζ 提交于 2019-12-03 07:54:58
问题 I have a multithreaded application that installs a handler for SIGCHLD that logs and reaps the child processes. The problem I see starts when I'm doing a call to system() . system() needs to wait for the child process to end and reaps him itself since it needs the exit code. This is why it calls sigprocmask() to block SIGCHLD. But in my multithreaded application, the SIGCHLD is still called in a different thread and the child is reaped before system() has a chance to do so. Is this a known

Using sigaction(), c

半城伤御伤魂 提交于 2019-12-03 07:47:28
I was doing a little reading about sigaction() (sources are from my course notes) and I'm not sure I understand this text: The signal mask is calculated and installed only for the duration of the signal handler. By default, the signal “sig” is also blocked when the signal occurs. Once an action is installed for a specific signal using sigaction, it remains installed until another action is explicitly requested. Does this mean that the default signal mask is restored after returning form the signal handler? Also, do I have to re-install the handler after using it, as if I was using signal() ?

How can I handle interrupt signal and call destructor in c++? [duplicate]

廉价感情. 提交于 2019-12-03 07:40:37
Possible Duplicate: Is destructor called if SIGINT or SIGSTP issued? My code like this: #include <iostream> #include <signal.h> #include <cstdlib> void handler(int) { std::cout << "will exit..." << std::endl; exit(0); } class A { public: A() {std::cout << "constructor" << std::endl;} ~A() {std::cout << "destructor" << std::endl;} }; int main(void) { signal(SIGINT, &handler); A a; for (;;); return 0; } When I pressed Ctrl-C, it printed: constructor ^Cwill exit... There is no "destructor" printed. So, how can I exit cleanly? With difficulty. Already, the code you've written has undefined

How to close a file?

≯℡__Kan透↙ 提交于 2019-12-03 07:10:16
问题 I felt at peace with Posix after many years of experience. Then I read this message from Linus Torvalds, circa 2002: int ret; do { ret = close(fd); } while(ret == -1 && errno != EBADF); NO. The above is (a) not portable (b) not current practice The "not portable" part comes from the fact that (as somebody pointed out), a threaded environment in which the kernel does close the FD on errors, the FD may have been validly re-used (by the kernel) for some other thread, and closing the FD a second

How to properly wait for foreground/background processes in my own shell in C?

末鹿安然 提交于 2019-12-03 07:03:35
In this previous question I posted most of my own shell code. My next step is to implement foreground and background process execution and properly wait for them to terminate so they don't stay as "zombies". Before adding the possibility to run them in the background, all processes were running in the foreground. And for that, I simply called wait(NULL) after executing any process with execvp(). Now, I check for the '&' character as the last argument and if it's there, run the process in the background by not calling wait(NULL) and the process can run happily in the background will I'm

How to signal slots in a GUI from a different process?

旧街凉风 提交于 2019-12-03 05:52:59
问题 Context: In Python a main thread spawns a 2nd process (using multiprocessing module) and then launches a GUI (using PyQt4). At this point the main thread blocks until the GUI is closed. The 2nd process is always processing and ideally should emit signal(s) to specific slot(s) in the GUI in an asynchronous manner. Question: Which approach/tools are available in Python and PyQt4 to achieve that and how? Preferably in a soft-interrupt manner rather than polling. Abstractly speaking, the solution