Re: Parallelling system_exec
- Posted by Andy Serpa <ac at onehorseshy.com> Jun 28, 2004
- 416 views
First, download Bernie Ryan's "Win32 Engine" from the archive. Below is an include file I use to spawn child processes without waiting on them before continuing. To use it:
handle = child_process(<command line here>) -- the above runs the program specified by command-line -- and returns a integer handle, or 0 on error -- example: handle = child_process("c:\\curl_dir\\curl.exe -whatever whatever")
If you aren't going to use the handle for anything (sounds like you aren't), then immediately call:
child_close(handle) -- closes the handle only (releases resources), doesn't affect the child program itself
To terminate immediately a child program (abnormal termination), use:
child_kill(handle) -- handle must still be open (it will close it upon termination)
I use the second two procedures because I monitor the child processes via interprocess communication -- doesn't sound like you need that. -- Andy
------------------------------------------------------ include w32engin.ew without warning constant TRUE = 1, FALSE = 0, NULL = 0 object void global function child_process(sequence cmdline) atom piProcInfo, siStartInfo, addr, hChildProcess integer bFuncRetn addr = allocate_string(cmdline) -- command to run child program ("exwc program.exw") -- Set up members of the PROCESS_INFORMATION structure. piProcInfo = struc("PROCESS_INFORMATION") -- Set up members of the STARTUPINFO structure. siStartInfo = struc("STARTUPINFO") put(siStartInfo,"cb",sizeof("STARTUPINFO")) -- Create the child process. bFuncRetn = CreateProcessA({ NULL, -- app name (we use command line instead) addr, -- command-line NULL, -- process security attributes NULL, -- primary thread security attributes FALSE, -- handles are not inherited 0, -- creation flags NULL, -- use parent's environment NULL, -- use parent's current directory siStartInfo, -- STARTUPINFO pointer piProcInfo}) -- receives PROCESS_INFORMATION free(addr) if bFuncRetn = 0 then free_mem(siStartInfo) free_mem(piProcInfo) return 0 -- error else hChildProcess = grab(piProcInfo,"hProcess") void = CloseHandle(grab(piProcInfo,"hThread")) free_mem(siStartInfo) free_mem(piProcInfo) return hChildProcess -- return handle to process end if end function global procedure child_kill(atom hChildProcess) if hChildProcess then void = TerminateProcess({hChildProcess,0}) void = CloseHandle(hChildProcess) end if end procedure global procedure child_close(atom hChildProcess) if hChildProcess then void = CloseHandle(hChildProcess) end if end procedure --------------------------------------------------------