C standard library |
---|
General topics |
Miscellaneous headers |
|
In the C Standard Library, signal processing defines how a program handles various signals while it executes. A signal can report some exceptional behavior within the program (such as division by zero), or a signal can report some asynchronous event outside the program (such as someone striking an interactive attention key on a keyboard).
The C standard defines only 6 signals. They are all defined in signal.h
header (csignal
header in C++):[1]
POSIX requires that signal is thread-safe, and specifies a list of async-signal-safe library functions that may be called from any signal handler. Signal handlers are expected to have C linkage and, in general, only use the features from the common subset of C and C.
SIGABRT
- 'abort', abnormal termination.SIGFPE
- floating point exception.SIGILL
- 'illegal', invalid instruction.SIGINT
- 'interrupt', interactive attention request sent to the program.SIGSEGV
- 'segmentation violation', invalid memory access.SIGTERM
- 'terminate', termination request sent to the program.Additional signals may be specified in the signal.h
header by the implementation. For example, Unix and Unix-like operating systems (such as Linux) define more than 15 additional signals; see Unix signal.[2]
SIGTRAP
for debugging purposes. It's platform-dependent and may be used on Unix-like operating systems.A signal can be generated by calling raise()
or kill()
system calls. raise()
sends a signal to the current process, kill()
sends a signal to a specific process.
A signal handler is a function which is called by the target environment when the corresponding signal occurs. The target environment suspends execution of the program until the signal handler returns or calls longjmp()
.
Signal handlers can be set be with signal()
or sigaction()
. The behavior of signal()
has been changed multiple times across history and is now considered deprecated.[3] It is only portable when used to set a signal's disposition to SIG_DFL or SIG_IGN. Signal handlers can be specified for all but two signals (SIGKILL and SIGSTOP cannot be caught, blocked or ignored).
If the signal reports an error within the program (and the signal is not asynchronous), the signal handler can terminate by calling abort()
, exit()
, or longjmp()
.
Function | Description |
---|---|
raise | artificially sends a signal to the calling process |
kill | artificially sends a signal to a specified process |
signal | sets the action taken when the program receives a specific signal |
Home > Articles > Programming > C/C++
␡The signals and slots mechanism is fundamental to Qt programming. It enables the application programmer to bind objects together without the objects knowing anything about each other. We have already connected some signals and slots together, declared our own signals and slots, implemented our own slots, and emitted our own signals. Let's take a moment to look at the mechanism more closely.
Slots are almost identical to ordinary C++ member functions. They can be virtual; they can be overloaded; they can be public, protected, or private; they can be directly invoked like any other C++ member functions; and their parameters can be of any types. The difference is that a slot can also be connected to a signal, in which case it is automatically called each time the signal is emitted.
The connect() statement looks like this:
where sender and receiver are pointers to QObjects and where signal and slot are function signatures without parameter names. The SIGNAL() and SLOT() macros essentially convert their argument to a string.
In the examples we have seen so far, we have always connected different signals to different slots. There are other possibilities to consider.
One signal can be connected to many slots:
When the signal is emitted, the slots are called one after the other, in an unspecified order.
Many signals can be connected to the same slot:
When either signal is emitted, the slot is called.
A signal can be connected to another signal:
When the first signal is emitted, the second signal is emitted as well. Apart from that, signal–signal connections are indistinguishable from signal–slot connections.
Connections can be removed:
This is rarely needed, because Qt automatically removes all connections involving an object when that object is deleted.
To successfully connect a signal to a slot (or to another signal), they must have the same parameter types in the same order:
Exceptionally, if a signal has more parameters than the slot it is connected to, the additional parameters are simply ignored:
If the parameter types are incompatible, or if the signal or the slot doesn't exist, Qt will issue a warning at run-time if the application is built in debug mode. Similarly, Qt will give a warning if parameter names are included in the signal or slot signatures.
So far, we have only used signals and slots with widgets. But the mechanism itself is implemented in QObject and isn't limited to GUI programming. The mechanism can be used by any QObject subclass:
Notice how the setSalary() slot is implemented. We emit the salaryChanged() signal only if newSalary != mySalary. This ensures that cyclic connections don't lead to infinite loops.