Gentoo Archives: gentoo-user

From: "Canek Peláez Valdés" <caneko@×××××.com>
To: gentoo-user@l.g.o
Subject: Re: [gentoo-user] Is my system (really) using nptl
Date: Sat, 13 Oct 2012 19:02:47
Message-Id: CADPrc837R8RqHUc4xip3zPxAa8JnXiDZ7wsKMyp3nALGRgtw1Q@mail.gmail.com
In Reply to: Re: [gentoo-user] Is my system (really) using nptl by Mark Knecht
1 On Sat, Oct 13, 2012 at 1:40 PM, Mark Knecht <markknecht@×××××.com> wrote:
2 > On Sat, Oct 13, 2012 at 11:16 AM, Canek Peláez Valdés <caneko@×××××.com> wrote:
3 >> On Sat, Oct 13, 2012 at 12:10 PM, Mark Knecht <markknecht@×××××.com> wrote:
4 >>> On Sat, Oct 13, 2012 at 9:15 AM, Canek Peláez Valdés <caneko@×××××.com> wrote:
5 >>> <SNIP>
6 >>>>
7 >>>> We can only know seeing the code. Timur, this is the little test I
8 >>>> made which creates 5 threads and runs them for 1 minute. In my case,
9 >>>> `ps x` shows only 1 PID, care to give it a try?
10 >>>>
11 >>>> ----------------------
12 >>>> #include <pthread.h> <<======
13 >>>> #include <unistd.h>
14 >>>> #include <stdlib.h>
15 >>>> #include <stdio.h>
16 >>>
17 >>> Thanks for the test case. Like you I see only one thread. However the
18 >>> test case wouldn't compile for me without the -pthread option so it
19 >>> makes me wonder what happens to a program like I had pointed to
20 >>> yesterday that uses the old style threading that did create lots of
21 >>> process ids? Possibly an nptl system would still generate lots of ids
22 >>> for that program and that's what he's seeing?
23 >>>
24 >>> Just curious. I don't program but I'm always sort of interested.
25 >>
26 >> You got your answer. NTPL stands for Native POSIX Thread *Library*. As
27 >> it name says, it is a library (with support in the kernel and in
28 >> glibc). If you don't use the library (-lpthread), you cannot make use
29 >> of its advantages.
30 >>
31 >> What "old style threading" did you use for your test case?
32 >>
33 >> Regards.
34 >> --
35 >> Canek Peláez Valdés
36 >> Posgrado en Ciencia e Ingeniería de la Computación
37 >> Universidad Nacional Autónoma de México
38 >>
39 >
40 > As for 'old style' I only meant code that did threads but didn't use
41 > the POSIX libraries. (I guess...)
42 >
43 > Actually I hadn't run the test case at the time but was referring to
44 > the one I pointed the OP at yesterday:
45 >
46 > http://www.makelinux.net/alp/032
47 >
48 > However it's essentially the same as yours (not as elegant, but
49 > functionally similar). However the results shown on that page show
50 > different pids for the threads. When I run that same code here I get
51 > the same pids:
52 >
53 > mark@c2stable ~ $ ./pthread2
54 > main thread pid is 5387
55 > child thread pid is 5387
56 > ^C
57 > mark@c2stable ~ $
58 >
59 > Now, this does make me curious about some things running on my system.
60 > Two for instance, Google Chrome and akonadi_agent, have LOTS of pids.
61 > I was assuming those were different threads and were demonstrating
62 > what the OP was asking about, but now I'm not so sure. How does a
63 > single program on an nptl system generate all these different pids?
64
65 Because Google Chrome is actually LOTS of programs. I don't know about
66 akonadi (don't use KDE), but Chrome doesn't use threads; it uses
67 different process for each tab (and for several plugins, I believe),
68 and it integrates all those process in a single GUI using come kind of
69 IPC.
70
71 The idea is that if a tab crashes (bad pulgin, rogue JavaScript,
72 etc.), it only crashes the tab, not the whole browser. It saves us
73 from the nightmare that forced us to "killall -9 mozilla" from time to
74 time some years ago.
75
76 A thread is a "lightweight process"; it has its own call stack, but it
77 shares the same memory space as its "parent" (actually, the thread
78 that created it). The advantages are many: since all threads in the
79 same process share the same memory space, they can easily and quickly
80 communicate between each other. The tradeoff is that if one thread
81 crashes, the whole program does (AFAIK, someone please correct me if
82 I'm wrong).
83
84 A process has its own call stack and its own memory space; and while
85 it can share file descriptors with its parent (the process where it
86 was created), including pipes, it cannot easily and quickly
87 communicate with a process different from its parent (hence little
88 wonders like dbus, whose job is precisely to provice Inter Process
89 Communication [IPC] between different processes).
90
91 For threads in Linux/Unix you usually use POSIX threads, although
92 there are alternatives. For processes you use fork; everytime you use
93 "ls" or "cp" in a terminal, or launch a program using KDE or GNOME,
94 your shell or desktops forks a new process for it.
95
96 Up until very recently most programs used threads to do several things
97 at once; some years ago apache started to do a "hybrid" approach,
98 where it forks or launches threads dependign on the load of the
99 system, other server programs followed it. AFAIK, Google Chrome was
100 the first desktop program in Linux which uses several processes
101 runnning under the same GUI.
102
103 Regards.
104 --
105 Canek Peláez Valdés
106 Posgrado en Ciencia e Ingeniería de la Computación
107 Universidad Nacional Autónoma de México

Replies

Subject Author
Re: [gentoo-user] Is my system (really) using nptl Michael Mol <mikemol@×××××.com>