通过编程获取Linux文件系统使用的详细信息

上传人:xmg****18 文档编号:118759641 上传时间:2019-12-25 格式:DOC 页数:11 大小:38.50KB
返回 下载 相关 举报
通过编程获取Linux文件系统使用的详细信息_第1页
第1页 / 共11页
通过编程获取Linux文件系统使用的详细信息_第2页
第2页 / 共11页
通过编程获取Linux文件系统使用的详细信息_第3页
第3页 / 共11页
通过编程获取Linux文件系统使用的详细信息_第4页
第4页 / 共11页
通过编程获取Linux文件系统使用的详细信息_第5页
第5页 / 共11页
点击查看更多>>
资源描述

《通过编程获取Linux文件系统使用的详细信息》由会员分享,可在线阅读,更多相关《通过编程获取Linux文件系统使用的详细信息(11页珍藏版)》请在金锄头文库上搜索。

1、. . . . .通过编程获取Linux文件系统使用的详细信息 去年的操作系统课程设计,我做了一个基于linux到任务管理器。大部分需要到系统信息都是从linux下的一个伪文件系统/proc中读出的。大家都知道,此文件系统只存在内存当中,而不占用外存空间。它以文件系统的方式为访问系统内核数据的操作提供接口。用户和应用程序可以通过proc得到系统的信息,并可以改变内核的某些参数。比如说我想看一下系统cpu的信息,由于其信息存于/proc/cpuinfo中,所以我们在终端用输入如下命令:cat/proc/cpuinfo即可查看要查看到信息,如果要使用其中到某项信息则通过文件操作读出即可(当然要有一

2、些处理)。其他像内存、进程等各项信息都能按照这种操作得到。可唯独在获取文件系统使用信息时遇到了问题,先来看一下我们这个项目中要做出的一个类似页面:500)this.width=500; height=478 width=514 border=0图中所显示的设备、目录等信息既是我们希望从系统中得到到信息,可/proc目录中记录文件信息的文件partitions记录的信息于我们想要得到到信息相差太远,changyanchangyan-fly:$ cat /proc/partitionsmajor minor #blocks name 8 0 244198584 sda 8 1 38427448 s

3、da1 8 2 1 sda2 8 5 63898506 sda5 8 6 61440561 sda6 8 7 10241406 sda7 8 8 46138648 sda8 8 9 21430678 sda9 8 10 650601 sda10 8 11 1967931 sda11看到了吧,里面的信息只有这几项,显然不行。后来,我又以为所需到信息可以从/dev下的文件中查找,可那里面到文件都是不可读的,我不停的在/proc下分析每个文件内容,可都没有我想要到内容,这个过程非常郁闷。可问题昨天晚上还是解决了,我通过系统调用实现的,在此,我将代码公开,一是让大家看一下,再一个就是如果哪位仁兄有能在

4、文件中直接读出的方法,希望能告知一二,在此不胜感激。下面这个程序是我和同学不断讨论后设计出的一个非常巧妙的解决方案,具体代码如下:/读出系统文件系统的使用情况,并打印输出#include<stdio.h>#include<stdlib.h>#include<string.h>#include <sys/vfs.h> #include <mntent.h>typedef struct char device 256 ; /设备名称 char mntpnt 256 ; /目录 char type256; /类型 long blocks;

5、/总容量(单位为:MB) long bfree; /空闲容量(单位为:MB) long bused; /已用容量(单位为:MB) long available_disk; /可用容量 int bused_percent; /使用百分比(使用率乘以100) DiskInfo;/所使用到到两个系统调用结构体/struct statfs / long f_type; /* type of file system (see below) */ long f_bsize; /* optimal transfer block size */ long f_blocks; /* total data blo

6、cks in file system */ long f_bfree; /* free blocks in fs */ long f_bavail; /* free blocks avail to non-superuser */ long f_files; /* total file nodes in file system */ long f_ffree; /* free file nodes in fs */ fsid_t f_fsid; /* file system id */ long f_namelen; /* maximum length of filenames */ ;/ s

7、truct mntent / char *mnt_fsname; /* name of mounted file system */ char *mnt_dir; /* file system path prefix */ char *mnt_type; /* mount type (see mntent.h) */ char *mnt_opts; /* mount options (see mntent.h) */ int mnt_freq; /* dump frequency in days */ int mnt_passno; /* pass number on parallel fsc

8、k */ ;int main() DiskInfo *disk_info; struct statfs fs_info; struct mntent *mnt_info; FILE *fh; float percent; /用于存储设备使用百分比 long total_free=0; /用于存储linux下尚余磁盘空间 if ( ( fh = setmntent( /etc/mtab, r ) ) = NULL ) printf( Cannot open /etc/mtab!n ); return -1; while ( ( mnt_info = getmntent( fh ) ) != NU

9、LL ) if ( statfs( mnt_info->mnt_dir, &fs_info ) < 0 ) continue; if ( ( disk_info = (DiskInfo *)malloc( sizeof( DiskInfo ) ) ) = NULL ) continue; /通过判断找到要找的格式 if ( strcmp( mnt_info->mnt_type, proc ) && strcmp( mnt_info->mnt_type, devfs ) && strcmp( mnt_info->mnt_typ

10、e, usbfs ) && strcmp( mnt_info->mnt_type, sysfs ) && strcmp( mnt_info->mnt_type, tmpfs ) && strcmp( mnt_info->mnt_type, devpts )&& strcmp( mnt_info->mnt_type, fusectl ) && strcmp( mnt_info->mnt_type, debugfs )&& strcmp( mnt_info->mnt_

11、type, binfmt_misc )&& strcmp( mnt_info->mnt_type, fuse.gvfs-fuse-daemon )&& strcmp( mnt_info->mnt_type, securityfs ) ) if ( fs_info.f_blocks != 0 ) percent = ( ( (float)fs_info.f_blocks - (float)fs_info.f_bfree ) * 100.0/ (float)fs_info.f_blocks ); else percent = 0; else continue; /将系统中的各项信息存于结构体相对应到变量中 strcpy(disk_info->type,mnt_info->mnt_type); strcpy(disk_info->device,mnt_info->mnt_fsname); strcpy(disk_info->mntpnt,mnt_info->mnt_dir); long block_size=fs_info.f_bsize/1024; disk_info->blocks = fs_info.f_blocks*blo

展开阅读全文
相关资源
相关搜索

当前位置:首页 > 大杂烩/其它

电脑版 |金锄头文库版权所有
经营许可证:蜀ICP备13022795号 | 川公网安备 51140202000112号