Find the answer to your Linux question:
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 ...
  1. #1
    Just 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?

  2. #2
    Super Moderator MikeTbob's Avatar
    Join Date
    Apr 2006
    Location
    Texas
    Posts
    7,144
    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.
    Code:
    man ps
    Maybe you want something like this
    Code:
    ps -ax |grep process-name
    Where process-name is the actual name of your process.
    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.

  3. #3
    Just 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?

  4. #4
    Super Moderator MikeTbob's Avatar
    Join Date
    Apr 2006
    Location
    Texas
    Posts
    7,144
    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.

  5. #5
    Linux Engineer wje_lf's Avatar
    Join Date
    Sep 2007
    Location
    Mariposa
    Posts
    1,192
    Code:
    ps -ax |grep process-name
    A couple of complications with this idea:
    1. If the program is running already, sometimes you'll get two lines, containing these two strings:
      Code:
      process-name
      ps -ax | grep process-name
      ... and sometime you won't. So if the program isn't running already, sometimes you'll get this line:
      Code:
      ps -ax | grep process-name
      and sometimes you won't. You can fix this problem by saying:
      Code:
      ps -ax | grep program-name | grep -v grep
      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.
    2. 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.
    3. 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.
    4. 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.

    what is the corresponding c system call?
    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:
    Code:
    int result;
    result=system("ps -ax > psfile");
    if(result==-1)
    {
      /* Do error processing here. */
    }
    Do this for more details:
    Code:
    man system
    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().

    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:
    Code:
    man fcntl
    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.

    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:
    1. 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.
    2. 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:
    1. One process starts running the program, does what it needs, and is just about to exit, but hasn't quite exited yet.
    2. 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.

  6. #6
    Super Moderator MikeTbob's Avatar
    Join Date
    Apr 2006
    Location
    Texas
    Posts
    7,144
    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.

  7. #7
    Just 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!!!

  8. #8
    Linux 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"

  9. #9
    Linux Engineer wje_lf's Avatar
    Join Date
    Sep 2007
    Location
    Mariposa
    Posts
    1,192
    Another quick "C" approach is to use a "system" call. Construct is:
    system (<bash command>). See "man system"
    Well, um, yeah, that's what I said. :)
    --
    Bill

    Old age and treachery will overcome youth and skill.

  10. #10
    Just 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

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  
...