Question: Processes vs Threads

Discussions on more advanced topics such as monolithic vs micro-kernels, transactional memory models, and paging vs segmentation should go here. Use this forum to expand and improve the wiki!
Post Reply
sebihepp
Member
Member
Posts: 184
Joined: Tue Aug 26, 2008 11:24 am
GitHub: https://github.com/sebihepp

Question: Processes vs Threads

Post by sebihepp »

Dear community,

I have a question about Threads and Processes from a Scheduler/Memory Management point of view during a task switch.

In my head the kernel is in the higher half of the memory and has its own stack there. Each Task is in user mode and has also its own stack.
During an Interrupt, the ISR saves all registers, etc. and switches to kernel stack.
In case of paging, each process has the kernel pages mapped to the higher half -> That means each Process needs its own paging structure, because they most likely run at the same virtual adress, but, of course, different physical adress. So every switch between a process needs to reload CR3 with its page directory. The kernel page tables can be reused of course, but the tables for each process are different.

This was what I implemented as a test long ago and it ran sucessfully with 2 processes, printing different characters on screen. I got some a's then some b's, then again some a's, etc.

Now I am thinking about threads. Threads are like processes inside a process, but they share the same memory, only have their own registers and stack.
So basically, when I am switching between threads within the same process, I only load the task conect from them, but not switching paging.
So the only difference is, if the CR3 gets reloaded or not.

Are my thoughts correct?

Best regards
Sebi
thewrongchristian
Member
Member
Posts: 424
Joined: Tue Apr 03, 2018 2:44 am

Re: Question: Processes vs Threads

Post by thewrongchristian »

sebihepp wrote:Now I am thinking about threads. Threads are like processes inside a process, but they share the same memory, only have their own registers and stack.
So basically, when I am switching between threads within the same process, I only load the task conect from them, but not switching paging.
So the only difference is, if the CR3 gets reloaded or not.

Are my thoughts correct?
In short, yes.

But it depends on the details of your process model. In a POSIX model, all threads share address space, open files, and user credentials associated with a process.

But granularity of sharing can be different. Linux clone() system call, for example, allows a whole host of attributes to be shared or private. For example, CLONE_FILES allows the "child" process to have its own file descriptors separate from the "parent", even if they share other attributes such as process id and address space. I believe this is the default level of sharing on the Symbian EPOC operating system, where each thread has its own open files.

But POSIX and Windows tend to follow the model you describe.
Post Reply