LinuxQuestions.org
Visit Jeremy's Blog.
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 06-07-2011, 08:19 AM   #1
robertH
LQ Newbie
 
Registered: Oct 2010
Posts: 1

Rep: Reputation: 0
Best way to gracefully exit a background process?


Hello,
I am using a program that reads in data from a serial port and then sends that data out over a TCP connection. The problem I'm having is that the only way I know to exit the program is to do a 'kill PID', but doing this means the program doesn't go through the motions of closing the TCP connection properly so I have to wait some random period of time for the port to free itself or else when I try to start it back up it tells me that it can't bind to the specified port.

The general structure of the program is as follows
Code:
int main(){
  // initialize some stuff
  // open sockets and serial ports
  while(1){
    // read from serial send to TCP
  }
  // close sockets and serial ports
}
I just need a way to trigger a 'break' in the while loop rather than killing the process. I have to run the program in background because I am running multiple instances of it for multiple serial ports/sockets. Any thoughts? thanks for your help,
-robert
 
Old 06-07-2011, 08:32 AM   #2
sundialsvcs
LQ Guru
 
Registered: Feb 2004
Location: SE Tennessee, USA
Distribution: Gentoo, LFS
Posts: 10,610
Blog Entries: 4

Rep: Reputation: 3905Reputation: 3905Reputation: 3905Reputation: 3905Reputation: 3905Reputation: 3905Reputation: 3905Reputation: 3905Reputation: 3905Reputation: 3905Reputation: 3905
kill works by "sending a signal" to the process in question. If the process does not "catch" the signal, the action of the default signal that is used is to kill the process gracefully. But, unless kill -9 is used, that signal can be caught or ignored.

You can use this signal-handling capability in your application to cause it to terminate its own loop gracefully, or to reload configuration files or whatever you need to do.

However, having issued a signal, you will still need to wait some unpredictable amount of time for the process to die.

One possibility is to write your code such that, if it cannot bind to the specified port, it waits for a random (short...) period of time and then tries again a few times, terminating with a "cannot bind" message only if it fails in all of its attempts.

(A simple example of how signals can be used to good effect is the implementation of the Apache command, apachectl graceful.)

Last edited by sundialsvcs; 06-07-2011 at 08:39 AM.
 
1 members found this post helpful.
  


Reply


Thread Tools Search this Thread
Search this Thread:

Advanced Search

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
[SOLVED] Make Telnet Gracefully Exit an Application tcsmedes Linux - General 2 02-10-2011 12:34 PM
How to gracefully exit a VNC/NX session? haertig Linux - Software 4 08-21-2009 12:03 AM
Bash - background process and exit ?? michael_util Programming 4 01-05-2005 11:03 AM

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

All times are GMT -5. The time now is 10:37 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