signals

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

别来无恙 提交于 2019-11-26 22:51:42
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 strings is easy. In place of the printf above I can use (without printing pid ): write(STDOUT_FILENO,

How can I tell in Linux which process sent my process a signal

感情迁移 提交于 2019-11-26 22:50:10
问题 I have a java application that got SIG TERM . I want to know the pid of the process that sent this signal. Is that possible? 回答1: Two Linux-specific methods are SA_SIGINFO and signalfd(), which allows programs to receive very detailed information about signals sent, including the sender's PID. Call sigaction() and pass to it a struct sigaction which has the desired signal handler in sa_sigaction and the SA_SIGINFO flag in sa_flags set. With this flag, your signal handler will receive three

Ctrl + C interrupt event handling in Linux

此生再无相见时 提交于 2019-11-26 22:14:24
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. Grijesh Chauhan 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 interesting thing about SIGINT is that it can be handled (caught) by your program. The default

How to send a signal SIGINT from script to script?

你说的曾经没有我的故事 提交于 2019-11-26 22:00:49
问题 I want to trap a signal send from Script-A.sh to Script-B.sh so in Script-A.sh i use the command: (Send SIGINT to Script-B.sh) kill -2 $PID_Script-B.sh And in Script-B.sh i catch the signal and call function Clean trap 'Clean' 2 It does not work, instead the Script-B.sh is killed right away without performing the Clean !! What i notice also is that if i want to send SIGINT from terminal to any script that traps it, a ctrl-c will be caught correctly, but not if i specify the signal via the

Need QGraphicsScene signal or event for _after_ change

时光怂恿深爱的人放手 提交于 2019-11-26 21:56:33
问题 I use QGraphicsScene of the Qt framework. Inside the scene I have some QGraphicsItem s which the user can select and move. I would like to have an info label where the current x and y coordinate of the currently moved selection (can consist of many items) is displayed. I have tried with the signal changed of QGraphicsScene . But it is fired before the x() and y() property of the items is set to the new values. So the labels always show the second-to-last coordinates. If one moves the mouse

Why do shells ignore SIGINT and SIGQUIT in backgrounded processes?

别来无恙 提交于 2019-11-26 21:48:04
问题 If I background a processes in a script or a -c snippet, the backgrounded processes ignores SIGINT and SIGQUIT: Example: $ alias ps='ps -o pid,ppid,pgrp,sid,stat,tty,ignored,blocked,caught,wchan,min_flt,pmem,args --forest' $ sh -c 'sleep 1000 & sleep 1000 | sleep 1000' & \ sleep 0.01; ps |grep -v -e ps -e grep PID PPID PGRP SID STAT TT IGNORED BLOCKED CAUGHT WCHAN MINFL %MEM COMMAND 6197 2143 6197 6197 Ss pts/28 0000000000380004 0000000000010000 000000004b817efb wait 10039 0.0 -bash 7593 6197

waiting for a signal

喜夏-厌秋 提交于 2019-11-26 21:34:34
问题 I am working on an application which uploads the content of the file to server. To upload the file to server I am using ‘QNetworkAccessManager’ class. Since it works as asynchronous way, I changed it to work as synchronous way by using QEventLoop. Class FileTransfer { Public : QNetworkAccessManager mNetworkManager; Void Upload(QNetworkRequest request, QIODevice *data) { responce = mNetworkManager.put(request, data); EventLoop.exec(); ReadResponce(responce); } Void Stop() { responce ->close();

longjmp() from signal handler

无人久伴 提交于 2019-11-26 21:24:29
问题 I'm using the following code to try to read an input from user and timeout and exit if more than 5 seconds pass. This is accomplished through a combination of setjmp/longjmp and the SIGALRM signal. Here's the code: #include <stdio.h> #include <setjmp.h> #include <unistd.h> #include <string.h> #include <sys/signal.h> jmp_buf buffer; // this will cause t_gets() to return -2 void timeout() { longjmp(buffer, 1); } int t_gets(char* s, int t) { char* ret; signal(SIGALRM, timeout); if (setjmp(buffer

How do I get tcsetpgrp() to work in C?

南笙酒味 提交于 2019-11-26 20:57:12
问题 I'm trying to give a child process (via fork() ) foreground access to the terminal. After I fork() , I run the following code in the child process: setpgid(0, 0); And: setpgid(child, child); In the parent process. This gives the child its own process group. The call to setpgid() works correctly. Now I want to give the child access to the terminal. I added the following to the child after the setpgid() call: if (!tcsetpgrp(STDIN_FILENO, getpid())) { perror("tcsetpgrp failed"); } After that,

Proper usage of volatile sig_atomic_t

最后都变了- 提交于 2019-11-26 20:39:30
问题 According to this site, one can use variables of type volatile sig_atomic_t inside a signal handler. Now my question is, would for example something like the following code still be atomic and thus introduce no race conditions? Assume that we are using a multicore processor ( EDIT : running a multithreaded program). Does volatile sig_atomic_t even work for multicore systems in the first place or should we use the atomic<unsigned int> of C++11 for signal handlers on a multicore system ( EDIT :