dustin2022
dustin2022

Reputation: 294

Do all threads really run the same program at the same time?

I'm studying system programming and following the book "The Linux programming interface".

In section 29.9 and 29.10, the author said that

In a multithreaded application, all threads must be running the same program (although perhaps in different functions). In a multiprocess application, different processes can run different programs.

and

In a multithreaded process, multiple threads are concurrently executing the same program.

So what does it mean by saying the same program in this context? After reading this statement, it makes me feel like, all threads are doing the same mission at the same time !?

Upvotes: 0

Views: 1269

Answers (2)

Stephen C
Stephen C

Reputation: 718886

So what does it mean by saying the same program in this context?

It means the same executable code running in the same address space. All threads in a process share the processes address space. This means that they share:

  • the executable code,
  • the global variables,
  • the heap,
  • any memory segment shared with the file system or other processes.

That is basically the executing program.

(The threads even share all thread stacks; i.e. one thread is not prevented (by the OS) from looking at another thread's stack. However, I can't think of any good reason for one thread to look at another thread's stack.)

After reading this statement, it makes me feel like, all threads are doing the same mission at the same time !?

Well ... ummm ... yes and no.

They could literally be doing exactly the same thing, but they probably aren't. It possible that they are doing similar computations on different parts of a large problem. Or processing different requests. But if they had unrelated "missions" it probably wouldn't make sense for them to be part of the same program and/or process.

The way that I think about it is that threads and processes are both ways of doing more than one thing at a time.

  • Processes can't share objects, variables, etcetera. (Unless you use shared memory segments.) They typically don't trust each other or cooperate with each other.

  • Threads can (and typically do) share share objects, variables, etcetera. They need to trust each other and cooperate with each other.

Upvotes: 3

Wyck
Wyck

Reputation: 11740

In an operating system:

  • a thread is the unit of execution,
  • a process is the unit of isolation.

The program here, represents all the executable code that is loaded into memory and mapped into a process's virtual address space. Each thread can be executing a different part of that program, and maintains its own set of CPU register values and has its own stack -- although even its stack is technically stored in memory that all threads in the same process can access. But a thread does not have access to the code loaded into a different process, nor can it access memory from another process or use handles to any files or kernel objects (without explicitly sharing such things first by making a special request of the operating system to share them). Therefore the author is claiming that all threads in a process are executing the same program -- but they are not necessarily each on the exact same instruction of that program. Each thread maintains its own instruction pointer, which is just a virtual address of the memory location at which the next bytes of machine code to be executed are stored. One thread could be currently at instruction #3 of a program and another could be at instruction #103 of that same program -- which could be code in completely different functions.

Allow me to use an extended metaphor

Each thread is like a baker, preparing some food creation by following along step-by-step on a particular recipe. The process is like the kitchen. Since all the bakers are in the same kitchen, they have to share ovens, bowls, utensils, etc. And they actually have easy access to the entire kitchen, including things other bakers are using. A misbehaving baker could potentially put an ingredient into a bowl another baker was using. Or modify the temperature setting on the common oven. Bakers in the same kitchen have to coordinate access to shared resources and be careful not to accidentally interfere with another baker's work.

Bakers in different kitchens are isolated from each other. If in one kitchen they're making seafood, but someone has a life-threatening seafood allergy, then it would be best to have their food prepared in a completely different kitchen, where there are no seafood ingredients in the refrigerator. This is really important if you can't trust the other bakers. This is how we isolate bakers from each other, with different kitchens (different processes). A fire in one kitchen (like a program crash) won't affect any baking going on in other kitchens.

Think of the program like anything in the kitchen with instructions printed on it. It's the collection of all the recipe books, all the manuals for all the appliances and even the emergency procedures written on the signs on the walls of the kitchen. The bakers can only follow instructions on literature that is actually in the kitchen. Although they can follow instructions on any page of any book or manual in the kitchen.

Different bakers can be following instructions on different pages of the same book, or completely different books, or can even be currently following the exact same instruction of the same recipe. But they cannot suddenly start following instructions in a book that is not in the kitchen (a different program). Like, for example, if there are no recipe books with recipes for seafood in the kitchen, then the bakers won't be able to follow those instructions and won't be able to make any seafood. Bakers can only bake recipes that are in the kitchen with them.

So imagine a kitchen that has only a book of Chef Pierre's French Cuisine Recipes. If that's the only book in the kitchen, then that's all the bakers can do is bake French Cuisine Recipes because there are simply no other recipes in the kitchen to be followed. All the bakers are using the same book of recipes (running the same program). But one baker may be just finishing preparing the Ratatouille while two other bakers are both putting the finishing touches on a soufflé recipe from the book. Meanwhile, down the street, there may be another kitchen outfitted with a completely different set of recipes -- our seafood restaurant from before, perhaps. The two kitchens would be utilizing different books of recipes (running different programs). There could conceivably be another kitchen somewhere where they also have a copy of Chef Pierre's book. In that case you would have two different kitchens capable of preparing the same recipes (two different processes running the same program.)

So:

  • Single-threaded = one baker.
  • Multi-threaded = more than one baker in the same kitchen.
  • Single-process - one kitchen (any number of bakers).
  • Multi-process = more than one kitchen (each with any number of bakers)

Upvotes: 2

Related Questions