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.
That is never going to work the way you have set it up. You can't guarantee that getPid() - 1 is going to equal the pid of the program the last time you ran it. You never know what you might be killing if you just do a getPid() and kill the result - 1.
What most programs which need to do this do is write a file to /var/run like /var/run/myprogram.pid which just contains the pid of the daemonized program. When the program exits normally it should remove that file. If you want to kill the program then you'd read the pid out of the file, kill that pid, and if the kill was successful you'd remove the file.
As we have already told you... you cannot assume that subtracting 1 from your pid is going to get the daemon.
A little background.....
The process ID is simply just some unique number for each individual process on a system. Typically what linux does is just take the next unused PID when a process starts. Once it reaches the maximum value for a PID it starts over again at the beginning and finds the next unused.
If you use some of the extra security options when you build the kernel (grsec/pax patches provides these) then it simply picks a pseudo-random pid from the unused list each time a new process starts.
Either way, while it isn't impossible that starting two processes back to back will cause you to get sequential pids you certainly cannot count on that behavior given the true multiuser multitasking nature of the Linux OS.
When you start up your daemon you are going to have to store the PID of the daemon process somewhere (often in a file such as /var/run/<programname>.pid or /var/run/<programname>/pid). Then you will have to look for that file and read the pid from it to then kill the appropriate thing.
You could also search in running process... but that gets a bit messy, especially if your process ever becomes multithreaded.
I also what to emphasis something that Jilliagre said... kill, like many syscalls, returns a value. You should ALWAYS check the return values of these system calls and handle them accordingly, even if all you do is log it and die. This may seem annoying at times, but it can save you from some nasty debugging down the road.
LinuxQuestions.org is looking for people interested in writing
Editorials, Articles, Reviews, and more. If you'd like to contribute
content, let us know.