Signals are the interrupts that force an OS to stop its ongoing task and attend the task for which the interrupt has been sent. These interrupts can pause a service in any programs of an OS. Similarly, C++ also offers various signals which it can catch and process in a program. Here is a list of various signals and its operations that C++ provides the user to work with.
This signal() function is provided by the signal library and is used to trap unexpected interrupts or events.
signal(registered signal, signal handler)
The first argument is an integer, representing the signal number and second is the pointer to a signal handling function. We must keep in mind that the signal that we would like to catch must be registered using a signal function and it must be associated with a signal handling function. The signal handling function should be of the void type.
Being in an infinite loop this code will show output as until an interrupt is faced:
Hello GeeksforGeeks... Hello GeeksforGeeks... Hello GeeksforGeeks... Hello GeeksforGeeks...
Now if we press ctrl+c to send an interrupt, the program will exit by printing:
Hello GeeksforGeeks... Hello GeeksforGeeks... Hello GeeksforGeeks... Hello GeeksforGeeks... The interrupt signal is (22).
The raise() function
The raise() function is used to generate signals.
raise( signal )
It takes an argument as any of the function mentioned in the list. Example:
Hello GeeksforGeeks... Hello GeeksforGeeks... Hello GeeksforGeeks... Hello GeeksforGeeks... Hello GeeksforGeeks... Interrupt signal is (11).
This article is contributed by Chinmoy Lenka. If you like GeeksforGeeks and would like to contribute, you can also write an article using contribute.geeksforgeeks.org or mail your article to email@example.com. See your article appearing on the GeeksforGeeks main page and help other Geeks.
Please write comments if you find anything incorrect, or you want to share more information about the topic discussed above.
- Comparison of Exception Handling in C++ and Java
- Exception handling and object destruction | Set 1
- Exception Handling in C++
- Four File Handling Hacks which every C/C++ Programmer should know
- Socket Programming in C/C++: Handling multiple clients on server without multi threading
- Set position with seekg() in C++ language file handling
- tellp() in file handling with c++ with example
- File Handling through C++ Classes
- Minimum cells to be flipped to get a 2*2 submatrix with equal elements
- Nested Loops in C++ with Examples
- _Find_first() function in C++ bitset with Examples
- _Find_next() function in C++ bitset with Examples
- Left-Right traversal of all the levels of N-ary tree
- Difference between Iterators and Pointers in C/C++ with Examples