3

I have an Ubuntu server, which has 16 CPUs. (nproc --all show me 16)

I wrote a bash script named test.sh as below:

#!/bin/bash


while :
do
    echo xxx
done

I executed it: ./test.sh >/dev/null &.

Then I used the command top to monitor the cpu usages and I found that one cpu had been used almost 100% because of the process test.sh:

6411 me       20   0   11240   3052   2852 R  93.8  0.0   0:11.71 test.sh
%Cpu5 : 96.7 us,  3.3 sy,  0.0 ni,  0.0 id,  0.0 wa,  0.0 hi,  0.0 si,  0.0 st

As we can see, the process test.sh has been assigned on the 5th CPU, which has been used almost 100%.

Is it possible to assign a heavy process on more than one CPU so that we could make more use of CPUs? Why didn't the OS assign the process test.sh on more than one CPU? Is it because that the process test.sh is not heavy enough or should we do some configuration for the OS to do so?

Yves
  • 3,291
  • 2
    This question pretty much reads like "what is threading?" In short, a script like the one you posted won't be able to take advantage of a multicore processor. To run something on multiple cores, you first need to create threads. – Panki Jun 06 '19 at 09:32
  • @Panki So do you mean that I have to create threads or child processes to run things on multiple cores? If the process has only one process and one thread, it can not be assigned on multi cores definitely? – Yves Jun 06 '19 at 09:39
  • Yes, exactly. A single thread can't be 'split up' to multiple cores. – Panki Jun 06 '19 at 09:44
  • It's the old computer science adage "One woman can make a baby in 9 months and 9 women can make 9 babies in nine months, but 9 women can't make 1 baby in 1 month". – Philip Couling Jun 06 '19 at 09:56

2 Answers2

4

A single thread can not be split between multiple cores:

A program needs to be written to have more than one thread (one per core), or there needs to be more than one program. If not then you won't use the cores.

Writing programs to use more cores is not trivial, and not all problems can be parallelised (written to run on more than one core). If a problem contains 20% essentially sequential code, then with an infinite number of processors, it will be no faster than 20% of original execution time (500% speed increase). Then there are the overheads (communication between threads).

If you don't have any application for the cores, then you are better off selling it, and getting a cheaper machine.

Each core will have a ton of parallelism, to deal with a single thread, but this is not visible. Now we are struggling to make a single core any faster, as we add cores. This works well at first.

Unix systems (such as Gnu/Linux e.g. Ubuntu), do a good job of using extra cores, up to around 2→4. Microsoft's Windows, gets improvements when you have a core for the virus scanner, and one for the defragmenter, and one for everything else.

After that it will only make a difference if you have applications designed for multi-core.

0

Here are examples of linux commands that can use multiple processors:

  • make -j
  • gcc -pipe

The make option is particularly helpful and simple when compiling large projects with gcc.

  • Add the -fopenmp option to your build options when you call gcc.

  • Try adding the following pragma right above your for loops:

    #pragma omp parallel for
    for(i=0; i<8000000000; i++) {
        ptr[i] = i/10000;
    }
    

By default, OpenMP will create as many threads as cores in your machine and will share workload evenly between them.

Stephen Kitt
  • 434,908
RGRHON
  • 1