C语言中内存分布及程序运行加载过程

C语言中内存分布及程序运行加载过程,第1张

一个程序内存分配:

下图是APUE中的一个典型C内存空间分布图(虚拟内存)

例如:

int g1=0, g2=0, g3=0;

int max(int i)

{

int m1=0,m2,m3=0, p_max;

static n1_max=0,n2_max,n3_max=0;

p_max = (int )malloc(10);

printf("打印max程序地址\n");

printf("in max: 0xx\n\n",max);

printf("打印max传入参数地址\n");

printf("in max: 0xx\n\n",&i);

printf("打印max函数中静态变量地址\n");

printf("0xx\n",&n1_max); //打印各本地变量的内存地址

printf("0xx\n",&n2_max);

printf("0xx\n\n",&n3_max);

printf("打印max函数中局部变量地址\n");

printf("0xx\n",&m1); //打印各本地变量的内存地址

printf("0xx\n",&m2);

printf("0xx\n\n",&m3);

printf("打印max函数中malloc分配地址\n");

printf("0xx\n\n",p_max); //打印各本地变量的内存地址

if(i) return 1;

else return 0;

}

int main(int argc, char argv)

{

static int s1=0, s2, s3=0;

int v1=0, v2, v3=0;

int p;

p = (int )malloc(10);

printf("打印各全局变量(已初始化)的内存地址\n");

printf("0xx\n",&g1); //打印各全局变量的内存地址

printf("0xx\n",&g2);

printf("0xx\n\n",&g3);

printf("======================\n");

printf("打印程序初始程序main地址\n");

printf("main: 0xx\n\n", main);

printf("打印主参地址\n");

printf("argv: 0xx\n\n",argv);

printf("打印各静态变量的内存地址\n");

printf("0xx\n",&s1); //打印各静态变量的内存地址

printf("0xx\n",&s2);

printf("0xx\n\n",&s3);

printf("打印各局部变量的内存地址\n");

printf("0xx\n",&v1); //打印各本地变量的内存地址

printf("0xx\n",&v2);

printf("0xx\n\n",&v3);

printf("打印malloc分配的堆地址\n");

printf("malloc: 0xx\n\n",p);

printf("======================\n");

max(v1);

printf("======================\n");

printf("打印子函数起始地址\n");

printf("max: 0xx\n\n",max);

return 0;

}

打印结果:

ELF目标文件格式的最前端是 ELF文件头(ELF Header)

包含了描述整个文件的基本属性,如ELF版本、目标机器型号、 程序入口地址

3 加载:

内存情况比较简单

MEMORYSTATUSEX mstx;

mstxdwLength = sizeof (mstx);

GlobalMemoryStatusEx( &mstx );

int iMemeryUsePercentage = mstxdwMemoryLoad;

int iTotalPhysMB = mstxullTotalPhys/1024/1024;

int iAvailPhysMB = mstxullAvailPhys/1024/1024;

int iTotalPageFileMB = mstxullTotalPageFile/1024/1024;

int iAvailPageFileMB = mstxullAvailPageFile/1024/1024;

char LogBuff[128];

memset( LogBuff , 0 , 128 );

sprintf( LogBuff , "MemAvailPct=%d%% Phys=%d/%d PageFile=%d/%d" , 100 - iMemeryUsePercentage , iAvailPhysMB , iTotalPhysMB , iAvailPageFileMB , iTotalPageFileMB );

printf("%s\n",LogBuff);

以上程序分别输出可用百分比,可以用物理内存/总物理内存,可用页面文件/总页面文件

获取CPU的比较复杂,我这边只有获取单个进程CPU占用的方法,不过可以遍历所有进程分别获取再求和就是整个cpu占用率了。

#include <stdioh>

#include <Windowsh>

typedef long long int64_t;

typedef unsigned long long uint64_t;

/// 时间转换

static uint64_t file_time_2_utc(const FILETIME ftime)

{

LARGE_INTEGER li;

liLowPart = ftime->dwLowDateTime;

liHighPart = ftime->dwHighDateTime;

return liQuadPart;

}

/// 获得CPU的核数

static int get_processor_number()

{

SYSTEM_INFO info;

GetSystemInfo(&info);

return (int)infodwNumberOfProcessors;

}

int get_cpu_usage(int pid)

{

//cpu数量

static int processor_count_ = -1;

//上一次的时间

static int64_t last_time_ = 0;

static int64_t last_system_time_ = 0;

FILETIME now;

FILETIME creation_time;

FILETIME exit_time;

FILETIME kernel_time;

FILETIME user_time;

int64_t system_time;

int64_t time;

int64_t system_time_delta;

int64_t time_delta;

int cpu = -1;

if(processor_count_ == -1)

{

processor_count_ = get_processor_number();

}

GetSystemTimeAsFileTime(&now);

HANDLE hProcess = OpenProcess(PROCESS_ALL_ACCESS, false, pid);

if (!GetProcessTimes(hProcess, &creation_time, &exit_time, &kernel_time, &user_time))

{

return -1;

}

system_time = (file_time_2_utc(&kernel_time) + file_time_2_utc(&user_time))

/ processor_count_;

time = file_time_2_utc(&now);

if ((last_system_time_ == 0) || (last_time_ == 0))

{

last_system_time_ = system_time;

last_time_ = time;

return -1;

}

system_time_delta = system_time - last_system_time_;

time_delta = time - last_time_;

if (time_delta == 0)

return -1;

cpu = (int)((system_time_delta 100 + time_delta / 2) / time_delta);

last_system_time_ = system_time;

last_time_ = time;

return cpu;

}

int main()

{

while(1)

{

int cpu;

// 参数为进程id

cpu = get_cpu_usage(5160);

printf("CPU使用率: %d%%\n",cpu);

Sleep(1000);

}

return 0;

}

可以通过编程实现,源代码如下:

// Sample output:

// There is 51 percent of memory in use

// There are 2029968 total KB of physical memory

// There are 987388 free KB of physical memory

// There are 3884620 total KB of paging file

// There are 2799776 free KB of paging file

// There are 2097024 total KB of virtual memory

// There are 2084876 free KB of virtual memory

// There are 0 free KB of extended memory

#include <windowsh>

#include <stdioh>

#include <tcharh>

// Use to convert bytes to KB

#define DIV 1024

// Specify the width of the field in which to print the numbers

// The asterisk in the format specifier "%I64d" takes an integer

// argument and uses it to pad and right justify the number

#define WIDTH 7

void _tmain()

{

MEMORYSTATUSEX statex;

statexdwLength = sizeof (statex);

GlobalMemoryStatusEx (&statex);

_tprintf (TEXT("There is %ld percent of memory in use\n"),

WIDTH, statexdwMemoryLoad);

_tprintf (TEXT("There are %I64d total KB of physical memory\n"),

WIDTH, statexullTotalPhys/DIV);

_tprintf (TEXT("There are %I64d free KB of physical memory\n"),

WIDTH, statexullAvailPhys/DIV);

_tprintf (TEXT("There are %I64d total KB of paging file\n"),

WIDTH, statexullTotalPageFile/DIV);

_tprintf (TEXT("There are %I64d free KB of paging file\n"),

WIDTH, statexullAvailPageFile/DIV);

_tprintf (TEXT("There are %I64d total KB of virtual memory\n"),

WIDTH, statexullTotalVirtual/DIV);

_tprintf (TEXT("There are %I64d free KB of virtual memory\n"),

WIDTH, statexullAvailVirtual/DIV);

// Show the amount of extended memory available

_tprintf (TEXT("There are %I64d free KB of extended memory\n"),

WIDTH, statexullAvailExtendedVirtual/DIV);

}

运行后结果就能获取当前PC的硬盘大小、已使用大小和CPU的使用率。

system(执行shell命令)相关函数fork,execve,waitpid,popen表头文件#include定义函数intsystem(constcharstring);函数说明system()会调用fork()产生子进程,由子进程来调用/bin/sh-cstring来执行参数string字符串所代表的命令,此命令执行完后随即返回原调用的进程。在调用system()期间SIGCHLD信号会被暂时搁置,SIGINT和SIGQUIT信号则会被忽略。返回值如果system()在调用/bin/sh时失败则返回127,其他失败原因返回-1。若参数string为空指针(NULL),则返回非零值。如果system()调用成功则最后会返回执行shell命令后的返回值,但是此返回值也有可能为system()调用/bin/sh失败所返回的127,因此最好能再检查errno来确认执行成功。附加说明在编写具有SUID/SGID权限的程序时请勿使用system(),system()会继承环境变量,通过环境变量可能会造成系统安全的问题。范例#includemain(){system(“ls-al/etc/passwd/etc/shadow”);}执行-rw-r--r--1rootroot705Sep313:52/etc/passwd-r---------1rootroot572Sep215:34/etc/shadow

以上就是关于C语言中内存分布及程序运行加载过程全部的内容,包括:C语言中内存分布及程序运行加载过程、请教下:C语言获取计算机系统CPU使用率,内存使用情况的思路或流程是怎样的!、C++ 怎么实现获取当前PC 硬盘 内存的大小以及已使用大小,加上CPU使用率等相关内容解答,如果想了解更多相关内容,可以关注我们,你们的支持是我们更新的动力!

欢迎分享,转载请注明来源:内存溢出

原文地址: http://outofmemory.cn/web/9412297.html

(0)
打赏 微信扫一扫 微信扫一扫 支付宝扫一扫 支付宝扫一扫
上一篇 2023-04-28
下一篇 2023-04-28

发表评论

登录后才能评论

评论列表(0条)

保存