OSDev.org

The Place to Start for Operating System Developers
It is currently Fri Jun 14, 2024 9:05 am

All times are UTC - 6 hours




Post new topic Reply to topic  [ 2 posts ] 
Author Message
 Post subject: Question: Processes vs Threads
PostPosted: Wed May 22, 2024 4:16 am 
Offline
Member
Member

Joined: Tue Aug 26, 2008 11:24 am
Posts: 162
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


Top
 Profile  
 
 Post subject: Re: Question: Processes vs Threads
PostPosted: Wed May 22, 2024 7:01 am 
Offline
Member
Member

Joined: Tue Apr 03, 2018 2:44 am
Posts: 406
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.


Top
 Profile  
 
Display posts from previous:  Sort by  
Post new topic Reply to topic  [ 2 posts ] 

All times are UTC - 6 hours


Who is online

Users browsing this forum: No registered users and 10 guests


You cannot post new topics in this forum
You cannot reply to topics in this forum
You cannot edit your posts in this forum
You cannot delete your posts in this forum
You cannot post attachments in this forum

Search for:
Jump to:  
Powered by phpBB © 2000, 2002, 2005, 2007 phpBB Group