Quantcast
Channel: VMware Communities : Popular Discussions - Hyperic User Forums
Viewing all articles
Browse latest Browse all 52618

getProcState may return wrong value on consecutive calls

$
0
0

Hi,

 

In my application code I found that consecutive calls to getProcState may return wrong value.

 

My box is a Linux amd64, and this test case fails with both 1.6.4 and 1.6.5:

 

package test;

import org.hyperic.sigar.ProcState;
import org.hyperic.sigar.Sigar;
import org.hyperic.sigar.SigarException;

public class ProcStateTest {

     private Process process;     private Sigar sigar;     private long processPid;     public static void main(String[] args) throws Exception {          ProcStateTest test = new ProcStateTest();          try {               test.setup();               test.testIsRunning();          } finally {               test.tearDown();          }     }     public void setup() throws Exception {          process = Runtime.getRuntime().exec("watch echo hellosigar");          if (!isAlive(process)) {               throw new RuntimeException("process is not alive");          }          sigar = new Sigar();          processPid = processPid();          if (processPid == -1) {               throw new RuntimeException("process not found");          }     }     public void tearDown() {          if (process != null) {               process.destroy();          }     }     public void testIsRunning() throws Exception {          if (!isRunning(getProcState())) {               throw new RuntimeException("Expected process to be running");          }          process.destroy();          do {               Thread.sleep(5000);          } while (isAlive(process));          for (int i = 0; i < 10; i++) {               if (isRunning(getProcState())) {                    throw new RuntimeException(                              "Expected process to be down at step[" + i + "]");               }          }     }     private long processPid() throws SigarException {          long[] pids = sigar.getProcList();          long processPid = -1;          for (long pid : pids) {               String[] procArgs = sigar.getProcArgs(pid);               for (String procArg : procArgs) {                    if (procArg != null && procArg.contains("hellosigar")) {                         processPid = pid;                         break;                    }               }               if (processPid != -1) {                    break;               }          }          return processPid;     }     private boolean isAlive(Process process) {          if (process == null) {               return false;          }          try {               process.exitValue();               return false;          } catch (IllegalThreadStateException e) {               return true;          }     }     private ProcState getProcState() {          ProcState procState = null;          try {               procState = sigar.getProcState(processPid);          } catch (SigarException ignore) {          }          return procState;     }     private boolean isRunning(ProcState procState) {          boolean running = false;          if (procState != null) {               running = (procState.getState() == ProcState.RUN                         || procState.getState() == ProcState.SLEEP || procState                         .getState() == ProcState.IDLE);          }          return running;     }

}

 

The only way to get it work is to have at least a 2 seconds interval between calls to getProcState.

 

Did I miss something or is this really a bug?

 

Thanks and regards,

Thomas


Viewing all articles
Browse latest Browse all 52618

Trending Articles



<script src="https://jsc.adskeeper.com/r/s/rssing.com.1596347.js" async> </script>