Function signal () in C
Void function (* signal (int sig, void (* func) (int)) (int) in Library C establishes a function for signal processing (eg a signal handler).
Declaring the signal () function in C
Below is the declaration for signal () function in C:
void (* signal ( int sig , void (* func )( int )))( int )
Parameters
sig - This is a digital signal for a processing function to be set. Listed below are some important digital signals.
macrosignalSIGABRT (Signal Abort) Abnormal termination SIGFPE (Signal Floating-Point Exception) Operation related to arithmetic is not correct, such as dividing by zero or overflow) SIGILL (Signal Illegal Instruction) A function instruction SIGINT (Signal Interrupt) valid signal, usually created by user application SIGSEGV (Signal Segmentation Violation) Invalid access to storage, when a program tries to read or write outside the memory Is allocated to it SIGTERM (Signal Terminate) The end request is sent to the programfunc - This is a pointer to a function. This can be a function defined by a programmer or one of the predefined functions:
SIG_DFL Default handling: Signals are handled by the default action for that specific signal. SIG_IGN Ignore the signal: The signal is ignored.Returns the value
This function returns the previous value of Signal Handler or SIG_ERR on the error.
For example
The following C program illustrates the usage of the signal () function in C:
#include #include #include #include void sighandler ( int ); int main () { signal ( SIGINT , sighandler ); while ( 1 ) { printf ( "Chuan bi sleep trong mot vai giay .n" ); sleep ( 1 ); } return ( 0 ); } void sighandler ( int signum ) { printf ( "Bat duoc tin hieu %d, chuan bi thoat .n" , signum ); exit ( 1 ); }
Compiling and running the above C program will create an infinite loop. To exit the program, press CTRL + C.
According to Tutorialspoint
Previous lesson: Function raise () in C
Next post: signal.h in C
You should read it
- How to delete Signal account
- How to use stickers / stickers expressing emotions in Signal
- What color is the turn signal light? It's easy to think but 90% of people answer wrong
- How to enable Registration Lock in Signal
- Signal and Telegram: Where is the privacy-focused cross-platform chat app better?
- How to fix 'Input Signal Out of Range' error on Windows
- How strong is the wireless signal in your Wi-Fi network?
- How to speed up Wifi network, increase WiFi signal
May be interested
- stdarg.h in Cthe file header named stdarg.h in library c defines a type of va_list variable and 3 macros that can be used to retrieve parameters in a function when the number of parameters is unknown (eg the number of parameters is can change).
- stddef.h in Cthe file header named stddef.h in c library defines various types of variables and macros. many of these definitions are also present in other headers.
- stdio.h in Cthe header file named stdio.h in standard c library defines three variable types, a number of macros and various functions to implement input and output.
- The function fclose () in Cint fclose function (file * stream) in library c closes stream. all buffers are flushed (erased or moved to the periphery).
- The function clearerr () in Cvoid function clearerr (file * stream) in library c deletes end-of-file and error indicator for the given stream.
- Function feof () in Cthe int feof function (file * stream) in the standard library c checks the end-of-file indicator for the given stream.