Thread: Linux C program needs to id if it is already running

  1. #1
    Registered User
    Join Date
    Oct 2013
    Location
    Midrand Gauteng South Africa
    Posts
    27

    Arrow Linux C program needs to id if it is already running

    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?

  2. #2
    Stoned Witch Barney McGrew's Avatar
    Join Date
    Oct 2012
    Location
    astaylea
    Posts
    420
    Some men annoy a lad called waitpid with their concerns.
    i dont believe in competition in da field of cboard posts, u see, i believe in a collection of posts each 2 be admired for der own personal statement. when in doubt, ask Willy!

  3. #3
    TEIAM - problem solved
    Join Date
    Apr 2012
    Location
    Melbourne Australia
    Posts
    1,907
    This should be in the Linux thread - You will get better answers there
    Fact - Beethoven wrote his first symphony in C

  4. #4
    Registered User
    Join Date
    Oct 2013
    Location
    Midrand Gauteng South Africa
    Posts
    27
    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.
    Last edited by straygrey; 11-17-2013 at 06:27 AM. Reason: clarity

  5. #5
    and the hat of int overfl Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    39,661
    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.
    If you dance barefoot on the broken glass of undefined behaviour, you've got to expect the occasional cut.
    If at first you don't succeed, try writing your phone number on the exam paper.

  6. #6
    Registered User
    Join Date
    Oct 2013
    Location
    Midrand Gauteng South Africa
    Posts
    27
    Thank you Salem. I will do just that.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Running Linux from a ramdisk (root)
    By cyberfish in forum Tech Board
    Replies: 2
    Last Post: 05-09-2009, 02:45 AM
  2. Running linux system commands
    By Creini in forum C Programming
    Replies: 2
    Last Post: 05-27-2006, 06:56 AM
  3. Running C program in Linux or Windows
    By kepler in forum C Programming
    Replies: 4
    Last Post: 09-30-2003, 08:31 AM
  4. MainBoard for running Linux
    By Kelvin in forum A Brief History of Cprogramming.com
    Replies: 19
    Last Post: 07-18-2002, 09:19 AM
  5. running program with arguments (linux)
    By N8760 in forum C++ Programming
    Replies: 2
    Last Post: 01-28-2002, 02:08 PM