-
Notifications
You must be signed in to change notification settings - Fork 1.6k
Commit
- Loading branch information
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -23,6 +23,9 @@ | |
#include <string.h> | ||
#include <sys/wait.h> | ||
#include <spawn.h> | ||
#include <sys/time.h> | ||
#include <sys/resource.h> | ||
#include <limits.h> | ||
|
||
#if defined(USE_PPOLL) | ||
#include <poll.h> | ||
|
@@ -48,7 +51,8 @@ Subprocess::~Subprocess() { | |
Finish(); | ||
} | ||
|
||
bool Subprocess::Start(SubprocessSet* set, const string& command) { | ||
bool Subprocess::Start(SubprocessSet* set, const string& command, | ||
int priority) { | ||
int output_pipe[2]; | ||
if (pipe(output_pipe) < 0) | ||
Fatal("pipe: %s", strerror(errno)); | ||
|
@@ -123,6 +127,12 @@ bool Subprocess::Start(SubprocessSet* set, const string& command) { | |
if (err != 0) | ||
Fatal("posix_spawn: %s", strerror(err)); | ||
|
||
if (priority != INT_MIN) { | ||
err = setpriority(PRIO_PROCESS, pid_, priority); | ||
This comment has been minimized.
Sorry, something went wrong.
This comment has been minimized.
Sorry, something went wrong.
digit-google
Contributor
|
||
if (err != 0 && err != ESRCH) | ||
Fatal("setpriority: %s", strerror(errno)); | ||
} | ||
|
||
err = posix_spawnattr_destroy(&attr); | ||
if (err != 0) | ||
Fatal("posix_spawnattr_destroy: %s", strerror(err)); | ||
|
@@ -238,9 +248,9 @@ SubprocessSet::~SubprocessSet() { | |
Fatal("sigprocmask: %s", strerror(errno)); | ||
} | ||
|
||
Subprocess *SubprocessSet::Add(const string& command, bool use_console) { | ||
Subprocess *SubprocessSet::Add(const string& command, int priority, bool use_console) { | ||
Subprocess *subprocess = new Subprocess(use_console); | ||
if (!subprocess->Start(this, command)) { | ||
if (!subprocess->Start(this, command, priority)) { | ||
delete subprocess; | ||
return 0; | ||
} | ||
|
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -52,7 +52,11 @@ struct Subprocess { | |
|
||
private: | ||
Subprocess(bool use_console); | ||
bool Start(struct SubprocessSet* set, const std::string& command); | ||
// priority: | ||
// - The range of possible values is platform dependent | ||
// - POSIX: variable between scheduling policies between sched_get_priority_min() and sched_get_priority_max() | ||
// - Windows: between -20 (highest priority) and 20 (lowest priority) | ||
bool Start(struct SubprocessSet* set, const std::string& command, int priority); | ||
void OnPipeReady(); | ||
|
||
std::string buf_; | ||
|
@@ -83,7 +87,8 @@ struct SubprocessSet { | |
SubprocessSet(); | ||
~SubprocessSet(); | ||
|
||
Subprocess* Add(const std::string& command, bool use_console = false); | ||
Subprocess* Add(const std::string& command, int priority = 0, | ||
bool use_console = false); | ||
This comment has been minimized.
Sorry, something went wrong.
digit-google
Contributor
|
||
bool DoWork(); | ||
Subprocess* NextFinished(); | ||
void Clear(); | ||
|
1 comment
on commit e922611
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
How do you check that this works correctly? Can you write a SubprocessTest unit-test where the spawned command prints its niceness value, then compare it with an expected value?
no, you should save the current priority level to restore it after the spawn() call.
Be aware that
getpriority()
can return a negative value even if no error occured, so you should clear errno specifically before that, e.g.:Then later,