tiny-process-library
tiny-process-library copied to clipboard
Specify Process Priority
Hi, I'd like to specify a priority when launching a process. For example, I want to launch a background process with a lower process priority than the main process. As of right now, new processes are spawned with the default priority class and tiny process library does not give us the ability to control that.
On Windows, we provide a priority class flag in the 'dwCreationFlags' parameter to CreateProcess as specified here: https://msdn.microsoft.com/en-us/library/windows/desktop/ms682425(v=vs.85).aspx
I'm assuming *nix has a similar mechanism.
I could create a PR if you'd like; though the implementation seems fairly straight forward.
Thank you
Tom
I would have to read up on this:
- what priority settings are default on Unix and Windows
- what priority settings are available on Unix and Windows
Feel free to do some research on this, I'm a bit swamped at the moment, and add relevant links if you'd like.
Sure thing.
Defaults:
Linux: http://linux.die.net/man/3/fork For the SCHED_FIFO and SCHED_RR scheduling policies, the child process shall inherit the policy and priority settings of the parent process during a fork() function. For other scheduling policies, the policy and priority settings on fork() are implementation-defined.
http://linux.die.net/man/2/setpriority A child created by fork(2) inherits its parent's nice value.
Windows: https://msdn.microsoft.com/en-us/library/windows/desktop/ms682425(v=vs.85).aspx If none of the priority class flags is specified, the priority class defaults to NORMAL_PRIORITY_CLASS unless the priority class of the creating process is IDLE_PRIORITY_CLASS or BELOW_NORMAL_PRIORITY_CLASS. In this case, the child process receives the default priority class of the calling process.
Available settings:
Linux: http://linux.die.net/man/2/setpriority
http://linux.die.net/man/1/renice Users other than the super-user may only alter the priority of processes they own, and can only monotonically increase their ''nice value'' within the range 0 to PRIO_MAX (20)
Windows: https://msdn.microsoft.com/en-us/library/windows/desktop/ms685100(v=vs.85).aspx
Discussion:
Due to the Linux restrictions on raising priority class, it's possible that setpriority might fail. I don't see any restrictions on Windows. This library could simply take an optimistic approach; attempt to change process priority and silently ignore errors, while documenting this possibility. Alternatively, return an error code or throw an exception.
Standardizing priority values between platforms would need some thinking, as Linux priorities are more fine-grained than Windows priorities. Potentially you could define an enum with linux equivalents for the windows named priorities; eg NORMAL_PRIORITY_CLASS = 0, BELOW_NORMAL_PRIORITY_CLASS = 10, IDLE_PRIORITY_CLASS = PRIO_MAX. In the Windows impl, use the named values; in the *nix impl, use the underlying int values of that enum. I would also recommend a DEFAULT flag/enum so that the child process will default to the inherted priority class, which is the current behavior.
Hope that helps/makes sense, let me know if I can assist further. Thank you
Thanks for the excellent explanation! I'll have a look at this later this week.