案例代碼
#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");
}
現(xiàn)象
fflush(NULL) 卡死.
pstack 顯示卡在 __lll_lock_wait_private
調(diào)用棧信息:
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.
簡(jiǎn)單來(lái)說(shuō)就是 fflush
會(huì)刷新打開(kāi)
的fd stream.
如果入?yún)⑹?code>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.
簡(jiǎn)單來(lái)說(shuō)佛析,fgets
會(huì)從給定的fd stream 讀取數(shù)據(jù)芥丧,并存入buffer.
但這里沒(méi)有說(shuō)到的一點(diǎn)是:fgets
is a blocking
read.
它會(huì)一直阻塞直到fd stream 有數(shù)據(jù)饵隙。
所以如果當(dāng)fd steam 沒(méi)有數(shù)據(jù)宵喂,又沒(méi)有結(jié)束,比如fd是一個(gè)還沒(méi)傳數(shù)據(jù)的pipe
蜜氨,那么read
這個(gè)動(dòng)作一直會(huì)阻塞在這個(gè)fd 上催享。
然后傅寡,當(dāng)fflush
嘗試去刷新這個(gè)fd時(shí)英岭,會(huì)因?yàn)槟貌坏芥i(鎖一直被read
拿著)而一直阻塞湾盒。
解決辦法
應(yīng)該盡量避免在代碼中以阻塞的方式讀fd stream, 除非我們確定當(dāng)前一定有數(shù)據(jù)。
可以通過(guò)select
, poll
, epoll
等異步I/O 來(lái)等到fd stream 有可用數(shù)據(jù)后诅妹,再去讀历涝,避免阻塞。
修改后的代碼
#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");
}