C में Signal Handling
In this page:
#include <signal.h>
void handler(int signum) {
// respond to the signal
}
signal(SIGINT, handler); // register handler for Ctrl+C
Signal क्या है?
एक signal एक asynchronous notification है जो operating system एक illegal instruction, एक segmentation fault, या user के Ctrl+C दबाने जैसी एक event report करने के लिए एक चलते process को देता है, program के normal flow को interrupt करते हुए ताकि यह उस event पर react कर सके।
उदाहरण: What is a Signal?
// Include standard input/output (printf, scanf)
#include <stdio.h>
#include <signal.h>
// Program execution starts in main()
int main() {
// Print formatted text to the screen
printf("Signals notify a process of events like Ctrl+C");
// Return 0 to signal that the program finished successfully
return 0;
}
Login to try C/C++/Java/PHP code in the editor
signal() Function
signal() function एक specific signal (SIGINT या SIGSEGV जैसे एक constant से identified) आपके process को deliver होने पर चलने के लिए एक custom handler function register करता है, आपको operating system के default behavior को replace करने देते हुए — जो आमतौर पर program को बिल्कुल terminate करना है — अपने खुद के response से।
उदाहरण: The signal() Function
// Include standard input/output (printf, scanf)
#include <stdio.h>
#include <signal.h>
void handler(int sig) {
// Print formatted text to the screen
printf("Caught signal %d", sig);
}
// Program execution starts in main()
int main() {
signal(SIGINT, handler);
raise(SIGINT);
// Return 0 to signal that the program finished successfully
return 0;
}
Login to try C/C++/Java/PHP code in the editor
SIGINT Catch करना
SIGINT वह signal है जो generate होता है जब user आपका program चला रहे terminal में Ctrl+C दबाता है, और इसे एक custom handler से catch करना आपके program को बीच में abruptly kill होने की बजाय open files बंद करने, resources free करने, और gracefully shut down होने देता है।
उदाहरण: Catching SIGINT
// Include standard input/output (printf, scanf)
#include <stdio.h>
#include <signal.h>
void handleSigint(int sig) {
// Print formatted text to the screen
printf("Ctrl+C caught, cleaning up");
}
// Program execution starts in main()
int main() {
signal(SIGINT, handleSigint);
raise(SIGINT);
// Return 0 to signal that the program finished successfully
return 0;
}
Login to try C/C++/Java/PHP code in the editor
Signals Ignore करना
signal() को handler argument की तरह special value SIG_IGN pass करना operating system को बताता है कि एक specific signal जब भी आए बस discard कर दे, इस पर act करने की बजाय — किसी section को deliberately SIGINT जैसे एक interrupt से immune बनाने का एक तरीका।
उदाहरण: Ignoring Signals
// Include standard input/output (printf, scanf)
#include <stdio.h>
#include <signal.h>
// Program execution starts in main()
int main() {
signal(SIGINT, SIG_IGN);
// Print formatted text to the screen
printf("SIGINT will now be ignored");
// Return 0 to signal that the program finished successfully
return 0;
}
Login to try C/C++/Java/PHP code in the editor
raise() से Signals Raise करना
raise() function एक program को programmatically खुद को एक signal generate और भेजने देता है, जो यह test करने के लिए useful है कि आपके अपने signal handlers कैसे behave करते हैं, या program की अपनी logic के अंदर से जानबूझकर एक internal fault-handling path trigger करने के लिए।
उदाहरण: Raising Signals with raise()
// Include standard input/output (printf, scanf)
#include <stdio.h>
#include <signal.h>
void handler(int sig) {
// Print formatted text to the screen
printf("Handler triggered by raise()");
}
// Program execution starts in main()
int main() {
signal(SIGTERM, handler);
raise(SIGTERM);
// Return 0 to signal that the program finished successfully
return 0;
}
Login to try C/C++/Java/PHP code in the editor
- एक signal handler के अंदर
printfयाmallocजैसा heavy काम करना, जो safe नहीं है। - यह भूल जाना कि
signalbehavior platforms में अलग हो सकता है। SIGINThandle करना और exit न करना, ताकि program रोकना मुश्किल हो जाए।
Chapter Quiz — Complete all 9 topics to unlock
0/9 topics done
Complete these topics first: