Home > Article > Backend Development > [php kernel]----Some situations when file system functions are used for ordinary files_PHP tutorial
Today, I discussed the role of the fflush function with my colleagues. I thought that the file system function of PHP should be built on the system's standard I/O library. Therefore, I arbitrarily believed that the function of fflush is to flush the buffer of the standard I/O library. , equivalent to the fflush function of the standard I/O library....
Later, I traced the code and found that the results were quite different...
Let’s talk about the results first:
1. When the file system functions (fopen, fwrite, fread, fseek, etc.) in PHP are applied to ordinary files, system calls such as open, write, read, seek, etc. are used internally for processing without going through the standard I/O library. .
2. When the fflush function is applied to ordinary files, it has no effect.
Tracking process:
From
in ext/standard/file.c
PHP_NAMED_FUNCTION(php_if_fopen)
As the entry point, finally 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 ordinary files
Take php_stdiop_flush (corresponding to fflush in PHP user mode) 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 send to the kernel using write(2). fsync'ing is
* something completely different.
*/
If (data->file) {
return fflush(data->file);
}
Return 0;
}
During the initialization process of ordinary files (you can see it when tracking the open process), the data->file field is not set, but data->fd....
Therefore, fflush will not be called here, which means that when fflush is used on ordinary files, it will have no effect.
author: selfimpr mail: lgg860911@yahoo.com.cn