I am trying to understand parallel processing in shell scripting and sequentially appending values deterministically (no random order) in the output through a simple example. Below is the code snippet:
x=""
appendnum() {
num=$1; x=`echo $x$num`
}
for no in {0..10}
do
appendnum $no &
done
wait $(jobs -rp)
echo $x
The expected output is 012345678910, but it’s resulting in a null value. I even tried it with iterating the PID to wait until it completes, but was unsuccessful. I want the main thread to wait till every parallel process completes. Appending number was just an example.
My problem statement looks like this:
considering I have 3 tasks, I want list of responses like [responseof(task1),responseof(task2),responseof(task3)]
. Count of tasks can be up to 50. Irrespective of number of tasks, my response time should be same. What is the most efficient and correct way of doing this?