signals

Detect when console application is closing/killed?

烈酒焚心 提交于 2019-11-27 07:34:31
问题 I wanted to make a safe exit for my console application that will be running on linux using mono but I can't find a solution to detect wether a signal was sent to it or the user pressed ctrl+c. On windows there is the kernel function SetConsoleCtrlHandler which does the job but that doesnt work on mono. How do I get a closing event on my console application to safe exit it ? 回答1: You need to use Mono.UnixSignal , there's a good sample posted by Jonathan Pryor : http://www.jprl.com/Blog

how to intercept linux signals ? (in C)

拈花ヽ惹草 提交于 2019-11-27 07:16:12
问题 I need to intercept and trace signals from any binaries, like strace does it under linux. I don't need a so verbose output like the real one strace. I just want to know how it works, how can I intercept signal and how can I trace them. Thanks in advance :) 回答1: strace uses the ptrace() system call for tracing, which also allows you to intercept (and possibly manipulate) signals sent to the process. Here's a tiny example: #include <sys/ptrace.h> #include <unistd.h> #include <sys/types.h>

Kill or terminate subprocess when timeout?

强颜欢笑 提交于 2019-11-27 06:55:57
I would like to repeatedly execute a subprocess as fast as possible. However, sometimes the process will take too long, so I want to kill it. I use signal.signal(...) like below: ppid=pipeexe.pid signal.signal(signal.SIGALRM, stop_handler) signal.alarm(1) ..... def stop_handler(signal, frame): print 'Stop test'+testdir+'for time out' if(pipeexe.poll()==None and hasattr(signal, "SIGKILL")): os.kill(ppid, signal.SIGKILL) return False but sometime this code will try to stop the next round from executing. Stop test/home/lu/workspace/152/treefit/test2for time out /bin/sh: /home/lu/workspace/153

About catching the SIGSEGV in multithreaded environment

末鹿安然 提交于 2019-11-27 06:55:57
问题 I'd like to know if it is possible/the recommended way to catch the SIGSEGV signal in multithreaded environment. I am particularly interested in handling the SIGSEGV raised by something like *((int *)0) = 0 . Some reading on this topic led me to signal() and sigaction() , which install a signal handler. While neither seem promising in multithreaded environment. I then tried the sigwaitinfo() , receiving the signals in one thread with a prior pthread_sigmask() call that blocks the signal on

Catch Segfault or any other errors/exceptions/signals in C++ like catching exceptions in Java

只愿长相守 提交于 2019-11-27 06:48:22
问题 I wrote a Linux program based on a buggy open source library. This library sometimes triggers segfaults that I cannot control. And of course once the library has segfaults, the entire program dies. However, I have to make sure my program keeps running even if the library has segfaults. This is because my program sort of serves as a "server" and it needs to at least tell the clients something bad happened and recover from the errors, rather than chicken out... Is there any way to do that? I

Debugging child process after fork (follow-fork-mode child configured)

自古美人都是妖i 提交于 2019-11-27 06:45:14
I'm developing an application which the parent forks a child to handle certain tasks. I'm having an issue where I've configured gdb to follow-fork-mode child but after fork, after reaching a breakpoint, it sends a SIGTRAP but the child somehow terminates and send SIGCHLD to the parent. I've configured signal(SIGTRAP, SIG_IGN) before fork so my understanding is that the child should inherit and ignore SIGTRAP when the breakpoint is reached but it's not happening. Please help me to understand this if I'm incorrect. How can I successfully debug the child process? Barath Ravikumar The child

How to send a signal to a process in C?

只愿长相守 提交于 2019-11-27 06:44:05
问题 I need to send a signal to a process and when the process receives this signal it does some things, how is this best achieved in C? 回答1: The way to send a signal to a process is kill(pid, signal); However, you should be aware that signals are not a robust means of inter-process communication except for parent-to-direct-child messages due to inherent race conditions. Pipes, files, directories, named semaphores, sockets, shared memory, etc. all provide greatly superior approaches to inter

What's the difference between SIGSTOP and SIGTSTP?

时间秒杀一切 提交于 2019-11-27 06:17:35
That's it. Just wondering about the difference between SIGSTOP and SIGTSTP. jlliagre Both signals are designed to suspend a process which will be eventually resumed with SIGCONT . The main differences between them are: SIGSTOP is a signal sent programmatically (eg: kill -STOP pid ) while SIGTSTP (for sig nal - t erminal stop ) may also be sent through the tty driver by a user typing on a keyboard, usually Control - Z . SIGSTOP cannot be ignored. SIGTSTP might be. /usr/include/x86_64-linux-gnu/bits/signum.h #define SIGSTOP 19 /* Stop, unblockable (POSIX). */ #define SIGTSTP 20 /* Keyboard stop

Run one command after another, even if I suspend the first one (Ctrl-z)

送分小仙女□ 提交于 2019-11-27 06:15:54
I know in bash I can run one command after another by separating them by semicolons, like $ command1; command2 Or if I only want command2 to run only if command1 succeeds, using && : $ command1 && command2 This works, but if I suspend command1 using Ctrl-z , in the first case, it runs command2 immediately, and in the second case, it doesn't run it at all. How can I run commands in sequence, but still be able to suspend the first command, but not have the second run until I have restarted it (with fg ) and it finishes? I'd prefer something as simple to type as possible, as I would like to do

How processor handles case of division by zero

穿精又带淫゛_ 提交于 2019-11-27 06:15:01
问题 Curious what the processor/CPU does in general or let say, on intel cpu & Linux, when it executes a division by zero instruction. Also how the error is relayed to the application, so that it can log the error or notify the developer? Thank you! 回答1: To answer in general terms, rather than going into gory details for Linux on x86_64, which are likely to obscure the concepts. CPUs tend to throw an exception interrupt, on things like division by zero, or dereferencing a NULL pointer. These