Still celebrating National IT Professionals Day with 3 months of free Premium Membership. Use Code ITDAY17

x
?
Solved

Running Processes from a Service

Posted on 1999-07-29
7
Medium Priority
?
170 Views
Last Modified: 2010-04-02
Hi all,
  I'm trying to write a service in Win NT that acts as a auto-starter which starts other processes. I will start the process by calling CreateProcess() in ServiceMain(), which works fine.
  But it's not so good when the created process crashes. There seems to be no console associated with the created process (it's a console app.) and it sort of lingers as a runaway process, taking up memory space. I can't kill it, even with the task manager, and the only way I can remove it is to restart.
  I've written my service such that it will restart the process if it comes down for any reason, but this 'lingering process' problem would not allow me to do that. My process therefore fails and would not recover automatically.
  Making the process crash-proof is the ultimate solution of course (let's just say I'm in the midst of sorting THAT out), but I'm more interested right now in the mechanics of creating processes inside services, because I need to behaviour of these processes to be at least manageable.

Thanks.

PS: I'm apologise for offering a bit low on points - it's all I have at the moment!!
0
Comment
Question by:jernhung
[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
  • 4
  • 2
7 Comments
 
LVL 86

Expert Comment

by:jkr
ID: 1201347
To get rid of the 'lingering' process, simply terminate it from your service using 'TerminateProcess()'. In order to do this, you'll have to store the process handle (returned in the PROCESS_INFO struct filled in by 'CreateProcess()' and enable the SE_DEBUG_NAME privilege for your service. e.g.

   HANDLE           hToken;
   TOKEN_PRIVILEGES tp;

   if   (   !OpenProcessToken   (   GetCurrentProcess   (),
                                    TOKEN_ADJUST_PRIVILEGES,
                                    &hToken
                                )
        )   return  (   FALSE);


   tp.PrivilegeCount    =   1;
   
   LookupPrivilegeValue (   NULL,
                            SE_DEBUG_NAME,
                            &tp.Privileges  [   0].Luid
                        );

   tp.Privileges    [   0].Attributes   =       bEnable
                                            ?   SE_PRIVILEGE_ENABLED
                                            :   0;

   AdjustTokenPrivileges    (   hToken,
                                FALSE,
                                &tp,
                                sizeof  (   tp),
                                NULL,
                                NULL
                            );


0
 

Author Comment

by:jernhung
ID: 1201348
Jkr,
  Erm.. that's kinda difficult, because I don't want to explicitly terminate my process when it's running. I want to restart it only when it fails or stops for any reason, but because it lingers, I have no way to tell if it's still active or if something bad has happened to it.
  I need to know therefore if it's possible for the process to terminate itself normally and yet not linger in memory.

Here the code I used to create my process, is there any special parameters that I need to set?

startProcess()
{
        int status = 0;
      STARTUPINFO stateInfo;
        PROCESS_INFORMATION processInfo;

      stateInfo.cb=sizeof(STARTUPINFO);
      stateInfo.lpReserved=NULL;
      stateInfo.lpDesktop=NULL;
         stateInfo.lpTitle=NULL;
      stateInfo.dwFlags=STARTF_FORCEONFEEDBACK;
      stateInfo.cbReserved2=0;
      stateInfo.lpReserved2=NULL;

        // Path name of process to run
      char* fullpath = "Executable.EXE";
      while (status == 0 && _pause == 0)
      {
            // try to start process every 3 mins. if start failed and service not paused or stopped
            status = CreateProcess(
(fullpath,NULL,NULL,NULL,FALSE,DETACHED_PROCESS,NULL,NULL,&stateInfo,&processinfo);
            if (status == 0)
                  _stream<<"Startup Failed"<<endl;
            Sleep(3000 * 60);      
      }
}

0
 
LVL 86

Expert Comment

by:jkr
ID: 1201349
>>I need to know therefore if it's possible for the process
>>to terminate itself normally and yet not linger in
>>memory.

Hmm, if the process crashes, it should be removed anyway...

My idea would be to have the child process send sth.' like a 'ping' to the service in order to let the service know that it is still alive...
0
Industry Leaders: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

 
LVL 2

Accepted Solution

by:
zbang earned 140 total points
ID: 1201350
The common technique for detecting dead or lingering processes is setting up a callback function to the parent process and calling that function every minute for example. If there is no callback activity from the child process for about twice the interval between the callbacks (2 minutes) the parent process should assume the child is dead and terminate it.

To address your problem with the console application: In order to relieve yourself from the headache in creating consoles for console applications, use ShellExecute() instead of CreateProcess. Basically, services are not allowed to interact with the desktop.

Another word about lingering processes - make your service run as a user which you have full control of (for example, YOUR user, or any user but the system account when you are the Administrator)
That way you will be able to terminate the process from Task Manager.

0
 
LVL 86

Expert Comment

by:jkr
ID: 1201351
>>The common technique for detecting dead or lingering
>>processes is setting up a callback function to the parent
>>process and calling that function every minute for
>>example

zbang - err, isn't that what I suggested? Please read the whole thread before throwing in answers that are already being discussed...
0
 
LVL 86

Expert Comment

by:jkr
ID: 1201352
?????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????
0
 

Author Comment

by:jernhung
ID: 1201353
jkr
  Apologies for awarding the points to someone else. I know zbang did repeat what you have said before, but to be fair, he/she also pointed out some other useful advice in addition to that.
  Nevertheless thanks for your help. If I could split the points amongst the 2 of you, I would.


0

Featured Post

On Demand Webinar: Networking for the Cloud Era

Did you know SD-WANs can improve network connectivity? Check out this webinar to learn how an SD-WAN simplified, one-click tool can help you migrate and manage data in the cloud.

Question has a verified solution.

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

Article by: SunnyDark
This article's goal is to present you with an easy to use XML wrapper for C++ and also present some interesting techniques that you might use with MS C++. The reason I built this class is to ease the pain of using XML files with C++, since there is…
Templates For Beginners Or How To Encourage The Compiler To Work For You Introduction This tutorial is targeted at the reader who is, perhaps, familiar with the basics of C++ but would prefer a little slower introduction to the more ad…
The viewer will learn how to use the return statement in functions in C++. The video will also teach the user how to pass data to a function and have the function return data back for further processing.
The viewer will be introduced to the technique of using vectors in C++. The video will cover how to define a vector, store values in the vector and retrieve data from the values stored in the vector.

688 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