〖Linux〗proc的一些文件读取操作

  1 /*
  2  * =====================================================================================
  3  *
  4  *       Filename:  proc_more.c
  5  *
  6  *    Description:  
  7  *
  8  *        Version:  1.0
  9  *        Created:  2013年08月07日 11时34分40秒
 10  *       Revision:  none
 11  *       Compiler:  gcc
 12  *
 13  *         Author:  linkscue (scue), linkscue@gmail.com
 14  *   Organization:  
 15  *
 16  * =====================================================================================
 17  */
 18 
 19 #include <stdio.h>
 20 #include <sys/time.h>
 21 #include <sys/types.h>
 22 #include <sys/stat.h>
 23 #include <unistd.h>
 24 #include <fcntl.h>
 25 #include <stdlib.h>
 26 #include <string.h>
 27 void sampleLoadAvg(){
 28     int f=0;
 29     char buffer[80]="";                         /* 定义字符串并初始化为'\0' */
 30     char buf[5][10];
 31     char *file="/proc/loadavg";
 32     f = open(file, O_RDONLY);
 33     if (f == 0)
 34     {
 35         printf("error to open: %s\n", file);
 36         exit(EXIT_FAILURE);
 37     }
 38     read(f, (void *)buffer, 80);
 39     sscanf(buffer, "%s %s %s %s %s",            /* sscanf()拆分成多个字符串 */
 40             &buf[0],&buf[1],&buf[2],&buf[3],&buf[4]);
 41     printf("一分钟平均负载:%s\n", buf[0]);
 42     printf("五分钟平均负载:%s\n", buf[1]);
 43     printf("一刻钟平均负载:%s\n", buf[2]);
 44     printf("采样时刻的间隔:%s\n", buf[3]);
 45     printf("最大线程的数目:%s\n", buf[4]);
 46     close(f);
 47 }
 48 void sampleTime(){
 49     int f=0;
 50     char buffer[80]="";
 51     char buf[2][10];
 52     float seconds=0;
 53     float secondr=0;
 54     char *file="/proc/uptime";
 55     f = open(file, O_RDONLY);
 56     if (f == 0)
 57     {
 58         printf("error to open: %s\n", file);
 59         exit(EXIT_FAILURE);
 60     }
 61     read(f, (void *)buffer, 80);
 62     sscanf(buffer, "%s %s", &buf[0], &buf[1]);
 63     close(f);
 64     printf("系统运行时间:\t%s秒\n", buf[0]);
 65     printf("系统空闲时间:\t%s秒\n", buf[1]);
 66     close(f);
 67     seconds=strtof(buf[0],NULL);
 68     secondr=strtof(buf[1],NULL);
 69     printf("系统运行时间:\t%03d天%02d时%02d分%02.6f秒\n", 
 70             (int)seconds/(24*3600),             /**/
 71             ((int)seconds/(3600))%(24),         /**/
 72             ((int)seconds/60)%60,               /**/
 73             ((int)seconds%60)+(seconds-(int)seconds)); /* 秒(精确至毫秒) */
 74     printf("系统空闲时间:\t%03d天%02d时%02d分%02.6f秒\n", 
 75             (int)secondr/(24*3600),             /**/
 76             ((int)secondr/(3600))%(24),         /**/
 77             ((int)secondr/60)%60,               /**/
 78             ((int)secondr%60)+(secondr-(int)secondr)); /* 秒(精确至毫秒) */
 79 }
 80 
 81 void sampleKernelVersion(){
 82     int f=0;
 83     char buffer[80]="";
 84     char *file="/proc/sys/kernel/version";
 85     f = open(file, O_RDONLY);
 86     if (f == 0)
 87     {
 88         printf("error to open: %s\n", file);
 89         exit(EXIT_FAILURE);
 90     }
 91     read(f, (void *)buffer, 80);
 92     buffer[strlen(buffer)-1]=0;                 /* 简单实现tr()函数的功能 */
 93     printf("当前内核版本:\t%s\n", buffer);
 94     close(f);
 95 }
 96 
 97 void sampleOsRelease(){
 98     int f=0;
 99     char buffer[80]="";
100     char *file="/proc/sys/kernel/osrelease";
101     f = open(file, O_RDONLY);
102     if (f == 0)
103     {
104         printf("error to open: %s\n", file);
105         exit(EXIT_FAILURE);
106     }
107     read(f, (void *)buffer, 80);
108     buffer[strlen(buffer)-1]=0;
109     printf("当前发行版本:\t%s\n", buffer);
110     close(f);
111 }
112 
113 void sampleOsType(){
114     int f=0;
115     char buffer[80]="";
116     char *file="/proc/sys/kernel/ostype";
117     f = open(file, O_RDONLY);
118     if (f == 0)
119     {
120         printf("error to open: %s\n", file);
121         exit(EXIT_FAILURE);
122     }
123     read(f, (void *)buffer, 80);
124     buffer[strlen(buffer)-1]=0;
125     printf("当前操作系统:\t%s\n", buffer);
126     close(f);
127 }
128 
129 void sampleDiskStat(){
130     int i;
131     FILE *fp;
132     int nread=0;
133     ssize_t len = 0;
134     char *buffer=NULL;
135     char buf[20][32];
136     char *file="/proc/diskstats";
137     char *p;
138     fp = fopen(file, "rb");
139     if (fp == NULL)
140     {
141         printf("error to open: %s\n", file);
142         exit(EXIT_FAILURE);
143     }
144     printf("  磁盘  读次数  写次数\n");
145     while((nread = getline(&buffer, &len, fp)) != -1) { /* 简单实现读行的功能 */
146         sscanf(buffer, "%04s%08s%s %s %s %s %s %s %s %s %s %s %s %s", 
147                 &buf[0],&buf[1],&buf[2],&buf[3],&buf[4],&buf[5],&buf[6],
148                 &buf[7],&buf[8],&buf[9],&buf[10],&buf[11],&buf[12],&buf[13]);
149         if ((p=strstr(buf[2], "loop"))!=NULL)
150         {
151             ;                                   /* loop本地回路不作操作 */
152         }
153         else {
154             printf("%06s%08s%08s\n", 
155                     &buf[2],&buf[3], &buf[7]);
156         }
157     }
158 }
159 
160 void sampleProcesses(void)
161 {
162     FILE *fp;
163     int nread=0;
164     ssize_t len = 0;
165     char *buf=NULL;
166     char *buffer=NULL;
167     char *file="/proc/stat";
168     fp = fopen(file, "rb");
169     if (fp == NULL)
170     {
171         printf("error to open: %s\n", file);
172         exit(EXIT_FAILURE);
173     }
174     while((nread = getline(&buffer, &len, fp)) != -1) {
175         if((buf=strstr(buffer, "processes"))!=NULL)  /* 简单实现grep的功能 */
176             break;
177     }
178     buffer[strlen(buffer)-1]=0;                 /* 简单实现tr()函数的功能 */
179     char count[16]="";
180     sscanf(buffer, "%s%s", count, count);
181     printf("执行线程数目:\t%s\n", count);
182 }
183 
184 void sampleContext(void)
185 {
186     FILE *fp;
187     int nread=0;
188     ssize_t len = 0;
189     char *buf=NULL;
190     char *buffer=NULL;
191     char *file="/proc/stat";
192     fp = fopen(file, "rb");
193     if (fp == NULL)
194     {
195         printf("error to open: %s\n", file);
196         exit(EXIT_FAILURE);
197     }
198     while((nread = getline(&buffer, &len, fp)) != -1) {
199         if((buf=strstr(buffer, "ctxt"))!=NULL)  /* 简单实现grep的功能 */
200             break;
201     }
202     buffer[strlen(buffer)-1]=0;                 /* 简单实现tr()函数的功能 */
203     char count[16]="";
204     sscanf(buffer, "%s%s", count, count);
205     printf("上下文切换次数:\t%s\n", count);
206 }
207 
208 void sampleMeminfo()
209 {
210     FILE *fp;
211     int nread=0;
212     ssize_t len = 0;
213     char *buf=NULL;
214     char *buffer=NULL;
215     char *file="/proc/meminfo";
216     char content[16]="";
217     fp = fopen(file, "rb");
218     if (fp == NULL)
219     {
220         printf("error to open: %s\n", file);
221         exit(EXIT_FAILURE);
222     }
223     while((nread = getline(&buffer, &len, fp)) != -1) {
224         if((buf=strstr(buffer, "MemTotal"))!=NULL)  /* 简单实现grep的功能 */
225         {
226             buffer[strlen(buffer)-1]=0;             /* 简单实现tr()函数的功能 */
227             sscanf(buffer, "%s%s", content, content);
228             int memtotal_kb=(int)(strtof(content, NULL));
229             printf("内存总容量:\t%dG%4dM %4dK\n",
230                     memtotal_kb/(1024*1024),           /* Gb */
231                     (memtotal_kb/(1024))%1024,         /* Mb */
232                     (memtotal_kb%(1024*1024))%1024);   /* Kb */
233         }
234         if((buf=strstr(buffer, "MemFree"))!=NULL)  /* 简单实现grep的功能 */
235         {
236             buffer[strlen(buffer)-1]=0;                 /* 简单实现tr()函数的功能 */
237             sscanf(buffer, "%s%s", content, content);
238             int memfree_kb=(int)(strtof(content, NULL));
239             printf("内存可用容量:\t%dG%4dM %4dK\n",
240                     memfree_kb/(1024*1024),           /* Gb */
241                     (memfree_kb/(1024))%1024,         /* Mb */
242                     (memfree_kb%(1024*1024))%1024);   /* Kb */
243         }
244         if((buf=strstr(buffer, "SwapTotal"))!=NULL)  /* 简单实现grep的功能 */
245         {
246             buffer[strlen(buffer)-1]=0;                 /* 简单实现tr()函数的功能 */
247             sscanf(buffer, "%s%s", content, content);
248             int swaptotal_kb=(int)(strtof(content, NULL));
249             printf("SWAP总容量:\t%dG%4dM %4dK\n",
250                     swaptotal_kb/(1024*1024),           /* Gb */
251                     (swaptotal_kb/(1024))%1024,         /* Mb */
252                     (swaptotal_kb%(1024*1024))%1024);   /* Kb */
253         }
254         if((buf=strstr(buffer, "SwapFree"))!=NULL)  /* 简单实现grep的功能 */
255         {
256             buffer[strlen(buffer)-1]=0;                 /* 简单实现tr()函数的功能 */
257             sscanf(buffer, "%s%s", content, content);
258             int swapfree_kb=(int)(strtof(content, NULL));
259             printf("SWAP可用容量:\t%dG%4dM %4dK\n",
260                     swapfree_kb/(1024*1024),           /* Gb */
261                     (swapfree_kb/(1024))%1024,         /* Mb */
262                     (swapfree_kb%(1024*1024))%1024);   /* Kb */
263             break;                              /* 所需的信息已满足,退出循环 */
264         }
265     }
266 }
267 /* 
268  * ===  FUNCTION  ======================================================================
269  *         Name:  main
270  *  Description:  
271  * =====================================================================================
272  */
273 int main ( int argc, char *argv[] )
274 {
275     printf(">>>系统负载<<<\n");
276     sampleLoadAvg();
277     printf("----------------------------------------\n");
278     printf("\n");
279     printf(">>>运行时间<<<\n");
280     sampleTime();
281     printf("\n");
282     printf(">>>版本信息<<<\n");
283     sampleOsType();
284     sampleOsRelease();
285     sampleKernelVersion();
286     printf("----------------------------------------\n");
287     printf("\n");
288     printf(">>>磁盘信息<<<\n");
289     sampleDiskStat();
290     printf("----------------------------------------\n");
291     printf("\n");
292     printf(">>>上下文切换<<<\n");
293     sampleContext();
294     printf("----------------------------------------\n");
295     printf("\n");
296     printf(">>>线程数目<<<\n");
297     sampleProcesses();
298     printf("----------------------------------------\n");
299     printf("\n");
300     printf(">>>内存信息<<<\n");
301     sampleMeminfo();
302     printf("----------------------------------------\n");
303     return EXIT_SUCCESS;
304 }

 

posted @ 2013-08-07 22:39  scue  阅读(4458)  评论(0编辑  收藏  举报