[net.unix-wizards] Strange time

chris@eneevax.UUCP (12/03/83)

I think this is a bug, but maybe someone can claim a "reasonable" reason
for it:

If I write to a tty device and then close it, a stat 10 seconds later
usually does not show it to have been modified when I did the write+close,
but instead, 5 to 10 seconds later!  If I use stat() instead of time() I
get the "right" value (which then remains consistent).  Using sync() will
also make the value remain consistent (thus "time()" will be OK in the
program below).

I suspect there's something odd going on with the delayed I/O system here.

---------------------------- BUG DEMONSTRATER ----------------------------
#include <stdio.h>
#include <sys/types.h>
#include <sys/stat.h>

main () {
	FILE *fp;
	time_t shouldbe;
	struct stat was;
	char *tty, *ttyname ();

	tty = ttyname (0);
	fp = fopen (tty, "w");
	fprintf (fp, "testing\n");
	fclose (fp);
	time (&shouldbe);
	sleep (10);
	stat (tty, &was);
	printf ("was %d should be <= %d\n", was.st_mtime, shouldbe);
}
--
Chris Torek, UMCP, CSD
UUCP: {allegra,seismo,brl-bmd}!umcp-cs!chris
ARPA: chris@maryland		CSNet: chris@umcp-cs

naftoli@aecom.UUCP (12/06/83)

>> If I write to a tty device and then close it, a stat 10 seconds later
>> usually does not show it to have been modified when I did the write+close,
>> but instead, 5 to 10 seconds later!  If I use stat() instead of time() I
>> get the "right" value (which then remains consistent).  Using sync() will
>> also make the value remain consistent (thus "time()" will be OK in the
>> program below).


I took a look at the code to the stat system call and it seems that the
access, change, and mod times are taken straight off the disk as opposed
to the in core version. This answers why sync() clears up the confusion.

As to possible reasons for this, I don't have a clue.  It seems to me
that this should have been detected earlier since programs like
finger(1) will have the terminal idle times only correct as of the last sync().

By the way, the code I looked at was INteractive's System 3.  Probably
the same in standard System III and others also.

Robert Berlinger
Systems Support
{esquire,philabs,cucard}!aecom!naftoli

smb@ulysses.UUCP (12/08/83)

To save memory space, in-core i-nodes don't have modification/acces/change
times included.  Instead, they have bit-flags that tell sync() to update
the disk version of those fields.  Since sync() is normally invoked every
30 seconds, the times are usually "accurate enough".

Now -- stat() prefers to use the disk-resident copy of the i-node.  This
is proper for many reasons, not least of which is so that it can get at
those timestamps.  To ensure that the disk copy is current, stat calls sync()
first -- which updates the disk copy, and uses the time value as of when
sync() was called.