1. 程式人生 > >獲取磁盤使用率(Linux C)

獲取磁盤使用率(Linux C)

磁盤 pen open get 獲取 code can per sent

#define SYS_DISK_BUFF_LEN 256 #define SYS_DISK_NAME_LEN 80 #define SYS_100_PERSENT 100 int get_sysDiskUsage() { FILE * fp; char filesystem[SYS_DISK_NAME_LEN],available[SYS_DISK_NAME_LEN],use[SYS_DISK_NAME_LEN],mounted[SYS_DISK_NAME_LEN],buf[SYS_DISK_BUFF_LEN]; double used,blocks,used_rate; fp=popen("df","r"); fgets(buf,SYS_DISK_BUFF_LEN,fp); double dev_total = 0,dev_used = 0; while(6 == fscanf(fp,"%s %lf %lf %s %s %s",filesystem,&blocks,&used,available,use,mounted)) { dev_total += blocks; dev_used += used; } used_rate = (dev_used/dev_total)*SYS_100_PERSENT; pclose(fp); return used_rate; }

獲取磁盤使用率(Linux C)