spawn : how to get back a string/multiple application intances
ProgrammingThis forum is for all programming questions.
The question does not have to be directly related to Linux and any language is fair game.
Notices
Welcome to LinuxQuestions.org, a friendly and active Linux Community.
You are currently viewing LQ as a guest. By joining our community you will have the ability to post topics, receive our newsletter, use the advanced search, subscribe to threads and access many other special features. Registration is quick, simple and absolutely free. Join our community today!
Note that registered members see fewer ads, and ContentLink is completely disabled once you log in.
If you have any problems with the registration process or your account login, please contact us. If you need to reset your password, click here.
Having a problem logging in? Please visit this page to clear all LQ-related cookies.
Get a virtual cloud desktop with the Linux distro that you want in less than five minutes with Shells! With over 10 pre-installed distros to choose from, the worry-free installation life is here! Whether you are a digital nomad or just looking for flexibility, Shells can put your Linux machine on the device that you want to use.
Exclusive for LQ members, get up to 45% off per month. Click here for more info.
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.
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.
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()"?
LinuxQuestions.org is looking for people interested in writing
Editorials, Articles, Reviews, and more. If you'd like to contribute
content, let us know.