How to achieve Parellelism for the function which has multiple cp commands

Hi,

Here is my requirement.

Currently, there is a function which gets called in a for loop 2 times.

doCopy() {

cp  /src/a*.jar  /target/
cp /src/b*.jar /target/
cp /src/c*.jar /target

}

Since it is called sequentially from the for loop, I was asked to make parellel copy to speed up.

I've done some thing like

for i in 1, 2 do:
  doCopy &
  PIDARR=&!
done
waitForAllCopiesFinish

In waitForAllCopiesFinish, I've a logic to wait till the PIDS in array are completed running and then I display "Completed"

But, my logic is not working. Whenever doCopy & is called, PID returns for some process called "bash" and there is some "cp" process parellely runs but I never see that PID returning. Not sure, how I get the PID for the "cp" process out of many "cp"s running in the system already.

Appreciate if you help me in this regard.

Thanks

Hi.

In your example, you seem to be copying the same files twice.

When you run a just in the background, you can, with proper job control access the job using %1, %2, etc.

i.e.

$ sleep 10 &
[1]	15536
$ kill %1
[1] + Terminated               sleep 10 &

Or

$ sleep 1000 &
[1]	15561
$ wait %1
(yawn)

Or

$ sleep 10 &
[1]	15696
$ fg %1
sleep 10 

etc.

So just

cp  /src/a*.jar  /target &
cp /src/b*.jar /target &
cp /src/c*.jar /target &

wait %1 %2 %3

might work for you.

Hi,

Thanks for the quick response. Here is one correction to my post.

doCopy() function takes an argument which will be a target directory, i.e. copying the same files to different target each time when it is called.

So, Can you suggest me now?

Thanks in advance

For example:

doCopy() {
  cp  /src/a*.jar "$1"
  cp /src/b*.jar "$1"
  cp /src/c*.jar "$1"
}

for i in 1 2; do
  doCopy /target_$i &
done

wait

Hi Scott

Thank you. I will try this out and get back to you.

Regards

---------- Post updated at 08:42 PM ---------- Previous update was at 07:51 PM ----------

Hi Scott

Can I ask you one question?

I know that &! returns the last PID of the process that runs in the back ground, but with this, there are some issues like it may return some other PID which has just after your bkground job and before &!.

So. Do you think "wait %1 %2" always wait for the jobs that I run just in the background? or any chances of waiting for some other PIDs.

Thanks again,

&! means nothing to me.

$! returns the PID of the last background job.

It's unlikely that %1 will return, or wait for, some PID other than that of the job to which it relates.

The "jobs" command returns the list of all background jobs. See "man" for your shell. Usefully it returns nothing when all the jobs have finished.

Thanks Scott,

Its a typo.. I was asking about $!. However, Thanks for all your clarifications.