zoukankan      html  css  js  c++  java
  • Linux 判断进程是否已经运行的程序

     1   bool
     2   ServerProcess::isAlreadyRunning()
     3   {
     4   #ifndef __linux__
     5      WarningLog(<<"can't check if process already running on this platform (not implemented yet)");
     6      return false;
     7   #else
     8      if(mPidFile.size() == 0)
     9      {
    10         // if no PID file specified, we do not make any check
    11         return false;
    12      }
    13   
    14      pid_t running_pid;
    15      std::ifstream _pid(mPidFile.c_str(), std::ios_base::in);
    16      if(!_pid.good())
    17      {
    18         // if the file doesn't exist or can't be opened, just ignore
    19         return false;
    20      }
    21      _pid >> running_pid;
    22      _pid.close();
    23   
    24      StackLog(<< mPidFile << " contains PID " << running_pid);
    25   
    26      Data ourProc = Data("/proc/self/exe");
    27      Data otherProc = Data("/proc/") + Data(running_pid) + Data("/exe");
    28      char our_exe[513], other_exe[513];
    29      int buf_size;
    30   
    31      buf_size = readlink(ourProc.c_str(), our_exe, 512);
    32      if(buf_size < 0 || buf_size == 512)
    33      {
    34         // if readlink fails, just ignore
    35         return false;
    36      }
    37      our_exe[buf_size] = 0;
    38   
    39      buf_size = readlink(otherProc.c_str(), other_exe, 512);
    40      if(buf_size < 0 || buf_size == 512)
    41      {
    42         // if readlink fails, just ignore
    43         return false;
    44      }
    45      other_exe[buf_size] = 0;
    46   
    47      if(strcmp(our_exe, other_exe) == 0)
    48      {
    49         ErrLog(<<"already running PID: " << running_pid);
    50         return true;
    51      }
    52      return false;
    53   #endif
    54   }
  • 相关阅读:
    Hdu 5396 Expression (区间Dp)
    Lightoj 1174
    codeforces 570 D. Tree Requests (dfs)
    codeforces 570 E. Pig and Palindromes (DP)
    Hdu 5385 The path
    Hdu 5384 Danganronpa (AC自动机模板)
    Hdu 5372 Segment Game (树状数组)
    Hdu 5379 Mahjong tree (dfs + 组合数)
    Hdu 5371 Hotaru's problem (manacher+枚举)
    Face The Right Way---hdu3276(开关问题)
  • 原文地址:https://www.cnblogs.com/stevensfollower/p/5674825.html
Copyright © 2011-2022 走看看