signals

Catch KeyboardInterrupt or handle signal in thread

你离开我真会死。 提交于 2019-12-23 03:12:26
问题 I have some threads running, and one of those threads contains an object that will be spawning subprocesses. I want one such subprocess to be able to kill the entire application. The aforementioned object will need to save some state when it receives this signal. Unfortunately I can't get the signal to be handled in the thread that causes the kill. Here is some example code that attempts to replicate the situation. parent.py : starts a thread. that thread runs some subprocesses, one of which

What's the proper way to construct a signal/slot wrapper to a blocking call?

浪尽此生 提交于 2019-12-23 01:01:50
问题 Suppose I have a QObject and a blocking method (say, it's a library call that needs to fetch a lot of data from the network before returning). class Foo : public QObject { Bar* _bar; public: // non blocking call, emits stuffDone when done void startStuff(int a, int b); signals: void stuffDone(int sum); } class Bar { public: // Blocking call int doStuff(int a, b) { for(int i=0; i<=100000000000; i++); return a + b; } } I'd like my Foo::startStuff method to run doStuff in the appropriate

Why print operation within signal handler may change deadlock situation?

蓝咒 提交于 2019-12-22 22:42:50
问题 I got simple program as below: import threading import time import signal WITH_DEADLOCK = 0 lock = threading.Lock() def interruptHandler(signo, frame): print str(frame), 'received', signo lock.acquire() try: time.sleep(3) finally: if WITH_DEADLOCK: print str(frame), 'release' lock.release() signal.signal(signal.SIGINT, interruptHandler) for x in xrange(60): print time.strftime("%H:%M:%S"), 'main thread is working' time.sleep(1) So, if you start that program and even Ctrl+C is pressed twice

Why print operation within signal handler may change deadlock situation?

时间秒杀一切 提交于 2019-12-22 22:42:46
问题 I got simple program as below: import threading import time import signal WITH_DEADLOCK = 0 lock = threading.Lock() def interruptHandler(signo, frame): print str(frame), 'received', signo lock.acquire() try: time.sleep(3) finally: if WITH_DEADLOCK: print str(frame), 'release' lock.release() signal.signal(signal.SIGINT, interruptHandler) for x in xrange(60): print time.strftime("%H:%M:%S"), 'main thread is working' time.sleep(1) So, if you start that program and even Ctrl+C is pressed twice

How to prevent user stopping script by CTRL + Z?

徘徊边缘 提交于 2019-12-22 20:12:08
问题 I want to prevent user to go back to shell_prompt by pressing CTRL + Z from my python command line interpreter script. 回答1: You could write a signal handler for SIGTSTP, which is triggered by Ctrl + Z . Here is an example: import signal def handler(signum, frame): print 'Ctrl+Z pressed, but ignored' signal.signal(signal.SIGTSTP, handler) while True: pass 回答2: The following does the trick on my Linux box: signal.signal(signal.SIGTSTP, signal.SIG_IGN) Here is a complete example: import signal

linux/glibc. Can I use fprintf in signal handler?

|▌冷眼眸甩不掉的悲伤 提交于 2019-12-22 18:48:23
问题 Can I use fprintf(stderr) in a signal (SIGALRM) handler with glibc/linux? 回答1: No you cannot. Check the manpage signal(7) for a list of async-signal-safe functions. fprintf is not included in that list. If you don't need formatting then you can use write(STDERR_FILENO, <buf>, <buflen>) to write to stderr. 回答2: This is not safe, quoting IBM DeveloperWorks article about Signal Handling Safety Suppose the signal handler prints a message with fprintf and the program was in the middle of an

Fatal Signal 11

|▌冷眼眸甩不掉的悲伤 提交于 2019-12-22 14:56:11
问题 I developed and tested an application on my emulator that had an API of 4.1 (Jelly Bean). When I debug my application on my actual device, which is a Samsung Galaxy Nexus, it gives me in the logcat- Fatal signal 11 (SIGSEGV) at 0x00000000 (code=1), thread 4395. Can you help? Java XML: package ID; import java.io.IOException; import java.io.InputStream; import android.app.Activity; import android.graphics.Bitmap; import android.graphics.BitmapFactory; import android.os.Bundle; import android

Fatal Signal 11

寵の児 提交于 2019-12-22 14:55:38
问题 I developed and tested an application on my emulator that had an API of 4.1 (Jelly Bean). When I debug my application on my actual device, which is a Samsung Galaxy Nexus, it gives me in the logcat- Fatal signal 11 (SIGSEGV) at 0x00000000 (code=1), thread 4395. Can you help? Java XML: package ID; import java.io.IOException; import java.io.InputStream; import android.app.Activity; import android.graphics.Bitmap; import android.graphics.BitmapFactory; import android.os.Bundle; import android

C++, linux, fork, execvp, waitpid and SIGTSP

元气小坏坏 提交于 2019-12-22 14:00:54
问题 I'm implementing a Terminal for a Home Work. I almost finished, I just need to implement a bg ( Background ) and a fg ( Foreground ) commands. my code looks like this: void run(){ string command[] = parseMyInput( getInput() ); int fork_result = fork(); if( -1 == fork_result ) //handle error else if( 0 == fork_result ){ // child setpgrp(); // I don't want the children to get the signals if( -1 == execvp( command[0], makeArgs(command) ) ) //handle error } else { // parent if( command[ length -

Stopping getline in C

五迷三道 提交于 2019-12-22 12:53:21
问题 Here I'm trying to get an user input with getline . Upon receiving an interrupt ^C I want it to signal getline to stop and resume with my program instead of terminating it. I tried to write a newline to stdin but apparently that doesn't work. fwrite("\n", 1, 1, stdin); So what would be a way to achieve this? 回答1: Assuming your code resembles this: int main(int argc, char **argv) { //Code here (point A) getline(lineptr, size, fpt); //More code here (point B) } Include <signal.h> and bind