fflush(NULL) deadlock (C/C++)

案例代碼

#include <stdio.h>
#include <time.h>
#include <pthread.h>
#include <unistd.h>

void *func(void *arg)
{
  char str[50];
  FILE* fp = fdopen((int*)arg, "r");
  printf("fgets\n");
  fgets(str, 20, fp);
  exit(-1);
}

void main() {
  pthread_t ntid;
  int pipefd[2] = {0};

  if (pipe(pipefd) == -1)
  {
    perror("pipe");
  }
  pthread_create(&ntid, NULL, func, (void*)pipefd[0]);
  sleep(1);
  printf("fflush\n");
  fflush(NULL);
  close(pipefd[0]);
  close(pipefd[1]);
  printf("success\n");
}

現象

fflush(NULL) 卡死.
pstack 顯示卡在 __lll_lock_wait_private

調用棧信息:

Thread 2 (Thread 0x7ffff7dcf700 (LWP 8812)):
#0  0x00007ffff7ec1f84 in read () from /lib64/libc.so.6
#1  0x00007ffff7e51c98 in __GI__IO_file_underflow () from /lib64/libc.so.6
#2  0x00007ffff7e52dd6 in _IO_default_uflow () from /lib64/libc.so.6
#3  0x00007ffff7e4622a in _IO_getline_info () from /lib64/libc.so.6
#4  0x00007ffff7e4523f in fgets () from /lib64/libc.so.6
#5  0x000000000040122e in p ()
#6  0x00007ffff7fa14a7 in start_thread () from /lib64/libpthread.so.0
#7  0x00007ffff7ed13e3 in clone () from /lib64/libc.so.6
Thread 1 (Thread 0x7ffff7dd0740 (LWP 8811)):
#0  0x00007ffff7e551cb in __lll_lock_wait_private () from /lib64/libc.so.6
#1  0x00007ffff7e5382c in _IO_flush_all_lockp () from /lib64/libc.so.6
#2  0x00000000004011eb in thr_fn ()
#3  0x00000000004012b6 in main ()

原因分析

先看看 fflush 的官方描述

If the given stream was open for writing (or if it was open for updating and the last i/o operation was an output operation) any unwritten data in its output buffer is written to the file.
If stream is a null pointer, all such streams are flushed.

簡單來說就是 fflush 會刷新打開的fd stream.
如果入參是NULL, 則刷新所有的fd sream.

再看看fgets

Reads characters from stream and stores them as a C string into str until (num-1) characters have been read or either a newline or the end-of-file is reached, whichever happens first.
A newline character makes fgets stop reading, but it is considered a valid character by the function and included in the string copied to str.

簡單來說,fgets 會從給定的fd stream 讀取數據,并存入buffer.
但這里沒有說到的一點是:fgets is a blocking read.
它會一直阻塞直到fd stream 有數據。

所以如果當fd steam 沒有數據,又沒有結束,比如fd是一個還沒傳數據的pipe,那么read這個動作一直會阻塞在這個fd 上。
然后,當fflush 嘗試去刷新這個fd時,會因為拿不到鎖(鎖一直被read拿著)而一直阻塞。

解決辦法

應該盡量避免在代碼中以阻塞的方式讀fd stream, 除非我們確定當前一定有數據。
可以通過select, poll, epoll等異步I/O 來等到fd stream 有可用數據后,再去讀,避免阻塞。

修改后的代碼

#include <stdio.h>
#include <time.h>
#include <pthread.h>
#include <unistd.h>
#include <poll.h>

void *func(void *arg)
{
  char str[50];
  FILE* fp = fdopen((int*)arg, "r");
  struct pollfd readFds = {0};
  readFds.fd = (int*)arg;
  readFds.events = POLLIN;

  printf("poll\n");
  if (0 < poll(&readFds, 1, -1))
  {
    printf("fgets\n");
    fgets(str, 20, fp);
  }
  exit(-1);
}

void main() {
  pthread_t ntid;
  int pipefd[2] = {0};

  if (pipe(pipefd) == -1)
  {
    perror("pipe");
  }
  pthread_create(&ntid, NULL, func, (void*)pipefd[0]);
  sleep(1);
  printf("fflush\n");
  fflush(NULL);
  close(pipefd[0]);
  close(pipefd[1]);
  printf("success\n");
}
?著作權歸作者所有,轉載或內容合作請聯系作者
平臺聲明:文章內容(如有圖片或視頻亦包括在內)由作者上傳并發布,文章內容僅代表作者本人觀點,簡書系信息發布平臺,僅提供信息存儲服務。

推薦閱讀更多精彩內容

  • pyspark.sql模塊 模塊上下文 Spark SQL和DataFrames的重要類: pyspark.sql...
    mpro閱讀 9,504評論 0 13
  • 函數調用 Built-in Functions abs(x) Return the absolute value ...
    叫我七夜閱讀 1,189評論 0 0
  • 大綱 一.Socket簡介 二.BSD Socket編程準備 1.地址 2.端口 3.網絡字節序 4.半相關與全相...
    VD2012閱讀 2,428評論 0 5
  • 生活阿,其實沒有我們想象的那么好,它是很復雜的,也有很多傷心。那些活得很好的人從來就不是因為他們的生活有多美好,...
    nia你阿閱讀 278評論 0 1
  • 屏讀 Screening 在古代文化都是圍繞言語的。我們曾經是言語之民(People of the word)。大...
    loyanda閱讀 289評論 0 2