Click to See Complete Forum and Search --> : Bash/Zsh multithreaded script problem.


H4XPK
08-03-2007, 03:32 PM
Problem with script below is (as you can see after running it) that it will "MISS" processess. I named this script as "test". Look at the results:

./test

a - 1
b - 2
c - 3
d - 4
f - 6
g - 7
i - 9
h - 8
k - 11

As you can see, where is number 5,8 and 10 !? More threads i use, less it will miss but this is absolutely no solution. Does anyone have any idea howto fix
this issue or howto make it catch numbers it "MISSED" ?

I have tried both latest Zsh/Bash, same problem. Is this bug in both shells or is my code buggy (i cant find any though).

Code:



#!/bin/sh

PROCID=1
THREADS=5

X=1

for Z in a b c d e f g h i j k l m n o p q s t ; do

function Module () {

A=$1
echo "$Z - $A" && sleep 0.5

}

if [ ! "$PROCID" -eq "$THREADS" ] ; then

Module $X &
let "PROCID++"

else

wait
PROCID=1

fi

X=$(( X+1 ))

done

wait

DrChuck
08-03-2007, 09:51 PM
There is no bug in the script, and this smells like a homework assignment. It looks to me like the script is skipping PROCID = 5,10,15, ... by design, and it was your job to explain why.

H4XPK
08-04-2007, 08:26 AM
There is no bug in the script, and this smells like a homework assignment. It looks to me like the script is skipping PROCID = 5,10,15, ... by design, and it was your job to explain why.
There is actually bug in that code. I found someone to help me from Efnet yesterday and i got this problem solved and it no longer miss processess.

Fix was actually very simple for it, it seems i have been just thinking it in too complicated way :)

Those who are interested im writing a Linux Web Vulnerability Scanner for Linux and because above issue is solved i can publish this withing some days.

More information about that tool can be found from:

http://xpkzxc.com/forum/index.php