0

Possible Duplicate:
Catch Ctrl-C in C

I am writing a C command line program and trying to ensure that ctrl-c won't kill it. Any ideas of how I can do that?

Community
  • 1
  • 1
user821863
  • 479
  • 5
  • 15

2 Answers2

6

When you press Ctrl-C while a program is running, what happens underneath is that your process receives a signal called SIGINT, the default action when a process receives this signal is to terminate itself.
So your goal should be to modify this behavior:
Use the function signal to catch the signal sent to your process when ctrl-c

you should go with something like this:

signal(SIGINT, handler_function);//handler_function is a void returning function that takes one int paramter, you can do nothing there if you just want to prevent your process from terminating.


P.S Here's the man page for further information :D
P.P.S If you're not familier with what a signal is, please refer to this wikipedia page if you find something confusing there, let us know.

Fingolfin
  • 5,363
  • 6
  • 45
  • 66
3

Any ideas of how I can do that

Ignore SIGINT using a SIG_IGN disposition or catch it in a do-nothing handler. In some implementations there's even a sigignore function.

Anyways, you should be fine with:

signal(SIGINT, SIG_IGN);

SIG_IGN is sppecified in C11-7.14 so it's as portable as it gets.

cnicutar
  • 178,505
  • 25
  • 365
  • 392