ºìÁªLinuxÃÅ»§
Linux°ïÖú

LinuxÏ»ñµÃCPUÀûÓÃÂʺÍÄÚ´æʹÓÃÇé¿ö

·¢²¼Ê±¼ä:2007-04-24 00:44:22À´Ô´:ºìÁª×÷Õß:Comeshappy
ÔÚLinuxÖÐÈç¹ûÒª¼àÊÓÒ»¸ö½ø³ÌµÄÔËÐÐÇé¿ö£¬Èç²é¿´ËüµÄCPUʹÓÃЧÂʺÍÄÚ´æʹÓÃÇé¿ö£¬¾ÍÐèÒª´ÓϵͳµÄ/procĿ¼µÄ¶ÁȡһЩϵͳÐÅÏ¢¡£È»ºó·ÖÎöµÃµ½½á¹û£¬ÌرðÊÇÔÚǶÈëʽÖеÄÓ¦ÓóÌÐòÕâ¸ö¹¦ÄܾͺÜÖØÒª¡£±¾ÎÄÖеĴúÂëÊÇ´ÓtopÃüÁîµÄÔ´´úÂë·ÖÎöÖлñµÃ£¬²¢×öÁ˲¿·ÖÐ޸ģ¬ÔÚFC6+GCC4.1µ÷ÊÔͨ¹ý¡£´ÓÕâ¸ö¹¤³ÌÖÐÎÒÒ²»ñµÃһЩ¸ÐÎò¡£

1. LinuxϵͳºÜÓÅÑÅ£¬Èç¹ûÔÚWindowsÖÐ×öÕâ¸ö¹¦ÄܾÍÐèÒªµ÷ÓÃActiveX¿Ø¼þ¡£¶øÔÚLinuxÖÐÖ»ÐèÒª¶ÁÈ¡Îı¾¡£
2.ÏëÍê³Éʲô¹¦ÄÜ£¬Èç¹û²»ÖªµÀÔõô×ö£¬¾ÍÏëÓÐûÓÐûÓÐÆäËüµÄÈí¼þÓÐÕâ¸ö¹¦ÄÜ£¬Èç¹ûÓУ¬²é¿´ËüµÄÔ´´úÂë¾Í¿ÉÒÔÁË£¬È»ºó¶¨ÖÆ×Ô¼ºÐèÒªµÄ¹¦ÄÜ¡£
3.¶àÏë¶à¿´¶à×ö£¬Ñ§Ï°¼¼ÊõµÄ²»¶þ·¨ÃÅ¡£

topÃüÁîÔ´´úÂëÏÂÔØ£ºhttp://www.groupsys.com/top/download.shtml
¹¤³ÌÏÂÔØ£º http://www.cppblog.com/Files/dyj057/mytop.zip
ÏÂÃæÊÇ»ñµÃϵͳ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;
}
ÎÄÕÂÆÀÂÛ

¹²ÓÐ 0 ÌõÆÀÂÛ