Differences between threads and processes

Source: Internet
Author: User

Process

1. An application running in the systemProgram;

2. A main thread is automatically started after a process is started;

3. A process can contain multiple threads.

 

Thread

1. It refers to the basic unit in which the operating system executes multi-task scheduling on threads and the system allocates processor time resources,

Or a unit that is executed independently within a process. For the operating system, the scheduling unit is a thread.

2. A process includes at least one thread, which is usually called as the main thread.

Coexistence of multiple threads in applications is a basic feature and an important symbol in modern operating systems. Readers who have used UNIX operating systems know the process. In UNIX operating systems, each application executes a process sign in the operating system kernel, the operating system schedules the application execution and allocates system resources based on the allocation flag. What is the difference between processes and threads?

Processes and threads are the basic units for running programs that the operating system understands. The system uses this basic unit to realize the system's concurrency for applications. The difference between a process and a thread is:

The division scale of threads is smaller than that of processes, making multi-threaded programs more concurrent.

In addition, the process has independent memory units during execution, and multiple threads share the memory, which greatly improves the program running efficiency.

The execution process of a thread is different from that of a process. Each Independent thread has a program running entry, sequence execution sequence, and program exit. But the thread cannot be executed independently. It must exist in the application and the application provides multiple thread execution control.

Logically, multithreading means that multiple execution parts in an application can be executed simultaneously. However, the operating system does not view multiple threads as multiple independent applications to implement process scheduling, management, and resource allocation. This is an important difference between processes and threads.

A process is a concept initially defined to indicate the basic execution unit of an application in a memory environment in a multi-user, multi-task operating system environment such as UNIX. Taking a UNIX operating system as an example, a process is a basic component in a UNIX operating system environment and a basic unit for system resource allocation. Almost all user management and resource allocation tasks completed in UNIX are implemented through the control of application processes by the operating system.

The source programs written in C, C ++, Java, and other languages are compiled into executable files by the corresponding compiler and submitted to the computer processor for running. An application in an executable state is called a process. From the user's perspective, a process is an execution process of an application. From the core perspective of the operating system, processes represent the basic units of the memory allocated by the operating system, CPU time slice, and other resources, and are the runtime environment provided for running programs.The difference between a process and an application is that an application is stored as a static file in a hard disk or other storage space of a computer system, A process is a system resource management entity maintained by the operating system under dynamic conditions.The main features of application processes in a multitasking environment include:

● The process has an initial entry point for memory units during execution, and it always has an independent memory address space during process survival;

● The lifetime status of a process includes creation, readiness, running, blocking, and death;

● The Running commands sent to the CPU during application processes are in different forms,Process states can be divided into user and core states.. In the user State, processes execute application commands, and in the core State, application processes execute operating system commands.

During UNIX startup, the system automatically creates Swapper, init, and other system processes to manage memory resources and schedule user processes. In a Unix environment, no matter the process created by the operating system or executed by the application, it has a unique process identifier (PID ).

 

Differences between processes and Java threads

The application has an initial entry point address of the memory space during executionCodeThe execution sequence and the memory exit point address used to identify the process end. Each time point during the process execution has a unique processor command corresponding to the memory unit address.

The thread defined in Java also includes a memory entry point address, an exit point address, and a code sequence that can be executed sequentially. However, the main difference between a process and a thread is that the thread cannot be executed independently and must run in an active application process, therefore, it can be defined that the thread is a concurrent sequential code stream within the program.

The UNIX and Microsoft Windows operating systems support multi-user and multi-process concurrent execution, while the Java language supports concurrent execution of multiple execution threads within the application process.Multithreading means that multiple logical units of an application can be executed concurrently.However, multithreading does not mean that multiple user processes are being executed, and the operating system does not allocate independent system resources to each thread as an independent process. A process can create its child processes. The child process and the parent process have different executable code and data memory space. In the process used to represent the application, multiple threads share the data memory space, but each thread must have an independent execution stack and context ).

Based on the preceding differences, a thread can also be called a light weight process (lwp ). Different threads allow task collaboration and data exchange, making computer system resource consumption very cheap.

Threads must be supported by the operating system. Not all types of computers support multi-threaded applications. The Java programming language combines thread support with the language runtime environment to provide multi-task concurrent execution capabilities. This is like a person in the Process of housework, put the clothes in the washing machine, automatically wash, put the rice in the rice cooker, and then start cooking. When the food is ready, the food is cooked and the clothes are washed.

Note that using multithreading in applications does not increase the CPU data processing capability. Only when Java programs are divided into multiple concurrent execution threads on a multi-CPU computer or under the network computing architecture, multiple threads can be started to run simultaneously, only by running different threads in Java virtual machines based on different processors can the execution efficiency of applications be improved.

A process is a running program and a unit for the operating system to allocate resources.

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.