home *** CD-ROM | disk | FTP | other *** search
- Path: sparky!uunet!think.com!barmar
- From: barmar@think.com (Barry Margolin)
- Newsgroups: comp.unix.questions
- Subject: Re: cat a > a , getting it straight
- Date: 17 Dec 1992 20:18:23 GMT
- Organization: Thinking Machines Corporation, Cambridge MA, USA
- Lines: 34
- Message-ID: <1gqnafINNn8e@early-bird.think.com>
- References: <1992Dec17.020805.28043@news.acns.nwu.edu> <1gp7lsINNlok@early-bird.think.com> <1992Dec17.074018.3226@news.acns.nwu.edu>
- NNTP-Posting-Host: telecaster.think.com
-
- In article <1992Dec17.074018.3226@news.acns.nwu.edu> navarra@casbah.acns.nwu.edu (John Navarra) writes:
- >[riemann:62] ~/tmp -> cat a | cat > a
- >[riemann:63] ~/tmp -> cat a
- >file a
- >
- >But, this output is not predictable. Sometimes, a was clobbered several
- >times in a row. Sometimes, a was preserved several times in a row. Once,
- >I even got this:
-
- The shell runs both parts of a pipeline concurrently. Sometimes the "cat
- a" manages to run to completion before "cat > a" gets started, so it can
- read the file before it gets clobbered. Other times the "cat > a" starts
- first, so "cat a" sees an empty input file.
-
- >[casbah:58] ~/tmp -> cat a | cat > a
- >cat: write error: Bad address
-
- In this case, "cat > a" started in the middle of "cat a". On Suns, the cat
- command works by mmap()ing the input file if it's an ordinary file, and
- then using that memory as the output buffer in a write() call. What
- happened here is that the file was truncated between the mmap() and the
- write(), so the write() tried to access memory that was no longer
- allocated.
-
- >Somebody is not doing their job right. I couldn't reproduce this on non-Sun
- >machines though.
-
- Depends on what you consider "right". You're not supposed to be able to do
- this, so anything that happens is right.
- --
- Barry Margolin
- System Manager, Thinking Machines Corp.
-
- barmar@think.com {uunet,harvard}!think!barmar
-