PDA

View Full Version : Linux C program needs to id if it is already running



straygrey
11-17-2013, 03:30 AM
Please share with me a better method of identifying that a program is already running from that program.
My current thoughts are a system call to "ps aux | grep programname" but then what?

Barney McGrew
11-17-2013, 04:06 AM
Some men annoy a lad called waitpid with their concerns.

Click_here
11-17-2013, 04:06 AM
This should be in the Linux thread - You will get better answers there :)

straygrey
11-17-2013, 06:25 AM
and what does one do if one does not know the pid of the process one is waiting for?
I know the name of the program concerned but not its pid so I cannot see waitpid solving the issue.

Salem
11-17-2013, 06:32 AM
The usual way is to create a lock file in a known location (say /tmp), with a name something like
.lock_my_program_name.pid

Each time you run the program, the first thing it does is try to find a file matching .lock_my_program_name.
If one is found, then check whether the .pid part of the name matches a running process ID, and whether that process ID is really another instance of your program.

straygrey
11-17-2013, 07:54 AM
Thank you Salem. I will do just that.