LinuxQuestions.org
Review your favorite Linux distribution.
Home Forums Tutorials Articles Register
Go Back   LinuxQuestions.org > Forums > Non-*NIX Forums > Programming
User Name
Password
Programming This forum is for all programming questions.
The question does not have to be directly related to Linux and any language is fair game.

Notices


Reply
  Search this Thread
Old 10-27-2008, 05:57 AM   #1
Ephracis
Senior Member
 
Registered: Sep 2004
Location: Sweden
Distribution: Ubuntu, Debian
Posts: 1,109

Rep: Reputation: 50
Listen for Ctrl+C


Hi,

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.

Is there a way to accomplish this?
 
Old 10-27-2008, 06:35 AM   #2
pinniped
Senior Member
 
Registered: May 2008
Location: planet earth
Distribution: Debian
Posts: 1,732

Rep: Reputation: 50
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.

Last edited by pinniped; 10-27-2008 at 06:39 AM.
 
Old 10-27-2008, 06:42 AM   #3
ta0kira
Senior Member
 
Registered: Sep 2004
Distribution: FreeBSD 9.1, Kubuntu 12.10
Posts: 3,078

Rep: Reputation: Disabled
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
 
Old 10-27-2008, 09:20 AM   #4
Ephracis
Senior Member
 
Registered: Sep 2004
Location: Sweden
Distribution: Ubuntu, Debian
Posts: 1,109

Original Poster
Rep: Reputation: 50
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.
 
Old 10-27-2008, 11:24 AM   #5
ta0kira
Senior Member
 
Registered: Sep 2004
Distribution: FreeBSD 9.1, Kubuntu 12.10
Posts: 3,078

Rep: Reputation: Disabled
Will the server be a session leader controlling process groups, i.e. moving them to and from the foreground of the terminal like a shell?
ta0kira
 
Old 10-28-2008, 05:11 AM   #6
pinniped
Senior Member
 
Registered: May 2008
Location: planet earth
Distribution: Debian
Posts: 1,732

Rep: Reputation: 50
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).
 
  


Reply



Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is Off
HTML code is Off



Similar Threads
Thread Thread Starter Forum Replies Last Post
KDE 3.5.9 - Konqueror - keyboard shortcuts ctrl+, and ctrl+. sometimes stop working bhy Linux - Desktop 1 07-26-2008 11:48 AM
Ctrl+F2 & Ctrl+F7 won't work alkos333 Slackware 15 12-07-2007 03:44 AM
Any way to create bash short cuts (like CTRL+l for clear and Ctrl+D for exit) supersubu123 Linux - General 5 05-30-2007 03:02 AM
ctrl+c & ctrl+v equivalent for Gnome terminal window? halfpower Linux - General 2 11-29-2005 02:57 AM
no ctrl-a or ctrl-z in emacs or on command line? ashlock Fedora 5 12-30-2003 08:00 PM

LinuxQuestions.org > Forums > Non-*NIX Forums > Programming

All times are GMT -5. The time now is 01:33 AM.

Main Menu
Advertisement
My LQ
Write for LQ
LinuxQuestions.org is looking for people interested in writing Editorials, Articles, Reviews, and more. If you'd like to contribute content, let us know.
Main Menu
Syndicate
RSS1  Latest Threads
RSS1  LQ News
Twitter: @linuxquestions
Open Source Consulting | Domain Registration