- 论坛徽章:
- 2
|
回复 3# carllai
回复 4# Shell_HAT
回复 11# zhaopingzi
回复 14# wsxedcer
本来想着问题重现比较麻烦,所以一直没有尝试,今天发现,我本来以为最笨的方法——是用文件指针直接写数据的方法反而是最方便的方法,附上最简单的源码:
- #include <iostream>
- #include <stdio.h>
- #include <string.h>
- using namespace std;
- int main(int argc,char *argv[])
- {
- FILE *fp;
- fp = fopen("df.log","wb+");
- if (NULL == fp)
- {
- cout<<"打开文件失败!"<<endl;
- return -1;
- }
- char szStr[1024*1024*1024] = {0};
- memset(szStr,1,sizeof(szStr));
- while(1)
- {
- fputs(szStr,fp);
- sleep(2);
- }
- int iRet = fclose(fp);
- if (0 != iRet)
- {
- cout<<"文件关闭失败!"<<endl;
- return -1;
- }
- return 0;
- }
复制代码 测试方法:
1、使用上述源码,生成可执行文件
2、通过df和du查看某分区的剩余空间和使用空间,例如:
[billing_dx@bmcs2 test]$df -h /tmp/
Filesystem Size Used Avail Use% Mounted on
/dev/mapper/rootvg-lvtmp
4.0G 139M 3.7G 4% /tmp
[billing_dx@bmcs2 test]$du -sh /tmp/
2.4M /tmp/
3、运行可可执行文件
4、删除生成的日志文件(我的也就是df.log)
5、重新使用df和du查看/tmp分区剩余空间和使用空间
[billing_dx@bmcs2 test]$df -h /tmp/
Filesystem Size Used Avail Use% Mounted on
/dev/mapper/rootvg-lvtmp
4.0G 3.8G 0 100% /tmp
[billing_dx@bmcs2 test]$du -sh /tmp/
2.4M /tmp/
注:通过上面,可以看到/tmp分区使用率已经达到了100% ,但是,该分区下查看文件占用空间依旧是2.4M
5、找到可执行文件对应进程,kill掉
6、重新使用df和du查看/tmp分区剩余空间和使用空间
[billing_dx@bmcs2 test]$df -h /tmp/
Filesystem Size Used Avail Use% Mounted on
/dev/mapper/rootvg-lvtmp
4.0G 139M 3.7G 4% /tmp
[billing_dx@bmcs2 test]$du -sh /tmp/
2.4M /tmp/
通过以上方法,就可以证明:当df和du统计空间的区别了:
The du user command gives the number of kilobytes contained in all files and,recursively, directories within each specified directory or file (filename).
The df user command displays the following information:
amount of disk space occupied by currently mounted file systems
the amount of used and available space
how much of the file system's total capacity has been used.
【扩展阅读:http://bbs.chinaunix.net/forum.php?mod=viewthread&tid=465673】
即du计算的文件的大小,而df计算的磁盘空间的使用和剩余空间。如果所有的文件指针都被正常的释放的话,两种方式获取的值不会有太大区别。
就像上面的例子,在我没有删除"df.log"文件之前,使用df和du查看/tmp分区剩余空间和使用空间如下:
[billing_dx@bmcs2 test]$df -h /tmp/
Filesystem Size Used Avail Use% Mounted on
/dev/mapper/rootvg-lvtmp
4.0G 139M 3.7G 4% /tmp
[billing_dx@bmcs2 test]$du -sh /tmp/
3.5G /tmp/
注:如果是和其他人共用一台服务器的话,一定要注意测试的时候,不要影响到其他人,毕竟,占用的分区立马升到了100%,有可能会影响到其他人的。
最后,感谢大家对这个问题的回答,谢谢~
|
|