简体   繁体   English

errno 11 [EAGAIN]阅读(2)

[英]errno 11 [EAGAIN] from read(2)

I have some code that reads the serial port on my pinguin box. 我有一些代码读取我的pinguin盒子上的串口。 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. 代码运行很长时间没关系,但是如果我试着强调一下,即使压力完成后我也会不断地开始犯错误11(EAGAIN)。 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. 现在我想知道我误解了什么,从man 2中选择我可以理解select返回句柄中可用的字节数。

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. TGM是一种用于保存数据读取的结构。 OnSignal is just logging the signal. OnSignal只记录信号。 _note: ques _note:问

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. readPort就像,主要任务是“只是”将位和字节转换为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_ 现在我的失败在哪里:D我在注入时获得的输出,在大约30个消息之后(意味着在aprox之后.30个线程 - 有时多一点,有时少一点) FAILED读取端口(11)资源暂时不可用 _Signal 11_

Thank you for using some time on me, I am very grateful. 感谢您对我使用一段时间,我非常感激。

你有没有30个线程全部被select()阻止,当它变得可读时所有竞争对手都读取了 - 那么当缓冲区耗尽时,输家会给你EAGAIN吗?

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM