process in the operating system


The operating system is the most important software in the computer. Common operating systems include Windows, Linux, iOS, Android, etc.

First, the basic positioning of the operating system

basic skills:

  1. Manage various hardware devices in your computer
  2. Provide a stable operating environment for various software resources on the computer

The location of the operating system in the computer is as follows:

insert image description here

For example, write a simple Java code System.out.println("hello world");

This code is written in the reference program.

Inside the println method, an API provided by the operating system is called. For Linux, this API is a function called write provided by the C language.

By calling the write function by the operating system, the logic will enter the kernel for execution.

The operating system kernel will find the driver of the display and transmit the data to be printed.

Finally, driven by the graphics card, operate the display, and print the data hello world to be printed on the

Second, the process in the operating system

进程It is a very important software resource managed by the operating system. We can view the process through the task manager

insert image description here

In the process column, various programs running on the computer and programs running in the background are displayed. When we click an executable program again, we will find that there will be a new record in the process.

Therefore, 进程实际上就是运行起来的程序, can also be considered 进程是一个可执行文件跑起来之后的动态过程. 在操作系统的内部,就是根据进程来分配资源of.

2.1 Process management

In the Linux operating system, a structure is used to describe the process, which is called PCB(Process Control Block) and contains various information of the process. Then by 双向链表organizing the PCBs (here just for a single thread in a process).

When an application is opened and a new process is run, a new PCB will be created and added to the doubly linked list. After closing a program, the operating system will find the corresponding PCB and delete it. All processes are displayed to the task manager, which is actually the result of traversing the doubly linked list.

2.2 Process attributes

Fields included in the PCB

  1. PID: ID
  2. 内存指针: Inside the operating system, a process is the basic unit for resource allocation by the operating system. As long as the process is run, a certain memory space will be allocated to the process. The memory pointer is describing the range of memory used by the process, describing where is the code segment and where is the data segment
  3. 文件描述符表: Describes which files are opened by the process. The file descriptor table can be regarded as an array of structures, each structure describes the information of the corresponding file, and the subscript of the array becomes the "file descriptor". When a system starts, three files, standard input, standard output, and standard error, are opened by default.
  4. 进程状态
  • Running state: when a process is running
  • Ready state: the process has the conditions to run, waiting for the system to allocate memory to run
  • Sleep state: also called blocking state, which means that the process does not have a running condition
  1. 优先级: When scheduling processes, the time and sequence for each process can be different
  2. 上下文: When the operating system executes a process, if the process needs to be scheduled away from the CPU, it needs to save the running scene of the CPU, including the value in the register and so on. When the process is scheduled next time, you can Continue to execute from the last saved position. Similar to the read archive operation.
  3. 记账信息: Counts the execution time of each process and the number of instructions, and balances the effect of scheduling according to this

Process status, priority, context, and accounting information 调度are attributes related to processes. So what is scheduling. The number of processes running at the same time on a system is much more than the number of CPU cores, which causes these processes to compete for CPU resources. At this time, process scheduling is required. According to a certain strategy, CPU resources are dynamically allocated to Processes in the ready state allow them to be executed in an orderly manner.

When will it cause process scheduling?

  1. A process has finished running or encountered an abnormal condition. At this time, it is necessary to execute the process of the new ready state to avoid the waste of CPU resources.
  2. A running process is blocked and goes to sleep
  3. In preemptive scheduling, a higher priority process is created
  4. In preemptive scheduling, a high-priority process is awakened by blocking
  5. The time slice has been used up in the time-sharing system

2.3 Parallelism and Concurrency

并行: There are two cores in the CPU, and the instructions of two processes are being executed at the same time. The two processes are running at the same time and do not interfere with each other.

并发: There is only one core, and the instructions of multiple processes are executed "simultaneously". In fact, they are only executed at the same time on a macro level. On a micro level, only one instruction is being executed by the CPU at the same time. multiple processes.

For example, the AirDesk made by Mr. He at station B can realize the function of charging multiple devices at the same time. The coil under the table will sense the device that needs to be charged, and then charge it. There is only one coil. When there are multiple devices that need to be charged on the table, it is only necessary to quickly switch the position of the coil among these devices. As long as the switching is fast enough, the macroscopically feels that the devices are being charged at the same time.

2.4 Process independence

In the operating system, it is necessary to provide a stable operating environment for software, and each process needs to maintain independence. If a process fails, it will not affect other processes, and it will not affect the entire system.

Just like the OJ system, the code submitted by the user needs to be a process and maintain a certain degree of independence. Otherwise, if any dangerous code is written, the whole system may be hung up, so that no one can use the system.

Therefore, the operating system will allocate an independent "virtual address space" to each process to ensure that the memory accessed by each process has no common area and cannot affect each other.

2.5 Interprocess Communication

There is independence between processes, but sometimes multiple processes need to cooperate with each other to complete some work. The operating system provides some "inter-process communication" mechanisms to allow processes to communicate and interact in limited circumstances. This mechanism provides some special areas for multiple processes to access at the same time based on the requirements of process independence. Such as shared memory, message mechanism, global variables, etc.

Finish!

Guess you like

Origin blog.csdn.net/weixin_46103589/article/details/123996844