Solved

Call a process into the background

Posted on 1998-04-18
18
257 Views
Last Modified: 2012-05-04
I have a C program in which I need to call a standard process ("c:\dos\chkdsk.exe", for instance), and let it run in the background. I cannot have the program wait for its completion, but it has to execute it, then go on with the code.

Later on I want to be able to check whether the process has terminated. How could i do that?

Please answer both questions...
0
Comment
Question by:MaDdUCK
  • 9
  • 8
18 Comments
 
LVL 8

Author Comment

by:MaDdUCK
ID: 1249473
Edited text of question
0
 
LVL 4

Expert Comment

by:piano_boxer
ID: 1249474
For what platform? (DOS/Win95/NT)
0
 
LVL 11

Expert Comment

by:alexo
ID: 1249475
For Win32 you can use the CreateProcess() function.  The handle of the process will be put into the PROCESS_INFORMATION structure.  Later you can call WaitForSingleObject() on this handle (specify timeout of zero if you don't want to wait, just to check if the process is still running).
0
ScreenConnect 6.0 Free Trial

Discover new time-saving features in one game-changing release, ScreenConnect 6.0, based on partner feedback. New features include a redesigned UI, app configurations and chat acknowledgement to improve customer engagement!

 
LVL 8

Author Comment

by:MaDdUCK
ID: 1249476
Could you give me an example?
0
 
LVL 11

Expert Comment

by:alexo
ID: 1249477
Sure.

////////////////////////////////////////////////////////////////////////////////

#define STRICT
#define WIN32_LEAN_AND_MEAN
#include <windows.h>

void main()
{
    STARTUPINFO si = { sizeof(STARTUPINFO) };
    PROCESS_INFORMATION pi;
    BOOL rc;

    rc = CreateProcess("c:\\dos\\chkdsk.exe", NULL,
                       NULL, NULL, FALSE,
                       CREATE_NEW_CONSOLE /* or DETACHED_PROCESS */,
                       NULL, NULL,
                       &si, &pi);

    if (!rc)
    {
        MessageBox(NULL, "Cannot start program", "", MB_OK);
        return;
    }

    Sleep(10000);
    if (WaitForSingleObject(pi.hProcess, 0) == WAIT_OBJECT_0)
        MessageBox(NULL, "Process ended", "", MB_OK);
    else
        MessageBox(NULL, "Process running", "", MB_OK);
}

////////////////////////////////////////////////////////////////////////////////

0
 
LVL 8

Author Comment

by:MaDdUCK
ID: 1249478
I see you can do this in Win32, but is there any way of doing this in DOS too? or UNIX?
0
 
LVL 8

Author Comment

by:MaDdUCK
ID: 1249479
Sorry, alexo
I did not mean to reopen the question.
Please lock it again.
0
 
LVL 11

Accepted Solution

by:
alexo earned 50 total points
ID: 1249480
No problem.  Mad ducks are known to be unpredictable.

DOS:
--------
There is no concept of "background process" as it is a single tasking operating system (and I mean OS in the broadest possible sense).
You can create a TSR (a program that stays resident in memory after it terminates) but you'll have a problem switching the CPU between it and the actual running process.  The TSR can catch the timer interrupt, etc. but this is beyond occult.

Unix:
-------
You can use the fork() function to create another process.
You better ask in a Unix forum.
0
 
LVL 8

Author Comment

by:MaDdUCK
ID: 1249481
give me a day or two, (or three, or four) to get back to this.

0
 
LVL 11

Expert Comment

by:alexo
ID: 1249482
What exactly are you trying to do?
0
 
LVL 8

Author Comment

by:MaDdUCK
ID: 1249483
I am trying to write a CGI that
(1) runs a packer like pkzip to compress a local directory based on the input and outs it into a public FTP directory.
(2) Right after execution displays the CGI's output with different parameters, including a self-reload every 5 secs.
(3) Every time it self reloads, it checks for the packer to be finished, in which case it forwards the user to the address of the archive for a download.
(4) Displays a final page
(5) Instructs the computer to delete the file in 3 hrs.

Complicated but I want to do it and I know it's possible.

I want the program to run in the background so that the browser does not timeout if the CGI has to compress 100 Mb...
0
 
LVL 11

Expert Comment

by:alexo
ID: 1249484
On what platform?

0
 
LVL 8

Author Comment

by:MaDdUCK
ID: 1249485
Primarily Windows, but I'll try to do one for UNIX too.
0
 
LVL 11

Expert Comment

by:alexo
ID: 1249486
Well, you have the Windows solution.
On Unix you can use the spawnl() function that spawns a (possybly asynchronous) process.  I don't know how to check its running status though.  Ask in the Unix area.
0
 
LVL 8

Author Comment

by:MaDdUCK
ID: 1249487
Thank you. Could you also tell me, or better: give me an example of how I can display something like a splash screen in win32 with a 'close' button? I don't want to use C++ because I want the exe not to use any libraries and to be as small as possible.
It should just pop up the splash screen (internally defined...not by reading a bmp) and wait for the close button to be clicked (or for a mouseClick event anywhere), then disappear.

thank you.
0
 
LVL 11

Expert Comment

by:alexo
ID: 1249488
In order to use a splash screen you have to create a dialog which consists of a picture and an "OK" button.  Use a resource editor for that.  The dialog is invoked using the DialogBox() function, one of whose arguments is a pointer to a procedure that will proccess the dialog messages (in your case, dismissing a dialog when "OK" is pushed).

Another option is to display a textual message with one of 4 predefined icons by a simple call to MessageBox().
0
 
LVL 8

Author Comment

by:MaDdUCK
ID: 1249489
What annoys me with DialogBox() is that it has a window title bar and I don't want that...
I just want a bare screen like the splash of word 97 that will go away if the user clicks the mouse (without a close button)
0
 
LVL 11

Expert Comment

by:alexo
ID: 1249490
If you use a dialog editor just uncheck the "titlebar" checkbox and it won't have one.
0

Featured Post

Efficient way to get backups off site to Azure

This user guide provides instructions on how to deploy and configure both a StoneFly Scale Out NAS Enterprise Cloud Drive virtual machine and Veeam Cloud Connect in the Microsoft Azure Cloud.

Question has a verified solution.

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

Suggested Solutions

Have you thought about creating an iPhone application (app), but didn't even know where to get started? Here's how: ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ Important pre-programming comments: I’ve never tri…
Preface I don't like visual development tools that are supposed to write a program for me. Even if it is Xcode and I can use Interface Builder. Yes, it is a perfect tool and has helped me a lot, mainly, in the beginning, when my programs were small…
The goal of this video is to provide viewers with basic examples to understand recursion in the C programming language.
The goal of this video is to provide viewers with basic examples to understand how to create, access, and change arrays in the C programming language.

828 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