D
D
Daniel2021-03-12 00:46:48
assembler
Daniel, 2021-03-12 00:46:48

What are threads at the OS level? Is there only 1 thread in a 1-core processor?

Threads are OS emulation. As far as I understand the processor does not know about them.
A couple of questions about threads.
How do they use Cash? They interfere with each other.
Are there implementations of Thread Emulation or Async on github? Maybe it's Unix code? But I don't know how to search there and where.
Why is it possible to get a gain in computational speed with the help of thread deparallization? It's each thread that will constantly load its snapshots into the registers, save, load. There will be more cache misses.
How many instructions are executed at a time 1, 10, 100? Before interruption? What does it depend on.

And For example, such commands are executed as an example of an instruction to read a line from stdin, why the whole OS does not stall. Like what happens at this moment of waiting, Or that the OS switches all 1000 threads there every 0.0001 seconds?
And this issue is solved with the help of asynchrony and callback. Then why isn't the entire OS asynchronous on a single thread like Node JS?

Answer the question

In order to leave comments, you need to log in

5 answer(s)
A
Armenian Radio, 2021-03-12
@gbg

Threads are switched in quantums of about 28 milliseconds or so.
Yes, if you run a lot of threads, the cache will be flushed. And you thought you were in a fairy tale? Good HPC programs don't run more threads than cores.

Why is it possible to get a gain in computational speed with the help of thread deparallization?

Because it is necessary to program carefully, not to allow context switching (not to pull the kernel for nothing, otherwise it will happen with some) and not to run more threads than there are cores in the system.
Then why isn't the entire OS asynchronous on a single thread like Node JS?

Congratulations on inventing Windows 3.1. You probably didn’t find it, but this OS would hang tightly along with the application if it hung.

S
Saboteur, 2021-03-12
@saboteur_kiev

In a multitasking system, each process has a bunch of stuff - its own memory area, its own parent, permissions, open file descriptors, and so on. Switching from process to process is quite a heavy task.
A thread or thread is, roughly speaking, the ability to run several "lightweight" processes in parallel that use the same set of descriptors, the same process ID and can use the same memory area.
Therefore, it will be faster and cheaper to calculate something in threads than in two processes.

How many instructions are executed at a time 1, 10, 100? Before interruption? What does it depend on.

Depends on the operating system kernel, or rather on the implementation of the process scheduler. Usually everything is tied to a timer interrupt, so slices of processor time are allocated - which means that a processor with a higher clock frequency will execute more instructions per slice. And then - the process scheduler can be quite smart, allocating several slices in a row to a specific process / thread, if it thinks it will be better.
But the process scheduler cannot have too complex logic, otherwise the switch itself will be too expensive.

J
jcmvbkbc, 2021-03-12
@jcmvbkbc

And For example, such commands are executed as an example of an instruction to read a line from stdin, why the whole OS does not stall. Like what happens at this moment of waiting, Or that the OS switches all 1000 threads there every 0.0001 seconds?

Only threads that are ready to run get CPU time. Threads can stop while waiting for some synchronization primitive, such as a semaphore or mutex. Such threads do not occupy the processor.
A thread reading from stdin can stop, for example, on a semaphore protecting the stdin data queue when it is empty. Some other thread must put new data on the stdin queue and open that semaphore, bringing the waiting thread into a ready state.

U
uvelichitel, 2021-03-12
@uvelichitel

In a multi-core processor and in a system with two or four stones, the instruction thread will be one or you will lose control.

1
12rbah, 2021-03-12
@12rbah

Are there implementations of Thread Emulation or Async on github? Maybe it's Unix code? But I don't know how to search there and where.

Well, you can look at the source codes of some kind of PL, I saw an article on Habré, about how a guy writes his PL, says that there is support for multithreading (I didn’t check it myself, but MB is what you will need)
article

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question