这段代码演示了如何在C语言中处理/proc/meminfo中的信息,并不难,C语言中的字符串处理函数和文件读写也很强大,可以方便的用来处理文件和字符串
static void parse_meminfo(unsigned long meminfo[MI_MAX]) { static const char fields[] ALIGN1 = "MemTotal" "MemFree" "MemShared" "Shmem" "Buffers" "Cached" "SwapTotal" "SwapFree" "Dirty" "Writeback" "AnonPages" "Mapped" "Slab"; char buf[60]; FILE *f; int i; memset(meminfo, 0, sizeof(meminfo[0]) * MI_MAX); f = xfopen_for_read("meminfo"); while (fgets(buf, sizeof(buf), f) != NULL) { char *c = strchr(buf, ':'); if (!c) continue; *c = ''; i = index_in_strings(fields, buf); if (i >= 0) meminfo[i] = strtoul(c+1, NULL, 10); } fclose(f); }
欢迎分享,转载请注明来源:内存溢出
评论列表(0条)