0

As far as I know, the way OS and computers in general operate is that machine code is arranged by the scheduler into a list of instructions, which are then carried out by the CPU.

Since the amount of time dedicated per process is variable, it would seem that programs should run at non-consistent "rates". Yet many programs in which this would be an issue, such as games, video players and drivers seem to run perfectly consistent. Of course, it can happen, then being especially noticeable in games, but I'd assume it'd happen much more often and for long periods of time.

The only way to avert this seems to be telling processes how much of some unit of time is spent executing its code, since I'd also imagine querying the system clock periodically would be very inefficient, and would have to happen very frequently.

Do OS provide this information to processes? If so, is there some standard in how?

  • 1
    I would guess the inconsistent "rates" are too minute to be detected by us- computers run way faster than our perception. Sometimes we do notice, when the computer "hangs" or slows down. pidstat seems to do be able to tell the process about it's cpu usage- haven't used it though. – Sush Jun 17 at 16:27
1

Since the amount of time dedicated per process is variable, it would seem that programs should run at non-consistent "rates".

They do. In modern Operating Systems each program/process is assigned a slice of the available CPU time. But most programs, especially services/daemons, do require less time than assigned, so other programs/processes can utilize more.

Yet many programs in which this would be an issue, such as games, video players and drivers seem to run perfectly consistent.

You're right: it seems that way.
But every serious player does know about the deteriorating effect of background processes/programs. The general rule is: the less background processes, the more CPU speed/capacity is available for the (foreground) game. So usually, in a "game" situation, most of the CPU processing time slices are not used by other processes; and the main consumer is the game.

The only way to avert this is to tell processes how much of some unit of time is spent executing its code

Here you are mistaken. The concept of Preemptive Multitasking avoids the necessity of cooperation of the processes with each other. What you assume is happening is called Cooperative multitasking which "is rarely used in modern larger systems".

Both are different approaches, here is a relevant part of the definition of Preemptive Multitasking:

In computing, preemption is the act of temporarily interrupting a task being carried out by a computer system, without requiring its cooperation, and with the intention of resuming the task at a later time.

This is the concept that you erroneously assumed to be invalid:

[...] since I'd also imagine querying the system clock periodically would be very inefficient, and would have to happen very frequently.

Preemptive Multitasking is exactly that:
interrupting every process at a given time induced by the timer interrupt and transferring the execution to the Scheduler (which then selects the next process to be executed).

Preemptive Multitasking is used by most modern Operating Systems like Windows, Linux and MacOS.

In-Depth information about multitasking approaches can be found here at OSDev.

  • Thanks! However, I think you may have misunderstood me at one point. When saying a program should be notified of its execution time, I was thinking of the program itself adjusting its speed to that time, for example, a video player skipping a frame when the previous frame has taken too long. – schuelermine Jun 17 at 17:38
  • 1
    To your surprise, this is essentially the same question. In a preemptive multitasking system a program gets a defined slice of time. If not everything can be finished in that slice: bad luck - a frame (or more) is dropped. I suppose that what you're looking for can be a Real-time OS. This type of OS's does guarantee (at least it tries) that a certain task gets executed in a certain amount of time. Notifying a program that the time's running out is of no use, because the tasks take the time they take. – zx485 Jun 17 at 17:44
  • Ok. How do I find out this slice of time, as a program? Are there API for doing that? – schuelermine Jun 17 at 17:49
  • 1
    I do not know of any API retrieving that value. I even don't know if it's static or dynamic. The purpose of the OS's kernel is that you don't need to/shouldn't care about it in a user program. – zx485 Jun 17 at 17:51

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.