Reputation: 3
So I have an assignment that compares algorithms for allocating resources. I've implemented the algorithms and they work fine. My problem is simulating work being done on those resources.
Originally, I figured just sleep for a second and that will do fine. However, that doesn't actually waste CPU time. All of the resources can go to sleep at the same time and 3 seconds of simulated work would be done in 1 second...which is impossible.
How can I make it such that the thread remains active and only after spending a total of 1 second in the CPU will it continue and do other things?
Upvotes: 0
Views: 113
Reputation: 11791
The function pthread_getcpuclockid(3)
is mandated by POSIX. It's (Linux) manual page contains a short example that gives the CPU time used up by a thread. Run in a loop until you've wasted enough time, looping around doing some thumb twiddling in between checks.
Upvotes: 1