7

I am using bzip2 to compress a file,the process takes more that 100% cpu.Is there any way to run bzip2 with minimum CPU precentage.

1
  • 1
    Are you specifically asking how to waste time the CPU could be working by having it snoozing instead? Why?! Mar 14, 2013 at 8:25

2 Answers 2

12

Is this process interfering with other processes on your system? Why do you want to limit the CPU bzip2 uses?

You can use the nice command to change a process's priority:

$ nice -n 19 bzip2 <file>

Additionally, you can try lowering the bzip2 compression level:

$ bzip2 -1 <file>
7
  • No luck with nice.Still the process is taking more than 100% cpu.
    – dddddd
    Mar 14, 2013 at 5:16
  • 4
    Well is there anything else happening on the system? You didn't answer my question - why do you want to do this? All the nice command does is change the priority, so if there are no other CPU-intensive tasks happening on the server then yes, bzip2 will gladly take all of your CPU, and there's really nothing you can or should do about that.
    – EEAA
    Mar 14, 2013 at 5:18
  • 6
    To clarify what EEAA is saying: there's nothing wrong with it using 100% CPU. That's what you want because it's the fastest. It will always use 100% CPU when there's nothing else for the processor to do. When the processor has other things to work on, then it will balance out the tasks and share the CPU. By setting the nice value, you are telling the kernel that bzip is not as important as your other processes, so it should give the other processes CPU time before giving it to bzip. Don't worry too much about the percentages. Mar 14, 2013 at 5:23
  • Thanks @TomMarthenal. In my midnight stupor, I wouldn't have been able to come up with as concise an explanation as that. Methinks it's time to go to bed. :)
    – EEAA
    Mar 14, 2013 at 5:25
  • 2
    @dddddd - Well then don't worry about it. If you run the bzip command with nice 19, then it'll get far less priority in the kernel's scheduler than other processes do. You still need to worry about things like IO contention, though, as compression/decompression are are also fairly IO intensive.
    – EEAA
    Mar 14, 2013 at 5:27
6

Recommended: using priorities

In addition to @EEAA answer, if you want to give the maximum of low priority to the bzip process, so that when other processes are impacted a minimum, you should use ionice in addition. Note, that ionice is only useful when using the CFQ IO scheduler (the default on the Kernel, but this could have been tweak by your installation). ionice will have no effect AFAIK if you are using the deadline or noop IO scheduler.

To verify which IO scheduler you are using:

cat /sys/block/<DEVICE>/queue/scheduler

where <DEVICE> is your hard disk device, e.g. sda

Now using ionice, you want to set the lowest IO priority which is the class 3. And you can combine it with nice together:

$ nice -n 19 ionice -c3 bzip2 <file>

As Tom and EEAA explained, this is good enough. The kernel will make sure that bzip can run as fast as possible when no other process needs CPU or IO. But it will give the priority to other processes if they need it. This is the recommended choice.

Alternative: cpulimit - limits the CPU usage in pourcentage

Now you can still use a "CPU limitation" on bzip. That would not be my choice, but if you want to experiment or like it better, you should use cpulimit. However, it is not often packaged or in a default Linux installation. So you will have to install it. NixCraft has a good document how to install and use cpulimit.
The official web site is: http://cpulimit.sourceforge.net/

You must log in to answer this question.