linux-c-programming.vger.kernel.org archive mirror
 help / color / mirror / Atom feed
From: "Steve Graegert" <graegerts@gmail.com>
To: Shriramana Sharma <samjnaa@gmail.com>
Cc: Linux C Programming List <linux-c-programming@vger.kernel.org>
Subject: Re: threads and kernel
Date: Sun, 21 Oct 2007 19:23:48 +0200	[thread overview]
Message-ID: <6a00c8d50710211023t898f17idf77bcad37c37a9b@mail.gmail.com> (raw)
In-Reply-To: <471B5276.9010800@gmail.com>

Hello Shriramana,

your questions can be answered in very great detail and fill whole
books (which has actually happened), so please forgive my brief
answers.

(Comments inline)

On 10/21/07, Shriramana Sharma <samjnaa@gmail.com> wrote:
> Hello, my knowledgeable friends! :)
>
> 1.
>
> As I understand it, the kernel is always running and whenever an app
> asks for a system resource the kernel does the needful. However, in my
> process tree I see no process named linux or kernel. I only see the init
> process at the root of the tree. Does the init process represent the kernel?

When the boot loader has finished loading the kernel image and invoked
startup_32() on x86 systems the kernel spawns the scheduler (PID 0)
and the init process (PID 1) separately.  The scheduler then takes
control over the system as soon as the kernel goes idle and init
starts the /etc/rc.d/rc.sysinit script.

Yes, there is no kernel process, but there are a couple of other
system processes: kflushd, kupdate, kpiod, kswapd, etc.

> 2.
>
> When an app does a system call, would some form of IPC between the
> process of that app and the kernel process (assuming there is one) be
> involved?

Code typically runs in two or more privilege levels.  Linux supports
two levels known as user  mode and kernel mode.  Higher privileged
code can perform operations which impact system performance and
stability like accessing hardware devices, changing the processor
state, enabling/disabling interrupts, etc.

System calls often make use of special CPU instructions that cause the
processor to transfer control to a higher privilege level allowing the
more privileged code to execute.

When a system call is invoked, the process which has invoked is being
interrupted and all the information needed to continue this process is
stored for later use.  Now, the process executes higher privileged
code that determines what to do next from examining the stack of the
less privileged code.  It contains information about the request to be
served by the kernel.  When it is finished, it control is returned to
the program, restoring the saved state and continuing program
execution.

The standard C library provides wrapper functions to make life easier
for developers as the semantic of system calls is system dependent.

> 3.
>
> When an application uses a library, the app and the library are
> processed in DIFFERENT threads in which of the following cases:
>
> 1. the library is statically linked
> 2. the library is dynamically linked
> 3. the library is loaded using dlopen
>
> My guess is "none of the above", am I right?

Runtime linking of libraries is done at the process level.

Dynamic applications, for example, consist of a dynamic executable and
one or more dynamic objects. As part of the initialization and
execution of a dynamic application, runtime linker  ld.so.1, performs
the following functions:

   1. analyze shared object dependencies,
   2. locate and map in these dependencies,
   3. calls initialization functions provided by the shared object,
   4. transfer control to application's main() function.

From this moment on all threads created __within__ the process share
its address space.

As a side note: you can safely use dlopen()  to load shared libraries,
whether or not they depend on libpthread.so, as long as the main
program was initially threaded.  The other way round is dangerous and
mostly not allowed.

> These are NOT homework questions! :)

Yes, I know.  You are a well known member of this list.  No need to apologize.

	\Steve

--

Steve Grägert
DigitalEther.de
-
To unsubscribe from this list: send the line "unsubscribe linux-c-programming" in
the body of a message to majordomo@vger.kernel.org
More majordomo info at  http://vger.kernel.org/majordomo-info.html

  parent reply	other threads:[~2007-10-21 17:23 UTC|newest]

Thread overview: 20+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2007-10-21 13:21 threads and kernel Shriramana Sharma
2007-10-21 16:26 ` Irfan Habib
2007-10-21 17:23 ` Steve Graegert [this message]
2007-10-22  6:50   ` Benoit Fouet
2007-10-22 13:01     ` Steve Graegert
2007-10-22 13:27       ` Benoit Fouet
2007-10-22 13:31         ` Steve Graegert
2007-10-22 13:38           ` Benoit Fouet
2007-10-22 14:20             ` Steve Graegert
2007-10-22 14:34               ` Benoit Fouet
2007-10-23  4:32               ` vibi
2007-10-23  5:14                 ` Steve Graegert
2007-10-23  5:53                   ` vibi
2007-10-23  6:58                     ` Steve Graegert
2007-10-23  7:10                       ` vibi
2007-10-23  7:30                         ` Steve Graegert
2007-10-22  7:55   ` Shriramana Sharma
2007-10-22 13:41     ` Steve Graegert
2007-10-22 17:18     ` Glynn Clements
2007-10-21 17:36 ` Glynn Clements

Reply instructions:

You may reply publicly to this message via plain-text email
using any one of the following methods:

* Save the following mbox file, import it into your mail client,
  and reply-to-all from there: mbox

  Avoid top-posting and favor interleaved quoting:
  https://en.wikipedia.org/wiki/Posting_style#Interleaved_style

* Reply using the --to, --cc, and --in-reply-to
  switches of git-send-email(1):

  git send-email \
    --in-reply-to=6a00c8d50710211023t898f17idf77bcad37c37a9b@mail.gmail.com \
    --to=graegerts@gmail.com \
    --cc=linux-c-programming@vger.kernel.org \
    --cc=samjnaa@gmail.com \
    /path/to/YOUR_REPLY

  https://kernel.org/pub/software/scm/git/docs/git-send-email.html

* If your mail client supports setting the In-Reply-To header
  via mailto: links, try the mailto: link
Be sure your reply has a Subject: header at the top and a blank line before the message body.
This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox;
as well as URLs for NNTP newsgroup(s).