How can i make sure there is only one my programme is running at the same time
I have writed a programe a.out,I can run it many times, so there are lots of running a.out in the systerm.I want to make sure there is only one a.out is running at the same time?
|
One way to make the issue less confusing is to call your program something other than a.out. To do that, use the -o tag. for example
gcc -o hello hello.c You could also run a script to see if your program is already running. For instance, the command ps shows all processes, or ps -e shows all processes for all users. Therefor, ps -e | grep hello would return any process containing hello and ps -e | grep -q hello would just set a condition that you could test before you start an instance of hello. |
A better way to do it is to store a file somewhere (in /tmp for example) that indicates a running instance of your executable. Once your program has finished its job it must delete it.
So the pseudo-code would be : Code:
main(){ Hope it helps. |
The tmp file method will work. But for a more solid method, see this thread.
|
I agree with Hko, given that you have the source for your program. Since you already said that, I DID know. Still, name your program something other than a.out, too.
|
thank you very much! a.out is just a exmaple.
|
In addition to the file-locking proposed by Hko, there are plenty of IPC mechanisms available (e.g., UNIX domain sockets or semaphores).
|
All times are GMT -5. The time now is 08:24 PM. |