Termination Signal or Interrupts like Ctrl+C on Linux - c++

I want to run a process that checks my key press state, parallel to my existing infinite loop (from pcap header). I was looking something very similar to GetAsyncKeyState that of Windows.
I tried for a whole week and found its hard to program something similar to GetAsyncKeyState. So, I was using Termination Signal like ctrl+c to perform certain operation.
I wanted to know, if there are some other similar Termination signals that I can catch using program to perform operation of my own?
P.S. I'm a beginner for Linux and C++. Sorry, if my question is stupid.

POSIX makes SIGUSR1 and SIGUSR2 available for application use. Additionally there are the set of realtime signals. A close reading of man (7) signal should provide the basics and ample reference material is available on the web.
That said, it sounds like you are headed toward expanding what is already an awkward hack. Perhaps you should ask a separate question detailing exactly what you are doing and someone can help you with a more appropriate path toward solving your primary problem rather than improvements on a work-around.

you can catch the pid (Process identifier of your program) and with another terminal put
kill -9 {pid}
to get the pid just type in terminal ps -u {username}
or you can open application monitor (it's like the task admin of windows)

Related

Is there a way to recieve a event about a process starting in windows?

TL;DR
I have written a program in C++ to close all "new" programs that start that were not running when my program started. Currently I do this by capturing all PIDs and then constantly checking all registered applications against this list. Those who are not on my list I attempt to close/kill. This is very CPU intensive for such a simple task. Is there a way to receive some sort of windows event so I don't need to have a very active thread?
I found this hook which might do what I need it to do, but it kind of seems geared towards other purposes, not quite what I need.
In a nutshell:
Is there a event I can receive from windows right after/before a process launches?
Ideally you would do this in user-mode and without polling and the only thing I can think of that comes close is WMI events.
A C++ example can be found here. You might also want to read about the differences between __InstanceCreationEvent and Win32_ProcessStartTrace.

How to report correctly the abrupt end of another process in Linux?

I'm working on a embedded solution where two apps are working: one is the user interface and the other runs in the background providing data for the UI.
Recently I came across with a memory leak or similar error that is making Linux kill the secondary process, leaving the UI in a stopped situation without telling anything for the user about what is going on. I reached the problem by reading Linux's message log file and the software's print on terminal "Kill -myapp".
My question is: how could I notice such an event (and other similar) coming from the secondary software so I could properly report it to the user and log it? I mean, it's easy to have a look time to time in the process 'tree' to see if the secondary app is running and, if it's not, report a "some event happened" in the UI and it's also plausible to have a error-handler system inside the secondary app that makes it write in a log file what just happened and make the UI read that file for new entries from time to time, but how could the UI app knows with better details what is going on in such more abrupt events? (in this case, "Linux killed process", but it could be a "segmentation pipe" or any other) (and if there is another, better solution that this "constant read a log file produced by the secondary app", I'ld also like to know)
Notes: the UI is written in C++/Qt and the secondary app is in C. Although a solution using the Qt library would be welcomed, I think it would be better for the entire programming community if a more generalized solution was given.
You can create a signal handler for POSIX signals such as SIGKILL in the backend process and notify the ui using for example another signal with sigqueue. Any IPC mechanism should work, as long as it's async safe. Read more about signals: tutorial and manual
It may still be a good idea to check from the ui side periodically because the handler might not succeed.
As for a better way to check if process is alive compared to reading the log file:
Check if process exists given its pid

Detect wakeup computer using C++ on linux

I don't know how can i detect that the computer has been waken up, or even I would prefer to detect wake-up on lan. I have no idea what is the common way, I found that in /etc/pm/sleep.d I can add a custom script, but i need to get invoked inside my C++ application. Now i know I could also add a custom executable written in C++ and send a socket and listen for it somewhere else but that sounds too complicated.
Your C++ application is a daemon? You may add a shell script to /etc/pm/sleep.d which should send a signal to your C++ application (SIGUSR1, for example). Inside C++ application you need to catch this signal.
see
man kill
man 7 signal
man signal
This is similar to solution with sockets but easier.

Qt check if external process crashes

I'm building a failsafe application for professional video. The Qt application checks the 4 corners of the 2nd screen and if they are a certain RGB value (I use a special background) the Qt program knows it crashed so it sends a signal to the videomixer to fade to the other input.
Now I also want to add a check to see if the video program didn't crash (it can be the video program doesn't respond but still shows an output so I can't see the desktop on the 2nd screen). I know I can use Qprocess to start an external process. It's not that easy to hook it up to a process that already runs.
Now the question: how can I check if the program crashed (so "not responding") and see this as quick as possible so I can fade to the other video input. And what happens when my Qt program crashes, will it also exit the child process?
Thanks!
Using QProcess creates an attached process, so unfortunately it will be killed when your process dies. When you create a detached process using the static method QProcess::startDetached, you don't get the monitoring functionality.
You need to write a little platform-specific monitoring class that can launch a detached process and inform you of changes in its status. You need to use the native APIs in implementing that. QProcess's sources can be a good inspiration for where to start.
#KubaOber is partially correct in his statement. If you start and detach a process indeed you loose the Qt way of communicating with it and monitory what it does. However you OS offers plenty solutions to oversee what happens with it.
On Linux you can use:
pgrep to check if the process is running or not (execute the command as a child process and see if it returns 0 (process is running) or 1 (process is no longer running)
you can use proc filesystem to see when a process terminates (see here) and then use $? or a variable (as in described in the link) to check its exit status
kill allows you a great amount of control possibilities along with pipes
You should note however that especially on Windows there are plenty of programs that do not follow the Unix convention for exit codes (0 = exited normally, anything else - error has occurred). Also a crash is just an error state that the process ended up with. The exit code tells you that an error has occurred but in terms of a crash you will probably not be able to make the difference just by looking at it.

What's the proper way to cleanup an infinite loop application in Linux?

I come from a primarily Windows environment, so I'm a bit lost making a transition to writing some things in Linux.
Say I've got a simple C/C++ application like so:
int main(int argc, char** argv)
{
int h = open("something");
while (true)
{
// do work
usleep(10000);
}
close(h);
}
In Windows, I'd use something like GetAsyncKeyState() (or some other sort of keyboard checking functionality) to look for say the escape key, and break out of my loop when it was pressed. That way my close(h) would get called, and I'd do all the cleanup I need to.
The way I've been terminating my applications in Linux has been using CTRL+C, which from my reading means it sends SIGINT, and is a 'friendly' way to cause an application to quit. However, in my experience, that's just caused it to sorta drop everything and close wherever it was when it got that signal (meaning my post-loop cleanup never runs.)
Some have suggested I use signal() to listen for SIGINT, but others disagree with that method. (Plus, it tends to create a bit more differences between how things are done between Windows and Linux, and I'd like to stay as close as possible to running on both platforms.)
Is there a 'best practice' for things like this?
Using a SIGINT handler is actually the normal way to handle these things in the POSIX world, and similar to using SetConsoleCtrlHandler in Windows.
In the SIGINT signal handler, you set a flag, and the loop checks this flag if it should exit or not.
However, it's not strictly needed, unless you have special needs in your cleanup (like for example sending a goodbye-message to other applications or similar). The OS will make sure all files are closed properly, all memory allocations are free'd, etc. Just the same as in Windows.
The default action of SIGINT is to terminate the program in a normal way. All open files are automatically closed when the program exits in this way, so it doesn't matter that close isn't explicitly run.
It's certainly possible to set up a signal handler to close the program, but for something like this it would be overkill. Generally if you're pressing ctrl-c to exit a program, you are expecting a very rapid termination.
If you've got several files open which have been written to, and they all need to be in a consistent state relative to each other, you may want to handle the signal.
Normally when you do a getch(); your program's input is blocked until a key is hit. This is called 'Canonical' processing in the unix world (canons=rules). You can turn it off. Thus when you do a getch(); and no key has been pressed, it will immediately return -1 or whatever kind of error code.
It's been 15 years for me so I'm not sure of the specifics, but, if you google ioctl, termio, canonical, noncanonical, you'll get answers right away.
But hopefully somebody will be kind enough to post a few lines of code for you! Sorry I only have a minute to answer.