; PARENT.KIX ======================================================
Break On
; Will spawn off 10 child processes. Each child will take two arguments. The first arg will be
; the child's ID, the second represents something to do..
; The child will perform a task, write the answer to a temporary INI file, then rename the ini
; file before exiting (simulating lots of work being done by the child, writing much data to the ini file)
; The parent will monitor the folder for the INI files, read them when they appear, and
; consolidate the data. The INI files will be deleted by the parent process when complete.
$ = SetOption('NoVarsInStrings', 'On')
Del '.\Summary.txt' ; start with a fresh "central database" for logging the results
If Not IsDeclared($Arg)
"You must supply a file containing a list of hosts"+@CRLF
" Usage: kix32 "+@SCRIPTNAME+" $$Arg=file (in same directory)"+@CRLF
Exit 0
EndIf
DIM $list[0]
DIM $ctr, $ctr2
$ctr = 0
IF Open(3, ".\" + $Arg) = 0
Dim $x
$x = ReadLine(3)
WHILE @ERROR = 0
redim preserve $list[$ctr]
$list[$ctr] = $x
$ctr = $ctr + 1
$x = ReadLine(3)
LOOP
Else
"Cannot find file " + $Arg + @CRLF
ENDIF
;$Data = ' AaAbAcAdAeBaBbBcBdBeCaCbCcCdCeDaDbDcDdDe'
$Count = 1 ; count of tasks to process
$Workers = 0 ; count of active workers
$ctr2 = $ctr + 1
For Each $Entry in $list
? $Entry + @CRLF
Next
For Each $Entry in $list
While $Count < $ctr2 ; max of 15
; limit number of child processes
If $Workers < 10
; fewer than 10 workers - start one!
; just a silly command here to pass the count and two characters from the data string
; in real world, A1 would be an ID (as it is here) and A2 might be the name of the remote system
; that the child should interrogate
;? "Line read: [" + $Entry + "]"
$Command = '%comspec% /C START /SEPARATE kix32.exe .\child.kix $A1=' + $Count + ' $A2="' + $Entry + '"'
Run $Command ; run asynchronously
$Workers = $Workers + 1 ; increment the worker count
$Count = $Count + 1 ; increment the process count
EndIF
; Process completed work, allowing more children to run
; any INI files found?
$File = Dir('*.ini')
While $File <> "" and @ERROR = 0
; Yes!
$Workers = $Workers - 1 ; worker completed, reduce the active count
; write the data from the child's INI file to the "central database"
$ = RedirectOutput('.\summary.txt')
$File ', '
ReadProfileString('.\' + $File, 'Common', 'Arg') ?
$ = RedirectOutput('')
; remove the worker file
Del '.\' + $File
; check for more as long as we're here..
$File = Dir() ; retrieve next file
Loop
; display status
' Count: ' $Count ', Workers: ' $Workers ' ' Chr(13)
Loop
; no more children to run, need to process remaining files that are waiting for completion
While $Workers > 0
$File = Dir('*.ini')
While $File <> "" and @ERROR = 0
; found a file - that means a worker completed it's task
$Workers = $Workers - 1
; update the "central database"
$ = RedirectOutput('.\summary.txt')
$File ', '
ReadProfileString('.\' + $File, 'Common', 'Arg') ?
$ = RedirectOutput('')
; delete the worker file
Del '.\' + $File
; Status display
' Count: ' $Count ', Workers: ' $Workers ' ' Chr(13)
Sleep 0.2
; any more files ready?
$File = Dir() ; retrieve next file
Loop
Loop
Next
?
' Displaying results:' ?
Display 'Summary.txt' ?
quit 0