How does the operating system distribute the processor through the various programs?

2

In the current operating systems there is the possibility of having several programs running. It is the job of the operating system to ensure that all programs have the opportunity to perform work (processing code instructions).

Everything would be easier if there were a limit to the number of programs that the operating system is running. For example, 1 program for each core of the processor. The limit on computers nowadays would be between 4 to 8 programs on computers for more modern consumers.

But the reality is that there are a lot more programs that I can have running. Not to mention that each program can have more than one thread , a thread always runs in a processor core (although it is possible to change cores during execution, as far as I know ).

How do these programs continue to operate? Why do not they crash? How does the operating system distribute the processor between the various / threads ?

    
asked by anonymous 02.12.2016 / 13:40

1 answer

3

Introduction

Often operating systems have been able to work that way. Of course some do not, but you do not have to have multiple processors, either physical or logical, so that multiple processes or even threads can run at the same time.

There is a gain in doing this whenever there is a waiting state for some reason. Either because the hardware is manipulating data by itself, or because the application itself is in the standby state. So limiting the number of runtimes would be damaging since nothing would be done during these breaks.

It would be easier, but it would not be ideal. The difficulty was the price that was decided to pay to take better advantage of available resources. The idea is to keep the processor busy.

Placing more processors does not solve this. The reason is explained in Is it always guaranteed that a multi-threaded application runs faster than using a single thread? .

Scheduling

There is a scheduling system that is a specific algorithm of operating systems to give control of the processor to each existing processing line. The operating system lets a line run for a while and then there is an interrupt that returns the processor control to the operating system that can then decide what to do with the processor.

This is done with processor-specific instructions that create the interrupt in execution after the time set by the operating system. The interruption, is like an event that many know at a high level, only it is controlled by the hardware.

These algorithms begin with the creation of lists of execution threads that are requested through the threads process API. According to the configurations the operating system will allocate a processor for that line, being able to change it following certain criteria, and a specific time (few microseconds) so that it executes until it returns to the operating system. This time can vary according to the number of existing rows.

The OS stays in a loop and when it gets back control it delivers to another line of execution. The frequency that a line is called can also be controlled by some OSs, so it is not necessary for all existing lines to execute once to re-execute again.

All this depends on the implementation of each system and the conditions of the environment it is running.

Some use fairly sophisticated algorithms to give the best possible result for each situation.

Think of it as a traffic cop or smart light. Well, technically a basic semaphore is already a good analogy, but OSs tend to be better than this.

This is preemptive multitasking. In the past some OSs use cooperative multitasking. There was no interruption, so the OS relied on the good will of the application to return control to it. This would catch everything if the application "screwed up". With the hardware helping and the OS using this the application can not take over the processor. The interrupt configuration statement is only accessible by the OS.

There are real-time operating systems that need to give more assurance about execution.

Obviously this is a summary for laypeople.

Wikipedia article on the subject .

    
02.12.2016 / 14:04