0

tl;dr : I wanna execute multiple commands in parallel (we're in 2013 after all) YET (that's where it become tricky) retrieve the exit error code of the processes.

From the following script : https://stackoverflow.com/a/11715437/3069988

I managed to implement this solution into my own batch file, my own version looked more like this :

- - - 8< - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
set /a "cmin=0"
set /a "cmax=%NUMBER_OF_PROCESSORS%-(1-%cmin%)"
set "lock=.\lock"
del "%lock%*" /f /q 1>nul 2>nul
set "lock=%lock%.%fdate%_%ftime%.cpu"

for %%i in (%vlst%) do (
    ==========

    rem Start the process on the first unlocked CPU
    set /a "pcnt=0"
    for /l %%b in (%cmin%,1,!mcpu!) do (
        if !pcnt! equ 0 (
            if not exist "%lock%.%%b" (
                set /a "pcnt+=1"
                call:tohex %%b
                rem The 'affinity' parameter BITFIELD select the CPU
                start "CPU%%b" /b /affinity !hex! /low cmd /c 9>"%lock%.%%b" !vcmd!
            ) 
        )
    ) 2>nul
:wait
    rem Wait for a CPU to get unlocked
    set /a "pcnt=0-1"
    for /l %%b in (%cmin%,1,!mcpu!) do (
        if exist "%lock%.%%b" (
            del "%lock%.%%b" /q 1>nul 2>nul
            if exist "%lock%.%%b" (
                set /a "pcnt+=1"
            )
        )
    ) 2>nul
    rem If all CPU are locked, wait again
    if !pcnt! geq !mcpu! (
        ping 1.1.1.1 /n 1 1>nul 2>nul
        goto wait
    )
    ==========
)

:final
    rem Wait for all remaining CPU to be unlocked
    for /l %%b in (%cmin%,1,!mcpu!) do (
        if exist "%lock%.%%b" (
            ping 1.1.1.1 /n 1 1>nul 2>nul
            del "%lock%.%%b" /q 1>nul 2>nul
            goto final
        )
    ) 2>nul
    rem del "%lock%*" /f /q 1>nul 2>nul


goto :eof
- - - 8< - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -

However I had lock issues.

Well, now that my script works (more or less) with the OP's solution, I have 4 processes running concurrently, and when one exit with an error code above 0, I'd like to catch that and exit my own script reflecting this error (not necessarely the same error code, a simple exit /b 1 should suffise)

Thanks for the feedback...

PS : created another thread because unable to ask OP as replying request 50+ reputations :) So let the topic flood begin :)

Community
  • 1
  • 1
Kochise
  • 504
  • 5
  • 10

1 Answers1

0

Use the trick you did for CPU locking - a file that holds the return code.

Launch your task via a another batch file that will capture the return code.

Within the "launcher" batch file do this:

@echo off
REM %1 is task number
REM launch app %2 is app name, %3 is first param, etc.
REM need to do this dynamically for number of arguments...
%2 %3

echo %ERRORLEVEL% > task%1.rc

I'm not a big batch file expert, maybe someone can provide a better script.

egur
  • 7,830
  • 2
  • 27
  • 47
  • Impossibru! There is no way to send the return code into a file, you can send STDOUT (1) or STDERR (2) but there is no channel for exit code. What had you in mind ? Any example so far ? – Kochise Dec 05 '13 at 13:16
  • Launch your task via a another batch file that will capture the return code. – egur Dec 05 '13 at 16:38
  • I've hit another wall : the Windows 7 cmd's command-line 8192 bytes limit (calling a compiler with several include paths then a linker with several object files to aggregate). Duh' And no, 640KB ain't for everybody :/ Calling a batch command-line longer than 8192 bytes is a mystery... – Kochise Dec 05 '13 at 18:11