Merge pull request #1620 from trustable-code/PR6
Fix terminate() and add kill()
This commit is contained in:
commit
33ce5c282e
2 changed files with 36 additions and 7 deletions
|
|
@ -164,7 +164,10 @@ proc resume*(p: PProcess) {.rtl, extern: "nosp$1", tags: [].}
|
||||||
## Resumes the process `p`.
|
## Resumes the process `p`.
|
||||||
|
|
||||||
proc terminate*(p: PProcess) {.rtl, extern: "nosp$1", tags: [].}
|
proc terminate*(p: PProcess) {.rtl, extern: "nosp$1", tags: [].}
|
||||||
## Terminates the process `p`.
|
## Stop the process `p`. On Posix OSs the procedure sends SIGTERM to the process. On Windows the Win32 API function TerminateProcess() is called to stop the process.
|
||||||
|
|
||||||
|
proc kill*(p: PProcess) {.rtl, extern: "nosp$1", tags: [].}
|
||||||
|
## Kill the process `p`. On Posix OSs the procedure sends SIGKILL to the process. On Windows kill() is an alias for terminate().
|
||||||
|
|
||||||
proc running*(p: PProcess): bool {.rtl, extern: "nosp$1", tags: [].}
|
proc running*(p: PProcess): bool {.rtl, extern: "nosp$1", tags: [].}
|
||||||
## Returns true iff the process `p` is still running. Returns immediately.
|
## Returns true iff the process `p` is still running. Returns immediately.
|
||||||
|
|
@ -475,6 +478,9 @@ when defined(Windows) and not defined(useNimRtl):
|
||||||
if running(p):
|
if running(p):
|
||||||
discard terminateProcess(p.fProcessHandle, 0)
|
discard terminateProcess(p.fProcessHandle, 0)
|
||||||
|
|
||||||
|
proc kill(p: PProcess) =
|
||||||
|
terminate(p)
|
||||||
|
|
||||||
proc waitForExit(p: PProcess, timeout: int = -1): int =
|
proc waitForExit(p: PProcess, timeout: int = -1): int =
|
||||||
discard waitForSingleObject(p.fProcessHandle, timeout.int32)
|
discard waitForSingleObject(p.fProcessHandle, timeout.int32)
|
||||||
|
|
||||||
|
|
@ -815,10 +821,10 @@ elif not defined(useNimRtl):
|
||||||
discard close(p.errHandle)
|
discard close(p.errHandle)
|
||||||
|
|
||||||
proc suspend(p: PProcess) =
|
proc suspend(p: PProcess) =
|
||||||
if kill(-p.id, SIGSTOP) != 0'i32: osError(osLastError())
|
if kill(p.id, SIGSTOP) != 0'i32: osError(osLastError())
|
||||||
|
|
||||||
proc resume(p: PProcess) =
|
proc resume(p: PProcess) =
|
||||||
if kill(-p.id, SIGCONT) != 0'i32: osError(osLastError())
|
if kill(p.id, SIGCONT) != 0'i32: osError(osLastError())
|
||||||
|
|
||||||
proc running(p: PProcess): bool =
|
proc running(p: PProcess): bool =
|
||||||
var ret = waitpid(p.id, p.exitCode, WNOHANG)
|
var ret = waitpid(p.id, p.exitCode, WNOHANG)
|
||||||
|
|
@ -826,10 +832,12 @@ elif not defined(useNimRtl):
|
||||||
result = ret == int(p.id)
|
result = ret == int(p.id)
|
||||||
|
|
||||||
proc terminate(p: PProcess) =
|
proc terminate(p: PProcess) =
|
||||||
if kill(-p.id, SIGTERM) == 0'i32:
|
if kill(p.id, SIGTERM) != 0'i32:
|
||||||
if p.running():
|
osError(osLastError())
|
||||||
if kill(-p.id, SIGKILL) != 0'i32: osError(osLastError())
|
|
||||||
else: osError(osLastError())
|
proc kill(p: PProcess) =
|
||||||
|
if kill(p.id, SIGKILL) != 0'i32:
|
||||||
|
osError(osLastError())
|
||||||
|
|
||||||
proc waitForExit(p: PProcess, timeout: int = -1): int =
|
proc waitForExit(p: PProcess, timeout: int = -1): int =
|
||||||
#if waitPid(p.id, p.exitCode, 0) == int(p.id):
|
#if waitPid(p.id, p.exitCode, 0) == int(p.id):
|
||||||
|
|
|
||||||
21
tests/stdlib/tosprocterminate.nim
Normal file
21
tests/stdlib/tosprocterminate.nim
Normal file
|
|
@ -0,0 +1,21 @@
|
||||||
|
import os, osproc
|
||||||
|
|
||||||
|
when defined(Windows):
|
||||||
|
const ProgramWhichDoesNotEnd = "notepad"
|
||||||
|
else:
|
||||||
|
const ProgramWhichDoesNotEnd = "/bin/sh"
|
||||||
|
|
||||||
|
echo("starting " & ProgramWhichDoesNotEnd)
|
||||||
|
var process = startProcess(ProgramWhichDoesNotEnd)
|
||||||
|
sleep(500)
|
||||||
|
echo("stopping process")
|
||||||
|
process.terminate()
|
||||||
|
var TimeToWait = 5000
|
||||||
|
while process.running() and TimeToWait > 0:
|
||||||
|
sleep(100)
|
||||||
|
TimeToWait = TimeToWait - 100
|
||||||
|
|
||||||
|
if process.running():
|
||||||
|
echo("FAILED")
|
||||||
|
else:
|
||||||
|
echo("SUCCESS")
|
||||||
Loading…
Add table
Add a link
Reference in a new issue