MESSAGE
DATE | 2016-02-04 |
FROM | Ruben Safir
|
SUBJECT | Subject: [Hangout-NYLXS] EOF
|
Path: reader1.panix.com!panix!goblin3!goblin1!goblin.stu.neva.ru!eternal-septembe r.org!feeder.eternal-september.org!mx02.eternal-september.org!barmar.motzarella.org!.POSTED!not-for-mail From: Barry Margolin Newsgroups: comp.unix.shell Subject: Re: EOF Date: Wed, 27 Jan 2016 23:29:30 -0500 Organization: A noiseless patient Spider Lines: 45 Message-ID: References: Injection-Info: barmar.motzarella.org; posting-host="78fb7125a45724f15e21604c94a7d968"; logging-data="24278"; mail-complaints-to="abuse-at-eternal-september.org"; posting-account="U2FsdGVkX19tH6Z8AqJjO/C32VQ8b5sh" User-Agent: MT-NewsWatcher/3.5.3b3 (Intel Mac OS X) Cancel-Lock: sha1:qZdvUav61qxBhIMh9M1e48I6kOU= Xref: panix comp.unix.shell:260721
In article , Michael Vilain wrote:
> In article , ruben > 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.
barmar-at-alum.mit.edu -- So many immigrant groups have swept through our town that Brooklyn, like Atlantis, reaches mythological proportions in the mind of the world - RI Safir 1998 http://www.mrbrklyn.com
DRM is THEFT - We are the STAKEHOLDERS - RI Safir 2002 http://www.nylxs.com - Leadership Development in Free Software http://www2.mrbrklyn.com/resources - Unpublished Archive http://www.coinhangout.com - coins! http://www.brooklyn-living.com
Being so tracked is for FARM ANIMALS and and extermination camps, but incompatible with living as a free human being. -RI Safir 2013 _______________________________________________ hangout mailing list hangout-at-nylxs.com http://www.nylxs.com/
|
|