5
$\begingroup$

When working on Linux what happens when a thread is created? It is a separate execution flow. Does each thread have their own code section? Personally I don't think threads would be able to share code section even if it's the same section. Say if the number of threads being created is dynamic. How is the memory allocated for each thread? Will the memory foot print, especially code section, increase as threads are created?

$\endgroup$
2
  • 3
    $\begingroup$ I think your question is legitimate in general, but overly broad, and it's hard to understand what research you did yourself so far. Pretty much any textbook on operating systems would answer your questions. Can you focus on one thing in one question and supplement it with what sources you tried to use to answer it yourself and what was unclear there? Also note that the "Related" section has a few very similar questions and may help you. $\endgroup$
    – jkff
    Dec 21, 2014 at 2:08
  • 1
    $\begingroup$ Maybe you should define more precisely your question. $\endgroup$ Dec 21, 2014 at 3:19

1 Answer 1

5
$\begingroup$

(Although you asked about Linux, which would be off-topic, the same principles apply on most operating systems, and general principles are on-topic here.)

Threads share their code section. In many systems, even separate tasks that are executing the same program can share their code section, because the code sections are read-only, so whatever one task does won't affect the other task. I don't know how to explain this better because you don't say why you don't think it would be possible. It is possible, and it's routinely done.

Each thread has its own set of register values, including the program counter. Each thread also has its own call stack. Having its own program counter and call stack (in a way, the program counter is the top of the call stack) pretty much defines a thread. Multiple threads of the same task have program counters that each point at some place in the shared code section.

Creating a thread requires allocating memory to store its stack, and to store its register values when switching between threads. It doesn't affect the code section.

$\endgroup$
1
  • $\begingroup$ It seems worth mentioning that threads with a process will typically share memory access permissions (often by sharing a page table) while separate processes typically will not. Each thread will have a separate stack and thread local storage area (comparable to global storage for a process but limited to data used by one thread), which will consume extra memory resources. Threads will increase memory use by having more values alive at a given time (for parallelism) plus some small overhead in memory allocation to avoid the allocator becoming a bottleneck. $\endgroup$
    – user4577
    Dec 22, 2014 at 12:16

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.