2

I am creating a bash script to losslessly compress jpeg and png files, so I use two package under Ubuntu named jpegoptim and optipng. A will use the script with 2 or 4 core processors. I would like to use the full capacity of my CPU.

The problem is that optipng does not support multithreading by default (it only uses one CPU core), so I decided, I launch 2 or 4 parallel processes to compress the images faster. I already sorted the image files to 4 almost equal arrays (based on number of pixels), and now I need to run the processes parallelized.

I am trying parallelizing the processes with the & character at the end of the command, but it does not do the job parallel.

optipngout=$(optipng -$pnglevel -dir $outdir ${threaddata_1[@]} &)
optipngout=$(optipng -$pnglevel -dir $outdir ${threaddata_2[@]} &)
optipngout=$(optipng -$pnglevel -dir $outdir ${threaddata_3[@]} &)
optipngout=$(optipng -$pnglevel -dir $outdir ${threaddata_4[@]} &)

I have to catch the output of the command, so I think the problem is with the $() structure.

3
  • I don't think bash has concurrency support, but I could be mistaken. Any reason you can't use a full programming language? Commented Mar 22, 2013 at 9:47
  • 1
    The util taskset is something you might look into. Although I must say, I haven't been able to realize much benefit if any in my limited use of it. Also, the term "processor affinity" might help you in your searches. Commented Mar 22, 2013 at 9:49
  • @jedwards it is working very well with the & character. Thanks for your help! Commented Mar 22, 2013 at 11:37

2 Answers 2

3

Have a look at Gnu parallel. It allows you to do what you want with only small modifications to your existing script.

Sign up to request clarification or add additional context in comments.

Comments

2

I solved my problem with the taskset and & commands. Thanks for @jedwards

The final code looks like this:

outstr=$(taskset 0xFFFFFFFF optipng -$pnglevel -dir $outdir ${threaddata_1[@]}) &
outstr=$(taskset 0xFFFFFFFF optipng -$pnglevel -dir $outdir ${threaddata_2[@]}) &
outstr=$(taskset 0xFFFFFFFF optipng -$pnglevel -dir $outdir ${threaddata_3[@]}) &
outstr=$(taskset 0xFFFFFFFF optipng -$pnglevel -dir $outdir ${threaddata_4[@]}) &

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.