Operation = work = system = system = of = progress = process = management = management

Process thread fiber interrupt

High-frequency interview questions: the difference between process and thread?

Spoken answer: A process is a program that runs in memory, and a thread is the execution path in the process.
Professional answer :Process is the basic unit for operating system to allocate resources, and thread is the basic unit for operating system to execute scheduling. The most important thing is that each process has an independent memory space, which is scheduled for execution by the thread.The most important difference between processes and threads is that threads share the memory space of a process and do not have their own independent memory space.

process

Linux also becomes a task, which is the basic unit of system resource allocation.
Resources include: independent address space, kernel data structure (process descriptor ...), global variables, data segment [please recite]
Insert picture description here

Process descriptor PCB

Each process has its own process descriptor PCB (Process Control Block). We just said that threads are also ordinary processes in Linux, so threads also have their own PCBs. It's just that some PCBs record the space shared with others, and some are their own independent spaces. This is the only difference.

Kernel thread (understand)

【Screenshot】

Process creation and start

【Screenshot】

System functions: Linux is written in C. It provides some interfaces to the outside world. For example, it provides a function called fork (), which can be used to start a process. andThe process cannot release its own PCB, and the caller needs to release it manually.

If you fork B from A, A calls it the parent process of B

Zombie process, orphan process

【Screenshot】

Zombie process

Features: Zombie process only accounts for PCB

The zombie process generally has no effect, because the process is all over, nothing more than a name hanged from its father, that is, PCB, in fact, all resources related to this process have been released, that is basically no Occupy any resources, the only resource he occupied in memory is the PCB, and its parent process does not release it.

Of course, the zombie process can be manually released and can be used wait() Method to release this process.

How did the zombie process occur? Under normal circumstances, its parent process may be the deamon daemon, and then the child process PCB is not released due to operational problems, resulting in the child process becoming a zombie process. The zombie process mentioned above, generally only such a piece of PCB data has not been released, and generally there will be no impact, but once there are more zombie processes, the accumulation will definitely have an impact.

Orphan process

Zombie process means that the parent process has already exited before the child process has ended. We said above that the child process cannot release its own PCB, and its father has launched it, which means that the child process has no father, and no one helps it release the PCB. How does the system handle this time? Under normal circumstances, it will be handed over to a special process (init process) To deal with, he is the father of all orphans.

Will there be any impact after the orphan process? In fact, there is no impact, ha ha ha ha ha. At most, he just changed a dad.

Process scheduling

The kernel process scheduler decides:

  • Which process should be run?
  • When does it start?
  • How long does it run?

There are also many scheduling schemes, and the execution method can also be very flexible. Taking Linux as an example, Linux can execute a specific scheduling scheme for a certain process. So there is a very important concept in Linux:Each process has its own exclusive scheduling scheme, and this scheduling scheme can still be customized.

Basic concepts of process scheduling

Process type:
  • IO-intensive : Most of the time is spent waiting for IO operations (waiting for network IO)
  • ** CPU-intensive **: Most of the time is used for bulk calculation
Process priority
  • Real-time progress (level: 0 ~ 99), the higher the number, the higher the level
  • Normal process nice value (-20 ~ 19)

The priority of the real-time process is always greater than the ordinary process, as long as the real-time process is running, the ordinary process has no chance to run

time management
  • linux adopts CPU time ratio according to priority
  • Other operating systems use time slices by priority

Multi-task scheduling strategy

  • Non-preemptive: unless the process voluntarily gives up the CPU, it will continue to run
  • Preemptive (mainstream): Forced to start or pause (preempt) the execution of a process by the process scheduler

Linux's default scheduling strategy

  • For real-time processes: use two strategies SCHED_FIFO (First In First Out) with SCHED_RR (Poll)
    The highest level is the FIFO. This process will continue to execute Linux unless it surrenders the CPU itself. Unless the higher level FIFO and RR preempt it,
    RR only has the average allocation in the same level FIFO in this thread.
  • For ordinary processes:CFS (Absolute Fairness Algorithm)
    As I said just now, the priority of real-time processes is much greater than that of ordinary processes. There are real-time processes running, and ordinary processes will always have to wait for me.

Linux process scheduling

Linux2.5 classic Unix O (1) scheduling strategy, biased towards the server, but not friendly to interaction
Linux2.6.23 uses CFS Completely Fair Scheduler

Thread

How to implement the operating system level thread, each different operating system, its thread implementation is not the same.

Implementation in Linux

Insert picture description here
In Linux, threads and processes are not much different, just share resources with other processes (Memory spaceGlobal data, etc ...) In
Linux, a new process pid can be started through the fork command, and the original memory space can be completely shared with the new process

Other operating systems

In Windows, for example, threads are Light Weight Process, called lightweight processes.

Fiber

A thread can be understood as a thread in a thread, that is, a thread in user space.

【Screenshot】

How to understand this Fiber, our JVM is running in user mode, and the operating system is running in kernel mode. How is the Thread object implemented in the JVM? It is an operating system-level thread, or LWP. A thread in the JVM space corresponds to an LWP process in the operating system space. But LWP is not equal to thread, but the JVM uses the operating system's LWP to implement the function of thread.Currently, HotSpot's threads and operating system LWP are one-to-one.
Fiber can be understood as a thread of a thread, which is an executable path in the thread, and all fibers in the thread share the LWP corresponding to the thread.

Why should there be fiber?

Because the fiber is in user space. It does not need to deal with the operating system kernel, and it does not need the operating system to start a corresponding LWP when it starts. There are not many threads that the operating system can start. In terms of the current configuration, starting from 10,000 threads has become stuck, and the performance of the CPU is spent on thread switching.The fiber switching occurs in the user space. I do n’t even need to deal with the hardware. I only need to talk to one of my JVM threads in my user space. The speed of getting up is also very fast and very lightweight. What is the number it can start, tens of thousands, or even hundreds of thousands, are possible.
The correspondence between fibers and threads in the JVM is many-to-one.

Summary of advantages

1. Running in user mode, the threads in the thread, switching and scheduling do not need to pass through the operating system OS
2. The resources occupied are very small (the operating system needs to start a thread, and the resource space to be allocated for it is about 1M) Fiber length only requires 4K space.
3. Because it is very lightweight, it is relatively simple to switch, and can start a lot (100,000+ no problems). Think about the concept of 100,000 threads. All the resources in it are spent on thread switching.

The difference between fiber and thread

The fiber runs in user space, and the JVM manages its own switching; the thread is managed by the operating system kernel and runs in the kernel space. The JVM is only responsible for communicating with the kernel, and the kernel manages and switches threads.
To be more specific, the thread call involves communication between the user mode and the kernel mode, and the switching between the thread context at the kernel level; while the fiber is only in the user mode, only the fiber context switch at the user mode level is involved. .

The concrete realization of fiber

The implementation method is basically the same as the thread. It also has its own fiber stack, including the context switching method, which can be implemented with reference to the fiber.

Currently supports languages ​​with built-in threads

  • kotlin, scala, go, python (), Java are not currently supported (There is a project in Java openJDK)

Application scenarios of fiber

Where is the fiber used?Very short computing tasks, no need to deal with the kernel. I only need to do the calculation task, and the time is very short, I am enough to complete in user space, there is no need to switch in kernel space.

Fiber vs thread pool

The main advantages of fiber are concurrency and efficiency.

Interrupt

The so-called interrupt is to interrupt the kernel and let the kernel give up the task it is currently doing to give you a response. For example, if you hit a key on the keyboard or your program wants to communicate with the network, the kernel sends you a request. Response mechanism.The interrupt is blunt, it is a signal, the kernel will respond accordingly based on your signal. Interrupts are divided into hard and soft interrupts.

Hard interrupt

Hard interrupts are interrupts given by hardware, such as keyboards, network cards, printers, etc. These hardware interrupts are called hard interrupts. Interrupt 1-keyboard, interrupt 2-network, interrupt 3-hard drive.

Soft interrupt

Soft interrupts are interrupts generated by software. When will software interrupt? That is, when I want to deal with the kernel, when I want to make any system calls, I make any system calls.Interrupt the kernel. For example, if your program wants to read data from the hard disk, only the kernel can do this IO operation, so your program must deal with the kernel and say "Hey old iron, read me a file from the hard disk!" This will produce a Soft interrupt.

The soft interrupt is special, it is the hexadecimal 80 interrupt –0x80 or 80H. All interrupts generated by software are 80 interrupts. 80 interrupt corresponds to a bunch of functions (there are probably more than 200 functions). When a soft interrupt is generated, it is saying "old iron, sorry, interrupt you, I want to call the system function, so I give you an 80 signal". From the perspective of the kernel, it means "Oh, a buddy gave me an 80 signal, it must call one of my more than 200 functions. Then you have to tell me which function you want to call, for example What you want to call is function No. 1, then I will check this table. Function No. 1 is read. Okay, then you can pass the parameters to me again. " After knowing the function and parameters, the kernel will be executed once and returned to you after the execution. This is commonly known as interrupt 80.

Interview questions asked by Ali P9

System call: int0x80 (old system, you need to call the interrupt function written in c language, pass 80 inside) or sysenter primitive (since 80 interrupt is too common, new hardware now provides native support The software level of c language has been upgraded to the current hardware level, so the efficiency is high).

  1. Call number passedaxRegisters are passed into the kernel. The call number refers to which function to call, and the kernel finds the specific called function through the function comparison table.
  2. Parameter passedbxcxdxandofInto the kernel. Are there five parameters? The longest kernel function has five parameters.
  3. The return value is passedaxreturn. The return value is generally of type int, for example, pid is of type int.
for example

Your program reads something on the network. The read () function in the JVM calls read () in the c library. The read () in the c library calls the sysenter primitive in kernel space, telling the kernel that I am in the ax register The call number has been filled in, and the parameters are read in bx, cx, dx, si, di, and then after the function is executed, the return value is put back in ax, and the last layer of feedback is fed back to the java program.

Tencent third round interview questions

1. Talk about what is thread
2. Talk about what is fiber
3. Fiber vs Thread
4. Why is it inefficient to deal with kernel mode? (The actual question is the 80 interrupt) This involves a system call process, you need to load a lot of things into the register, and then generate a call at 80 interrupt, and then get the result. Then, after directly calculating the user space and taking the result directly, it must be less efficient.

Published 40 original articles · Likes0 · Visits 382

Guess you like

Origin blog.csdn.net/weixin_43780400/article/details/105670676