Sun Nov 24 23:14:30 2024
EVENTS
 FREE
SOFTWARE
INSTITUTE

POLITICS
JOBS
MEMBERS'
CORNER

MAILING
LIST

NYLXS Mailing Lists and Archives
NYLXS Members have a lot to say and share but we don't keep many secrets. Join the Hangout Mailing List and say your peice.

DATE 2015-03-01

LEARN

2024-11-24 | 2024-10-24 | 2024-09-24 | 2024-08-24 | 2024-07-24 | 2024-06-24 | 2024-05-24 | 2024-04-24 | 2024-03-24 | 2024-02-24 | 2024-01-24 | 2023-12-24 | 2023-11-24 | 2023-10-24 | 2023-09-24 | 2023-08-24 | 2023-07-24 | 2023-06-24 | 2023-05-24 | 2023-04-24 | 2023-03-24 | 2023-02-24 | 2023-01-24 | 2022-12-24 | 2022-11-24 | 2022-10-24 | 2022-09-24 | 2022-08-24 | 2022-07-24 | 2022-06-24 | 2022-05-24 | 2022-04-24 | 2022-03-24 | 2022-02-24 | 2022-01-24 | 2021-12-24 | 2021-11-24 | 2021-10-24 | 2021-09-24 | 2021-08-24 | 2021-07-24 | 2021-06-24 | 2021-05-24 | 2021-04-24 | 2021-03-24 | 2021-02-24 | 2021-01-24 | 2020-12-24 | 2020-11-24 | 2020-10-24 | 2020-09-24 | 2020-08-24 | 2020-07-24 | 2020-06-24 | 2020-05-24 | 2020-04-24 | 2020-03-24 | 2020-02-24 | 2020-01-24 | 2019-12-24 | 2019-11-24 | 2019-10-24 | 2019-09-24 | 2019-08-24 | 2019-07-24 | 2019-06-24 | 2019-05-24 | 2019-04-24 | 2019-03-24 | 2019-02-24 | 2019-01-24 | 2018-12-24 | 2018-11-24 | 2018-10-24 | 2018-09-24 | 2018-08-24 | 2018-07-24 | 2018-06-24 | 2018-05-24 | 2018-04-24 | 2018-03-24 | 2018-02-24 | 2018-01-24 | 2017-12-24 | 2017-11-24 | 2017-10-24 | 2017-09-24 | 2017-08-24 | 2017-07-24 | 2017-06-24 | 2017-05-24 | 2017-04-24 | 2017-03-24 | 2017-02-24 | 2017-01-24 | 2016-12-24 | 2016-11-24 | 2016-10-24 | 2016-09-24 | 2016-08-24 | 2016-07-24 | 2016-06-24 | 2016-05-24 | 2016-04-24 | 2016-03-24 | 2016-02-24 | 2016-01-24 | 2015-12-24 | 2015-11-24 | 2015-10-24 | 2015-09-24 | 2015-08-24 | 2015-07-24 | 2015-06-24 | 2015-05-24 | 2015-04-24 | 2015-03-24 | 2015-02-24 | 2015-01-24 | 2014-12-24 | 2014-11-24 | 2014-10-24

Key: Value:

Key: Value:

MESSAGE
DATE 2015-03-25
FROM Ruben Safir
SUBJECT Subject: [LIU Comp Sci] Threads and Speed
From owner-learn-outgoing-at-mrbrklyn.com Wed Mar 25 13:00:25 2015
Return-Path:
X-Original-To: archive-at-mrbrklyn.com
Delivered-To: archive-at-mrbrklyn.com
Received: by mrbrklyn.com (Postfix)
id C424116133C; Wed, 25 Mar 2015 13:00:25 -0400 (EDT)
Delivered-To: learn-outgoing-at-mrbrklyn.com
Received: by mrbrklyn.com (Postfix, from userid 28)
id B202116133B; Wed, 25 Mar 2015 13:00:25 -0400 (EDT)
Delivered-To: learn-at-nylxs.com
Received: from mailbackend.panix.com (mailbackend.panix.com [166.84.1.89])
by mrbrklyn.com (Postfix) with ESMTP id ACB4716133A;
Wed, 25 Mar 2015 13:00:01 -0400 (EDT)
Received: from [10.0.0.19] (unknown [96.57.23.82])
by mailbackend.panix.com (Postfix) with ESMTPSA id B665812AEA;
Wed, 25 Mar 2015 13:00:00 -0400 (EDT)
Message-ID: <5512E990.6030805-at-panix.com>
Date: Wed, 25 Mar 2015 13:00:00 -0400
From: Ruben Safir
User-Agent: Mozilla/5.0 (X11; Linux x86_64; rv:31.0) Gecko/20100101 Thunderbird/31.3.0
MIME-Version: 1.0
To: learn-at-nylxs.com
Subject: [LIU Comp Sci] Threads and Speed
Content-Type: text/plain; charset=utf-8
Content-Transfer-Encoding: 8bit
Sender: owner-learn-at-mrbrklyn.com
Precedence: bulk
Reply-To: learn-at-mrbrklyn.com

I'm covering threads in the Operating Systems class and the text is in
adequate. But years ago I recall have some terse converations about the
threading model and how overblown I thought it was, and how it killed
programs, slowing them down badly.

A big response back was the discussion of contact overhead. But there
is a lot of overhead with threads as well, and especially kernel level
threads. Today we have multiple cores, so in order to get concurrency
and parallelism with code instruction, threading helps move processes
across multiple nodes and cpus (although I don't know why that can not
inherently be done on a process level). Regardless, the means to
inherent acceleration of threads is still not a proven fact as fara as i
can tell.

So I have this question in the Homework:

4.2 What are two differences between user-level threads and kernel-level
threads? Under what circumstances is one type better than the other?


Well that is a real head scratcher. It is an open ended lazy question
that is given without much thought on the part of the author.

Under what platform? Under what conditions?

But I did do some research and uncovered two decent POVs on this top,
opposed to each other, FWIW

~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~`

A) For clarity, I usually say "OS-level threads" or "native threads"
instead of "Kernel-level threads" (which I confused with "kernel threads"
in my original answer below.) OS-level threads are created and managed
by the OS. Most languages have support for them. (C, recent Java, etc)
They are extremely hard to use because you are 100% responsible for
preventing problems. In some languages, even the native data structures
(such as Hashes or Dictionaries) will break without extra locking code.


The opposite of an OS-thread is a green thread that is managed by your
language. These threads are given various names depending on the language
(coroutines in C, goroutines in Go, fibers in Ruby, etc). These threads

only exist inside your language and not in your OS. Because the language
chooses context switches (i.e. at the end of a statement), it prevents
TONS of subtle race conditions (such as seeing a partially-copied
structure, or needing to lock most data structures). The programmer sees
"blocking" calls (i.e. data = file.read() ), but the language translates
it into async calls to the OS. The language then allows other green
threads to run while waiting for the result.

Green threads are much simpler for the programmer, but their performance
varies: If you have a LOT of threads, green threads can be are better for
both CPU and RAM. On the other hand, most green thread languages can't
take advantage of multiple cores. (You can't even buy a single-core
computer or phone anymore!). And a bad library can halt the entire
language by doing a blocking OS call.

The best of both worlds is to have one OS thread per CPU, and many green
threads that are magically moved around onto OS threads. Languages like
Go and Erlang can do this.

system calls and other uses not available to user-level threads

This is only half true. Yes, you can easily cause problems if you call the
OS yourself (i.e. do something that's blocking.) But the language usually
has replacements, so you don't even notice. These replacements do call
the kernel, just slightly differently than you think. Kernel threads
vs User Threads

Edit: This is my original answer, but it is about User space threads vs
Kernel-only threads, which (in hindsight) probably wasn't the question.

User threads and Kernel threads are exactly the same. (You can see by
looking in /proc/ and see that the kernel threads are there too.)

A User thread is one that executes user-space code. But it can call
into kernel space at any time. It's still considered a "User" thread,
even though it's executing kernel code at elevated security levels.

A Kernel thread is one that only runs kernel code and isn't associated
with a user-space process. These are like "UNIX daemons", except they are
kernel-only daemons. So you could say that the kernel is a multi-threaded
program. For example, there is a kernel thread for swap. This forces
all swap issues to get "serialized" into a single stream.


If a user thread needs something, it will call into the kernel, which
marks that thread as sleeping. Later, the swap thread finds the data,
so it marks the user thread as runnable. Later still, the "user thread"
returns from the kernel back to userland as if nothing happened.

In fact, all threads start off in kernel space, because the clone()
operation happens in kernel space. (And there's lots of kernel accounting
to do before you can 'return' to a new process in user space.)



~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~`

Now, FWIW, the refernece to Green threads MIGHT be a reference to the SunOS
Green Threads, but maybe not since this response seems up to date on
language
implementations and SunOS has been dead for a while. The Linux Kernel
breaks
all procedures and threads into TASKS that is a kernel data structure.



Now here is the second post: This one is more classical and is likely
what the
text is looking for,

~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
Kernel-Level Threads

To make concurrency cheaper, the execution aspect of process is separated
out into threads. As such, the OS now manages threads and processes. All
thread operations are implemented in the kernel and the OS schedules all
threads in the system. OS managed threads are called kernel-level threads
or light weight processes. • NT: Threads • Solaris: Lightweight
processes(LWP).

In this method, the kernel knows about and manages the threads. No runtime
system is needed in this case. Instead of thread table in each process,
the kernel has a thread table that keeps track of all threads in the
system. In addition, the kernel also maintains the traditional process
table to keep track of processes. Operating Systems kernel provides
system call to create and manage threads.

Advantages:

Because kernel has full knowledge of all threads, Scheduler may decide
to give more time to a process having large number of threads than
process having small number of threads. Kernel-level threads are
especially good for applications that frequently block.

Disadvantages:

The kernel-level threads are slow and inefficient. For instance,
threads operations are hundreds of times slower than that of
user-level threads. Since kernel must manage and schedule threads
as well as processes. It require a full thread control block (TCB)
for each thread to maintain information about threads. As a result
there is significant overhead and increased in kernel complexity.

User-Level Threads

Kernel-Level threads make concurrency much cheaper than process
because, much less state to allocate and initialize. However, for
fine-grained concurrency, kernel-level threads still suffer from too
much overhead. Thread operations still require system calls. Ideally, we
require thread operations to be as fast as a procedure call. Kernel-Level
threads have to be general to support the needs of all programmers,
languages, runtimes, etc. For such fine grained concurrency we need still
"cheaper" threads.

To make threads cheap and fast, they need to be implemented at user
level. User-Level threads are managed entirely by the run-time system
(user-level library). The kernel knows nothing about user-level threads
and manages them as if they were single-threaded processes. User-Level
threads are small and fast, each thread is represented by a PC, register,
stack, and small thread control block. Creating a new thread, switching
between threads, and synchronizing threads are done via procedure
call. i.e no kernel involvement. User-Level threads are hundred times
faster than Kernel-Level threads.

Advantages:

The most obvious advantage of this technique is that a user-level
threads package can be implemented on an Operating System that
does not support threads. User-level threads does not require
modification to operating systems. Simple Representation: Each
thread is represented simply by a PC, registers, stack and a small
control block, all stored in the user process address space.
Simple Management: This simply means that creating a thread,
switching between threads and synchronization between threads can
all be done without intervention of the kernel. Fast and Efficient:
Thread switching is not much more expensive than a procedure call.

Disadvantages:

User-Level threads are not a perfect solution as with everything else,
they are a trade off. Since, User-Level threads are invisible to the
OS they are not well integrated with the OS. As a result, OS can
make poor decisions like scheduling a process with idle threads,
blocking a process whose thread initiated an I/O even though the
process has other threads that can run and unscheduling a process with
a thread holding a lock. Solving this requires communication between
between kernel and user-level thread manager. There is a lack of
coordination between threads and operating system kernel. Therefore,
process as whole gets one time slice irrespective of whether process
has one thread or 1000 threads within. It is up to each thread to
relinquish control to other threads. User-level threads requires
non-blocking systems call i.e., a multithreaded kernel. Otherwise,
entire process will blocked in the kernel, even if there are runnable
threads left in the processes. For example, if one thread causes a
page fault, the process blocks.

~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~

So putting the two responses together, you can get a broad look at Threading
topography in the modern system. And together they are more
understandable and
comprehenssive then the assigned textbooks on the subject.

Ruben



  1. 2015-03-01 Ruben <ruben.safir-at-my.liu.edu> Subject: [LIU Comp Sci] Fwd: Re: Linked List Test Question
  2. 2015-03-02 Ruben Safir <mrbrklyn-at-panix.com> Subject: [LIU Comp Sci] [ruben-at-mrbrklyn.com: fork problem]
  3. 2015-03-03 Ruben Safir <mrbrklyn-at-panix.com> Subject: [LIU Comp Sci] Re: fork problem
  4. 2015-03-04 Maneesh Kongara <maneeshkongara-at-gmail.com> Re: [LIU Comp Sci] Mid term in Allorithms
  5. 2015-03-04 Ruben Safir <mrbrklyn-at-panix.com> Re: [LIU Comp Sci] Mid term in Allorithms
  6. 2015-03-04 Maneesh Kongara <maneeshkongara-at-gmail.com> Re: [LIU Comp Sci] Mid term in Allorithms
  7. 2015-03-04 Ruben Safir <mrbrklyn-at-panix.com> Re: [LIU Comp Sci] Mid term in Allorithms
  8. 2015-03-04 Ruben Safir <ruben.safir-at-my.liu.edu> Re: [LIU Comp Sci] Mid term in Allorithms
  9. 2015-03-04 Maneesh Kongara <maneeshkongara-at-gmail.com> Re: [LIU Comp Sci] Mid term in Allorithms
  10. 2015-03-04 Ruben Safir <mrbrklyn-at-panix.com> Subject: [LIU Comp Sci] Kanpsack problemi
  11. 2015-03-04 Ruben Safir <mrbrklyn-at-panix.com> Subject: [LIU Comp Sci] Mid term in Allorithms
  12. 2015-03-05 Kamran <kamran.mirzayev-at-my.liu.edu> Re: [LIU Comp Sci] Canonical's 2015 OpenStack Roadshow
  13. 2015-03-05 mrbrklyn-at-panix.com Subject: [LIU Comp Sci] Canonical's 2015 OpenStack Roadshow
  14. 2015-03-05 Ruben <mrbrklyn-at-panix.com> Subject: [LIU Comp Sci] Forks - Operating systems class
  15. 2015-03-06 Ruben <mrbrklyn-at-panix.com> Subject: [LIU Comp Sci] KDE Mentoring -
  16. 2015-03-06 Ruben <mrbrklyn-at-panix.com> Subject: [LIU Comp Sci] networking class
  17. 2015-03-07 Ruben Safir <mrbrklyn-at-panix.com> Subject: [LIU Comp Sci] Operating Systems class submition
  18. 2015-03-08 From: "keisha.sylvester" <keisha.sylvester-at-my.liu.edu> RE: [LIU Comp Sci] Operating Systems Supplement Text
  19. 2015-03-08 Ruben <ruben.safir-at-my.liu.edu> Re: [LIU Comp Sci] Operating Systems Supplement Text
  20. 2015-03-08 Ruben Safir <mrbrklyn-at-panix.com> Subject: [LIU Comp Sci] Operating systems HW
  21. 2015-03-08 Ruben Safir <mrbrklyn-at-panix.com> Subject: [LIU Comp Sci] Operating Systems Supplement Text
  22. 2015-03-08 Ruben Safir <mrbrklyn-at-panix.com> Subject: [LIU Comp Sci] Re: Operating systems HW
  23. 2015-03-11 Ruben Safir <mrbrklyn-at-panix.com> Re: [LIU Comp Sci] Operating Systems class submition
  24. 2015-03-11 Ruben Safir <mrbrklyn-at-panix.com> Subject: [LIU Comp Sci] February Journal almost completed
  25. 2015-03-16 mrbrklyn-at-panix.com Subject: [LIU Comp Sci] Operating Systems Chapter 3 HW complete
  26. 2015-03-16 mrbrklyn-at-panix.com Subject: [LIU Comp Sci] [ruben-at-mrbrklyn.com: os hw]
  27. 2015-03-17 Ruben Safir <mrbrklyn-at-panix.com> Subject: [LIU Comp Sci] Fwd: NYC local: UNIGROUP 19-MAR-2015 (Thu): Penguin Computing presents
  28. 2015-03-19 Ruben Safir <mrbrklyn-at-panix.com> Subject: [LIU Comp Sci] LIU Center for Entrepreneurship + Innovation
  29. 2015-03-20 Ruben Safir <mrbrklyn-at-panix.com> Subject: [LIU Comp Sci] Optimum
  30. 2015-03-20 mrbrklyn-at-panix.com Subject: [LIU Comp Sci] [jkeen-at-verizon.net: 2nd New York City Perl Hackathon: hold the
  31. 2015-03-22 Ruben Safir <mrbrklyn-at-panix.com> Subject: [LIU Comp Sci] Re: Kernel thread scheduling
  32. 2015-03-22 mrbrklyn-at-panix.com Subject: [LIU Comp Sci] [jkeen-at-verizon.net: New York City Perl Hackathon 2015: Registration
  33. 2015-03-22 mrbrklyn-at-panix.com Subject: [LIU Comp Sci] [vinc94-at-gmail.com: Re: Kernel thread scheduling]
  34. 2015-03-22 mrbrklyn-at-panix.com Subject: [LIU Comp Sci] [xerofoify-at-gmail.com: Re: Kernel thread scheduling]
  35. 2015-03-23 Keisha Sylvester <keisha.sylvester-at-my.liu.edu> Re: [LIU Comp Sci] Meeting Announcement II
  36. 2015-03-23 Ruben Safir <mrbrklyn-at-panix.com> Subject: [LIU Comp Sci] Meeting Announcement II
  37. 2015-03-23 mrbrklyn-at-panix.com Subject: [LIU Comp Sci] [gabor-at-szabgab.com: [Perlweekly] #191 - Hackathon after Hackathon]
  38. 2015-03-24 Maneesh Kongara <maneeshkongara-at-gmail.com> Re: [LIU Comp Sci] GNU/Linux 1
  39. 2015-03-24 Maneesh Kongara <maneeshkongara-at-gmail.com> Re: [LIU Comp Sci] GNU/Linux 1
  40. 2015-03-24 Ruben Safir <mrbrklyn-at-panix.com> Subject: [LIU Comp Sci] GNU/Linux 1
  41. 2015-03-25 Ruben Safir <mrbrklyn-at-panix.com> Re: [LIU Comp Sci] GNU/Linux 1
  42. 2015-03-25 Ruben <ruben.safir-at-my.liu.edu> Re: [LIU Comp Sci] GNU/Linux 1
  43. 2015-03-25 Ruben Safir <mrbrklyn-at-panix.com> Re: [LIU Comp Sci] GNU/Linux 1
  44. 2015-03-25 Ruben Safir <mrbrklyn-at-panix.com> Subject: [LIU Comp Sci] decent threading outline that fills in where our notes let off
  45. 2015-03-25 Ruben Safir <mrbrklyn-at-panix.com> Subject: [LIU Comp Sci] I love this HW problem
  46. 2015-03-25 Ruben Safir <mrbrklyn-at-panix.com> Subject: [LIU Comp Sci] Java Crimes
  47. 2015-03-25 Ruben Safir <mrbrklyn-at-panix.com> Subject: [LIU Comp Sci] jobs jobs
  48. 2015-03-25 Ruben Safir <mrbrklyn-at-panix.com> Subject: [LIU Comp Sci] Threaded Programming example
  49. 2015-03-25 Ruben Safir <mrbrklyn-at-panix.com> Subject: [LIU Comp Sci] Threads and Speed
  50. 2015-03-26 Ruben Safir <mrbrklyn-at-panix.com> Subject: [LIU Comp Sci] Cpt 4 HW from pr 13-18
  51. 2015-03-26 Ruben <ruben.safir-at-my.liu.edu> Subject: [LIU Comp Sci] Fwd: [NYLXS - HANGOUT] GNU/Linux 1
  52. 2015-03-26 Ruben Safir <mrbrklyn-at-panix.com> Subject: [LIU Comp Sci] Kernel Learning Resource - #1
  53. 2015-03-27 Ruben Safir <mrbrklyn-at-panix.com> Subject: [LIU Comp Sci] AVl Trees
  54. 2015-03-27 Ruben Safir <mrbrklyn-at-panix.com> Subject: [LIU Comp Sci] Re: Cpt 4 HW Programming Project 4-20 (and 3-20)
  55. 2015-03-28 Ruben Safir <mrbrklyn-at-panix.com> Subject: [LIU Comp Sci] Jobs
  56. 2015-03-28 Ruben Safir <mrbrklyn-at-panix.com> Subject: [LIU Comp Sci] JOUNAL MEETING AT 10AM TOMORROW
  57. 2015-03-29 Ruben Safir <mrbrklyn-at-panix.com> Subject: [LIU Comp Sci] Fwd: usage of dma_common_mmap
  58. 2015-03-30 Ruben Safir <mrbrklyn-at-panix.com> Subject: [LIU Comp Sci] HW 4.18 Problem

NYLXS are Do'ers and the first step of Doing is Joining! Join NYLXS and make a difference in your community today!