Linux下C语言的各种信息获取

原始路径http://blog.chinaunix.net/uid-312504-id-2133194.html

Linux下获得系统时间的C语言的实现方法

#include<time.h> //C语言的头文件
#include<stdio.h> //C语言的I/O

void main()
{
time_t now;   //实例化time_t结构
struct tm *timenow;    //实例化tm结构指针
time(&now);   //time函数读取现在的时间(国际标准时间非北京时间),然后传值给now
timenow = localtime(&now); //localtime函数把从time取得的时间now换算成你电脑中的时间(就是你设置的地区)
printf("Local time is %s\n",asctime(timenow)); //上句中asctime函数把时间转换成字符,通过printf()函数输出
}

注释:time_t是一个在time.h中定义好的结构体。而tm结构体的原形如下:

struct tm
{
int tm_sec;//seconds 0-61
int tm_min;//minutes 1-59
int tm_hour;//hours 0-23
int tm_mday;//day of the month 1-31
int tm_mon;//months since jan 0-11
int tm_year;//years from 1900
int tm_wday;//days since Sunday, 0-6
int tm_yday;//days since Jan 1, 0-365
int tm_isdst;//Daylight Saving time indicator
};

Linux下获得CPU利用率和内存使用情况
 
在Linux中如果要监视一个进程的运行情况,如查看它的CPU使用效率和内存使用情况,就需要从系统的/proc目录的读取一些系统信息。然后分析得 到结果,特别是在嵌入式中的应用程序这个功能就很重要。本文中的代码是从top命令的源代码分析中获得,并做了部分修改,在FC6+GCC4.1调试通 过。从这个工程中我也获得一些感悟。

1. Linux系统很优雅,如果在Windows中做这个功能就需要调用ActiveX控件。而在Linux中只需要读取文本。

2.想完成什么功能,如果不知道怎么做,就想有没有没有其它的软件有这个功能,如果有,查看它的源代码就可以了,然后定制自己需要的功能。

3.多想多看多做,学习技术的不二法门。

下面是获得系统CPU和内存情况的代码:

void

get_system_info(info)

struct system_info * info;

{

char buffer[ 4096 + 1 ];

int fd, len;

char * p;

int i;



/* get load averages */

{

fd = open( " loadavg " , O_RDONLY);

len = read(fd, buffer, sizeof (buffer) - 1 );

close(fd);

buffer[len] = ' \0 ' ;

info -> load_avg[ 0 ] = strtod(buffer, p);

info -> load_avg[ 1 ] = strtod(p, p);

info -> load_avg[ 2 ] = strtod(p, p);

p = skip_token(p); /* skip running/tasks */

p = skip_ws(p);

if ( * p)

info -> last_pid = atoi(p);

else

info -> last_pid = - 1 ;

}



/* get the cpu time info */

{

fd = open( " stat " , O_RDONLY);

len = read(fd, buffer, sizeof (buffer) - 1 );

close(fd);

buffer[len] = ' \0 ' ;

p = skip_token(buffer); /* "cpu" */

cp_time[ 0 ] = strtoul(p, & p, 0 );

cp_time[ 1 ] = strtoul(p, & p, 0 );

cp_time[ 2 ] = strtoul(p, & p, 0 );

cp_time[ 3 ] = strtoul(p, & p, 0 );

/* convert cp_time counts to percentages */

percentages( 4 , cpu_states, cp_time, cp_old, cp_diff);

}

/* get system wide memory usage */

{

char * p;

fd = open( " meminfo " , O_RDONLY);

len = read(fd, buffer, sizeof (buffer) - 1 );

close(fd);

buffer[len] = ' \0 ' ;

/* be prepared for extra columns to appear be seeking

to ends of lines */

p = buffer;

p = skip_token(p);

memory_stats[ 0 ] = strtoul(p, & p, 10 ); /* total memory */

p = strchr(p, ' \n ' );

p = skip_token(p);

memory_stats[ 1 ] = strtoul(p, & p, 10 ); /* free memory */

p = strchr(p, ' \n ' );

p = skip_token(p);

memory_stats[ 2 ] = strtoul(p, & p, 10 ); /* buffer memory */

p = strchr(p, ' \n ' );

p = skip_token(p);

memory_stats[ 3 ] = strtoul(p, & p, 10 ); /* cached memory */

for (i = 0 ; i < 8 ;i ++ ) {

p ++ ;

p = strchr(p, ' \n ' );

}

p = skip_token(p);

memory_stats[ 4 ] = strtoul(p, & p, 10 ); /* total swap */

p = strchr(p, ' \n ' );

p = skip_token(p);

memory_stats[ 5 ] = strtoul(p, & p, 10 ); /* free swap */

}

/* set arrays and strings */

info -> cpustates = cpu_states;

info -> memory = memory_stats;

}
 
 
linux小程序,可以通过参数显示内存使用情况和内核版本号,支持长短两种参数
 

版权声明:转载时请以超链接形式标明文章原始出处和作者信息及本声明
http://fanshaorui.blogbus.com/logs/6758031.html

# include<stdio.h>
# include<stdlib.h>
# include<getopt.h>
int doit(char *filename)
 {
int ch;
FILE *op;
op=fopen(filename,"r");
ch=getc(op);
while(ch!=EOF)
{
putchar(ch);
ch=getc(op);
}
fclose(op);
}

int main(int argc,char *argv[])

{
char c1;
char c2;
int option;
const char *const short_option="rm";
const struct option long_option[]={
{"release",0,NULL,'r'},
{"memory",0,NULL,'m'},
{NULL,0,NULL,0}
};

if(argc>1)
{
sscanf(argv[1],"%c%c",&c1,&c2);
if(c1!='-')
{
fprintf(stderr,"Usage:kinfo [-r,-m,--release,--memory]");
exit(1);
}
}
do
{
option=getopt_long(argc,argv,short_option,long_option,NULL);
switch(option)
{
case 'r':
doit("/proc/sys/kernel/osrelease");
case 'm':
doit("/proc/meminfo");
case -1:
break;
default:
abort();
}}
while(option!=-1);
return 0;
}
Linux下用Mtrace来检查程序内存溢出
 

对于内存溢出之类的麻烦可能大家在编写指针比较多的复杂的程序的时候就会遇到。Debug起 来也是比较累人。其实Linux系统下有一个使用的工具可以帮忙来调试的,这就是Mtrace。Mtrace主要能够检测一些内存分配和泄漏的失败等。下 面我们来学习一下它的用法。

使用Mtrace来调试程序有4个基本的步骤,需要用到GNU C 函数库里面的一些辅助的函数功能。

1. 在需要跟踪的程序中需要包含头文件,而且在main()函数的最开始包含一个函数调用:mtrace()。由于在main函数的最开头调用了mtrace(),所以该进程后面的一切分配和释放内存的操作都可以由mtrace来跟踪和分析。

2. 定义一个环境变量,用来指示一个文件。该文件用来输出log信息。如下的例子:

$export MALLOC_TRACE=mymemory.log

3. 正常运行程序。此时程序中的关于内存分配和释放的操作都可以记录下来。

4. 然后用mtrace使用工具来分析log文件。例如:

$mtrace testmem $MALLOC_TRACE

下面是具体一个例子,大家可以看一下。

[hwang@langchao test]$ cat testmtrace.c
#include 
#include 
#include 
int main()
{
        char *hello;
        mtrace();
        hello = (char*) malloc(20);
        sprintf(hello,"
hello world!");
        return 1;
}
[hwang@langchao test]$export MALLOC_TRACE=mytrace.log
[hwang@langchao test]$ gcc testmtrace.c -o testmtrace
[hwang@langchao test]$./testmtrace
[hwang@langchao test]$ mtrace testmtrace mytrace.log
Memory not freed:
-----------------
Address Size Caller
0x08049860 0x14 at /usr/src/build/53700-i386/BUILD/glibc-2.2.4/csu/init.c:0
 

 

  • 0
    点赞
  • 2
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值