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 08-25-2008, 10:39 AM   #1
philipina
Member
 
Registered: May 2003
Posts: 77

Rep: Reputation: 15
Question spawn : how to get back a string/multiple application intances


Hello,

I newbie in C programming under Linux and would like to get some tips from you.
I have a C application running under Suse which is starting a script by using the spawn (spawnle) command.
This is working fine.
I use this script at the startup of the application to make sure that there is no other instance of my application running. If yes my new instance of the application should kill the old one.
The output of the script looks like this if 3 instance of my application are already running:
2245
3375
9967
The problem is that I don't know how to do to get back these PIDs in my C application because spawn can only get back an integer as returned value.

Could you please help me to find a solution to this problem.
Maybe there is another way to detect some another instance are running and get their PID to kill them.

Thanks in advance.

Alain
 
Old 08-25-2008, 12:10 PM   #2
David1357
Senior Member
 
Registered: Aug 2007
Location: South Carolina, U.S.A.
Distribution: Ubuntu, Fedora Core, Red Hat, SUSE, Gentoo, DSL, coLinux, uClinux
Posts: 1,302
Blog Entries: 1

Rep: Reputation: 107Reputation: 107
Quote:
Originally Posted by philipina View Post
The problem is that I don't know how to do to get back these PIDs in my C application because spawn can only get back an integer as returned value.
There are lots of ways to do inter-process communication. You can use a local socket. If your system supports it, you can use named pipes. If you are really stuck, you can have one process write to a file and another process read from it. What you really need is to do a little research.
 
Old 08-25-2008, 02:01 PM   #3
paulsm4
LQ Guru
 
Registered: Mar 2004
Distribution: SusE 8.2
Posts: 5,863
Blog Entries: 1

Rep: Reputation: Disabled
Hi, Alain -

Please step back and look at the problem you're trying to solve - there might be a simpler implementation:

a) I imagine your "script" is something like "ps -C myapp|cut -d' ' -f 1", and you're trying to to parse the PIDs that are returned.

b) Rather than use "spawnle()", an easier solution might be to run the script with "popen()". That way, you could read back the PIDs (exactly as though the script output were a disk file).

c) Even easier, instead of having a separate script, you could code the "ps -C ..." directly into your "popen()" statement.

d) A final optimization: if you want to kill all instances of "myapp", which not just substitute the command "killall mypid"? And why not call it from "system()" (simply execute it), instead of "popen()" or "spawnle()"?

Just a thought ... PSM
 
  


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
how to match for multiple pattern at the end of given string Santoshkb Programming 2 06-23-2008 10:42 AM
Multiple string Replacement Abhilasha Linux - Newbie 4 04-12-2007 07:19 AM
Java - Convert String to Date and back Kamikazee Programming 1 04-27-2006 02:52 AM
cannot get FC3 to spawn three KDM (multiple gdm works fine) alesz Fedora 0 06-29-2005 02:53 AM
string to ASCII and back... skora Programming 2 11-24-2003 04:05 AM

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

All times are GMT -5. The time now is 12:13 PM.

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