Making a program remember it's priority

Status
Not open for further replies.

Vehementi

Posts: 2,644   +2
Hello all,

I'm wondering if there's anything I can do to make a program, when automatically started, to resume a preset priority for itself. This is Windows XP, of course. The program in question uses alot of CPU cycles over a long period of time, and is by default set to 'Normal'. This of course slows my system down horribly and I always have to set it down to Low. Any way I can make it automatically go to Low? I'm sure there's some registry entry that I can use, anyone got any ideas?

Thanks in advance
 
Well, you could make a shortcut. This is a little idea of mine actually, rather the usual batch file that has been widely spread on the net.

Just make a shortcut, and instead of making it "C:\notepad.exe", make it "C:\WINDOWS\system32\cmd.exe /c START /low C:\windows\notepad.exe"

The key here is the cmd.exe, START and the path to the program.

"C:\WINDOWS\system32\cmd.exe" = Opens the command prompt
"/c START /low" prepares to run the program in low priority

"C:\notepad.exe" = opens notepad.

If you do not want to see the command prompt when you run the program, right click on the shortcut after you make it and adjust the properties. You can make it start off-screen by choosing the pixels down and right that is starts in the shortcut. I usually set mine to 2000x2000 so it is off screen.
 
Nice - I got it to work after I removed the spaces from the path. Thanks Rick! I'll just pin it to my start menu.
 
I forgot to mention that you cannot use quotes for paths, so that means all of your paths have to be strictly alpha-numeric or MSDOS compatible.
 
On a side note, Rick, do you know how, in 2k/XP, to launch an app with affinity towards a specific CPU? For instance, to remove its affinity from cpu 0 and only let it execute under cpu 1, from the command prompt?
 
Wish I did know, but since I'm not exposed to dual CPU setups, I've never bothered looking - That is - Until now! I will post back with any leads I find on how to appropriate selective CPU usage.
 
Unfortunatley, I have not been able to find anything.

If it is possible to control CPU affinity from command prompt, then it should be doable in a shortcut liek the one I gave Vehementi. You would have to add the proper command and switch to the shortcut.

But I was unable to find any command that will change CPU affinity from the console.
 
A rather late reply, but still, it didn't take too long to whip up a tool to start a program with affinity and priority as arguments:

Download from http://www.valhallalegends.com/adron/prioaff/prioaff.zip if you still need it!


Code:
#include <windows.h>

int WINAPI WinMain(HINSTANCE, HINSTANCE, LPSTR lpCmdLine, int nCmdShow)
{
	char *p = lpCmdLine;
	char *args[3];
	int n = 0;
	while(*p) {
		while(*p == ' ')
			p++;
		args[n++] = p;
		if(n == 3)
			break;
		while(*p && *p != ' ')
			p++;
	}
	

	if(n != 3) {
		MessageBox(0, 
			"Prioaff by Adron\n\n"
			"A simple tool to start a process with a given\n"
			"priority and affinity\n\n"
			"Syntax:\n"
			"prioaff <priority> <affinity> <executable> [arguments]\n"
			"priority: 0..3 for idle..realtime\n"
			"affinity: binary process affinity mask\n\n"
			"You are free to use this program as you please.\n"
			"Please drop me a message and let me know\n"
			"if you're making a derived work from it.\n\n"
			"As always, there are no express or implied\n"
			"warranties as to the functionality\n"
			"of this application!", "Prioaff", MB_OK);
		return 1;
	}

	DWORD priorities[4] = {IDLE_PRIORITY_CLASS, NORMAL_PRIORITY_CLASS, HIGH_PRIORITY_CLASS, REALTIME_PRIORITY_CLASS};
	DWORD priority = strtoul(args[0], 0, 10);
	DWORD affinity = strtoul(args[1], 0, 2);
	DWORD processmask, systemmask;
	GetProcessAffinityMask(GetCurrentProcess(), &processmask, &systemmask);

	if(priority > 3) {
		MessageBox(0, "Invalid priority value!", "Prioaff", MB_OK);
		return 1;
	}
	if((affinity & processmask) != affinity) {
		MessageBox(0, "Invalid affinity value!", "Prioaff", MB_OK);
		return 1;
	}

	STARTUPINFO si;
	PROCESS_INFORMATION pi;

	ZeroMemory(&si, sizeof si);
	si.cb = sizeof si;
	si.dwFlags = STARTF_USESHOWWINDOW;
	si.wShowWindow = nCmdShow;

	BOOL success = CreateProcess(0, args[2], 0, 0, 0, CREATE_SUSPENDED | priorities[priority], 0, 0, &si, &pi);
	
	if(!success) {
		char message[128];
		wsprintf(message, "CreateProcess failed, error: %d\n", GetLastError());
		MessageBox(0, message, "Prioaff", MB_OK);
		return 0;
	}

	success = SetProcessAffinityMask(pi.hProcess, affinity);

	if(!success) {
		char message[128];
		wsprintf(message, "SetProcessAffinityMask failed, error: %d\n", GetLastError());
		MessageBox(0, message, "Prioaff", MB_OK);
	}

	ResumeThread(pi.hThread);

	CloseHandle(pi.hProcess);
	CloseHandle(pi.hThread);
	
	return 0;
}
 
nope, "'s can be used. for instance for photoshop:
C:\WINDOWS\system32\cmd.exe /c start "lowered priority shortcut to Adobe Photoshop" /belownormal "C:\Program Files\Adobe\Adobe Photoshop"
 
Mircosoft (server at least) does SMP(symmetric multi-processor) support well.
With more than one processors, the TCP processing is dedicated to the last
CPU and services are spread across the others.

As a general rule, users attempting to futz with affinity usually just get in the way
and mess up the processor assignments. User selected priorities is great;
affinity should be left to the scheduler.

just my opinion; Jeff
 
Soul Harvester said:
On a side note, Rick, do you know how, in 2k/XP, to launch an app with affinity towards a specific CPU? For instance, to remove its affinity from cpu 0 and only let it execute under cpu 1, from the command prompt?

you can use imagecfg.exe. IMPORTANT: this modifies the exe slightly, so keep a spare backup.
 
Status
Not open for further replies.
Back