Windows features you didn’t know existed: start /b and start /priority

In popular Unix/Linux shells, there is an option to start a process in the “background” by (i.e. in bash) typing “./something &“, or pressing Ctrl-Z and then “bg“. The process then prints its output to stdout as usual, but the bash runs in the foreground and receives console stdin.

In Windows, something similar (much less powerful than the bash “jobs”, though) can be done by doing: start /b something

This runs the process in the background, and its output is printed to your stdout, even though cmd continues running immediately and receives your stdin. (If the process tries to read from stdin, weird things happen that I can’t explain.)

If you do “start something” without the “/b” switch, the process runs in another window, which closes when it exits.

Another switch for “start” is /priority, where priority is the name of a process priority. This can be combined with /b to give you:

start /b /realtime pslist

to get a pslist output that runs in realtime priority.

Important note about this: You may think this is unnecessary if your cmd already runs in high priority, because the priority will carry on to the child process by default anyway, right?

Wrong! Here is the relevant excerpt from the documentation of the CreateProcess function:

dwCreationFlags
[in] Flags that control the priority class and the creation of the process. For a list of values, see Process Creation Flags.This parameter also controls the new process’s priority class, which is used to determine the scheduling priorities of the process’s threads. For a list of values, see GetPriorityClass. 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.

Well, that was news to me, anyway.

I really recommend reading start /? and cmd /?, to catch up with the other cool features you didn’t know about.

About these ads

2 Responses to Windows features you didn’t know existed: start /b and start /priority

  1. Thanks a lot for taking your time to provide this cool information.

    Once i used to program the Win32 API, but nowadays i don’t. So, your hints came up handy to provide the tricks needed for the Win32 programming.

    All the best,
    hilton

  2. Use HIGH_PRIORITY_CLASS with care. If a thread runs at the highest priority level for extended periods, other threads in the system will not get processor time. If several threads are set at high priority at the same time, the threads lose their effectiveness. The high-priority class should be reserved for threads that must respond to time-critical events. If your application performs one task that requires the high-priority class while the rest of its tasks are normal priority, use SetPriorityClass to raise the priority class of the application temporarily; then reduce it after the time-critical task has been completed. Another strategy is to create a high-priority process that has all of its threads blocked most of the time, awakening threads only when critical tasks are needed. The important point is that a high-priority thread should execute for a brief time, and only when it has time-critical work to perform.

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s

Follow

Get every new post delivered to your Inbox.

%d bloggers like this: