basic C question

fcsoft-3Emkkp+1Olsmp8TqCH86vg at public.gmane.org fcsoft-3Emkkp+1Olsmp8TqCH86vg at public.gmane.org
Mon Jul 12 19:54:28 UTC 2004


I've been struggling off and on with problems of this nature for a while.

Essentially the problem is how to detect that a file descriptor which was 
previously successfully opened no longer points to a valid inode.

Here is some code which uses a standard file which illustrates quite simply  
that write() will not fail when the inode is removed after a successful 
open().

Compile this code and run it.      It will write "go1" and "go2" into a file 
called bob.dat separated by a sleep of 20 sec.

Repeat the test,  but after the first write go to another console and remove 
the bob.dat file.      The second write will still not return an error.    
ie. file it is supposedly writing to is gone but the write() apparently quite 
happily succeeds.

The question remains how can we verify the veracity of the file descriptor we 
are about to write to before we actually allow the write to "succeed" 
regardless.

Any help appreciated.


=====================
#include <stdio.h>
#include <unistd.h>
#include <stdlib.h>
#include <fcntl.h>

main()
{
int fd;
char msg[80];
int rc;

fd=open("./bob.dat", O_RDWR | O_CREAT | O_TRUNC);

sprintf(msg,"go1\n");

rc=write(fd,msg,strlen(msg));

printf("1st write rc=%d\n",rc);

sleep(20);

sprintf(msg,"go2\n");

rc=write(fd,msg,strlen(msg));

printf("2nd write rc=%d\n",rc);
close(fd);

}
--
The Toronto Linux Users Group.      Meetings: http://tlug.ss.org
TLUG requests: Linux topics, No HTML, wrap text below 80 columns
How to UNSUBSCRIBE: http://tlug.ss.org/subscribe.shtml





More information about the Legacy mailing list