Solved

Call a process into the background

Posted on 1998-04-18
18
250 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
 
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
How your wiki can always stay up-to-date

Quip doubles as a “living” wiki and a project management tool that evolves with your organization. As you finish projects in Quip, the work remains, easily accessible to all team members, new and old.
- Increase transparency
- Onboard new hires faster
- Access from mobile/offline

 
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

Find Ransomware Secrets With All-Source Analysis

Ransomware has become a major concern for organizations; its prevalence has grown due to past successes achieved by threat actors. While each ransomware variant is different, we’ve seen some common tactics and trends used among the authors of the malware.

Join & Write a Comment

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…
This is a short and sweet, but (hopefully) to the point article. There seems to be some fundamental misunderstanding about the function prototype for the "main" function in C and C++, more specifically what type this function should return. I see so…
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.

707 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

Need Help in Real-Time?

Connect with top rated Experts

13 Experts available now in Live!

Get 1:1 Help Now