kernelnewbies.kernelnewbies.org archive mirror
 help / color / mirror / Atom feed
From: michi1@michaelblizek.twilightparadox.com (Michael Blizek)
To: kernelnewbies@lists.kernelnewbies.org
Subject: Questions about preemption and concurrency
Date: Fri, 27 May 2011 18:09:34 +0200	[thread overview]
Message-ID: <20110527160934.GA2064@michaelblizek.twilightparadox.com> (raw)
In-Reply-To: <308552.44045.qm@web137608.mail.in.yahoo.com>

Hi!

On 17:14 Fri 27 May     , Naman shekhar Mishra wrote:
> In Operating System Concepts 6th ed, chapter 20:
> "Kernel code can thus assume that it will never be preempted by another process and that no special care must be
> taken to protect critical sections.

This is very simplified and hardly true:
1) In some cases kernel code can be preempted. Otherwise latencies would be
very long. Even if you disable kernel preemption, calls to schedule() may
still cause "preemption". This can actually be complex on its own: You have to
make sure that you are in a consistent state when you resume.
2) On a system with more than one CPU, you need to make sure that one one CPU
is running in kernel mode. Linux had this thing called "big kernel lock" to do
this. However, it did not scale very well.

> The only requirement is that critical sections do not contain references to user memory
> or waits for I/O completions"
> 
> Why us that the process cannot wait for I/O to complete in critical sections?

This is not really a hard requirement. But I guess that you do not want the
CPU to be waiting for IO completion while there are other things it could do.
If you do swapping this means that any access to user space memory can cause
disk access. This is why you should not do user memory accesses either.

> My another question is: While developing kernel modules (for kernels before 2.6), do we need to keep concurrency in mind?
> Because the modules run as part of the kernel and no two processes can run in the kernel at the same time? Or my question is wrong since they CAN run in the kernel at the same time (like when one process waits for I/O completion and another is scheduled to run which then makes a system call)?

When doing development for 2.6 you have to do locking pretty much the same
way when doing multithreaded user space programming. The main difference is
that there is a also locking type called "spinlock", which is needed when
running uninterrupted (e.g. in interrupt handlers). Instead of invoking the
scheduler to run a different task (which cannot be done if you want to run
uninterrupted), it does busy waiting. It hopefully will not have to wait
long: The task which has taken the lock is also running without
interruptions. And tasks which do this have to be quick anyway...

	-Michi
-- 
programing a layer 3+4 network protocol for mesh networks
see http://michaelblizek.twilightparadox.com

      parent reply	other threads:[~2011-05-27 16:09 UTC|newest]

Thread overview: 4+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2011-05-27 11:44 Questions about preemption and concurrency Naman shekhar Mishra
2011-05-27 13:21 ` Daniel Baluta
2011-05-27 15:04 ` Jonathan Neuschäfer
2011-05-27 16:09 ` Michael Blizek [this message]

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=20110527160934.GA2064@michaelblizek.twilightparadox.com \
    --to=michi1@michaelblizek.twilightparadox.com \
    --cc=kernelnewbies@lists.kernelnewbies.org \
    /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).