You are here:

C++/What is signal?


Hey thanks for ur reply to my earlier question.

there are two functions available ,signal() and raise().
what are there uses?
what all inputs can be provided in these functions and how there output should be interpreted?

inshort i have no clue about these functions please guide me.

and what is software signal?

A signal is a limited form of inter-process communication used in Unix, Unix-like, and other POSIX-compliant operating systems.
It is an asynchronous notification sent to a process in order to notify it of an event that occurred.
When a signal is sent to a process, the operating system interrupts the process' normal flow of execution.
Execution can be interrupted during any non-atomic instruction.
If the process has previously registered a signal handler, that routine is executed.
Otherwise the default signal handler is executed.

Each signal may have a signal handler, which is a function that gets called when the process receives that signal.
The function is called in "asynchronous mode", meaning that no where in your program you have code that calls this function directly.
Instead, when the signal is sent to the process, the operating system stops the execution of the process, and "forces" it to call the signal handler function.
When that signal handler function returns, the process continues execution from wherever it happened to be before the signal was received, as if this interruption never occurred.

Signals are usually used by the operating system to notify processes that some event occurred, without these processes needing to poll for the event.

One common way of sending signals to processes is using the keyboard.
eg. Ctrl-C sends a INT signal (SIGINT) to interrupt the running process.
   Ctrl-Z sends a TSTP signal (SIGTSTP) to suspend the running process.
   Ctrl-\ sends an ABRT signal (SIGABRT) to abort the running process.
Another way of sending signals to processes is done using various commands, sometimes internal to the shell:
For example, in order to send the INT signal to process with PID 5342,
at the command prompt, type: kill -INT 5342

A third way of sending signals to processes is by using the kill system call.
Such signals are sometimes called software signals. eg.

#include <unistd.h> // standard unix functions, like getpid()
#include <sys/types.h> // various typedefs, like pid_t
#include <signal.h> // signal name macros, and the kill() prototype

// ...
pid_t my_pid = getpid(); // find my own process ID
kill(my_pid, SIGSTOP); // send myself the STOP signal.
// ...

the raise(int) function sends a signal to the current process, so  
raise(SIGSTOP) ; is equivalent to the above code.

The signal() system call is used to set a signal handler for a single signal type.
As an example, here is a snippest that causes the program to print "Don't do that" when a user presses Ctrl-C:

#include <iostream>
#include <unistd.h>
#include <sys/types.h>
#include <signal.h>

void catch_int(int sig_num) // the signal handler
   signal(SIGINT, catch_int); // re-set the signal handler again to catch_int (linux)
   std::cout << "Don't do that\n" << std::flush ;

int main()
  signal(SIGINT, catch_int); // set the INT (Ctrl-C) signal handler to 'catch_int'
  for ( ;; ) pause() ; // get into an infinite loop of doing nothing.


All Answers

Answers by Expert:

Ask Experts




my primary areas of interest are generic and template metaprogramming, STL, algorithms, design patterns and c++11. i would not answer questions about gui and web programming.


about 15 years or so

post graduate engineer

©2016 All rights reserved.