All threads in a process share all memory segments except stack segment. Each thread has its own private stack segment.
Linux has a unique implementation of threads.To the Linux kernel, there is no concept of a thread. Linux implements all threads as standard processes.The Linux kernel does not provide any special scheduling semantics or data structures to represent threads. Instead, a thread is merely a process that shares certain resources with other processes. Each thread has a unique task_struct and appears to the kernel as a normal process threads just happen to share resources, such as an address space, with other processes.
Threads are created the same as normal tasks, with the exception that the clone() system call is passed flags corresponding to the specific resources to be shared:
clone(CLONE_VM | CLONE_FS | CLONE_FILES | CLONE_SIGHAND, 0);
The previous code results in behavior identical to a normal fork() , except that the address space, filesystem resources, file descriptors, and signal handlers are shared. In other words, the new task and its parent are what are popularly called threads. In contrast, a normal fork() can be implemented as
clone(SIGCHLD, 0);
And vfork() is implemented as
clone(CLONE_VFORK | CLONE_VM | SIGCHLD, 0);
User thread vs kernel thread:
-User threads are fast & run in user space. Kernel is not aware of user level threads. User threads are run by threads library in user space.
-Kernel threads are slow & run in kernel space. Kernel manages kernel threads by its own.
-User threads can not take advantage of multiprocessing while kernel level threads can. Means kernel threads run parallelly on different cores.
No comments:
Post a Comment