• C

Call a process into the background

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...
LVL 8
MaDdUCKAsked:
Who is Participating?
 
alexoConnect With a Mentor Commented:
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
 
MaDdUCKAuthor Commented:
Edited text of question
0
 
piano_boxerCommented:
For what platform? (DOS/Win95/NT)
0
Improved Protection from Phishing Attacks

WatchGuard DNSWatch reduces malware infections by detecting and blocking malicious DNS requests, improving your ability to protect employees from phishing attacks. Learn more about our newest service included in Total Security Suite today!

 
alexoCommented:
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
 
MaDdUCKAuthor Commented:
Could you give me an example?
0
 
alexoCommented:
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
 
MaDdUCKAuthor Commented:
I see you can do this in Win32, but is there any way of doing this in DOS too? or UNIX?
0
 
MaDdUCKAuthor Commented:
Sorry, alexo
I did not mean to reopen the question.
Please lock it again.
0
 
MaDdUCKAuthor Commented:
give me a day or two, (or three, or four) to get back to this.

0
 
alexoCommented:
What exactly are you trying to do?
0
 
MaDdUCKAuthor Commented:
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
 
alexoCommented:
On what platform?

0
 
MaDdUCKAuthor Commented:
Primarily Windows, but I'll try to do one for UNIX too.
0
 
alexoCommented:
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
 
MaDdUCKAuthor Commented:
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
 
alexoCommented:
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
 
MaDdUCKAuthor Commented:
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
 
alexoCommented:
If you use a dialog editor just uncheck the "titlebar" checkbox and it won't have one.
0
Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.

All Courses

From novice to tech pro — start learning today.