1 BOOL2 serverprocess::isalreadyrunning ()3 {4 #ifndef __linux__5Warninglog (<<"can ' t check if process already running on the platform (not implemented yet)");6 return false;7 #else8 if(mpidfile.size () = =0)9 {Ten //if no PID file specified, we don't make an any check One return false; A } - - pid_t running_pid; theStd::ifstream _pid (Mpidfile.c_str (), Std::ios_base::inch); - if(!_pid.good ()) - { - //if the file doesn ' t exist or can ' t be opened, just ignore + return false; - } +_pid >>Running_pid; A _pid.close (); at -Stacklog (<< mpidfile <<"contains PID"<<running_pid); - -Data Ourproc = data ("/proc/self/exe"); -Data Otherproc = data ("/proc/") + data (running_pid) + data ("/exe"); - Charour_exe[513], other_exe[513]; in intbuf_size; - toBuf_size = Readlink (Ourproc.c_str (), Our_exe, +); + if(Buf_size <0|| Buf_size = = +) - { the //If Readlink fails, just ignore * return false; $ }Panax NotoginsengOur_exe[buf_size] =0; - theBuf_size = Readlink (Otherproc.c_str (), Other_exe, +); + if(Buf_size <0|| Buf_size = = +) A { the //If Readlink fails, just ignore + return false; - } $Other_exe[buf_size] =0; $ - if(strcmp (Our_exe, other_exe) = =0) - { theErrLog (<<"already running PID:"<<running_pid); - return true;Wuyi } the return false; - #endif Wu}
A program that Linux determines whether the process is already running