user236682
user236682

Reputation: 3

How can I make it such that a pthread wastes cpu time for a total of 1 second in C?

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

Answers (1)

vonbrand
vonbrand

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

Related Questions