LinuxQuestions.org

LinuxQuestions.org (/questions/)
-   Linux - Newbie (http://www.linuxquestions.org/questions/linux-newbie-8/)
-   -   I want to preserve a pid (http://www.linuxquestions.org/questions/linux-newbie-8/i-want-to-preserve-a-pid-208261/)

linuxprogrammer 07-22-2004 08:05 AM

I want to preserve a pid
 
In a program of mine, I've forked a new process. I want the pid of the new process to be preserved with the original parent program. In fact, I want it to be preserved even after the parent program has terminated, so that next time this program is executed, it knows the pid of the process that had been created in its previous execution (the user may want it to be 'killed', for which the pid is required). I know I can store the pid in a text file and later on read it, but that'll be quite risky, since anyone can alter the contents of the file, or even delete it. Is there some other way of retaining the pid?

btmiller 07-22-2004 10:43 AM

Most software I'm familiar with just writes it out to a file (usually in /var/run -- obviously this file should only be root writeable (or writeable by only the user the program runs as) to keep just anybody from messing with it. I suppose you could try some esoteric strategy of storing it in a shared memory segment, but you still need a key to access the segment so that really doesn't gain you anything except for more trouble. You could also have your program parse the ps output, but that brings on its own set of troubles (for instance, an instance may not be the instance kicked off the last time this program ran).

Tuttle 07-22-2004 10:52 AM

all pids and program states are browsable in /proc, try 'grep commandname /proc/*/cmdline'

linuxprogrammer 07-23-2004 08:26 AM

can u tell me what is the /var/run folder all about?

btmiller 07-23-2004 09:55 AM

It's just a place where programs can store data about their execution such as a file containing their PID or a socket that can be used to send data to the program. There's no reason this has to be stored in /var/run, but a lot of programs do. Why not look at /var/run on your own system and see what's there to get an idea about this?


All times are GMT -5. The time now is 09:42 AM.