0

I have to figure out how to write a programming that loops but cannot be exited using CTRL-C for 5 seconds, but after 5 seconds I can use CTRL-C to end the program. I have been reading up on system calls and what not, but I can't figure out where to start. Here are the exact instructions, if someone could point me in the right direction. Thank you.

Write a C program that uses system calls that creates a loop and cannot be killed with control C for five seconds. When it starts out it prints “I’ve started looping and can’t be killed with ^C. . .” Then every second it prints a message that says “Still looping . . .” After five seconds allow the loop to be killed with control C and display the message “I'm still looping but I can be killed with ^C ...” this is displayed every second until the user kills it.

Vivid Debugger
  • 83
  • 2
  • 11
  • 1
    You can use [sigaction](http://man7.org/linux/man-pages/man2/sigaction.2.html) to capture the Ctrl-C(SIGINT). Possible [example code](http://stackoverflow.com/questions/4217037/catch-ctrl-c-in-c) on sigaction. – SSC Dec 22 '14 at 04:04
  • 1
    An interesting way to do it would be to set the interrupt character to, say, control-T for the first 5 seconds, and then set it back to control-C. – Jonathan Leffler Dec 22 '14 at 07:18

2 Answers2

1

You want to look at the signal(2) interface. When certain process-related events occur in a UNIX/Linux environment, the operating system will send a signal (essentially a software interrupt) to the process. You can "catch" a signal using the signal function to set a callback function that gets notified when the given signal occurs. For a Ctrl-C you want to look at SIGINT, but there are other signals you can handle with the same interface. As for pausing, that's easy - just use sleep() in a loop.

frasnian
  • 1,973
  • 1
  • 14
  • 24
  • I like your approach to having the asker learn how to self-service, but it's worth mentioning that `sleep()` will block any work from happening on that thread. –  Dec 22 '14 at 04:29
  • Thanks, I added 'in a loop` to my initial answer. OP needs to print a message every second, so after setting a signal handler they can do this (pseudo-code only, of course): `for (number of seconds) do { sleep 1 second; print a message }` – frasnian Dec 22 '14 at 04:36
  • It is better to use [`sigaction()`](http://pubs.opengroup.org/onlinepubs/9699919799/functions/sigaction.html) than to use [`signal()`](http://pubs.opengroup.org/onlinepubs/9699919799/functions/signal.html), though it is slightly harder work to use `sigaction()`. There are numerous problems with `signal()` that are avoided by `sigaction()`. – Jonathan Leffler Dec 22 '14 at 07:20
1

You can use this one.

void handler(int signo)
{
  signal(SIGINT,SIG_DFL);
}

main()
{
   signal(SIGINT,SIG_IGN);
   signal(SIGALRM,handler);
   // your code.
   alarm(5);// for 5 seconds.
   while(1)
   { sleep(1);
     printf("your message\n");
   }    
}

First ignoring the SIGINT using the singal. And handler for sigalrm. so first 5 seconds ctrl+c will not work. When the sigalrm is found after the ctrl+c will work. So as per your question for particular time period ctrl+c will be stopped.

Karthikeyan.R.S
  • 3,991
  • 1
  • 19
  • 31
  • Are you sure you want to do this person's homework for them by posting the complete program? At least this OP only asked to be pointed in the right direction (unlike many other people with homework-related questions who actually *ask* for a complete program). I think OP deserves the chance to learn for him/herself. IMHO. – frasnian Dec 22 '14 at 04:33
  • I wouldn't presume to ask you to delete your answer - that's entirely up to you. I just think it's rare for a questioner to actually ask for *help* with their homework rather than asking for it to be done for them. It makes me want to send them a "Good Job!" e-card or something. Using `alarm()` is a different approach than mine that provides an example of other uses for `signal`, so OP might learn something even more from the assignment. You might want to assign additional homework to them: "Using this approach, how would you still print the message every one second?" – frasnian Dec 22 '14 at 04:43