I have some code that reads the serial port on my pinguin box. Code is like:
while ( 1 )
if ( select( handle + 1, &h, NULL, NULL, &tm ) > 0 )
{
if( read( handle, &msg, 1 ) > 0 )
{
... tips and trixes
}
if ( gotWhatINeed ) break;
Code runs for pretty long time okay, but if I try to stress it a little I start to get errno 11 (EAGAIN) constantly, even after the stress completed. And now I am wondering what I misunderstand, from man 2 select I can understand select returns the number of bytes availible from the handle.
Maybe it is of interest that the code always runs in a detached thread.
Based on the comments, I now post more details of the code.
In main I have
pthread_mutex_t mut = PTHREAD_MUTEX_INITIALIZER;
int main ( int argc, char **argv )
{
pthread_t scan_01
signal( 11, OnSignal );
pthread_mutex_init( &mut, NULL );
.....
pthread_create(&scan_01, NULL, (void *)readDevice, NULL);
pthread_detach(scan_01);
And the method where the device is read. TGM is a structure to hold the data read. OnSignal is just logging the signal. _note: ques
void *readDevice(void)
{
int r;
char b[256];
struct TGM tgm;
pthread_t new, self;
pthread_mutex_lock( &mut );
self = pthread_self( );
while( 1 )
{
FD_ZERO( &out );
FD_SET( fOut, &out );
tm.tv_sec = LOOP_DELAY;
tm.tv_usec = com_scan_time;
if ( select( fOut + 1, & out, NULL, NULL, &tm ) > 0 )
{
r = readPort( fOut, 10, b, 1 );
pthread_mutex_unlock( &mut );
pthread_create( &new, NULL, (void *)readDevice, NULL );
pthread_detach( new );
iThreads++;
...
break;
}
}
self = pthread_self();
iThreads--;
pthread_exit( & self );
readPort is like, main task is "just" to translate bits and bytes to a TGM.
int readPort(const int handle, char terminator, char b[256], int crc)
{
char msg;
fd_set h;
struct timeval tm;
do
{
FD_ZERO( &h );
FD_SET( handle, &h );
tm.tv_sec = LOOP_DELAY;
tm.tv_usec = com_scan_time;
if ( select( handle + 1, &h, NULL, NULL, &tm ) > 0 )
{
if( read( handle, &msg, 1 ) > 0 )
{
if( msg == 3 ) // marks end of message
....
}
else
{
log( ERRLOG, "FAILED to read port (%d) %s\n",
errno,
strerror( errno ) );
return -1;
}
Now where is my failure :D The output I get when injecting, after some 30 messages ( means after aprox. 30 threads - sometimes a little more, and sometimes a little less is ) FAILED to read port (11) Resource temporarily unavailable _Signal 11_
Thank you for using some time on me, I am very grateful.