This is a requirement to incorporate parallel processing of a Unix code .
I have two pieces of unix code , one of which will act as a parent process .
This script will invoke multiple ( say four ) instances of the second script at one go ,and each of these instances will run parallely , exclusive of the status of the other process.
How will I implement this logic ? What aspects must I consider while designing this kind of relationship ?
Please guide me on this .
No , loops would make the execution sequential, because unless the process within a SINGLE loop ietration completes , the control cannot be passed back to the shell prompt for the next iteration processing to commence.
What I want is :
Start instance 1 , immediately start instance 2 and do not wait for the exit status of instance 1 .
SIMULTANEOUS PROCESSING . Not sequential .
Similar to the "tee" command , process input and write to a file at one go.
Is this a homework assignment? If not, please explain why you need to do this.
If you're going to ignore our suggestions without understanding what was said, we'll quickly decide to spend our time working with people who will pay attention to our suggestions.
Starting a process in the background (AKA starting a process asynchronously) in a loop will allow you to run x instances simultaneously if you go through the loop x times.
Does any of the wordings in my original post give you the feeling that I am seeking help for a HOMEWORK ASSIGNMENT ? It soley a need out of my profession , and if I had even the most basic idea about how to set up asynchronous processing feature (processing at the background or even where to start my search from) , I wouldnt even have taken the pain scribbing a single word for this issue here in this forum .
@rajamadhavan : Thanks for the lead , will take it here-on .
It is not possible to launch many programs SIMULTANEOUSLY from a single Terminal!
However with VERY small increments in time one can launch as many copies of similar or differing scripts and hope that the OS multi(tasking/threading) takes care of the rest.
Starter example:-
#!/bin/bash
xterm -e /full/path/to/somescript.sh &
xterm -e /full/path/to/some_other_script.sh &
# NOTE the "&" character! Google is your friend here.
x=0
while true
do
echo "Doing a loop number, $x here..."
x=$[ ( $x + 1 ) ]
done
This will launch "somescript.sh" in a separate terminal window and "some_other_script.sh" in another terminal window whilst carrying on with its own work!
There is a caveat to this method but I suspect you'll never come across it...
Why spin up another terminal? You can background the processes with this:-
counter=1
until [ $counter -ge 10 ]
do
some_script ${counter} &
((i=$i+1))
done
wait # Script will not continue until they all finish
This, of course, assumes that some_script can recognise the counter as meaning something. If you just have five distinct scripts to run, you could just:-
first_script &
second_script &
third_script &
fourth_script &
fifth_script &
wait # Script will not continue until they all finish
Why indeed, except that with these extra terminals running one can keep a watchful eye on each _child_ as it _runs_. Also you can see immediately when the script has done its all, as it will autoclose, _no_longer_to_be_seen_... ;o)
No other reason than that.
You can also give each terminal its own number that shows in its header bar so you know how each task is progressing...
@ wisecraker : I am using the method specified by millan ( for loop , and within each loop execute the command as background proces ) , and it works fine , but after all the iterations of the for loop is complete , the control does not come back to the shell prompt so that any subsequent conmmand could be executed . Please tell me how to bring back the shell prompt again at the end of the for loop.
#!/bin/bash --posix
var=""
for n in $( seq 1 2 )
do
xterm -e ~/childmultitask.sh &
done
echo "Done!"
echo "You are still in your original calling script!!!"
echo "Your background srcipts are running now so......"
read -p "Press <CR> to go back to the terminal:- " -e var
exit
This is the child process:-
#!/bin/bash --posix
x=0
while true
do
echo -n "$x "
x=$[ ( x + 1 ) ]
sleep 1
done