In article <
vilain-E8B125....@news.individual.net>,
Michael Vilain <vil...@NOspamcop.net> wrote:
> In article <n8aqei$g7q$
1...@reader1.panix.com>, ruben <
c...@iran.gov>
> wrote:
>
> > Is EOF is a signal, how is it propagated though the OS?
>
> The suspicious person in me wonders if this is an interview question or
> a homework question or the poster is a newbie.
>
> I've never heard this asked this way. This assumes that reading or
> writing past the End Of a File marker is a implemented as a signal when
> that's strictly up to the implementation.
Since he posted this in c.u.shell, I suspect he's asking about how
typing Control-d works to send EOF. He wonders if it's similar to the
way Control-c sends an interrupt signal.
Here's how it works (a bit simplified).
Normally, when the program calls read() on a terminal in "cooked" mode,
it doesn't return until the user types Return. But it also returns
immediately when the user types the EOF character (Control-d by default).
The way that programs tell when they've reached EOF on any stream is
that read() returns 0 characters. Because otherwise read() would stay
blocked until there's something available to return. But since the EOF
character forces read() to return immediately, typing it at the
beginning of the line will return 0 characters, and the application
interprets this as EOF.
A little known fact about this is that you can also type Control-d
anywhere on the line. That will cause the line to be sent to the
application, but it won't be treated as EOF. If the application is
reading whole lines, it should just buffer this partial line and call
read() again, because it doesn't consider a line to be input until it
gets a newline character. In particular, if read() was called because
the program uses stdio and called fgets(), stdio will do the buffering
and wait for the newline.
--
Barry Margolin,
bar...@alum.mit.edu
Arlington, MA
*** PLEASE post questions in newsgroups, not directly to me ***