Sharat Chandra
Sharat Chandra

Reputation: 125

How to implement thread library?

Is writing a code for implementing the thread library a part of kernel code ? Is the function implementation of pthread_create() et al a part of kernel ?

Upvotes: 4

Views: 889

Answers (2)

asveikau
asveikau

Reputation: 40226

In Linux, pthread_create() et al. is implemented as part of the glibc project. It uses the (non-portable, Linux-specific) syscall clone(). (Linux's fork() is also implemented in terms of clone()). Some of the BSDs also have similar syscall called rfork().

My understanding is that clone() or rfork() will both create a new process, but you can specify a flag that says, "use copy-on-write semantics to give this a different address space". So, if you want fork(), you specify that flag, but if you want to create a thread, you don't, and you end up with a shared address space.

(edited to provide more detail)

Upvotes: 5

Martin v. Löwis
Martin v. Löwis

Reputation: 127447

Threads are sometimes implemented purely in user space (then also called "green threads"), but typically in kernel space. The wikipedia article explains it nicely.

Upvotes: 3

Related Questions