Today, I discussed with my colleagues the role of the fflush function. I thought that the file system function of php should be built on the standard I/O library of the system. Therefore, the role of fflush is to fl the buffer of the standard I/O library, which is equivalent to the fflush function of the standard I/O library ....
Later I tracked the code and found that the results were quite different...
Let's talk about the result first:
1. when the file system functions (fopen, fwrite, fread, fseek, etc.) in php are applied to common files, they use open, write, read, seek and other internal system calls for processing, without passing through the standard I/O library.
2. The fflush function does not apply to common files.
Tracking process:
From ext/standard/file. c
PHP_NAMED_FUNCTION (php_if_fopen)
As the entry, find the following definition in main/streams/plain_wrapper.c:
PHPAPI php_stream_ops php_stream_stdio_ops = {
Php_stdiop_write, php_stdiop_read,
Php_stdiop_close, php_stdiop_flush,
"STDIO ",
Php_stdiop_seek,
Php_stdiop_cast,
Php_stdiop_stat,
Php_stdiop_set_option
};
This is the underlying implementation of the main file system functions applied to common files.
Take php_stdiop_flush (corresponding to the php user State fflush) as an example:
Static int php_stdiop_flush (php_stream * stream TSRMLS_DC)
{
Php_stdio_stream_data * data = (php_stdio_stream_data *) stream-> abstract;
Assert (data! = NULL );
/*
* Stdio buffers data in user land. By calling fflush (3), this
* Data is sent to the kernel using write (2). fsync 'ing is
* Something completely different.
*/
If (data-> file ){
Return fflush (data-> file );
}
Return 0;
}
While a common file is not set with the data-> file field during initialization (which can be seen during tracking the open process), data-> fd ....
Therefore, fflush will not be called. That is to say, fflush has no effect when it is used in common files.
Author: selfimip mail: lgg860911@yahoo.com.cn