SoylentNews
SoylentNews is people
https://soylentnews.org/

Title    Raw Linux Threads via System Calls
Date    Sunday April 23 2017, @12:17AM
Author    Fnord666
Topic   
from the silk-threads dept.
https://soylentnews.org/article.pl?sid=17/04/22/0022247

Phoenix666 writes:

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:


Original Submission

Links

  1. "Phoenix666" - https://soylentnews.org/~Phoenix666/
  2. "The article is a bit old" - http://nullprogram.com/blog/2015/05/15/
  3. "clone()" - http://man7.org/linux/man-pages/man2/clone.2.html
  4. "NASM" - http://www.nasm.us/
  5. "nasm-mode" - http://nullprogram.com/blog/2015/04/19/
  6. "Pure assembly, library-free Linux threading demo" - https://github.com/skeeto/pure-linux-threads-demo
  7. "Original Submission" - https://soylentnews.org/submit.pl?op=viewsub&subid=19766

© Copyright 2024 - SoylentNews, All Rights Reserved

printed from SoylentNews, Raw Linux Threads via System Calls on 2024-07-19 11:33:39