In base alla pagina man per read (2), restituisce zero solo quando EOF viene raggiunto.Può leggere (2) restituire zero quando non è in EOF?
Tuttavia, sembra che questo non sia corretto e che a volte può restituire zero, forse perché il file non è pronto per essere letto ancora? Dovrei chiamare select() per vedere se è pronto prima di leggere un file dal disco?
noti che nBytes è: 1.445.888
Alcuni codice di esempio:
fd_set readFdSet;
timeval timeOutTv;
timeOutTv.tv_sec = 0;
timeOutTv.tv_usec = 0;
// Let's see if we'll block on the read.
FD_ZERO(&readFdSet);
FD_SET(fd, &readFdSet);
int selectReturn = ::select(fd + 1, &readFdSet, NULL, NULL, &timeOutTv);
if (selectReturn == 0) {
// There is still more to read.
return false; // But return early.
} else if (selectReturn < 0) {
clog << "Error: select failure: " << strerror(errno) << endl;
abort();
} else {
assert(FD_ISSET(fd, &readFdSet));
try {
const int bufferSizeAvailable = _bufferSize - _availableIn;
if (_availableIn) {
assert(_availableIn <= _bufferSize);
memmove(_buffer, _buffer + bufferSizeAvailable, _availableIn);
}
ssize_t got = ::read(fd, _buffer + _availableIn, bufferSizeAvailable);
clog << " available: " << bufferSizeAvailable << " availableIn: "
<< _availableIn << " bufferSize: " << _bufferSize << " got "
<< got << endl;
return got == 0;
} catch (Err &err) {
err.append("During load from file.");
throw;
}
}
L'uscita legge (quando non riesce senza dati letti):
available: 1445888 availableIn: 0 bufferSize: 1445888 got: 0
Questo è in esecuzione su CentOS4 32 bit come macchina virtuale utilizzando VMware Server 1.0.10. Il file system in lettura è locale alla macchina virtuale. Il computer host è Windows Server 2008 a 32 bit.
L'uname -a dice:
Linux q-centos4x32 2.6.9-89.0.25.ELsmp #1 SMP Thu May 6 12:28:03 EDT 2010 i686 i686 i386 GNU/Linux
mi accorgo che il link indicato di seguito http://opengroup.org/onlinepubs/007908775/xsh/read.html stati:
The value returned may be less than nbyte if the number of bytes left in the file is less than nbyte, if the read() request was interrupted by a signal...
If a read() is interrupted by a signal before it reads any data, it will return -1 with errno set to [EINTR].
If a read() is interrupted by a signal after it has successfully read some data, it will return the number of bytes read.
Così, forse io sono sempre un segnale di interruzione della lettura e quindi il valore restituito è zero a causa di un bug o pensa che siano stati letti zero byte?
Non riesco a pensare perché restituire 0 quando non in EOF. Puoi fornire un esempio specifico di quando ciò accade? –
Si verifica in circa uno su 50.000 tentativi identici. – WilliamKF
Non è necessario selezionare select() sul descrittore di file. (Perché diavolo nel terzo millennio usi ancora select() e non il sondaggio() ??) I file sono sempre leggibili - non sono socket o dispositivi, non possono bloccare.Un po 'più di informazioni sul sistema - distro, versione del kernel, file system usato - potrebbero aiutare. – Dummy00001