linux核心程式設計實驗一
問題 A :
1、提取cpu 型別
2、提取核心版本
問題 B:
1、啟動以來經歷的時間,以 dd:hh:mm:ss 報告
問題 C:
1、cpu 執行使用者態、系統態、空閒態所用時間
2、多少次磁碟請求
3、多少次上下文切換
4、啟動了多少次程序
實驗問題 D:
1、記憶體總量
2、可用記憶體
3、系統平均負荷
解決問題A:
1、讀取檔案/proc/cpuinfo,並且將結果顯示即可。
2、讀取檔案/proc/version,並且將結果顯示即可。
解決問題B:
讀取檔案/proc/uptime,第一個引數是代表從系統啟動到現在的時間(以秒為單位),第二個引數是代表系統空閒的時間(以秒為單位)。因此將第一個引數轉成dd:hh:mm:ss即可。
解決問題C:
1、讀取檔案/proc/stat即可。第一行的數值表示的是CPU總的使用情況,所以我們只要用第一行的數字計算就可以了。
如:cpu 25579 49 1340 333457 23139 33 343 0 0
25579:使用者態的執行時間
1340:系統態執行時間
333457:空閒態時間(單位為節拍,與系統程序排程的最小時間片有關)
那麼OK,1就解決了。
2、磁碟請求在/proc/diskstat檔案中。磁碟請求次數為讀和寫次數之和。檔案引數含義
3、上下文請求次數為/proc/stat檔案中包含ctxt:的行。
4、啟動了多少次程序為/proc/stat檔案中包含process的行。
解決問題D:
1、2、/proc/meminfo的前兩行資訊。
3、/proc/loadavg檔案中,第一個為系統近1分鐘內的平均負荷。第二個為近5分鐘的平均負荷。第三個為近15分鐘的平均負荷。
實驗難點:找到對應資訊的檔案,弄清楚檔案中每個引數的含義。
程式碼:
#include <stdio.h>
#include <string.h>
int main(int argc,char *argv[])
{
if(argc != 2)
{
printf("arguments number error!\n");
return 0;
}
if(argv[1][0]!='-' || argv[1][1]<'a' || argv[1][1]>'d')
{
printf("arguments format error!\n");
return 0;
}
FILE *fp;
char linebuf[1024],*cputype,*tmp;
int leng,i,t,index,disknumb;
long systime,day,hour,minute,second;
char *cdtime[4];
char *disknum[2];
if(argv[1][1] == 'a')
{
printf("-----PART A-----\n");
fp = fopen("/proc/cpuinfo","r");
while(fgets(linebuf,1024,fp)!=NULL)
{
if(strstr(linebuf,"model name")!=NULL)
{
cputype = strstr(linebuf,":");
cputype += 2;
printf("-----cpu type-----\n");
printf("%s",cputype);
break;
}
}
fclose(fp);
fp = fopen("/proc/version","r");
printf("-----kernel version-----\n");
fgets(linebuf,1024,fp);
tmp = strstr(linebuf,"(gcc");
*(tmp-1) = '\n';
*tmp = 0;
printf("%s",linebuf);
fclose(fp);
}
else if(argv[1][1] == 'b')
{
printf("-----PART B-----\n");
fp = fopen("/proc/uptime","r");
printf("-----system time-----\n");
fgets(linebuf,1024,fp);
tmp = linebuf;
*strchr(linebuf,'.') = 0;
leng = strlen(tmp);
t = 1;
systime = 0;
for(i=leng-1; i>=0; i--)
{
systime += (tmp[i]-'0')*t;
t *= 10;
}
day = systime/86400;//60*60*24
systime %= 86400;
hour = systime/3600;
systime %= 3600;
minute = systime/60;
second = systime%60;
printf("%ld:%ld:%ld:%ld\n",day,hour,minute,second);
fclose(fp);
}
else if(argv[1][1] == 'c')
{
printf("-----PART C-----\n");
fp = fopen("/proc/stat","r");
fgets(linebuf,1024,fp);
leng = strlen(linebuf);
index = 0;
for(i=0; i<leng-1; i++)
{
if(linebuf[i]==' '&&linebuf[i+1]!=' ')
{
linebuf[i] = 0;
if(index < 4)
cdtime[index++] = &linebuf[i+1];
else
break;
}
}
printf("-----usertime systime idletime-----\n");
printf("%s %s %s\n",cdtime[0],cdtime[2],cdtime[3]);
while(fgets(linebuf,1024,fp)!=NULL)
{
if((tmp=strstr(linebuf,"ctxt"))!=NULL)
{
printf("-----context switch-----\n");
printf("%s",tmp+5);
}
if((tmp=strstr(linebuf,"processes"))!=NULL)
{
printf("-----processes-----\n");
printf("%s",tmp+10);
}
}
fclose(fp);
fp = fopen("/proc/diskstats","r");
while(fgets(linebuf,1024,fp)!=NULL)
{
if((tmp=strstr(linebuf,"sda"))!=NULL)
{
printf("-----disk visit-----\n");
disknum[0] = tmp+4;
tmp = tmp + 4;
for(i=0; i<4; i++)
{
while(*tmp != ' ')
tmp++;
*tmp = 0;
tmp++;
}
disknum[1] = tmp;
while(*tmp != ' ')
tmp++;
*tmp = 0;
disknumb = 0;
leng = strlen(disknum[0]);
t = 1;
for(i=leng-1; i>=0; i--)
{
disknumb += (disknum[0][i]-'0')*t;
t *= 10;
}
leng = strlen(disknum[1]);
t = 1;
for(i=leng-1; i>=0; i--)
{
disknumb += (disknum[1][i]-'0')*t;
t *= 10;
}
printf("%d\n",disknumb);
break;
}
}
fclose(fp);
}
else
{
printf("-----PART D-----\n");
fp = fopen("/proc/meminfo","r");
fgets(linebuf,1024,fp);
tmp = strchr(linebuf,':');
tmp++;
while(*tmp == ' ')
tmp++;
printf("-----total memory-----\n");
printf("%s",tmp);
fgets(linebuf,1024,fp);
tmp = strchr(linebuf,':');
tmp++;
while(*tmp == ' ')
tmp++;
printf("-----free memory-----\n");
printf("%s",tmp);
fclose(fp);
fp = fopen("/proc/loadavg","r");
fgets(linebuf,1024,fp);
tmp = strchr(linebuf,' ');
*tmp = 0;
printf("-----average load-----\n");
printf("%s\n",linebuf);
fclose(fp);
}
return 0;
}