waitstate function, detect the state of a process iteratively
having some bugs about the I/O