signal-handling

How are asynchronous signal handlers executed on Linux?

我是研究僧i 提交于 2019-12-02 14:14:06
I would like to know exactly how the execution of asynchronous signal handlers works on Linux. First, I am unclear as to which thread executes the signal handler. Second, I would like to know the steps that are followed to make the thread execute the signal handler. On the first matter, I have read two different, seemingly conflicting, explanations: The Linux Kernel, by Andries Brouwer, §5.2 "Receiving signals" states : When a signal arrives, the process is interrupted, the current registers are saved, and the signal handler is invoked. When the signal handler returns, the interrupted activity

catching signals while reading from pipe with select()

♀尐吖头ヾ 提交于 2019-11-30 04:05:51
using select() with pipe - this is what I am doing and now I need to catch SIGTERM on that. how can I do it? Do I have to do it when select() returns error ( < 0 ) ? First, SIGTERM will kill your process if not caught, and select() will not return. Thus, you must install a signal handler for SIGTERM . Do that using sigaction() . However, the SIGTERM signal can arrive at a moment where your thread is not blocked at select() . It would be a rare condition, if your process is mostly sleeping on the file descriptors, but it can otherwise happen. This means that either your signal handler must do

catching signals while reading from pipe with select()

别等时光非礼了梦想. 提交于 2019-11-29 01:14:15
问题 using select() with pipe - this is what I am doing and now I need to catch SIGTERM on that. how can I do it? Do I have to do it when select() returns error ( < 0 ) ? 回答1: First, SIGTERM will kill your process if not caught, and select() will not return. Thus, you must install a signal handler for SIGTERM . Do that using sigaction() . However, the SIGTERM signal can arrive at a moment where your thread is not blocked at select() . It would be a rare condition, if your process is mostly

Get Keyboard Interrupt in C

烈酒焚心 提交于 2019-11-28 10:30:02
问题 Program: #include<stdio.h> void main() { int time=1800; while(1){ system("clear"); time-=1; printf("%d\n",time); sleep(1); if(time==0) pause(); } } The above program stops when the time reaches 0. My requirement is during the runtime of the program, If I press any key like spacebar or any other key, the program gets paused and once again I press the key, the program gets resumed. So for doing this, before execution of while condition, we submit the signal handler for keyboard interrupt. In C

Providing/passing argument to signal handler

落花浮王杯 提交于 2019-11-28 04:17:57
Can I provide/pass any arguments to signal handler? /* Signal handling */ struct sigaction act; act.sa_handler = signal_handler; /* some more settings */ Now, handler looks like this: void signal_handler(int signo) { /* some code */ } If I want to do something special i.e. delete temp files, can I provide those files as an argument to this handler? Edit 0: Thanks for the answers. We generally avoid/discourage use of global variables. And in this case, If you have a huge program, things can go wrong at different places and you might need to do a lot of cleanup. Why was the API designed this way

Which signal does ctrl-x send when used in a terminal?

≡放荡痞女 提交于 2019-11-28 04:06:38
On Linux/Unix there are signals. The Ctrl C one ( SIGINT ) is obvious to me. Now, in some other applications there are signals via Ctrl X ?! Is that even a signal or does it generate an escape sequence? Is there anything else I can use as something similar to Ctrl C ( Ctrl V , Ctrl X ...)? If anyone has a clue, im familiar with C more than bash, but answers in both languages are appreciated! jfg956 To get all the terminal control character assignments: stty -a Damon There is possibly a misunderstanding. Ctrl C does not generate a signal. It is perfectly possible to press Ctrl C anywhere, and

What's the difference between various $SIG{CHLD} values?

僤鯓⒐⒋嵵緔 提交于 2019-11-28 00:56:36
问题 What is the difference between these settings? $SIG{CHLD} = 'IGNORE' $SIG{CHLD} = 'DEFAULT' $SIG{CHLD} = '' $SIG{CHLD} = undef According to "Advanced Programming in the UNIX Environment, 2nd edition", figure 10.1 the default value of SIGCHLD is "ignore." If "ignore" meant "SIG_IGN", then no child would ever be a zombie, and that's not the case. It doesn't get much more clear from there: If the process specifically sets its disposition to SIG_IGN, children of the calling process will not

What constitutes asynchronous-safeness

陌路散爱 提交于 2019-11-28 00:46:19
It is said that you should only call asynchronous-safe functions inside a signal handler. My question is, what constitutes asynchronous-safeness ? A function which is both reentrant and thread safe is asynchronous-safe I guess? Or No? Re-entrance and thread safety has a little or nothing to do with this. Side effects, state and interruption of those functions are facts that matter. asynchronous-safe function [GNU Pth] A function is asynchronous-safe, or asynchronous-signal safe, if it can be called safely and without side effects from within a signal handler context. That is, it must be able

signal handler function in multithreaded environment

十年热恋 提交于 2019-11-27 20:41:55
In my multithreaded GUI application I have the following signal handling code. I want to improve this code so that it will be correct and threading safe but there are some things I don't fully understand in signal handling: is signal handled at the process or thread level (can I have thread-specific signal handlers) ? in which thread context is signal_handler function executed ? is it possible to send many SIGTERM signals in a short time ? does it make sense to use a mutex to prevent parallel execution of signal_handler ? void signal_handler(int sig) { switch (sig) { case SIGTERM: :

Is it possible to use signal inside a C++ class?

吃可爱长大的小学妹 提交于 2019-11-27 11:34:35
I am doing something like this: #include <signal.h> class myClass { public: void myFunction () { signal(SIGIO,myHandler); } void myHandler (int signum) { /** * Handling code */ } } I am working on Ubuntu, using gcc. But it won't compile. It is complaining with: error: the argument with type void (MyClass::)(int) doesn't agree with void (*) (int) Any clues? Or maybe it is just that I cannot use a signal inside classes? Are signals only allowed in C? The error message is an approximate translation because my compiler is not in English. The second parameter of signal should be a pointer to a