LinuxQuestions.org
Visit Jeremy's Blog.
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 04-25-2013, 06:06 PM   #1
Karl Godt
Member
 
Registered: Mar 2010
Location: Kiel , Germany
Distribution: once:SuSE6.2,Debian3.1, aurox9.2+3,Mandrake?,DSL? then:W7st,WVHB, #!8.10.02,PUPPY4.3.1 now:Macpup
Posts: 314

Rep: Reputation: 45
Howto check for other instances in C and kill already running ones


In bash I would write something like this :
Code:
full_progname=`readlink -e "$0"`
base_progname="${full_progname##*/}"
ALL_PIDS=`pidof "$base_progname"`
pid_program=$$
PIDS_TO_KILL=`echo "$ALL_PIDS" | tr ' ' '\n' | grep -v -w "$pid_program"`
for a_pid in $PIDS_TO_KILL
do
kill -9 $a_pid
done
I have a line in C as
Code:
system("kill `pidof freememapplet | grep ' ' | cut -f 2 -d ' '`");
or
Code:
system("FREEMEMPIDS=`pidof -o $$ -o %PPID freememapplet`;for PiD in $FREEMEMPIDS;do kill $PiD;done");
which works as long as the new instance has a higher pid number as the former instance . $$ and %PPID also refer apparently to the system() call, not to the program .

/* The program "freememapplet" is a Puppy Linux specific tray application . */

These variables I am able to compute already :
Code:
char* PROGRAMNAME = argv[0];
pid_t pid;
int MYPID = getpid();
extern char *__progname;
printf("%s" " " "%s" "\n", PROGRAMNAME, __progname);
printf("%d" " " "%d" "\n", pid, MYPID );
 
Old 04-25-2013, 06:13 PM   #2
sundialsvcs
LQ Guru
 
Registered: Feb 2004
Location: SE Tennessee, USA
Distribution: Gentoo, LFS
Posts: 10,659
Blog Entries: 4

Rep: Reputation: 3939Reputation: 3939Reputation: 3939Reputation: 3939Reputation: 3939Reputation: 3939Reputation: 3939Reputation: 3939Reputation: 3939Reputation: 3939Reputation: 3939
What you should do is to attempt to lock a file (or some global named semaphore of some kind), and terminate if you can't get it ... because it means that there's another instance out there already running.

If you attempt to have one process "murder its rivals," there are all sorts of timing-hole problems associated with that (in addition to blood 'n guts .. very messy). But a process can atomically deduce that "it's not alone."
 
  


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
[SOLVED] How do I kill all instances of Firefox browsers? laredotornado Linux - General 3 06-22-2011 02:38 PM
pppd, running instances Daniel T Linux - Networking 1 05-23-2010 11:29 AM
Running multiple instances of apache2 jordankoppole Linux - Software 8 04-23-2008 04:55 PM
number of running instances hk_linux Programming 0 01-05-2005 11:47 PM
9 httpd instances running!?? jlinden Linux - Newbie 2 09-17-2003 11:32 AM

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

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