Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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 ?

like image 798
Sharat Chandra Avatar asked Mar 01 '23 03:03

Sharat Chandra


1 Answers

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)

like image 106
asveikau Avatar answered Mar 07 '23 22:03

asveikau