English
[Interview Question] Difference between Process and Thread, Multi Thread

[Interview Question] Difference between Process and Thread, Multi Thread

Organizing interview questions about the difference between Processes and Threads

Still writing! 23. 05. 30. writing completed → 23. 06. 09. add content

Program

A set of executable instructions written for a computer to perform a specific task.

It's defined in many different ways, but this is how I understood it when I read it! The reason I checked the definition of a program first is because the definition of a process is a narrower scope program.

Process

  • 'instance' of a 'running' program (used almost interchangeably with Task)

  • An operating system-based unit of work that runs in memory.

  • Consumes a lot of time and resources to create (high overhead)

I think the unique part is that it is possible to have multiple instances of a program simultaneously, rather than the program itself.

Thread

  • A CPU-based unit of execution that performs work within a process.

  • Every processor has at least one thread

  • Threads have their own registers and stack

  • Heap memory space is shared

So what's the difference between the two?

Processes can be viewed as a larger categorical unit

However, I think this would be a very comprehensive answer if I simply left it at that, so I've added some additional clarification by referring to some well-organized articles!

How it works

1. For Process

  • The process is allocated a separate memory area by the operating system when it runs.

  • The memory area has the following format: Code/Data/Stack/Heap

  • Cannot access memory regions of other processes

2. For Threads

  • Process's Stack region is partitioned and has its own region

  • The rest of the Code, Data, and Heap areas are shared

  • Errors in one process don't spread to other processes' regions

  • But if one thread fails, all threads terminate. → because there are shared memory regions

Summary

What is the difference between Process and Thread?

  • A process is a program in dynamic use, while a thread is a unit of execution of some kind of work.

  • A process contains at least one thread

  • Processes do not typically share memory space, but threads do.

  • Processes go down alone on each failure, threads go down all within the same process


+ About Multi-Thread

Multiple threads performing tasks simultaneously within one process.

Context Switching (Context Switching)

By default, the maximum number of tasks that can be processed simultaneously is the number of cores. If more threads are running than the number of cores, each core alternately performs multiple tasks. Saving and reading the current state of the task or the data required for the next task when the thread is switched.

Advantages and disadvantages of multithreading and processes

1) Advantages of Multi-Threading
  • System calls for process creation and resource allocation are sleepy, allowing for efficient resource management.

  • Less processing cost for sending and receiving data between threads because they share memory space except for the stack within one process.

  • Reduced program response time due to less complex communication methods between threads.

2) Disadvantages of Multi-Threading
  • Increased debugging difficulty and design difficulty

  • Cannot control threads from other processes

  • Synchronization problems due to memory sharing (accessing the same resources at the same time)

  • Problems with a thread affect the entire process

3) Advantages of multiprocessing
  • If something goes wrong in a process, the impact doesn't spread outside that process
4) Disadvantages of Multi-Process
  • High overhead in context exchange

  • Inter-process communication is more difficult and complex than inter-thread communication

Note

Wikipedia's entry on processes and threads Difference between process and thread by raejoonee Difference between process and thread by heejeong Kwon

댓글 작성

게시글에 대한 의견을 남겨 주세요.

댓글 0