Network Security Internet Technology Development Database Servers Mobile Phone Android Software Apple Software Computer Software News IT Information

In addition to Weibo, there is also WeChat

Please pay attention

WeChat public account

Shulou

Process, thread, and context switching

2025-03-29 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Network Security >

Share

Shulou(Shulou.com)06/01 Report--

What is the process?

Narrow definition: a process is an instance of a running program (an instance of a computer program that is being executed).

Broad definition: a process is a running activity of a program with certain independent functions about a data set. It is the basic unit of dynamic execution of the operating system. In the traditional operating system, the process is not only the basic allocation unit, but also the basic execution unit.

The concept of process has two main points: first, a process is an entity. Each process has its own address space, which generally includes text areas (textregion), data areas (data region), and stacks (stack region). The text area stores the code executed by the processor; the data area stores variables and dynamically allocated memory used during process execution; and the stack area stores instructions and local variables for active procedure calls. Second, a process is an "executing program". A program is an inanimate entity, and only when the processor gives life to the program (executed by the operating system) can it become an active entity, which we call a process. [3]

Process is the most basic and important concept in the operating system. After the emergence of the multiprogramming system, in order to describe the dynamic situation in the system and describe the activity law of each program in the system, all multiprogramming operating systems are based on the process.

Features

Dynamic: the essence of a process is an execution process of a program in a multiprogramming system. The process is dynamically generated and dies out dynamically.

Concurrency: any process can be executed concurrently with other processes

Independence: the process is not only a basic unit that can run independently, but also an independent unit of system resource allocation and scheduling.

Asynchrony: due to the mutual constraints between processes, the process has the discontinuity of execution, that is, the process moves forward at an independent and unpredictable speed.

Structural features: the process consists of three parts: program, data and process control block.

Multiple different processes can contain the same program: a program forms different processes in different data sets and can get different results, but the program cannot be changed during execution.

What is a thread?

A thread, sometimes called a lightweight process (LightweightProcess,LWP), is the smallest unit of a program's execution flow. A standard thread consists of thread ID, current instruction pointer (PC), register set, and stack. In addition, a thread is an entity in a process and a basic unit that is independently dispatched and dispatched by the system. The thread itself does not own system resources, but has only a little bit of essential resources in running. However, it can share all the resources owned by the process with other threads belonging to the same process. One thread can create and undo another thread, and multiple threads in the same process can execute concurrently. Due to the mutual restriction between threads, threads are intermittent in operation. Threads also have three basic states: ready, blocked, and running. The ready state means that the thread has all the conditions to run and can logically run and wait for the processor; the running state means that the thread occupies the processor is running; the blocking state means that the thread is waiting for an event (such as a semaphore). Logically unexecutable. Every program has at least one thread, and if the program has only one thread, it is the program itself.

Characteristics

In multithreaded OS, multiple threads are usually included in a process, and each thread is the basic unit to take advantage of CPU and is the entity with the least overhead. A thread has the following properties.

1) lightweight entities

The entities in the thread basically do not own system resources, but there are only a few essential resources that can be run independently.

The entities of a thread include programs, data, and TCB. Thread is a dynamic concept, and its dynamic characteristics are described by thread control block TCB (Thread Control Block). TCB includes the following information:

(1) Thread status.

(2) on-site resources that are saved when the thread is not running.

(3) A set of execution stacks.

(4) the local variable main memory area of each thread.

(5) access to main memory and other resources in the same process.

A set of registers and stacks used to indicate a sequence of executed instructions, reserved local variables, a few state parameters, a return address, and so on.

2) the basic unit of independent dispatching and dispatching.

In multithreaded OS, threads are the basic units that can run independently, so they are also the basic units of independent scheduling and dispatching. Because threads are "light", thread switching is very fast and inexpensive (in the same process).

3) can be executed concurrently.

Multiple threads in a process can be executed concurrently, even allowing all threads in a process to execute concurrently; similarly, threads in different processes can execute concurrently, making full use of and giving full play to the ability of processors and peripherals to work in parallel.

4) share process resources.

All threads in the same process can share the resources owned by the process, first of all, all threads have the same address space (the address space of the process), which means that the thread can access each virtual address in the address space; in addition, it can also access the open files, timers, semaphore mechanisms and so on owned by the process. Because threads within the same process share memory and files, threads do not have to call the kernel to communicate with each other.

What is the relationship between processes and threads?

1. A thread can only belong to one process, and a process can have multiple threads, but at least one thread (commonly referred to as the main thread).

2. Resources are allocated to a process, and all threads of the same process share all the resources of the process.

3. In the process of execution, threads need to cooperate and synchronize. The threads of different processes should use the method of message communication to achieve synchronization.

4. The processor is allocated to the thread, that is, what is really running on the processor is the thread.

5. A thread is not only an execution unit within a process, but also a schedulable entity within a process.

Analyze the difference between the two from three angles

1. Scheduling: thread is the basic unit of scheduling and allocation, and the process is the basic unit of owning resources.

2. Concurrency: not only processes can execute concurrently, but also multiple threads of the same process can execute concurrently.

3. Owning resources: a process is an independent unit that owns resources. Threads do not own system resources, but can access resources belonging to the process.

What is a daemon?

In linux or unix operating systems, a Daemon is a special process that runs in the background, which is independent of the control terminal and periodically performs certain tasks or waits for certain events to be processed. Because in linux, every interface that the system communicates with the user is called the terminal, every process running from the terminal will be attached to this terminal, this terminal is called the control terminal of these processes, when the control terminal is closed, the corresponding process will automatically shut down. But the daemon can break through this restriction, it is separated from the terminal and runs in the background, and the purpose of leaving the terminal is to prevent the information of the process from being displayed in any terminal and the process will not be interrupted by the terminal information generated by any terminal. It runs from the time it is executed and does not exit until the entire system is shut down.

Context switching

Context switching (Context Switch), also known as PCB, is environment switching in nature.

Context switching, sometimes referred to as process switching or task switching, refers to CPU switching from one process or thread to another.

In the operating system, CPU switching to another process requires saving the state of the current process and restoring the state of another process: the current running task becomes ready (or suspended, deleted), and another selected ready task becomes the current task. Context switching includes saving the running environment of the current task and restoring the running environment in which the task will be run.

The process context is represented by the process's PCB (process control block, also known as PCB, that is, task control block), which includes the process state, the value of the CPU register, and so on.

Typically, you save the current state of CPU by performing a state save, and then perform a state recovery to start running again.

During context switching, CPU stops processing the currently running program and saves the exact location where the current program is running so that it can continue later. From this point of view, context switching is a bit like reading several books at the same time, and we need to remember the current page number of each book while switching back and forth. In a program, the "page number" information during context switching is saved in the process control block (PCB). PCB is also often referred to as "switchframe". The "page number" information is saved in CPU's memory until they are used again.

Context switching may occur in three cases: interrupt handling, multitasking, and user mode switching. In interrupt handling, other programs "interrupt" the currently running program. When CPU receives an interrupt request, it makes a context switch between the running program and the program that initiated the interrupt request. In multitasking, CPU switches back and forth between different programs, each program has a corresponding processing time slice, and CPU switches context between two time slices. For some operating systems, a context switch is also performed when switching in user mode, although this is not necessary.

Context switching is supported by the operating system or computer hardware. Some modern operating systems control context switching through the system itself, and the whole switching process does not depend on the support of hardware, which allows the operating system to save more context switching information.

Consumption of context switching

Context switching is usually computationally intensive. In other words, it requires considerable processor time, and each switch requires nanosecond time in tens of hundreds of switches per second. Therefore, context switching means consuming a lot of CPU time for the system, in fact, it is probably the most time-consuming operation in the operating system.

Performance impact:

Context switching has a negative impact on performance. Some context switches are more expensive than others; one of the more expensive context switches is cross-core context switching (Cross-Core Context Switch). A thread can run on a dedicated processor or across processors. Threads serviced by a single processor have processor associations (Processor Affinity), which is more efficient. Preemption and scheduling threads in another processor core can cause cache loss and access local memory as a result of cache loss and excessive context switching. In short, this is called "cross-core context switching".

Welcome to subscribe "Shulou Technology Information " to get latest news, interesting things and hot topics in the IT industry, and controls the hottest and latest Internet news, technology news and IT industry trends.

Views: 0

*The comments in the above article only represent the author's personal views and do not represent the views and positions of this website. If you have more insights, please feel free to contribute and share.

Share To

Network Security

Wechat

© 2024 shulou.com SLNews company. All rights reserved.

12
Report