0

hi everyone.

my program uses an infinite loop & runs forever.
i want to run some codes when i decide to stop the program using CTRL-C.

Is this possible ? Please advise me.

thanks

4
Contributors
7
Replies
8
Views
8 Years
Discussion Span
Last Post by CoolAtt
0

There isn't a standard way to do it -- depends on the operating system. You might try calling onexit(), but it may not get called when Ctrl-C is hit.

0

I'm not exactly sure what you mean, but as CTRL-C is used to terminate your program I doubt any code will be allowed to run.

0

You could try signal():

#include <stdio.h>
#include <signal.h>

void cleanup(int unused) {
    printf("Cleaning up\n");
    exit(-1);
}

int main() {
    signal(SIGINT, cleanup);
    for (;;) /* Ctrl-C to exit */
        ;
}
0

thanks nucleon & Ancient Dragon.

I modified the way the program will be running.It will run in the background so no output will be visible to the console.

I want to use the kill command & achieve same result.
Plz advise me what i need to modify.

thanks.

0

you must be running *nix because MS-Windows doesn't have such a command. kill -9 123 where 123 is the pid of the running process.

This topic has been dead for over six months. Start a new discussion instead.
Have something to contribute to this discussion? Please be thoughtful, detailed and courteous, and be sure to adhere to our posting rules.