Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Signal Handling in C

Tags:

How can I implement signal Handling for Ctrl-C and Ctrl-D in C....So If Ctrl-C is pressed then the program will ignore and try to get the input from the user again...If Ctrl-D is pressed then the program will terminate...

My program follows:

int main(){  char msg[400];   while(1){    printf("Enter: ");    fgets(msg,400,stdin);    printf("%s\n",msg);   } } 

Thanks,

Dave

like image 562
Dave Avatar asked Mar 20 '10 22:03

Dave


People also ask

How are signals handled in C?

Signal Handling The process can ignore the signal, can specify a handler function, or accept the default action for that kind of signal. If the specified action for the signal is ignored, then the signal is discarded immediately. The program can register a handler function using function such as signal or sigaction.

What is signal () in C?

Description. The C library function void (*signal(int sig, void (*func)(int)))(int) sets a function to handle signal i.e. a signal handler with signal number sig.

What is Handler in C programming?

A signal handler is just a function that you compile together with the rest of the program. Instead of directly invoking the function, you use signal or sigaction to tell the operating system to call it when a signal arrives. This is known as establishing the handler.

What is signal handling in operating system?

24 Signal Handling A signal is a software interrupt delivered to a process. The operating system uses signals to report exceptional situations to an executing program. Some signals report errors such as references to invalid memory addresses; others report asynchronous events, such as disconnection of a phone line.


1 Answers

When dealing with POSIX signals, you have two means at your disposal. First, the easy (but deprecated) way, signal(). Second, the more elegant, current but complex way, sigaction(). Please use sigaction() unless you find that it isn't available on some platform that you need to work on.

This chapter of the glibc manual explains differences between the two and gives good example code on how to use both. It also lists the signals that can be handled, recommends how they should be handled and goes more in depth on how to tell how any given signal is (or is not) currently being handled. That's way more code than I'd want to paste into an answer here, hence the links.

It really is worth the hour or two it would take you to read the links and work through the examples. Signal handling (especially in programs that daemonize) is extremely important. A good program should handle all fatal signals that can be handled (i.e. SIGHUP) and explicitly ignore signals that it might not be using (i.e. SIGUSR1 / SIGUSR2).

It also won't hurt to study the difference between normal and real time signals, at least up to the understanding of how the kernel merges the prior and not the latter.

Once you work through it, you'll probably feel inclined to write up an easy to modify set of functions to handle your signals and re-use that code over and over again.

Sorry for not giving a quick and dirty code snippet to show you how to solve your immediate need, but this isn't a quick and dirty topic :)

like image 101
Tim Post Avatar answered Oct 27 '22 01:10

Tim Post