← Back to C Course | Chapter 14: Additional Topics | Lesson 6 of 9

C में Signal Handling

एक signal system से एक program को बताने वाला एक message है कि कुछ हुआ, एक doorbell बजने की तरह। C आपको यह चुनने देता है कि जब यह बजे तो क्या करें, जैसे जब कोई Ctrl+C दबाए।
Syntax
c
#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?

c
// 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;
}

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

c
// 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;
}

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

c
// 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;
}

Signals Ignore करना

signal() को handler argument की तरह special value SIG_IGN pass करना operating system को बताता है कि एक specific signal जब भी आए बस discard कर दे, इस पर act करने की बजाय — किसी section को deliberately SIGINT जैसे एक interrupt से immune बनाने का एक तरीका।

उदाहरण: Ignoring Signals

c
// 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;
}

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()

c
// 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;
}
Related Topics
{# common_mistakes/chapter_summary/browser_support: on Hindi pages the view already swaps in the hi_ translation fields (or blanks these out if untranslated), so this renders correctly for both languages without a lang_code check here. #}
आम गलतियां
  1. एक signal handler के अंदर printf या malloc जैसा heavy काम करना, जो safe नहीं है।
  2. यह भूल जाना कि signal behavior platforms में अलग हो सकता है।
  3. SIGINT handle करना और exit न करना, ताकि program रोकना मुश्किल हो जाए।

Login to run this code

C/C++/Java/PHP execution requires a free account. Your code is saved — you'll land right back in the editor after logging in.