ProgrammingThis forum is for all programming questions.
The question does not have to be directly related to Linux and any language is fair game.
Notices
Welcome to LinuxQuestions.org, a friendly and active Linux Community.
You are currently viewing LQ as a guest. By joining our community you will have the ability to post topics, receive our newsletter, use the advanced search, subscribe to threads and access many other special features. Registration is quick, simple and absolutely free. Join our community today!
Note that registered members see fewer ads, and ContentLink is completely disabled once you log in.
If you have any problems with the registration process or your account login, please contact us. If you need to reset your password, click here.
Having a problem logging in? Please visit this page to clear all LQ-related cookies.
Get a virtual cloud desktop with the Linux distro that you want in less than five minutes with Shells! With over 10 pre-installed distros to choose from, the worry-free installation life is here! Whether you are a digital nomad or just looking for flexibility, Shells can put your Linux machine on the device that you want to use.
Exclusive for LQ members, get up to 45% off per month. Click here for more info.
I have written a small server program in C. I am using ncurses as the interface and when the server gets fired up it enters an infinite loop where it listens for new connections using accept() and then forks each new connection, creating a child process, then it goes through the loop and back to accept(), where it waits for the next connection.
Now, to close the server I want to be able to just hit Ctrl+C and the program should clean up and shut down the ncurses windows nicely.
You mean the server process isn't put into the background? If it's running in the foreground, then the shell is usually set up so that "ctl-C" will send "SIGTERM" to the process (or was it SIGINT?). So all you have to do is catch the signal. You will find that many daemons out there (like pppd) will respond to SIGTERM and SIGHUP (and sometimes SIGUSR1, SIGUSR2, and occasionally others). Depending on how you use the sockets, you may also have to deal with SIGPIPE.
Unless your software was set up to handle SIGTERM, the default action is to kill your process. If you handle SIGTERM you can close things cleanly. It is not advisable to ignore SIGTERM because when the system shuts down, SIGTERM is sent to all processes in an attempt to be friendly and get them to shut down in an orderly fashion; a few seconds later SIGKILL is sent - so the friendly thing to do is shut down in a nice way when SIGTERM is received.
[edit] OK, I just checked - it's SIGINT which is sent when you press ctl-C.
I'm pretty sure it's SIGINT, but I think you can map signals to keys using termios.h. I don't know how curses works, though, so I can't tell you how to enable a signal for a keystroke.
If your program has cleanup to do, you definitely need to look at man 7 signal and deal with every possible signal that can end your program. Some aren't defined, so you need to use a whole lot of #ifdef etc. and register a handler for those present. SIGPIPE is better ignored when using multiple forks (if you use pipes, that is) because that signal comes up regularly.
ta0kira
Ok, so I need to checkout signals. The main problem I got when I introduced curses was that I need to close it up properly in order to return to a functional command line. So Ctrl+C will terminate the server but it will leave a broken curses window running.
I guess my question was how to use signals when I have forks. Since the server will enter an infinite loop I will probably need to create a new fork (or thread) which will listen for signals. So I was just wondering if that will cause any problems, if the parent will create a child process which will be the signal handler.
I guess my best starting point will be Googling about signals in Linux.
If you use pthread, then all threads will use the same signal handler as the parent and you can use something like a global flag to indicate that a signal was received. If you use fork() then you need to use other mechanisms to check if the parent or child quit (and why). Personally I use pthread whenever I can get away with it, although fork() is the more traditional mechanism used by UNIX (you actually have completely separate processes which need to interact via IPC).
LinuxQuestions.org is looking for people interested in writing
Editorials, Articles, Reviews, and more. If you'd like to contribute
content, let us know.