当前位置: 技术问答>linux和unix
如何在C程序中获得linux下硬盘的使用率?
来源: 互联网 发布时间:2016-03-23
本文导语: 我想在程序中通过一个线程每隔1分钟获取一次linux系统的硬盘使用率,然后根据使用率来做一些其他处理?我利用df -lh系统调用的方法来获取,可是每次程序执行完这个命令后就退出了,这个问题怎么解决啊,除此...
我想在程序中通过一个线程每隔1分钟获取一次linux系统的硬盘使用率,然后根据使用率来做一些其他处理?我利用df -lh系统调用的方法来获取,可是每次程序执行完这个命令后就退出了,这个问题怎么解决啊,除此之外还有更好的方法吗?
请GGJJ尽量说的详细点,谢谢了!
请GGJJ尽量说的详细点,谢谢了!
|
具体的磁盘利用率不好写。
你使用df 只是查看的文件系统的利用率。还有你使用df调用,效率有点低。
可以用这些函数来调用
int statfs(const char *path, struct statfs *buf);
int fstatfs(int fd, struct statfs *buf);
struct statfs {
long f_type; /* type of filesystem (see below) */
long f_bsize; /* optimal transfer block size */
long f_blocks; /* total data blocks 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 */
};
int fstatvfs(int fildes, struct statvfs *buf);
int statvfs(const char *restrict path, struct statvfs *restrict buf);
struct statvfs {
unsigned long f_bsize; /* file system block size */
unsigned long f_frsize; /* fragment size */
fsblkcnt_t f_blocks; /* size of fs in f_frsize units */
fsblkcnt_t f_bfree; /* # free blocks */
fsblkcnt_t f_bavail; /* # free blocks for non-root */
fsfilcnt_t f_files; /* # inodes */
fsfilcnt_t f_ffree; /* # free inodes */
fsfilcnt_t f_favail; /* # free inodes for non-root */
unsigned long f_fsid; /* file system id */
unsigned long f_flag; /* mount flags */
unsigned long f_namemax; /* maximum filename length */
};
#include
#include
#include
#include
#include
int gethd(char *path);
int main()
{
char buf[256],*ptr;
FILE *file;
while(1)
{
file=fopen("/etc/fstab","r");
if(!file)return;
memset(buf,0,sizeof(buf));
while(fgets(buf,sizeof(buf),file))
{
ptr=strtok(buf," ");
if(ptr&&((strncmp(ptr,"/dev",4)==0)))
{
ptr=strtok(NULL," ");
gethd(ptr);
}
}
fclose(file);
sleep(2);
}
}
int gethd(char *path)
{
struct statvfs stat1;
statvfs(path,&stat1);
if(stat1.f_flag)
printf("%s total=%dK free=%dK %0.1f%%n",path,stat1.f_bsize*stat1.f_blocks/1024,stat1.f_bsize*stat1.f_bfree/1024,((float)stat1.f_blocks-(float)stat1.f_bfree)/(float)stat1.f_blocks*100);
}
你让他一分种显示一次,那就一分钟调用一次就可以了。
你使用df 只是查看的文件系统的利用率。还有你使用df调用,效率有点低。
可以用这些函数来调用
int statfs(const char *path, struct statfs *buf);
int fstatfs(int fd, struct statfs *buf);
struct statfs {
long f_type; /* type of filesystem (see below) */
long f_bsize; /* optimal transfer block size */
long f_blocks; /* total data blocks 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 */
};
int fstatvfs(int fildes, struct statvfs *buf);
int statvfs(const char *restrict path, struct statvfs *restrict buf);
struct statvfs {
unsigned long f_bsize; /* file system block size */
unsigned long f_frsize; /* fragment size */
fsblkcnt_t f_blocks; /* size of fs in f_frsize units */
fsblkcnt_t f_bfree; /* # free blocks */
fsblkcnt_t f_bavail; /* # free blocks for non-root */
fsfilcnt_t f_files; /* # inodes */
fsfilcnt_t f_ffree; /* # free inodes */
fsfilcnt_t f_favail; /* # free inodes for non-root */
unsigned long f_fsid; /* file system id */
unsigned long f_flag; /* mount flags */
unsigned long f_namemax; /* maximum filename length */
};
#include
#include
#include
#include
#include
int gethd(char *path);
int main()
{
char buf[256],*ptr;
FILE *file;
while(1)
{
file=fopen("/etc/fstab","r");
if(!file)return;
memset(buf,0,sizeof(buf));
while(fgets(buf,sizeof(buf),file))
{
ptr=strtok(buf," ");
if(ptr&&((strncmp(ptr,"/dev",4)==0)))
{
ptr=strtok(NULL," ");
gethd(ptr);
}
}
fclose(file);
sleep(2);
}
}
int gethd(char *path)
{
struct statvfs stat1;
statvfs(path,&stat1);
if(stat1.f_flag)
printf("%s total=%dK free=%dK %0.1f%%n",path,stat1.f_bsize*stat1.f_blocks/1024,stat1.f_bsize*stat1.f_bfree/1024,((float)stat1.f_blocks-(float)stat1.f_bfree)/(float)stat1.f_blocks*100);
}
你让他一分种显示一次,那就一分钟调用一次就可以了。