Linux Study Notes

Source: Internet
Author: User

A few days ago, I talked to my colleagues about multi-core and multi-task tasks. I lamented the trouble of synchronizing multiple threads when writing a program. I was worried that the program I wrote would have a problem. I heard that the multi-task was invented by Bill Gates. Then, what if there is no multitasking in the world?

 

One CPU can only do one thing at a time. The so-called multi-task mechanism is actually a false time-sharing mechanism. One of my colleagues wrote a single-chip microcomputer operating system similar to UCOS and used a timer.

 

Interrupt to Implement Task Switching and message-driven. What is the difference between this approach and multitasking in modern operating systems?

 

Linux defines processes with four elements: a piece of code that can be executed, an independent user space, a system stack, and task_struct.

 

If there is no independent user space, it is called a thread; if there is no user space, it is called a kernel thread.

 

You can understand these four elements from how to implement multi-task in Linux. Each process has its own 4G virtual address space. User processes do not interfere with each other. That is, user process a cannot access user process B's

 

User process B cannot access the space of user process. This is the difference between a modern operating system and a simple operating system. In the system of UCOS and my colleagues, the machine that implements process switching

 

Similar to the modern operating system, the difference is that in a simple operating system, there is no independence between processes, that is, the part of storage management. This is also the ability of modern operating systems to be divided

 

The operating system layer, application layer, and the reason why there are so many applications on the application layer. From this perspective, no matter how open a simple operating system is, it is still closed. This feature makes it impossible to develop.

 

.

 

In Linux, how does one implement mutual independence between processes? It is through its storage management mechanism and process management mechanism. A piece of executable code, an independent user space, a system stack space and

 

A task_struct structure. The system stack space and task_struct must be modified in system mode, while the independent user space and executable code are implicitly

 

The thought is: the user process can modify the content of the space allocated by itself, and cannot modify the content of the space not allocated by itself, thus ensuring the independence between processes.

 

We can imagine the process's behavior:

 

1. The birth of a process. In the system mode, a system stack and task_struct are allocated to a process;

 

2. Import an executable code segment, allocate global and static variables in the user space according to the content in the code segment, and allocate the stack space;

 

3. Execute the code segment. When the required data is operated on the stack, adjust the stack pointer and MMU maps the virtual address to the physical address.

 

4. Execute the code segment. When the required data is operated in the heap, first request to allocate the heap, enter the kernel mode, allocate the corresponding virtual address space, and add the virtual storage range list in task_struct.

 

To create a ing relationship. Then, access these virtual addresses for address ing.

 

How does the kernel manage processes?

 

There are two parts: Process Creation and process scheduling and switching.

 

The process has two parts in the system space. The task_struct structure and system stack space occupy 8 K physical space and two consecutive page tables. Task_struct is the process ID, and the kernel uses

 

Task_struct is used to manage processes. There are four queues in task_struct:

 

1. Linear queue, used to traverse the entire task_struct list;

2. tree-like queue; in Linux, processes must be created by processes, so that the parent-child relationship and sibling relationship can be established. The tree-like queue can be used to associate all task_struct;

3. Hash table; each process has an ID called PID. The kernel uses the hash list method for faster process search based on PID;

4. An executable queue. Five statuses are available for a process: Ready, paused, dead, light sleep, and deep sleep. The status of the process being executed is also ready.

 

How does the kernel schedule processes? There are two concepts: Kernel threads and the system space of processes. For example, the task scheduling thread is the kernel thread, and its task is based on the current ready state.

 

The priority of the process also has some parameters, calculate the weight, and decide to schedule the process. How does it obtain information about a ready process? Use the task_struct structure;

 

There are two methods:

1. Store the task_struct address of the current process in a certain location and read the address as needed.

2. When you need to obtain the task_struct address of the current process, calculate it!

 

Linux uses the second method, because the system stack and task_struct are in two consecutive 4 K physical pages, and task_struct is located at a low address, there is a dedicated register called

 

ESP, which points to the system stack of the current process and obtains the task_struct address of the current process through calculation.

 

Process Scheduling: During process scheduling, you must switch the context. The context includes:

 

1. General Register group;

2. segment register group: CS, DS, es, SS, FS;

3. Instruction address register: EIP;

4. Special register: eflags;

5. The control register () records the address of the page number table. During task switching, You need to import the address of the page number table of the process to this register.

 

Some information is recorded in the system Stack:

1. When you enter the system mode through a system call, you need to enter the system stack;

2. During the interruption, the system stack is required;

 

 

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.