Results 1 to 10 of 10
Hello, everybody!
I'm recently learning system programming in Linux. I'm writing a program, which should have at most one instance, so every time it starts, it should first check whether ...
- 12-06-2008 #1Just Joined!
- Join Date
- Feb 2008
- Posts
- 21
How to check whether a process is running?
Hello, everybody!
I'm recently learning system programming in Linux. I'm writing a program, which should have at most one instance, so every time it starts, it should first check whether an instance of the same program si running. How should I make this?
- 12-06-2008 #2
Use the ps command. Open a terminal/konsole window and issue this command to read the man page for ps, inlcuding a lot of options.
Maybe you want something like thisCode:man ps
Where process-name is the actual name of your process.Code:ps -ax |grep process-name
I do not respond to private messages asking for Linux help, Please keep it on the forums only.
All new users please read this.** Forum FAQS. ** Adopt an unanswered post.
- 12-06-2008 #3Just Joined!
- Join Date
- Feb 2008
- Posts
- 21
Thanks. But I'm writing a c program, so I must incorporate this command into my code. So what is the corresponding c system call?
- 12-06-2008 #4
Aww man, my bad......I apologize. I don't know any C programming but there is a ton of guys here that know this stuff. Hopefully one will be by here very shortly.
I do not respond to private messages asking for Linux help, Please keep it on the forums only.
All new users please read this.** Forum FAQS. ** Adopt an unanswered post.
- 12-06-2008 #5A couple of complications with this idea:Code:
ps -ax |grep process-name
- If the program is running already, sometimes you'll get two lines, containing these two strings:
... and sometime you won't. So if the program isn't running already, sometimes you'll get this line:Code:process-name ps -ax | grep process-name
and sometimes you won't. You can fix this problem by saying:Code:ps -ax | grep process-name
Even better yet, if you're going to do this command, leave out the grep entirely, and parse the output of the ps command yourself, in your C program.Code:ps -ax | grep program-name | grep -v grep
- It's entirely possible that someone else is running a program called program-name-xxx or program-name-2.0 or something. If you're parsing the ps command yourself, you can figure out how to avoid this problem.
- You'll be firing up at least one new process, the ps process, just to check for duplicate running. Extra overhead. This is a really tiny problem, unless you're running your program several hundred times a second or something.
- You may get a race condition. You don't need to worry too much, unless this is a mission-critical application where life or property (or perhaps your job) is at stake. That is, two people might fire this program up at about the same time. Each instantiation of the program sees that the other one is running and quits, so you get six more weeks of winter. It's usually a good idea to get into the habit of avoiding race conditions.
If you want to send the output of the ps command to a file and read that file with your C program, use system(), as follows:what is the corresponding c system call?
Do this for more details:Code:int result; result=system("ps -ax > psfile"); if(result==-1) { /* Do error processing here. */ }
If you want the output of the command to come directly to your program so you can parse the output on the fly, use popen().Code:man system
I recommend that you ignore all the suggestions I've made so far.
To avoid all the problems I listed above, including the race condition. use a lock file, with what is known as "advisory locking". Search for Advisory locking as you read the man page:
Lock byte zero of the file (length would be one byte) with F_WRLCK. The file doesn't need to contain any actual bytes; a zero-length file will do. Only one process can hold this lock at a time.Code:man fcntl
If you succeed in setting this lock, keep the file descriptor open. Other than that, you can ignore the file and its lock for the rest of the process. When the process exits, the file will be closed and the lock will be released.
It's a good idea to specify the whole path name to the file.
The program can, if it wishes, create the file, so you don't have to do that manually. If the program is to be used by more than one user account, then the program which creates the file should make it universally writeable, and make sure that all the file's ancestor directories are universally "executable". This does present a security risk, in that a malicious user can use this file to store lots and lots of data. This is a concern if disk space is tight or users are charged for disk space usage.
If the program creates the lock file on the fly, you can avoid race conditions as follows:
- If you find that the file does not exist and you attempt to create it and the creation fails because another program is doing the same thing. you can either just ignore the error and continue to attempt to lock the file, or you can just exit and let the other instantiation of the program do what it wants.
- If you succeed in creating the file but fail to lock it because some other process has locked it, that's not a problem. Just exit. You created the file and the other process snuck in and started running. No biggie.
Note that the following is not a race condition:
- One process starts running the program, does what it needs, and is just about to exit, but hasn't quite exited yet.
- Another process wants to run the program, but fails to lock the file, because the first process hasn't gotten around to exiting yet.
This is not a race condition, because your requirements have been met: the seond process will fail to run the program when, and only when, the first process is running the program.
Hope this helps.--
Bill
Old age and treachery will overcome youth and skill.
- If the program is running already, sometimes you'll get two lines, containing these two strings:
- 12-06-2008 #6
wje_lf:
Ironically, you are at the top of this ton-of-guys-that-program list. And you are right on time! =-)I do not respond to private messages asking for Linux help, Please keep it on the forums only.
All new users please read this.** Forum FAQS. ** Adopt an unanswered post.
- 12-06-2008 #7Just Joined!
- Join Date
- Feb 2008
- Posts
- 21
wje_lf:
Wow! Incredible...
It took me half an hour to figure out the main idea of your explanation. Your explanation is even more detailed than the textbook I'm reading! And in such limited time.
Anyhow, after reading your post, I've arrived at my goal by using a lock file. I didn't pay much attention to so many detailed suggestions you listed, though. And I'm content with my little success.
Thanks again!!!
- 12-07-2008 #8Linux User
- Join Date
- Mar 2008
- Posts
- 287
Another quick "C" approach is to use a "system" call. Construct is:
system (<bash command>). See "man system"
- 12-07-2008 #9Well, um, yeah, that's what I said. :)Another quick "C" approach is to use a "system" call. Construct is:
system (<bash command>). See "man system"--
Bill
Old age and treachery will overcome youth and skill.
- 03-05-2009 #10Just Joined!
- Join Date
- Mar 2009
- Posts
- 2
Can't you just do "ps -C <command name>" and check the exit status?
If you want to get the pid of a process running, do 'ps -C <command> -opid=' or just 'pidof <command name>'. Let unix commands do most of the work and shouldn't be any parsing on the output line. Awk is your friend.
If you want to get command output, just do popen and pclose. fcntl!!?? yuck.
See Making the most of unix commands when programming (Part 1) for details
Hope this helps


Reply With Quote