From 6cd172c32ebe79692e19433f49716e58c57c8677 Mon Sep 17 00:00:00 2001 From: Boris Kolpackov Date: Wed, 27 Apr 2016 18:47:01 +0200 Subject: Rework process internals, add current_id() --- butl/process.cxx | 32 ++++++++++++++++++++++---------- 1 file changed, 22 insertions(+), 10 deletions(-) (limited to 'butl/process.cxx') diff --git a/butl/process.cxx b/butl/process.cxx index 4203735..f00f03a 100644 --- a/butl/process.cxx +++ b/butl/process.cxx @@ -5,7 +5,7 @@ #include #ifndef _WIN32 -# include // execvp, fork, dup2, pipe, chdir, *_FILENO +# include // execvp, fork, dup2, pipe, chdir, *_FILENO, getpid # include // waitpid #else # ifndef WIN32_LEAN_AND_MEAN @@ -36,12 +36,12 @@ namespace butl (err == -1 && pipe (in_efd) == -1)) throw process_error (errno, false); - id = fork (); + handle = fork (); - if (id == -1) + if (handle == -1) throw process_error (errno, false); - if (id == 0) + if (handle == 0) { // Child. If requested, close the write end of the pipe and duplicate // the read end to stdin. Then close the original read end descriptor. @@ -109,10 +109,10 @@ namespace butl bool process:: wait () { - if (id != 0) + if (handle != 0) { - int r (waitpid (id, &status, 0)); - id = 0; // We have tried. + int r (waitpid (handle, &status, 0)); + handle = 0; // We have tried. if (r == -1) throw process_error (errno, false); @@ -124,14 +124,14 @@ namespace butl bool process:: try_wait (bool& s) { - if (id != 0) + if (handle != 0) { - int r (waitpid (id, &status, WNOHANG)); + int r (waitpid (handle, &status, WNOHANG)); if (r == 0) // Not exited yet. return false; - id = 0; // We have tried. + handle = 0; // We have tried. if (r == -1) throw process_error (errno, false); @@ -141,8 +141,20 @@ namespace butl return true; } + process::id_type process:: + current_id () + { + return getpid (); + } + #else // _WIN32 + process::id_type process:: + current_id () + { + return GetCurrentProcessId (); + } + static void print_error (char const* name) { -- cgit v1.1