preface

Welcome to pay attention to the public number: Coder programming to obtain the latest original technical articles and related free learning materials, anytime, anywhere to learn technical knowledge!

This chapter mainly introduces the difference and connection between process and thread, which is also a question we often ask in the interview process. Hope that through this article, we can understand the relevant knowledge points ~

Questions related to:

  • 1. What is the difference between a process and a thread?
  • 2. What are the characteristics of processes and threads?
  • 3. How do processes interact?
  • 4. What is a buffer overflow?
  • 5. How do processes interact?
  • 6. How do threads interact?

As you can see from the interview questions above, they are all the same, just a different way of asking questions. As long as we can grasp the core points, we can easily handle any question the interviewer asks us!

1. Chestnut:

In our life, there are many small things about processes and threads, such as: 1. When we open a wechat software, we start a process, when we do various operations in wechat (check moments, scan...). So many operations are threads. So we can say that "process" contains "thread", and "thread" is a subset of "process".Copy the code

Source BCO:

Process is a running activity of a program on a data set in a computer. It is the basic unit of resource allocation and scheduling in the system and the basis of operating system structure. In modern thread-oriented computer architectures, processes are containers for threads. A program is a description of instructions, data and their organizational form, while a process is an entity of the program. Is a computer program about a data set on a running activity, is the system for resource allocation and scheduling of the basic unit, is the basis of the operating system structure. A program is a description of instructions, data and their organizational form, while a process is an entity of the program.

A thread is the smallest unit in which an operating system can schedule operations. It is contained within the process and is the actual operating unit within the process. A thread is a single sequential flow of control in a process, and multiple threads can be concurrent in a process, each performing a different task in parallel.

Let’s briefly summarize:

Process: an application that is running in the system; Once a program runs, it is a process; Process – the smallest unit of resource allocation.

Thread: A basic unit of the system that allocates processor time resources, or a stream of unit execution that executes independently within a process. Thread – The smallest unit of program execution.

2. Deep understanding:

2.1 Process (Thread + Memory + file/network handle)

Let’s take a closer look at the picture above:

“Memory” : What we usually think of as memory is what we see as physical memory (2G/4G/8G/16G), why is it in the process? In fact, the memory here is logical memory. Refers to the addressing space of memory. The memory of each process is independent of each other. Otherwise there will be a problem: we put the pointer value changed to point to other processes memory, through so that we can not see the other in the process of “WeChat” or “online banking” information, if so, then we WeChat chat logs or bank account information will be found by others, this is a very dangerous signal. Obviously that’s not going to happen.

“File/network handles” : these are common to all processes, and operations such as opening the same file to rob the same network port are allowed.

“Threads” : Next, we will introduce our knowledge of “threads”

2.2 Threads (Stack +PC+TLS)

2.2.1 the stack:

We usually talk about the call stack, but the heap doesn’t mean anything, the call stack is just the call stack. So what’s in our stack? We call the main function from the main thread entry over and over again, each time pushing all the arguments and return addresses onto the stack.

2.2.2 PC:

Program Counter, the operating system is really running thread by thread, and our process is just a container for it. The PC refers to the current instruction, which is stored in memory. Each thread has its own set of Pointers to its current memory. Computers are mostly stored programmatically, which means that our data and our programs are stored in the same piece of memory that contains both our data variables and our programs. So our PC pointer is pointing to our memory.

2.2.2.1 Buffer Overflow

For example, we often hear about a vulnerability: buffer overflow what does that mean? For example: We have a place to enter a user name, originally used for storing data. Then the hacker enters the data into a very long format. This length exceeds the amount of memory we allocate to the data store and goes into the portion of memory we allocate to the program. In this way, a hacker can embed the code he wants to run by writing it into the username box. Our solution is to limit the size of the user name buffer to the length of the user name.

The TLS 2.3:

Thread local Storage (thread local Storage) thread local Storage (thread local storage) The answer is yes, TLS. Can be used to store data unique to our thread. As you can see, threads are what our operating system really runs on, and processes are containers that put together things that we need, and separate things that we don’t need.

3. How do processes interact?

This is done through TCP/IP ports

We will introduce them in detail in subsequent articles.

4. How do threads interact?

Thread communication is relatively simple, there is a large block of shared memory, as long as everyone is the same pointer can see their own memory.

We will introduce them in detail in subsequent articles.

5. Summary:

A process allocates a large portion of memory, while a thread allocates only a portion of the stack. 3. A process is the smallest unit of resource allocation, and a thread is the smallest unit of program execution. 4. One thread can create and destroy another thread, and multiple threads in the same process can execute concurrently.

At the end of the article

This chapter introduces the differences and connections between processes and threads, as well as other aspects of small knowledge, which is also the content of the interview process. It involves a lot of small knowledge points, we did not expand the explanation, will be put in the future article to do further elaboration. Welcome to pay attention to the public number: Coder programming to obtain the latest original technical articles and related free learning materials, anytime, anywhere to learn technical knowledge!

Github personal directory

Gitee personal directory

Welcome to follow and Satr~