LinuxQuestions.org
LinuxAnswers - the LQ Linux tutorial section.
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-16-2007, 07:13 AM   #1
shifter
Member
 
Registered: May 2006
Distribution: Slackware, DragonFly
Posts: 233

Rep: Reputation: 30
how know if process running


Hi all, I have a new programming question.
I am writing my personal daemon in C language.
How can I know if my program (daemon) is running?
( with C language, and not with shell )

For example, I try:

#./my program

Exist it a system call that I can use within myprogram to know if istance of myprogram is running?

Thank you in advance.
 
Old 06-16-2007, 07:37 AM   #2
kstan
Member
 
Registered: Sep 2004
Location: Malaysia, Johor
Distribution: Dual boot MacOS X/Ubuntu 9.10
Posts: 851

Rep: Reputation: 31
in shell, type this:-
ps aux
 
Old 06-16-2007, 07:45 AM   #3
shifter
Member
 
Registered: May 2006
Distribution: Slackware, DragonFly
Posts: 233

Original Poster
Rep: Reputation: 30
Quote:
in shell, type this:-
ps aux
No, I want it in C code!!
Have you read above post?
 
Old 06-16-2007, 01:13 PM   #4
theNbomr
LQ 5k Club
 
Registered: Aug 2005
Distribution: OpenSuse, Fedora, Redhat, Debian
Posts: 5,396
Blog Entries: 2

Rep: Reputation: 903Reputation: 903Reputation: 903Reputation: 903Reputation: 903Reputation: 903Reputation: 903Reputation: 903
How about this:
Code:
   printf( "I'm running now\n" );
--- rod.
 
Old 06-16-2007, 01:17 PM   #5
theNbomr
LQ 5k Club
 
Registered: Aug 2005
Distribution: OpenSuse, Fedora, Redhat, Debian
Posts: 5,396
Blog Entries: 2

Rep: Reputation: 903Reputation: 903Reputation: 903Reputation: 903Reputation: 903Reputation: 903Reputation: 903Reputation: 903
Oh, I think I get it now. You want to know whether a prior instance of the daemon was already running? Lock files are commonly used for this purpose. No matter what scheme you use, the tricky part is making sure the 'flag', of what ever sort you use, always gets cleared when the daemon exits. If it doesn't, any subsequent attempt to restart the process will fail.
--- rod.
 
Old 06-16-2007, 05:13 PM   #6
shifter
Member
 
Registered: May 2006
Distribution: Slackware, DragonFly
Posts: 233

Original Poster
Rep: Reputation: 30
For example, I can to set an exit code to 1 at first daemon instance, and when daemon exit, then I set exit code to 0.
 
Old 06-16-2007, 09:53 PM   #7
theNbomr
LQ 5k Club
 
Registered: Aug 2005
Distribution: OpenSuse, Fedora, Redhat, Debian
Posts: 5,396
Blog Entries: 2

Rep: Reputation: 903Reputation: 903Reputation: 903Reputation: 903Reputation: 903Reputation: 903Reputation: 903Reputation: 903
In the your case, the mere presence of the entity is enough to indicate that an instance of the daemon already exists. The entity can be a file, a message queue, shared memory segment, or anything else that can be created and mutually located by arbitrary processes.
--- rod.
 
Old 06-18-2007, 04:37 AM   #8
bigearsbilly
Senior Member
 
Registered: Mar 2004
Location: england
Distribution: FreeBSD, Debian, Mint, Puppy
Posts: 3,298

Rep: Reputation: 175Reputation: 175
ho ho, now this is a question!

there is no definitive way to do it.
lock files are the simplest method and OK ish.

if it's a network server maybe you could have a method of poking it to see if it's alive.

the bottom line is these computer thingies still need us monkeys for some jobs.
when you solve this problem you will be helping them on the road to phasing us out

Last edited by bigearsbilly; 06-18-2007 at 04:38 AM.
 
  


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
how do i bring up a running process? szandor Linux - General 2 02-28-2007 05:14 AM
hide running process jim656 Linux - Security 1 02-15-2006 09:37 AM
Running process sonu1082 Linux - Software 2 11-25-2004 06:32 AM
Process not running LinuxRam Linux - Newbie 4 09-09-2004 01:45 AM
creating/running process... gmasterleo2000 Programming 4 12-07-2003 01:37 PM


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

Main Menu
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
identi.ca: @linuxquestions
Facebook: linuxquestions Google+: linuxquestions
Open Source Consulting | Domain Registration