Slash Boxes

SoylentNews is people

SoylentNews is powered by your submissions, so send in your scoop. Only 9 submissions in the queue.
posted by Fnord666 on Sunday April 23 2017, @12:17AM   Printer-friendly
from the silk-threads dept.

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

This discussion has been archived. No new comments can be posted.
Display Options Threshold/Breakthrough Mark All as Read Mark All as Unread
The Fine Print: The following comments are owned by whoever posted them. We are not responsible for them in any way.
  • (Score: 3, Touché) by c0lo on Sunday April 23 2017, @08:56AM (3 children)

    by c0lo (156) Subscriber Badge on Sunday April 23 2017, @08:56AM (#498238) Journal

    Data should be shared through files and sockets, not through direct memory access.

    Sounds like a Plan... 9.

    Starting Score:    1  point
    Moderation   +1  
       Touché=1, Total=1
    Extra 'Touché' Modifier   0  
    Karma-Bonus Modifier   +1  

    Total Score:   3  
  • (Score: 2) by maxwell demon on Sunday April 23 2017, @10:25AM (2 children)

    by maxwell demon (1608) on Sunday April 23 2017, @10:25AM (#498262) Journal

    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)

      by c0lo (156) Subscriber Badge on Sunday April 23 2017, @11:02AM (#498271) Journal

      Great software is never dead. It gets recycled... like the memory in GC... or like a Harvey [].