この質問は、 Bad file descriptorと似ていますが、まったく同じではありません。これが「悪い質問」であることはわかっていますが(「ローカライズされすぎている」可能性があります)、理解できず、今ではアイデアがありません。
序章
75 の他のスレッドを開始するマネージャー スレッドがあります。これらの各スレッドは多くのことを行うため、関連するスレッドのみを説明します。
注意: いくつかのスレッド (たとえば 3、5、または 10) のみを開始した場合、このエラーは表示されません。これはマルチスレッドの問題だと思いますが、そうではないようです..そして、次のセクションでその理由がわかります。
したがって、次の 2 つのケースでは、このエラーが表示されることがありBad file descriptor
ます。
ケース1
エラーが表示されますTinyXML
すべてのスレッドで必要な xml ファイルがあります。これらのスレッドはすべてTinyXML
、ファイルの解析に使用されます。これらのスレッドはすべて、このファイルを読み取り専用で使用します。(これは最適化できることは知っていますが、何でも)。
したがって、Bad file descriptor
エラーの原因となるコードは次のとおりです。
// ...
// NOTE: this is LOCAL, other threads do NOT have access to it
TiXmlDocument doc;
doc.LoadFile( filename );
// and here's the LoadFile:
bool TiXmlDocument::LoadFile( const char* _filename, TiXmlEncoding encoding )
{
//...
FILE* file = fopen( value.c_str (), "rb" );
if ( file )
{
// this IS executed, so file is NOT NULL for sure
bool result = LoadFile( file, encoding );
//...
}
//...
}
bool TiXmlDocument::LoadFile( FILE* file, TiXmlEncoding encoding )
{
// ...
long length = 0;
fseek( file, 0, SEEK_END );
// from the code above, we are SURE that file is NOT NULL, it's valid, but
length = ftell( file ); // RETURNS -1 with errno: 9 (BAD FILE DESCRIPTOR)
// how is this possible, as "file" is not NULL and it appears to be valid?
// ...
}
ケース 2
これはもう少し複雑です。戻り値のチェックを削除しましたが、実際のコードにはあるので問題ありません
int hFileR = open( sAlarmFileName.c_str(), O_CREAT | O_RDONLY,
S_IRUSR | S_IWUSR | S_IRGRP | S_IWGRP | S_IROTH );
// hFileR is > 0 for sure, so success
flock( hFileR, LOCK_EX ) /* the result is > 0 for sure, so success*/
// read the file into a string
while( (nRes = read(hFileR, BUFF, MAX_RW_BUFF_SIZE)) > 0 ) // ...
//Write new data to file: reopen/create file - write and truncate mode
int hFileW = open( sAlarmFileName.c_str(),
O_CREAT | O_WRONLY | O_TRUNC, S_IRUSR |
S_IWUSR | S_IRGRP | S_IWGRP | S_IROTH );
// hFileW is > 0 for sure, so success
do
{
int nWrtRes = write( hFileW,
szText + nBytesWritten, nSize - nBytesWritten );
// nWrtRes is always >= 0, so success
nBytesWritten += nWrtRes;
}
while( nSize > nBytesWritten );
close( hFileW ); // this one is successful too
if( flock(hFileR, LOCK_UN) == -1 )
{
// THIS FAILS and executes _Exit( FAILURE );
}
if( close( hFileR ) < 0 )
{
// if the previous one do not fail, this one is successful too
}
長い質問で申し訳ありません。何か案は?