2

I am using Ubuntu 18.10 and running my own program using terminal, which requires many calculations, but when I open task manager, terminal CPU usage (including running program) is never taking more than 26%. Here is very simple program, that behaves the same way. I would like to know, how can I force my program (or terminal) to use all of my CPU speed. I also tried to run multiple terminals and start program, or use threads in my code, but it appears that those 26% splits into these terminals, and run slowly. Is it terminal limit? How can I fix it and make program run faster? Thank you

#include <stdio.h>

int main(){
    int a=0;
    while(1){
        printf("%d\n",a);
    }
    return 0;
}

EDIT: using just "a" instead of "a++", to avoid overflow

victory
  • 185
  • 1
  • 2
  • 16
  • 6
    The program you show is mostly dependent on output to the terminal, which is relatively slow. I/O bound processes will have a hard time utilizing the CPU in full. Remove the `printf` call, and only do `a++` and you should see the CPU utilization skyrocket (***but*** only for the core the process is running on). – Some programmer dude Feb 01 '19 at 10:59
  • 1
    Your program has undefined behavior due to overflow of signed type. You got to correct that before trying anything else. – Sourav Ghosh Feb 01 '19 at 11:01
  • @Someprogrammerdude Thank you for reply, I just tried "program &> output.txt" to send output to file, but still not getting more thank 26%. Is it the same limitation? – victory Feb 01 '19 at 11:04
  • 2
    That's still I/O bound. If you redirect to `/dev/null` the output will be discarded and you might get higher utilization, but it's still an I/O bound process. To stop it you should not have any output (or input) at all. – Some programmer dude Feb 01 '19 at 11:05
  • 1
    “Speed up program” and “use all of CPU speed” are completely different things. Car trips are not faster when you take a long roundabout highway route that lets you race your engine instead of a much shorter route with a few stop signs. – Eric Postpischil Feb 01 '19 at 11:18
  • @Someprogrammerdude Thats a good point, I just tried to remove printf, but still getting like 25%. I have CPU with 4 cores, may it use only 1 core? How can I set up more cores to do task? – victory Feb 01 '19 at 11:18
  • @EricPostpischil that is right, but just like I can upgrade CPU in my computer to make programs run faster, I can also attach more unused CPU power to proces to make it run faster, or am I wrong? :) – victory Feb 01 '19 at 11:24
  • I guess you should uncheck "Divide CPU usage by CPU Count" checkbox to get 100% cpu in Ubuntu task manager. – ks1322 Feb 01 '19 at 11:27
  • The CPU utilization is usually divided up into two parts: For the whole CPU and per process. For the whole CPU it's for *all* cores combined. If the total for the whole CPU on a 4-core system is 25%, then you're maxing out a single core. For per process, it depends on the number of threads it have. Multiple threads can be scheduled on multiple cores. that's why a single process can show utilization of e.g. 200% if it's maxing out two cores. – Some programmer dude Feb 01 '19 at 11:31
  • @victory: CPU speed is only one factor in program performance. The limiting factors in program performance vary from program to program. Some are limited by speed of I/O to disk. Some are limited by speed of I/O to a display. Some are limited by main memory speed. Some are limited by CPU speed. Some are limited by network speed. Some are limited by cache speed. Some are limited by combinations of these. When a program is limited by I/O, neither increasing the CPU speed nor changing the program to use more CPU will increase the program speed. – Eric Postpischil Feb 01 '19 at 11:34
  • And you really need to better explain your *real* problem. Why do you need to maximize the CPU utilization? If you max out for all cores, then no other processes or threads can run, and the system will become very slow and sluggish. Are you trying to *optimize* your program? Then that's totally unrelated to CPU utilization. Optimization is first of all solved by measuring and *profiling* to find bottlenecks in your code, and then optimize one bottle-neck at a time until your programs performance is *good enough* (which often *is* good enough). – Some programmer dude Feb 01 '19 at 11:34
  • Okay I just found out only 1 core is doing task at one time, and using 100% of power. Sometimes it is core #1, or #2 etc. and it keeps changing from time to time. Can I use all cores at the same time? – victory Feb 01 '19 at 11:36
  • 2
    The “25%” statistic in a system with four processors suggests CPU use is being reported as a percentage of all available processor time, not a percentage of one processor’s time. In this case, a single-threaded program cannot use more than 25% of CPU, because it can execute on only one processor at a time. To use more CPU, you would have to write a multithreaded program that executed several things simultaneously. – Eric Postpischil Feb 01 '19 at 11:36
  • 1
    *"I also tried to run multiple terminals and start program, or use threads in my code"* - if 1 process is taking 25%, 4 processes should get to 100%, unless they are competing for a resource. – vgru Feb 01 '19 at 11:50

1 Answers1

1

Okay, I managed that my task was using 100% of one of 4 CPU cores (thats why I got ~25 CPU%, it was divided by 4 cores). Using "pthread_create" function a am able to use multiple threads, and use all CPU power for that task. I learned that I/O are limiting task speed, and I should not use all of CPU power. Thank you everybody for help and knowledge, that really helped me! Some sources that also helped me to solve problem, for future readers:

pthread_create function http://man7.org/linux/man-pages/man3/pthread_create.3.html

command for CPU usage https://www.booleanworld.com/guide-linux-top-command/

compilation Undefined reference to pthread_create in Linux

victory
  • 185
  • 1
  • 2
  • 16