how similar are threads to processes?

Viewed 85

I am learning about OS and here is what I get so far :

  • a process is a program in execution
  • a thread is the "unit" of execution of a process

a cool story I think my smol brain understands that part

  • a thread is useful for "multiplying" tasks to do and this is mostly used when a task can "block" and we want other tasks to proceed

so here is my question:

  • do threads run at the same time or is there a priority on how they are executed? for example how operating systems decide which process is to be "Active" at a time. do threads have similar behavior and if so is the order of threads active decided by the os or the process itself?

The next question is :

  • do threads handle one task at a time or multiple task at a time or just a single task in general?
1 Answers

The big difference between processes and threads is that processes have their own memory, threads share the memory of their process.

Threads can make sense when you have separate activities you want to work independently, yet have some sharing. It's a trade-off, using threads is more complex and more error-prone due to the memory-sharing.

The OS decides which threads run when. Since usually there are more threads than CPUs, the OS has to schedule threads so that everybody has a chance to run. Historically there have been two kinds of multitasking, cooperative (where threads yield the cpu voluntarily) and preemptive (where the scheduler assigns time slices and switches threads out), preemptive seems to be the favored kind now.

Threads do have priorities assigned. Those vary by platform. Relying on prioritization too much can result in priority inversion bugs (where a high priority task needs a low priority task to execute before it can proceed).

Threads do what you program them to do. That can be one task or a combination of things.

Related