Advertisement

09.22.2007 at 05:47AM PDT, ID: 22846015
[x]
Attachment Details
[x]
The Solution Rating System

With so many solutions, how can you tell which solutions are most likely to help you and which ones are not? To provide you with a tool to use, we rate our solutions based on various elements that most accurately determine if a solution is a quality solution. To explain what factors affect the solution rating, here are the elements we take into consideration when formulating our solution rating.

  • The Grade of the Solution
  • The Zone Rank of the Expert Providing the Solution
  • The Number of Author and Expert Comments
  • The Number of Experts Contributing
  • The Feedback of the Community

Your Input Matters
Because of the way the system is set up, the most important variable in this equation is you. As a member of Experts Exchange, you are able to cast your vote on the quality of the solutions in regard to how complete, accurate, helpful and easy to understand each solution is. When you provide your feedback, each rating is adjusted accordingly. So, if you see a solution that has a poor rating that you think is a good solution, let us know by rating it. As you do, the rating will be adjusted and will become more accurate for other members of our site.

If you have any suggestions that you would like to make for our rating system, please ask a question in the Suggestions Zone of Community Support.

Thank you!

7.6

Multithreading in Bash. Possible?

Asked by atlantis_ in Bourne-Again Shell (bash), Linux, Linux Programming

Tags: ,

I am trying to get a concept where I can create simultaneous running child processes from a parent and able to read their exit status as soon as they come in. Once the exit code is read it will start a new child to take its place. That way there is alway 10 children running at once.

In the end I am going to be using this for a way to do asynchronous wgets to speed up my wget scripting. So if you have an alternative, let me know. Thats also why I set a randomized timer on the example child process to simulate wgets and so I can check if it doesnt do things in order or not.

Here is the child process I am threading, just for reference:

[CODE]number=0
let "number = $RANDOM % 20"
time=$(echo "1+$number*0.1" | bc)
sleep $time
echo "I got $1 ($time)"
exit $1
[/CODE]


I first started out with this job script I found. I realized that it didnt get the exit codes fast enough. I wanted the exit code retrieved as soon as the process is exited.

[CODE]

#!/bin/bash

MAXJOBS=10

function jobidfromstring()
{
        local STRING;
        local RET;
 
        STRING=$1;
        RET="$(echo $STRING | sed 's/^[^0-9]*//' | sed 's/[^0-9].*$//')"
 
        echo $RET;
}
 
function clearToSpawn
{
    local JOBCOUNT="$(jobs -r | grep -c .)"
    if [ $JOBCOUNT -lt $MAXJOBS ] ; then
        echo 1;
        return 1;
    fi
 
    echo 0;
    return 0;
}
 
JOBLIST=""
 
for (( i=0; i<=20; i++ ));
do
        while [ `clearToSpawn` -ne 1 ] ; do
                sleep 0.5
        done  
        ./child $i &
        LASTJOB=`jobidfromstring $(jobs %%)`
       JOBLIST="$JOBLIST $LASTJOB"
done

for JOB in $JOBLIST ; do
        wait %$JOB
        echo "Job $JOB exited with status $?"
done
echo "Done."[/CODE]


Then I decided to tryout the SIGCHLD handler and see how that worked. It turned out that I wasn't able to define the handler in a way that would capture every child for some reason.

[CODE]#!/bin/bash

iNext=0

function ChildReturned() {
        wait $1
        echo "$1 was returned. Its exits status was: $?"
        iNext=$((iNext+1))
        StartChild $iNext
}


function StartChild {
        ./child $1 &
        pid=$!
        echo "Started: $1  PID= $pid"
        trap 'ChildReturned $pid' SIGCHLD
}


set -bm

for (( iNext=0; iNext<=10; iNext++ ));
do
        StartChild $iNext
done

wait
[/CODE]


Finally I tried just starting the children and looping through a while statement that would check when each one died, retrieve the exit code, and then start a new child. However I realized that a child that terminates (SIGCHLD) still exists as a process until you get the exit code with wait(). Therefore using kill -0 wasnt going to work.

[CODE]#!/bin/bash


throttle=10
iNext=0
JobIDArray[0]=""

function jobidfromstring()
{
        local STRING;
        local RET;

        STRING=$1;
        RET="$(echo $STRING | sed 's/^[^0-9]*//' | sed 's/[^0-9].*$//')"
        echo $RET;
}

function AnyAlive {
        for (( i=1; i<=throttle; i++ )); do
                if kill -0 %${JobIDArray[i]} > /dev/null 2>&1; then
                        echo 1
                        return 1
                fi
        done
        echo 0
        return 0
}

for (( iNext=1; iNext<=throttle; iNext++ )); do
        ./child $iNext &
        JobIDArray[iNext]=`jobidfromstring $(jobs %%)`
        echo "          JobIDArray[$iNext] = ${JobIDArray[iNext]}"
done


while [[ `AnyAlive` -eq 1 ]] ; do
        for (( i=1; i<=throttle; i++ )); do
                jID=${JobIDArray[i]}
                if ! kill -0 %$jID; then
                        wait %$jID
                        return=$?
                        iNext=$((iNext+1))
                        ./child $iNext &
                        JobIDArray[i]=`jobidfromstring $(jobs %%)`
                        echo "$jID was returned. Its exits status was: $return   |   JobIDArray[$i] = ${JobIDArray[i]}"
                fi
        done
done[/CODE]



---------------------------

I am pretty much stuck at where to begin next. I am very new to bash and I don't even know if its possible to do what I want. My third script would work fine if I just had a way to identify if a job was terminated or not. I know that if you try to kill -s SIGCHLD the job when its terminated, it will give "no job exists" or something. I can't seem to know how I can get that to work in an IF statement. And if I did, would wait() still get the exit code.Start Free Trial
[+][-]09.22.2007 at 03:38PM PDT, ID: 19942858

At Experts Exchange, members can ask their questions to thousands of technology professionals, also known as Experts. Experts compete and collaborate to answer those questions by leaving comments like this one.

Start your 7-day free trial to view this Expert Comment or ask the Experts your question.

 
[+][-]09.22.2007 at 07:24PM PDT, ID: 19943260

Often, when Experts are collaborating with members who have asked questions, they will request additional information about the problem. Askers respond with an author comment like this one.

Start your 7-day free trial to view this Author Comment or ask the Experts your question.

 
[+][-]09.22.2007 at 08:51PM PDT, ID: 19943404

At Experts Exchange, members can ask their questions to thousands of technology professionals, also known as Experts. Experts compete and collaborate to answer those questions by leaving comments like this one.

Start your 7-day free trial to view this Expert Comment or ask the Experts your question.

 
[+][-]09.22.2007 at 10:29PM PDT, ID: 19943561

View this solution now by starting your 7-day free trial. Setting up your free trial is quick, easy, and secure. We will return you to this solution, unlocked, when you're done.

 

About this solution

Zones: Bourne-Again Shell (bash), Linux, Linux Programming
Tags: bash, multithreading
Sign Up Now!
Solution Provided By: duncan_roe
Participating Experts: 2
Solution Grade: A
 
 
[+][-]09.22.2007 at 10:47PM PDT, ID: 19943597

Often, when Experts are collaborating with members who have asked questions, they will request additional information about the problem. Askers respond with an author comment like this one.

Start your 7-day free trial to view this Author Comment or ask the Experts your question.

 
[+][-]09.22.2007 at 11:10PM PDT, ID: 19943623

At Experts Exchange, members can ask their questions to thousands of technology professionals, also known as Experts. Experts compete and collaborate to answer those questions by leaving comments like this one.

Start your 7-day free trial to view this Expert Comment or ask the Experts your question.

 
[+][-]09.22.2007 at 11:17PM PDT, ID: 19943637

At Experts Exchange, members can ask their questions to thousands of technology professionals, also known as Experts. Experts compete and collaborate to answer those questions by leaving comments like this one.

Start your 7-day free trial to view this Expert Comment or ask the Experts your question.

 
[+][-]09.24.2007 at 06:06AM PDT, ID: 19948065

At Experts Exchange, members can ask their questions to thousands of technology professionals, also known as Experts. Experts compete and collaborate to answer those questions by leaving comments like this one.

Start your 7-day free trial to view this Expert Comment or ask the Experts your question.

 
 
Loading Advertisement...
20080716-EE-VQP-32 / EE_QW_1_20070628