2013-04-11 36 views
1

我有一個通過termios串行API使用的FTDI USB串行設備。我設置了端口,以便它在半秒內(通過使用VTIME參數)在read()調用上超時,並且這在Linux和FreeBSD上都可以工作。然而,在OpenBSD 5.1中,當沒有數據可用時,read()調用會永遠阻塞(見下文)。我期望在500ms後read()返回0。OpenBSD串行I/O:即使使用termios VTIME進行設置,-lpthead也會永久保留read()塊?

任何人都可以想到termios API在OpenBSD下表現不同的原因,至少就超時功能而言?

編輯:無超時問題是由對pthread鏈接引起的。無論我是否實際使用任何pthread,mutexes等,只需鏈接到該庫,都會導致read()永久阻塞,而不是基於VTIME設置進行超時。同樣,這個問題只在OpenBSD - Linux和FreeBSD上按預期工作。

if ((sd = open(devPath, O_RDWR | O_NOCTTY)) >= 0) 
{ 
    struct termios newtio; 
    char input; 

    memset(&newtio, 0, sizeof(newtio)); 

    // set options, including non-canonical mode 
    newtio.c_cflag = (CREAD | CS8 | CLOCAL); 
    newtio.c_lflag = 0; 

    // when waiting for responses, wait until we haven't received 
    // any characters for 0.5 seconds before timing out 
    newtio.c_cc[VTIME] = 5; 
    newtio.c_cc[VMIN] = 0; 

    // set the input and output baud rates to 7812 
    cfsetispeed(&newtio, 7812); 
    cfsetospeed(&newtio, 7812); 

    if ((tcflush(sd, TCIFLUSH) == 0) && 
     (tcsetattr(sd, TCSANOW, &newtio) == 0)) 
    { 
    read(sd, &input, 1); // even though VTIME is set on the device, 
         // this read() will block forever when no 
         // character is available in the Rx buffer 
    } 
} 
+0

您使用的是-pthread還是-lpthread? – ramrunner

+0

對不起,我只是再次看到標題;) – ramrunner

+0

你可以嘗試OpenBSD 5.2,我們切換到默認地址?在我的5.3系統上,你的例子不會阻塞。實測值/ AMD64。 – ramrunner

回答

0

從termios的手冊頁:

Another dependency is whether the O_NONBLOCK flag is set by open() or 
fcntl(). If the O_NONBLOCK flag is clear, then the read request is 
blocked until data is available or a signal has been received. If the 
O_NONBLOCK flag is set, then the read request is completed, without 
blocking, in one of three ways: 

     1. If there is enough data available to satisfy the entire 
      request, and the read completes successfully the number of 
      bytes read is returned. 

     2. If there is not enough data available to satisfy the entire 
      request, and the read completes successfully, having read as 
      much data as possible, the number of bytes read is returned. 

     3. If there is no data available, the read returns -1, with errno 
      set to EAGAIN. 

您可以檢查是否是這樣? 歡呼聲。

編輯:OP將問題追溯到與導致讀取功能阻塞的pthread鏈接。通過升級到OpenBSD> 5.2,這個問題通過改變新的線程實現作爲openbsd上的默認線程庫來解決。 [email protected] EuroBSD2012 slides

+0

我發現我的無超時問題是由於鏈接到pthread庫(我需要)造成的。我創建了一個獨立的測試程序,只有上面顯示的代碼,並且超時只在構建它時才起作用_without_與pthread鏈接(無論我是否實際使用任何pthread設備)。爲什麼會這樣? – Colin

相關問題