Re: [LIP] check instance of a running application

<ripley-gGZFqZ9RcmRmR6Xm/[email protected]>
Newsgroups gmane.user-groups.linux.india.programmers
Organization Portal Agory S.A.
Message-ID <[email protected]>
Hi

I think the most safely way is to at the start of your program scan 
the /proc catalog - enter into every process catalog /it can be 
easly recognized, because it is a number/ and read the name of each 
process from file stat - files stat have got the "read" permissions 
for everyone - so you are able to read stat file from every process
.
This is just like works the ps function

It could look like this:

int findprocess(char *programname) //the argument is the name of 
your daemon
    {   
    char myname[256];
    sprintf(myname, "(%s)", programname); //add brackets
    DIR *procdir = opendir("/proc");
    struct dirent *process;
    char fullpath[256] = {}; //it will be used for making full path 
to stat for the scanf function
    int procid = 0; //the process id from stat file
    char procname[32]={}; //the process name from stat file
    FILE* pstat=0;
   
   //here is the start of loop that goes through every entry 
in /proc - of course only on the top level :)
    while(process = readdir(procdir))
    {
     if (atol(process->d_name)) //the program check if the name of 
the entry is a number - if it is - thats the process!
        {
         sprintf(fullpath, "/proc/%s/stat", process->d_name);
         pstat = fopen(fullpath, "r");
         if (pstat != NULL)
            {     
            fscanf(pstat, "%d %s", &procid, procname); //read the 
process id and name - the name is in brackets
            fclose(pstat); //closing the file
            if (!strcasecmp(myname, procname)) //if the (names) are 
the same - process was found - stop loop exit function with 1
                return 1;
            };  
         };
    };
    //if the loop end naturally, that means, that the process 
wasn't found
    return 0;
};

This method has got one minus - it is not resist for changing names 
of the program

Hope To Help
Asia Sledzik

------ Wiadomość oryginalna ------
Od:    Ram <rammohhan2000-/[email protected]>
Data:  2003-12-24 13:45
Temat: [LIP] check instance of a running application

:-> Hi all,
:-> 
:->      In Linux , Thr Coding in c how can i check
:-> whether my application is running or not . i.e i have
:-> written a small c program which will run as a
:-> daemon.When i run the same program  again it should
:-> check whether previous  instance  is running or
:-> not(preferbly with the application name)  , if running
:-> then i have to pass a value to the running application
:-> ..
:-> 
:-> For checking whether app is still running , I tried by
:->  writing the  PID value to a file and later checked
:-> whether it exists or not. is there any other way to do
:-> this....
:-> 
:-> Any solution !.
:-> 
:-> 






-------------------------------------------------------
This SF.net email is sponsored by: Perforce Software.
Perforce is the Fast Software Configuration Management System offering
advanced branching capabilities and atomic changes on 50+ platforms.
Free Eval! http://www.perforce.com/perforce/loadprog.html
lmpx.com only provides a reader for public news (NNTP) servers. It is not affiliated with the servers or forums shown here and is not responsible for the content of articles, which is written by their respective authors.