Unix编程/应用问答中文版 ---4.系统资源相关问题
clude <unistd.h>
int main ( int argc, char * argv[] )
{
int fd = open( THEMEM, O_RDONLY );
char c;
unsigned long pos, mapstart = 0;
int totmb = 0;
if ( fd == -1 )
{
perror( THEMEM );
exit( 1 );
}
for ( pos = 0; pos < MAXMEM; pos += MEGABYTE )
{
if (lseek( fd, pos, 0 ) == -1 )
{
perror( "lseek" );
exit( 1 );
}
if ( read( fd, &c, 1 ) == -1 )
{
int size = ( pos - mapstart ) / MEGABYTE;
if ( size != 0 )
{
printf( "found %3d MB starting at 0x%p\n", size, ( void * )mapst
art );
totmb += size;
}
mapstart = pos + MEGABYTE; /* start of next possible mapping */
}
}
printf( "Total memory size: %d MB\n", totmb );
exit( 0 );
}
--------------------------------------------------------------------------
由于需要读访问/dev/mem,普通用户用户无法使用该程序。
int main ( int argc, char * argv[] )
{
int fd = open( THEMEM, O_RDONLY );
char c;
unsigned long pos, mapstart = 0;
int totmb = 0;
if ( fd == -1 )
{
perror( THEMEM );
exit( 1 );
}
for ( pos = 0; pos < MAXMEM; pos += MEGABYTE )
{
if (lseek( fd, pos, 0 ) == -1 )
{
perror( "lseek" );
exit( 1 );
}
if ( read( fd, &c, 1 ) == -1 )
{
int size = ( pos - mapstart ) / MEGABYTE;
if ( size != 0 )
{
printf( "found %3d MB starting at 0x%p\n", size, ( void * )mapst
art );
totmb += size;
}
mapstart = pos + MEGABYTE; /* start of next possible mapping */
}
}
printf( "Total memory size: %d MB\n", totmb );
exit( 0 );
}
--------------------------------------------------------------------------
由于需要读访问/dev/mem,普通用户用户无法使用该程序。

