Thread: Program to check for running pid using system call in UNIX/linux

  1. #1
    Registered User
    Join Date
    Dec 2013
    Posts
    2

    Post Program to check for running pid using system call in UNIX/linux

    I have written a program which uses a pid to check if the process is currently running and return a value based on the system call result.

    But the program core dumps

    Code:
    #include <stdio.h>
    #include <string.h>
     
    int main( argc, argv )
    int argc;
    char * argv[];
    {
         int p_pid = 99;
         char buff[1000];
          
     
        const char *program_name = basename(argv[0]);
     
        memset( buff, '\0', sizeof( buff));
        sprintf(buff, " ps -ef | grep %s | grep %d ", program_name,  p_pid);
     
        printf("this is %s", buff);
     
        if( system(buff) == 0 )
         {
           printf(" system call returns 0 ");
         }
        else
         {
            printf("system call is non zero");
         }
     
    }
    What is the mistake in this code and is it portable in both unix/linux , is the method secure (grepping for program name )?
    Last edited by selvankj; 12-11-2013 at 11:35 PM. Reason: corrected a mistake in code

  2. #2
    misoturbutc Hodor's Avatar
    Join Date
    Nov 2013
    Posts
    1,787
    You want the pid of the current process? (I'm assuming this because you're using argv[0], which by the way may be an empty string)

    If so, getpid() is probably what you want

  3. #3
    and the hat of int overfl Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    39,661
    > int main( argc, argv )
    > int argc;
    > char * argv[];
    This style was officially obsolete in 1989.
    What ancient dusty text are you learning from?

    > int p_pid = 99;
    What's this got to do with anything.
    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.

  4. #4
    Registered User
    Join Date
    Nov 2012
    Posts
    1,393
    As for your core dump, maybe you need to compile with warnings turned on. basename is defined in a different header, so maybe you're ending up with a wrong prototype for this function, I couldn't produce this error on my system.

    As for your problem (finding the pid), then Hodor is right you should use getpid() rather than passing commandlines to system. The following works for me

    Code:
    #include <stdio.h>
    #include <stdlib.h>
    #include <libgen.h>
    #include <unistd.h>
    
    int main(int argc, char *argv[])
    {
        const char *program_name = basename(argv[0]);
        if (argc != 1) {
            printf("Usage: %s\n", program_name);
            exit(1);
        }
        
        pid_t program_pid = getpid();
        printf("%s has pid %d\n", program_name, (int)program_pid);
        
        return 0;
    }

  5. #5
    Registered User
    Join Date
    Dec 2013
    Posts
    2
    I don't have to get the pid, I have it already, just wanted to check if the pid I have matches with the pid of the current running process,....thanks for letting me know of using an obselete style of program which am using, but you have just not answered by question... The reason for hard coding the pid is just to show in the code that I have pid, which again I will be getting through cmd....

    Quote Originally Posted by c99tutorial View Post
    As for your core dump, maybe you need to compile with warnings turned on. basename is defined in a different header, so maybe you're ending up with a wrong prototype for this function, I couldn't produce this error on my system.

    As for your problem (finding the pid), then Hodor is right you should use getpid() rather than passing commandlines to system. The following works for me

    Code:
    #include <stdio.h>
    #include <stdlib.h>
    #include <libgen.h>
    #include <unistd.h>
    
    int main(int argc, char *argv[])
    {
        const char *program_name = basename(argv[0]);
        if (argc != 1) {
            printf("Usage: %s\n", program_name);
            exit(1);
        }
        
        pid_t program_pid = getpid();
        printf("%s has pid %d\n", program_name, (int)program_pid);
        
        return 0;
    }

  6. #6
    misoturbutc Hodor's Avatar
    Join Date
    Nov 2013
    Posts
    1,787
    Quote Originally Posted by selvankj View Post
    I don't have to get the pid, I have it already, just wanted to check if the pid I have matches with the pid of the current running process,....thanks for letting me know of using an obselete style of program which am using, but you have just not answered by question... The reason for hard coding the pid is just to show in the code that I have pid, which again I will be getting through cmd....
    Huh?

  7. #7
    Registered User
    Join Date
    Nov 2012
    Posts
    1,393
    Quote Originally Posted by selvankj View Post
    I don't have to get the pid, I have it already, just wanted to check if the pid I have matches with the pid of the current running process,....thanks for letting me know of using an obselete style of program which am using, but you have just not answered by question...
    If you have the pid, let's call it MYPID. And you have the real pid, let's call it REALPID, then how does this not allow you to check if the pid you have matches the pid of the current running process. I think you might be overthinking the problem.

    Code:
    /* Pseudocode 
    
    if MYPID = REALPID, then:
       "Yes"
    else:
       "No"
    
    */

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Replies: 5
    Last Post: 03-26-2009, 11:02 AM
  2. Replies: 1
    Last Post: 02-04-2009, 06:54 AM
  3. Running linux system commands
    By Creini in forum C Programming
    Replies: 2
    Last Post: 05-27-2006, 06:56 AM
  4. problem with lseek system call on unix
    By kheinz in forum C Programming
    Replies: 2
    Last Post: 06-03-2003, 12:50 PM
  5. Is malloc() a system call on UNIX?
    By Waiting_11 in forum C Programming
    Replies: 4
    Last Post: 10-29-2002, 11:51 AM