Categories
multithreading operating-system process

What is the difference between a process and a thread?

1918

What is the technical difference between a process and a thread?

I get the feeling a word like ‘process’ is overused and there are also hardware and software threads. How about light-weight processes in languages like Erlang? Is there a definitive reason to use one term over the other?

3

  • 2

    Related: stackoverflow.com/questions/32294367/…

    – zxq9

    Aug 16, 2017 at 14:24

  • 10

    It probably warrants saying that each OS has a different idea of what is a ‘thread’ or ‘process’. Some mainstream OS’ don’t have a concept of ‘thread’, there are also some embedded OS’ that only have ‘threads’.

    – Neil

    Aug 22, 2018 at 16:39

  • TLDR: Sibling “threads” (in most operating systems) share the same virtual address space, the same sockets and open files, all the same resources. “Processes,” on the other hand are isolated/protected from one another, and they share nothing except when they explicitly request to share some specific thing. In an OS that has both “processes” and “threads,” a process often can be thought of as a container for one or more threads and, for all of the resources that they share.

    Dec 2, 2021 at 21:03


1684

Both processes and threads are independent sequences of execution. The typical difference is that threads (of the same process) run in a shared memory space, while processes run in separate memory spaces.

I’m not sure what “hardware” vs “software” threads you might be referring to. Threads are an operating environment feature, rather than a CPU feature (though the CPU typically has operations that make threads efficient).

Erlang uses the term “process” because it does not expose a shared-memory multiprogramming model. Calling them “threads” would imply that they have shared memory.

13

  • 88

    Hardware threads are probably referring to multiple thread contexts within a core (e.g. HyperThreading, SMT, Sun’s Niagara/Rock). This means duplicated register files,extra bits carried around with the instruction through the pipelines,and more complex bypassing/forwarding logic,among other things.

    – Matt J

    Mar 6, 2009 at 6:10

  • 6

    @greg, one doubt I have in threads. let me consider I have a process A, which got some space in RAM. If the process A creates a thread, the thread also need some space to execute. So will it increase size of the space which is created for process A, or space for thread created somewhere else ? so what is that virtual space process creates ? Please correct me if my question is wrong. Thanks

    – duslabo

    Sep 20, 2012 at 15:45

  • 11

    @JeshwanthKumarNK: Creating a new thread allocates at least enough memory for a new stack. This memory is allocated by the OS in process A.

    Sep 20, 2012 at 19:20

  • 52

    This answer seems wrong. If both processes and threads were independent sequences of execution, then a process that contained two threads would have to have three sequences of execution, and that can’t be right. Only a thread is a sequence of execution — a process is a container that can hold one or more sequences of execution.

    May 28, 2016 at 0:37

  • 17

    “Hardware threads” are threads that are given individual hardware resources (a separate core, processor, or hyperthread). “Software threads” are threads that have to compete for the same processing power.

    – jpmc26

    Jan 19, 2017 at 19:02

940

Process
Each process provides the resources needed to execute a program. A process has a virtual address space, executable code, open handles to system objects, a security context, a unique process identifier, environment variables, a priority class, minimum and maximum working set sizes, and at least one thread of execution. Each process is started with a single thread, often called the primary thread, but can create additional threads from any of its threads.

Thread
A thread is an entity within a process that can be scheduled for execution. All threads of a process share its virtual address space and system resources. In addition, each thread maintains exception handlers, a scheduling priority, thread local storage, a unique thread identifier, and a set of structures the system will use to save the thread context until it is scheduled. The thread context includes the thread’s set of machine registers, the kernel stack, a thread environment block, and a user stack in the address space of the thread’s process. Threads can also have their own security context, which can be used for impersonating clients.


This information was found on Microsoft Docs here: About Processes and Threads

Microsoft Windows supports preemptive multitasking, which creates the effect of simultaneous execution of multiple threads from multiple processes. On a multiprocessor computer, the system can simultaneously execute as many threads as there are processors on the computer.

8

  • 23

    For people who want to know why cant you format a floppy at the same time : stackoverflow.com/questions/20708707/…

    Dec 20, 2013 at 17:34


  • 10

    @LuisVasconcellos – If there were no threads, then the process wouldn’t do anything. The process would only be some code and program state loaded into memory. It’s not much use. It’d be like having a road with no vehicles travelling along it.

    Mar 31, 2015 at 12:59


  • 19

    This answer is way better than the accepted answer because it talks about the ideal of processes and threads: They should be separate things with separate concerns. The fact is, most operating systems have history that goes back farther than the invention of threads, and consequently, in most operating systems, those concerns are still somewhat entangled, even if they are slowly improving over time.

    Mar 23, 2016 at 13:50


  • 2

    with the utmost respect sir this answer is a reference to those who already know, and does not help those who don’t know. it reads much like a wikipedia entry.

    – BenKoshy

    Jul 5, 2016 at 22:55

  • 7

    @BKSpurgeon With every explanation one gives, you have to take your reader from one level of understanding to the next level. Unfortunately, I can’t tailor the answer to every reader and so have to assume a level of knowledge. For those who don’t know, they can make further searches of terms I use they don’t understand, can’t they, until they reach a base point they do understand. I was going to suggest you offer your own answer, but am happy to see you already have.

    Jul 6, 2016 at 20:48

337

Process:

  • An executing instance of a program is called a process.
  • Some operating systems use the term ‘task‘ to refer to a program that is being executed.
  • A process is always stored in the main memory also termed as the primary memory or random access memory.
  • Therefore, a process is termed as an active entity. It disappears if the machine is rebooted.
  • Several process may be associated with a same program.
  • On a multiprocessor system, multiple processes can be executed in parallel.
  • On a uni-processor system, though true parallelism is not achieved, a process scheduling algorithm is applied and the processor is scheduled to execute each process one at a time yielding an illusion of concurrency.
  • Example: Executing multiple instances of the ‘Calculator’ program. Each of the instances are termed as a process.

Thread:

  • A thread is a subset of the process.
  • It is termed as a ‘lightweight process’, since it is similar to a real process but executes within the context of a process and shares the same resources allotted to the process by the kernel.
  • Usually, a process has only one thread of control – one set of machine instructions executing at a time.
  • A process may also be made up of multiple threads of execution that execute instructions concurrently.
  • Multiple threads of control can exploit the true parallelism possible on multiprocessor systems.
  • On a uni-processor system, a thread scheduling algorithm is applied and the processor is scheduled to run each thread one at a time.
  • All the threads running within a process share the same address space, file descriptors, stack and other process related attributes.
  • Since the threads of a process share the same memory, synchronizing the access to the shared data within the process gains unprecedented importance.

I borrowed the above info from the Knowledge Quest! blog.

7

  • 106

    Kumar: From my knowledge, threads do not share the same stack. Otherwise it wouldn’t be possible to run different code on each of them.

    Apr 23, 2013 at 23:51

  • 34

    Yup I think @MihaiNeacsu is right. Threads share “code, data and files” and have their own “registers and stack”. Slide from my OS course: i.imgur.com/Iq1Qprv.png

    – Shehaaz

    Oct 24, 2013 at 17:22

  • This is quite useful, as it expands on what threads and processes are and how they relate to each other. I’d suggest adding an example of a Thread, especially since there’s one for a Process. Good stuff!

    – Smithers

    Jan 20, 2015 at 18:02

  • 2

    Kquest.co.cc links are dead.

    Apr 27, 2015 at 12:50

  • 3

    @Rndp13 The problem is just the use of the word “stack” rather than “stacks”. Threads do share stacks since the stack is just a portion of virtual memory and threads share all virtual memory. Threads can even stash their stack pointers and the execution can be resumed by another thread with no issues. That one thread happens to be executing one stack at one particular time doesn’t mean threads don’t share stacks just like the fact that one thread is operating on a file descriptor at one time doesn’t mean threads don’t share file descriptors.

    Feb 17, 2019 at 19:40