9fans - fans of the OS Plan 9 from Bell Labs
 help / color / mirror / Atom feed
From: "Russ Cox" <rsc@swtch.com>
To: "Fans of the OS Plan 9 from Bell Labs" <9fans@cse.psu.edu>
Subject: Re: [9fans] Interrupted alarm(2)
Date: Tue, 12 Dec 2006 23:13:07 -0500	[thread overview]
Message-ID: <ee9e417a0612122013g5e7b54c4g111519b647540c25@mail.gmail.com> (raw)
In-Reply-To: <68313859d562920ef34e99b7dc1a4046@plan9.jp>

Use the source, Luke!

>                 // back to our regularly scheduled programming...
>                 alarm(quantum == 0 ? 1 : quantum);
>         }
> I included that ugly last line on the off chance that taskfork() is
> called with less than a millisecond left and so alarm(0) will return
> 0.  Is this neccessary, or can alarm(0) not ever return 0?  Would it
> be better to write:

Certainly I would guess that alarm(0) returning 0 is a perfectly
reasonable thing to do, if the alarm was either imminent or
not scheduled in the first place.

System calls are implemented in /sys/src/9/port/sys*.c, by
functions named sysfoo for the call foo.  In this case sysalarm
just calls procalarm, which is in /sys/src/9/port/alarm.c.
Inspecting that, it is clear that alarm(0) can in fact return 0
in the two cases I just mentioned.  Perhaps more worrying,
it looks like it can also return a negative number if the alarm
time has passed without the alarm actually going off.
And it might not even be the right negative number.

I also don't see anything that keeps one from scheduling
alarms at times in the past, though they will just be treated
as happening as soon as possible (e.g., alarm(-100) looks
like it will cause an alarm to go off at the next clock tick).

It also looks like the delivery of alarms can be stopped by a
single rogue process that can manage to make its p->debug
qlock unlockable and then schedule a quick alarm to get to
the front of the alarm queue.  Making p->debug unlockable is
easy: one way is to arrange that the demand load fault caused
by memmove in case Qnote in procread causes i/o to a file
server that sits on the request.  Then there will be no more
alarms delivered anywhere in the system until that i/o finishes.
This may be less than ideal.

Then again, the whole idea of alarms is broken if you are
depending on them for very much at all, so the fact that the
implementation is also a little broken probably doesn't matter.

Russ


      reply	other threads:[~2006-12-13  4:13 UTC|newest]

Thread overview: 2+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2006-12-13  3:39 Joel Salomon
2006-12-13  4:13 ` Russ Cox [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=ee9e417a0612122013g5e7b54c4g111519b647540c25@mail.gmail.com \
    --to=rsc@swtch.com \
    --cc=9fans@cse.psu.edu \
    /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).