-4

My goal is to have a function that receives an int (0-100) and uses that amount of cpu usage on a thread. So if it was given 17, the cpu usage would be 17%. 54, 54%.

I've tried writing functions that add/multiply varying amounts of numbers depending on the input but they always ended up using similar amounts of cpu usage.

This is the basic idea I had but I would continue the varying amounts of multiplications based on the input.

#include <cstdlib>
#include <iostream>
using namespace std;

int main() {
  int cpu_use;
  cin >> cpu_use;

  int a, b, c, d, e, f, g, h, i, j, k, l, m, n, o, p, q, r, s, t, u, v, w, x, y,
      z, aa, ba, ca, da, ea, fa, ga, ha, ia, ja, ka, la, ma, na, oa, pa, qa, ra,
      sa, ta, ua, va, wa, xa, ya, za, ab, bb, cb, db, eb, fb, gb, hb, ib, jb,
      kb, lb, mb, nb, ob, pb, qb, rb, sb, tb, ub, vb, wb, xb, yb, zb, ac, bc,
      cc, dc, ec, fc, gc, hc, ic, jc, kc, lc, mc, nc, oc, pc, qc, rc, sc, tc,
      uc, vc;

    switch (cpu_use) {
    case 1:
      a + a;
      break;
    case 2:
      a *a *b *b;
      break;
    case 3:
      a *a *b *b *c *c;
      break;
    case 4:
      a *a *b *b *c *c *d *d;
      break;
    }
  } while (1);

  return 0;
}
Jeff P
  • 1
  • A given thread has some control over how much it hammers the CPU, but there is no way you can precisely control the thread's CPU usage to such a degree. What is the practical reason for your desire to do this? What is that supposed to accomplish? P.S.: every self-respecting C++ compiler will compile away most of the above code to nothing. – Sam Varshavchik Feb 16 '20 at 22:33
  • What's with all those variables???? Also, you seem to be missing `do {` somewhere. – interjay Feb 16 '20 at 22:33
  • @SamVarshavchik My professor wants us to do this so we can test a tool that basically mimics task manager. He showed us in class a tool he created which does what is actually intended so it is possible. – Jeff P Feb 16 '20 at 22:35
  • This could help to crank up the CPU usage https://stackoverflow.com/questions/9244481/how-to-get-100-cpu-usage-from-a-c-program – Zen Monkey Feb 16 '20 at 22:36
  • @interjay Those numbers basically are 100 different integers. I guess I did it pretty stupidly, probably could have used a vector or an array but I was just thinking for each case + 1 I would increase the multiplications with the next integer squared. This wasn't a very thoughtful piece of code, I was just puzzled because it always used around 53% of cpu usage – Jeff P Feb 16 '20 at 22:39
  • @ZenMonkey I don't need a tool to use 100% usage, I need a tool to use a specific amount based on the number given in input. [0-100]. – Jeff P Feb 16 '20 at 22:40
  • 1
    Sorry to hear that your professor doesn't really has any desire to actually teach you C++, instead of focusing on these completely useless tasks. – Sam Varshavchik Feb 16 '20 at 22:40
  • @JeffParker I understand what you're looking for. I wasn't offering a solution to getting a specific CPU level, I was showing you one way you could increase the CPU usage of a program which seems to be something you are also struggling with. I'll quote another thread on this issue: "It's really not the program's right or responsibility to demand additional resources from the system. That's the OS' job, as resource scheduler. If it is necessary to use more CPU time than the OS sees fit, you should request that from the OS using the platform-dependent API." – Zen Monkey Feb 16 '20 at 22:42
  • This thread could also be of use https://stackoverflow.com/questions/6449332/increasing-c-program-cpu-use – Zen Monkey Feb 16 '20 at 22:43
  • @SamVarshavchik I agree – Zen Monkey Feb 16 '20 at 22:44
  • @SamVarshavchik He's not my favorite professor. – Jeff P Feb 16 '20 at 22:49
  • @JeffParker - Unfortunately this question is off-topic, as it's very broad with no single objectively correct answer. You'll primarily get opinions on viability and possible approaches (which seems to be what you're getting in comments). That said: It's maybe not the best idea to badmouth your professor on a very popular software development q&a site, especially when you have your school affiliation in your profile... – David Makogon Feb 17 '20 at 22:32
  • @DavidMakogon You're right it's not a good idea to do that, and I didn't. – Jeff P Feb 18 '20 at 20:49

1 Answers1

1

There isn't a definitive way to do this without the use of external tools. One potential method could be to write a program that attempts to use as much CPU as possible, such as the ones described here - How to get 100% CPU usage from a C program. Then you could try to limit the CPU usage to a value you want. There are methods described here -C++: Limiting CPU usage intentionally.

This problem is difficult to solve and seems out of scope for someone learning C++ to be given.

Zen Monkey
  • 101
  • 6