bug-bash
[Top][All Lists]
Advanced

[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]

Re: how to enable thread


From: Steven W. Orr
Subject: Re: how to enable thread
Date: Thu, 04 Aug 2011 11:20:19 -0400
User-agent: Mozilla/5.0 (Windows; U; Windows NT 6.1; en-US; rv:1.9.2.18) Gecko/20110616 Thunderbird/3.1.11

On 8/4/2011 9:51 AM, lina wrote:
Actually I got a bash script which contains several process (jobs)

The waiting time is so long, I mean, run one by one,

I can submit one by one separately  I can see the cores were used 100%
one by one,
but it's better use some bash script so I can easily modify in large amounts.

How can I enable thread in bash script,

like the one they did in 'make' file. I tried to understand but it's so hard,

Thanks for any further suggestions,

There is no thread interface in bash. You can only create processes. But, there are some tricks you can do to better leverage your resources. Here's an example:

waitpids=()
for machine in $list_of_machine
do
    {
    lets do a bunch of stuff here on $machine
    } &
    waitpids+=( $! $machine )
done

# Now just wait for all the machines to finish what they were doing:
sleeptime=0
def_sleeptime=2
while (( address@hidden > 0 ))
do
    sleep $sleeptime
    for (( kk=0; kk < address@hidden; kk+=2 ))
    do
        sleeptime=$def_sleeptime
        pid=${waitpids[kk]}
        machine=${waitpids[kk+1]}
        # Check each pid to see if it still exists
        # If it doesn't, then harvest the exit status
        # and remove it from the list of waitpids.
        # Don't wait for a pid that's still alive or you just hang.
        # kill returns 0 if it successfully delivered the signal.
        # Signal 0 is a trick to see if a pid is there.
        if ! kill -0 $pid 2> /dev/null
        then
            wait $pid
            stat=$?
            echo -n "=== $machine has finished "
            if (( stat != 0 ))
            then
                if (( stat > 128 ))
                then
                    echo -n "BUT TERMINATED BECAUSE OF SIGNAL $(( stat - 128 ))"
                else
                    echo -n "BUT IT DIED WITH EXIT STATUS:$stat"
                fi
            fi
            echo
            sleeptime=0
            unset waitpids[kk] waitpids[kk+1]
            # Reset waitpids or else the old indexes will return a null value.
            # The reassignment will actually shrink it.
            waitpids=("address@hidden")
            break
        fi
    done
    #If any of the pids still exist, we continue to loop
done




--
Time flies like the wind. Fruit flies like a banana. Stranger things have  .0.
happened but none stranger than this. Does your driver's license say Organ ..0
Donor?Black holes are where God divided by zero. Listen to me! We are all- 000
individuals! What if this weren't a hypothetical question?
steveo at syslang.net



reply via email to

[Prev in Thread] Current Thread [Next in Thread]