Process -Thread & Single-threaded – Multithreaded

Tram Ho

1. Process and Thread.

  1. Procses: are “jobs” that the operating system performs.
  2. Thread is a basic unit in the CPU. One thread will share with other threads in the same process about its data and data.
  • A process may contain multiple threads within it. When we run the application, the operating system creates a process and starts running the main threads of that process.
  • The most important point to note is that a thread can do whatever task a process can do. However, because a process may contain multiple threads, each thread can be treated as a small process.
  • Another difference is that many threads in the same process use the same memory space, while the process does not . This allows threads to read and write the same type of structure and data, easily communicating between threads. Inter-process communication, also known as IPC (inter-process communication), is relatively complex because the data is more centralized.

2. Differences between single-threaded and multithreaded.

  • Most software in modern computers is multithreaded, or multithreaded. Most computer applications run a certain process along with many threads running inside.
  • Applications can also be designed to take advantage of processing capabilities on multicore systems, helping to perform multiple CPU tasks in parallel.

2.1 Multithreaded

Thread, of course, allows multithreading. An easy illustration of the superiority of multithreading is that the Word processor can print the document using one background thread, while simultaneously running another thread that receives input from the user to type a new document.

Each process has its own memory area, but threads in the same process share the same memory address. And threads also share any resources that are part of the process. This means that it is very easy to share data between threads, but it is also easy to jump from one thread to another, resulting in some poor results.

Multi-threaded programs need to be carefully programmed to avoid such a jump. The code that changes the structure of the data shared between multiple threads is called critical segments. When a critical thread is running in this thread, make sure no other threads are allowed to use this critical segment. This is the synchronization process, to avoid unexpected program stopping here. And that is also why multithreading requires very careful programming.

2.1.2 Benefits of Multithreaded: There are 4 main benefits:

  1. Responsiveness: Multithread makes interactive applications work better because even if a part of the program is blocked or takes a long time to run, the program can generally run.
  2. Resource sharing capabilities: processes can only share data through techniques such as shared memory and message sharing.
  3. Saving: providing resources and data for process creation is very expensive. And since threads automatically share the data for the process it belongs to, creating threads for context-switching will save a lot of cost. Not only the cost but also the time, because creating a new process will take much longer than creating a new thread.
  4. Scalability: The benefits of multithreaded are more evident in multiprocessor architecture, because multithread helps threads work in parallel in different processing cores, while for single-threaded processes, a The thread can only run on one processor, no matter how many threads are in the current system.

3. Summary of differences between Thread and Process.

  • Creating multiple threads is easier than processes because they don’t need separate memory addresses.
  • Multithreading needs to be programmed in detail because threads share common constructs that can only be used by each thread at a time. Unlike threads, processes do not share the same memory address.
  • Threads are ranked “lightweight” because they use fewer resources than processes.
  • The processes run independently of each other. Threads use shared memory addresses, so care must be taken to avoid jumping from one thread to another. (What was mentioned in the 2nd above mentioned)
  • A process can contain multiple threads.


Share the news now

Source : Viblo