Solved

Setting the priority on a windows service from within the service itself.

Posted on 2010-11-10
2
395 Views
Last Modified: 2012-05-10
Hi.

Recently, I've been updating a PHP extension to allow PHP scripts to run as a Windows Service. I wasn't the initial author, but I've been adding some facilities that I have been wanting.

Documentation : http://docs.php.net/manual/en/book.win32service.php
PECL Package : http://pecl.php.net/package/win32service
SVN Repository : http://svn.php.net/viewvc/pecl/win32service/trunk/)

You can install/uninstall/start/stop/pause/continue the service from the command line as well as the normal Windows Services Management Console.

As the service is running the scripting engine, the scripting engine is greedy in its processor utilisation. That's perfectly normal for conventional execution (CLI, FastCGI, ISAPI, etc.).

In this instance, I'd like to allow assign a priority so that the service doesn't hog all the processor. Essentially, making it a real background task.

The setting needs to be configurable (but not dynamic). So, by using the win32_create_service() call, I want to allow the end-user to assign the priority that they want the script to run at (by default, I'd like it to be IDLE).

Getting the value into the code is easy enough, but I need to be able to hold it prior to the execution of the service. I assume this has to be the registry. If so, is accessing the registry for a windows service particularly easy? Do I just work it out myself?

Once saved, reading the registry during the service_main should be fairly simple, but this would then be within the PHP engine itself. It is at that stage that I need to alter the priority.

This is the main part I'm stuck with.

I've googled altering a running processes priority, external to the process, but not how to alter THIS process.

Any ideas?

I'm using MS VC++ 2008 Express Edition (VC9) with SDK6.1 on a Windows 2003 Server virtual machine. The code is built to run on XP x86 and is deployed on Windows XP SP3, Windows 2003 Server and in the next few weeks, a 2008 server (Vista/2008 have additional features for services).

Regards,

Richard.
0
Comment
Question by:Richard Quadling
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 2
2 Comments
 
LVL 86

Expert Comment

by:jkr
ID: 34102834
Process priority is unrelated to the fact that the process is a service, you can adjust that on a per-thread basis using 'SetThreadPriority()' (http://msdn.microsoft.com/en-us/library/ms686277%28VS.85%29.aspx) or for the whole process with 'SetPriorityClass()' (http://msdn.microsoft.com/en-us/library/ms686219%28v=VS.85%29.aspx).
0
 
LVL 86

Accepted Solution

by:
jkr earned 500 total points
ID: 34102992
Ooops, forgot the 'itself' aspect, that's what 'GetCurrentProcess()' (http://msdn.microsoft.com/en-us/library/ms683179(VS.85).aspx) enters the picture, e.g.

SetPriorityClass(GetCurrentProcess(),IDLE_PRIORITY_CLASS);
0

Featured Post

NFR key for Veeam Backup for Microsoft Office 365

Veeam is happy to provide a free NFR license (for 1 year, up to 10 users). This license allows for the non‑production use of Veeam Backup for Microsoft Office 365 in your home lab without any feature limitations.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

The recent Microsoft changes on update philosophy for Windows pre-10 and their impact on existing WSUS implementations.
For anyone that has accidentally used newSID with Server 2008 R2 (like I did) and hasn't been able to get the server running again because you were unlucky (as I was) and had no backups - I was able to get things working by doing a Registry Hive rec…
This tutorial will give a an overview on how to deploy remote agents in Backup Exec 2012 to new servers. Click on the Backup Exec button in the upper left corner. From here, are global settings for the application such as connecting to a remote Back…
This tutorial will show how to configure a new Backup Exec 2012 server and move an existing database to that server with the use of the BEUtility. Install Backup Exec 2012 on the new server and apply all of the latest hotfixes and service packs. The…
Suggested Courses

617 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question