Thursday, December 11, 2008

Parallel Program Execution using Windows Batch File

The requirement is to initiate 4 program executions parallely. The main calling program should not exit until all 4 executions are complete. This is how I did my POC:

a.bat : (Main Calling Script)

start cmd /T:A0 /k "call D:\scratch\sanbox\a1.bat"
start cmd /T:B0 /k "call D:\scratch\sanbox\a2.bat"
start cmd /T:C0 /k "call D:\scratch\sanbox\a3.bat"
start cmd /T:D0 /k "call D:\scratch\sanbox\a4.bat"

:TimePlease

ping 1.1.1.1 -n 3 -w 120000
echo Checking if everything is done
findstr /N ^$ a.out | findstr 4

if NOT %ERRORLEVEL% == 0 goto TimePlease



The 4 programs that were to be executed parallely:

a1.bat :

@echo "In A1 Execution"
ping 1.1.1.1 -n 10 -w 7000
@echo "A Done" >> a.out

a2.bat :

@echo "In A2 Execution"
ping 1.1.1.1 -n 30 -w 1000
@echo "AA Done" >> a.out

a3.bat :

@echo "In A3 Execution"
ping 1.1.1.1 -n 70 -w 3000
@echo "AAA Done" >> a.out

a4.bat :

@echo "In A4 Execution"
ping 1.1.1.1 -n 50 -w 5000
@echo "AAAA Done" >> a.out


The way it works is, when I invoke master script a.bat, it invokes 4 programs, named a1,a2,a3,a4. All these programs at the end of their execution, create an entry into common file a.out. Master script keeps polling a.out until it finds 4 lines in it.


1 comment:

chandu said...

is there any way around to avoid that ping portion used as wait and have the parent bat file sleep till the child bat files are completed and then invoke the parent bat file via some touch file creation as a signal.