Operating System-Process Management and Interruption

The following is explained for java

1. Process thread fiber

Insert picture description here

The difference between process and thread

A process is the state in which a program is running, and a thread is a different execution path in a process.
Process is the basic unit of OS allocation of resources, thread is the basic unit of execution scheduling. The most important thing in allocating resources is: independent memory space, thread scheduling execution (threads share the memory space of the process, and there is no independent memory space of their own)

Implementation of Thread in Linux

"Linux Kernel Design and Implementation" Third Edition, page 28. A
thread is an ordinary process in Linux, but it shares resources (memory space, global data, etc.) with other processes.

Other systems have their own so-called LWP implementation Light Weight Process (lightweight process)

Fiber

Insert picture description here
Namely: thread in user space

Why fiber is needed:
Java starts a thread, and at the operating system level, it starts an LWP. This is a heavyweight thread. Because the java startup thread needs to apply for resources from the operating system and interact with the operating system kernel, system calls are required.
The fiber is the thread in the thread, corresponding to the blue box at the top of the figure, in the user space, there is no need to apply to the operating system.
The fiber is inside the thread, which is very lightweight and can be switched quickly in the thread. JVM manages by itself, implements scheduling by itself, switches by itself, and has nothing to do with the operating system.

Fiber advantage:

  1. Occupies few resources OS: Thread: 1M vs Fiber: 4K
  2. Switching is relatively simple
  3. Start a lot of 10W+

Fiber application scenarios:
very short computing tasks, no need to deal with the kernel, high concurrency

2. Process

Linux, also known as task, is the basic unit of system allocation of resources.
Resources include: independent address space, kernel data structure (process descriptor...), global variables, data segment...
Linux process descriptor: PCB (Process Control Block), Process management for Linux (thread has its own PCB)

Zombie process

ps -ef |grep defunct (defunct means useless zombie process)

After the parent process spawns the child process, it will maintain the PCB structure of the child process. After the child process exits, it will be released by the parent process. If the parent process is not released, then the child process will become a zombie process (defunct)

Orphan process

Before the end of the child process, if the parent process has exited, an orphan process will be generated. The orphan process will become a child process of the Init process and will be maintained by process 1 (thread 1457 in graphical Linux)

Scheduling strategy

The early Linux 2.5 kernel used the Unix O(1) scheduling strategy, which gave the program a fixed time slice

Linux kernel 2.6.23 adopts the CFS scheduling strategy: Completely Fair Scheduler
allocates the proportion of time slices according to priority, and records the execution time of each process. If there is a process whose execution time is less than the proportion it should allocate, it will be executed first. (For example, some programs will give up to other programs if they don't do anything at the beginning, and when they need to be done at the beginning, they will be executed first)

Default scheduling strategy:
real-time process> ordinary process.
Real-time process priority is divided into high and low-FIFO (First In First Out), when the priority is the same-RR polling (Round Robin)
ordinary process: CFS

Among them, the highest level is FIFO. This kind of process will always execute it unless it
surrenders the CPU, unless more advanced FIFO and RR preempt it. RR is just an even distribution in the same level of FIFO in this kind of process --- -For example, there are two processes with a priority of 90 in the FIFO. At this time, they are executed according to the RR.
Only the real-time process voluntarily yields, or after the execution is completed, the ordinary process has the opportunity to run.

3. Interrupt

Hard interrupt

A mechanism
Insert picture description here
for communication between hardware and operating system. Execution process:
keyboard-----interrupt controller (notify cpu that an interrupt signal is coming)--------cpu (find the handler at a fixed location in the memory)- ------Execute the program (check the interrupt vector table, give the interrupt signal to the kernel)-------kernel (find the program according to the interrupt signal)----------Interrupt handler (already A bunch of processing programs have been written, some for keyboards and printers) ----- first processed by the OS kernel (first half)-------------------------- -------Processed by the app again (second half)

Soft interrupt (80 interrupt)

The special symbols of the interrupt vector table.
System call: int 0x80 (INT is an assembly instruction for x86 processors) or sysenter primitives (current cpu directly supports at the hardware level, assembly code)

Fill in the call number through the ax register (for example, 1 represents the exit function, 2 represents the fork function)
parameters are passed to the kernel through bx cx dx si di The
return value is returned through ax

The hello world code implemented by the system call:

;以下例子主要调用了内核空间的write函数 
;write(int fd, const void *buffer, size_t nbytes) 
;fd文件描述符,buffer要写的内容,nbytes内容的长度

.section .data ;数据段声明
msg:
        .ascii "Hello world!\n"
.section .text ;代码段声明
.globl _start ;指定入口函数
_start: ;在屏幕上显示一个字符串
        movl $4, %eax ;系统调用sys_write
        movl $1, %ebx ;文件描述符1,std_out
        movl $msg, %ecx
        movl $13, %edx
        int $0x80
        movl $1, %eax ;系统调用sys_exit
        movl $0, %ebx
        int $0x80

The system call is implemented through int 0x80, the eax register is the function number called, and the ebx, ecx, edx, esi, etc. registers are sequentially parameters. You can see exit from /usr/include/asm/unistd.h The function number of _NR_exit is 1, and the function number of write(_NR_write) is 4. Therefore, before the first int 0x80 call, the value of the eax register is 4, ebx is the file descriptor, the file descriptor of stdout is 1, and the ecx is buffer. Memory address, edx is the buffer length. Before the second int0x80, eax is 1 to call exit, and ebx is 0 to pass in parameter 0.

Note: eax is 32-bit, ax is 16-bit

Examples in
java java read network-jvm read()-c library read() -> kernel space -> system_call() (system call handler) -> sys_read()

Guess you like

Origin blog.csdn.net/qq_33873431/article/details/109008979