The article is a bit old, but still interesting.
Linux has an elegant and beautiful design when it comes to threads: threads are nothing more than processes that share a virtual address space and file descriptor table. Threads spawned by a process are additional child processes of the main "thread's" parent process. They're manipulated through the same process management system calls, eliminating the need for a separate set of thread-related system calls. It's elegant in the same way file descriptors are elegant.
Normally on Unix-like systems, processes are created with fork(). The new process gets its own address space and file descriptor table that starts as a copy of the original. (Linux uses copy-on-write to do this part efficiently.) However, this is too high level for creating threads, so Linux has a separate clone() system call. It works just like fork() except that it accepts a number of flags to adjust its behavior, primarily to share parts of the parent's execution context with the child.
It's so simple that it takes less than 15 instructions to spawn a thread with its own stack, no libraries needed, and no need to call Pthreads! In this article I'll demonstrate how to do this on x86-64. All of the code with be written in NASM syntax since, IMHO, it's by far the best (see: nasm-mode).
I've put the complete demo here if you want to see it all at once:
(Score: -1, Offtopic) by Ethanol-fueled on Sunday April 23 2017, @01:22AM (3 children)
Is Linux development racist and sexist? We need more women pthreads there.
%Black_women_allowed = 1
%Fuck_you_nigga_allowed = 2
%Exit_zero goddamn niggas = uint16_t value = *(uint16_t*)ptr;
printf("\nlVptr[60 ] is %d \n", *(int*)"Goddamn Nigga" lVptr);
(Score: 0) by Anonymous Coward on Sunday April 23 2017, @01:46AM
Linus Torvalds, is that you?
(Score: 0) by Anonymous Coward on Sunday April 23 2017, @01:53AM (1 child)
SIGTERM - execution aborted, core dumped.
(Score: 1, Touché) by Anonymous Coward on Sunday April 23 2017, @01:27PM
If only his mother aborted....
(Score: 3, Interesting) by darkfeline on Sunday April 23 2017, @08:17AM (5 children)
Threads are the new malloc(). A few decades ago we discovered that programmers just aren't smart enough to do manual memory management. It turns out programmers aren't smart enough to do preemptive asynchronous programming either. Either you end up locking everything, or you end up with subtle race conditions that blow up everything every few weeks.
The solution for manual memory management was garbage collection and smart pointers, the solution for threads are RPCs and microservices. Really, we're just going back to the UNIX roots of processes only doing one thing. Data should be shared through files and sockets, not through direct memory access.
Join the SDF Public Access UNIX System today!
(Score: 3, Touché) by c0lo on Sunday April 23 2017, @08:56AM (3 children)
Sounds like a Plan... 9.
https://www.youtube.com/watch?v=aoFiw2jMy-0
(Score: 2) by maxwell demon on Sunday April 23 2017, @10:25AM (2 children)
Resurrection of the dead?
The Tao of math: The numbers you can count are not the real numbers.
(Score: 2) by c0lo on Sunday April 23 2017, @11:02AM (1 child)
Great software is never dead. It gets recycled... like the memory in GC... or like a Harvey [harvey-os.org].
https://www.youtube.com/watch?v=aoFiw2jMy-0
(Score: 2) by LoRdTAW on Sunday April 23 2017, @01:29PM
Or 9front [9front.org].
(Score: 2) by maxwell demon on Sunday April 23 2017, @10:24AM
What is a shared memory segment other than an in-memory file used by two processes? It even has an "inode" in the form of a descriptor table entry!
The Tao of math: The numbers you can count are not the real numbers.
(Score: 2, Insightful) by Anonymous Coward on Sunday April 23 2017, @01:03PM
The example 15 instructions include 2 system calls.
Call back after you have counted the number of instructions executed to process those.