问题1:

A. 编写一个C程序,常驻内存且占用100M的内存。 #include

#include

#include

#include

#define MAXFILE 65535

#define sig_atomic_t_running = 1 ;

int main()

{

pid_t pc;

pc = fork();

if(pc<0)

{

printf("error fork\n");

exit(1);

}

else if(pc>0)

exit(1);

char *p = (char *)malloc(1024*1024*100);

if(!p)

{

printf("Not enough Memory!\n")

}

memset(p, 'a', 1024*1024*100);

while(1)

sleep(1000);

}

http://bbs.chinaunix.net/thread-1101177-1-1.html

以下在32位的系统上讨论,并且操作系统没有做任何的调整。

malloc函数第一件事会判断本进程的内存地址空间有没有连续的你要申请的空间,如果没有,就算系统有足够的物理内存,那么你申请内存也是失败的。比如你在一个物理内存有4G的系统上申请3G内存,却是失败的。

当本进程的内存地址空间中有足够的空间后,malloc会去判断系统的内存够不够,包括物理内存和各种虚拟内存,如果不够,那么malloc也返回失败。

但是要注意,malloc只是告诉了操作系统:"那一块内存我要用",所以此时系统的物理内存并没有发现变化。只有当进程往这块内存写入数据的时候,这块内存才被真正占用了。

可以试一下:

void * ptr = malloc(1G); /* 观察物理内存的变化 */

*(int *)ptr = 1000; /* 此时再观察物理内存的变化 */

对malloc分配的一篇幅有意思的讨论

http://www.linuxforum.net/forum/showflat.php?Cat=&Board=program&Number=333174&page=3&view=collapsed&sb=9&o=&fpart=3&vc=1

http://www.cppblog.com/matrix/archive/2010/07/07/119531.aspx?opt=admin

在C中 malloc和memset是2个常用的对内存操作的函数。首先还是来看一下这2个函数的函数原型。

1.malloc函数

malloc函数用于从堆上分配指定字节的内存空间。

void

*

malloc(

size_t

n);

其中,形参n为要求分配的内存字节数。如果执行成功,函数范围获得的内存空间的首地址;执行失败,返回值为NULL。由于函数返回值值的类型为void的指针,因此,可以将void指针类型转换后赋值给任意类型指针,这样就可以通过操作该类型指针来操作从堆上获得的内存空间。

malloc分配的是虚拟地址空间,和用到的实实在在的物理内存是两码事,只有真正往空间里

写东西了,os内核会触发缺页异常,然后才真正得到物理内存。

需要注意的是malloc函数分配得到的内存空间是未初始化的。有时候,在使用前需要对该内存空间进行初始化,memset就派上用场了。

2.memset函数

void

*

memset (

void

*

p,

int

c,

size_t

n);

其中,指针p为所操作的内存空间的首地址,c为每个字节所赋的值,n为所操作内存空间的字节长度,也就是内存被赋值为c的字节数。

在使用memset时经常要注意的它是以字节为单位进行赋值的,所赋值的范围是0x00~0xFF。若想要对一个double或int型的数组赋值时,就特别需要注意这一点:

example1:

char

a[4

];

memset(a, '\0', 4);

example2:

int

a[4

];

memset(a,

1

, 4

);

//

这里改成memset(a,1,5*sizeof(int))也是不可以的

第一个例子是正确的,memset对字符数组里的每个char类型元素赋值为NULL。第二个例子显然得到的结果不是把int型数组里的每个元素赋值为了1。因为memset函数以字节为单位进行赋值,那么数组中一个int型元素的4个字节都将被赋值为1(或者说ASCII码1所对应的字符),实际上它所表示的整数是0x01010101。

另外,在使用malloc为一个二维数组分配内存空间时,要特别注意使用memset进行初始化可能会出错。

int

m

=

2

;

int

n

=

3

;

int

i;

//

二位数组a[m][n]

int

**

a;

a

=

(

int

**

) malloc(m

*

sizeof

(

int

*

));

for

(i

=

0

; i

<

m;

++

i)

{

a[i]

=

(

int

*

) malloc(n

*

sizeof

(

int

));

}

memset(a,

0

,

sizeof

(

int

)

*

m

*

n);

对所有二维以上的数组使用memset时,若此多维数组是通过多次调用malloc函数搭积木分配得到的,那么该数组的内存空间可能不连续。使用memset函数进行连续的统一赋值就毫无意义了。直接声明的多维数组如a[2][3]的内存空间显然是连续的,此时使用memset函数初始化就没有问题。

其实不算是真正意义上的常驻程序...

如果内存负载高,此程序将会被换出内存。解决方法需要再找

B. 调整账户的内存限制,使得该程序无法运行。

方法 1 ulimit -v 102400

缺点:只对当前tty启动的进程有效

优点:即时生效

方法 2 编辑/etc/security/limits.conf ,

增加如 :gao  hard    as    102400 即可对相应用户、用户组进行限制

缺点:1:需要重新登录才能让配置生效

2:要root权限才能操作

http://www2.nbu.edu.cn/wanglihong/LBS/article.asp?id=27

问题2:

使用python,编写脚本,收集当前系统的CPU id情况,并以时间为横坐标进行绘图。(rrd tools)

#author gaozhiwen

#email gaozhiwen@baidu.com

#date 2011.10.9

import rrdtool

import subprocess

import time

#获取2秒的cpu id值,并求均值后存入rrd文件

############################################################

def get_cpu_id():

vmstats = subprocess.check_output(['vmstat',"-n","1","3"]).strip().split('\n')

id=0

for stat in vmstats[3:]:

vmstat_split = stat.split()

id = id + int(vmstat_split[-2].strip())

id = id / len(vmstats[3:])

rrdtool.update('cpu.rrd',str(time.time()).split('.')[0]+':'+str(id))

#创建rrd文件,每两秒传送值到RRA.其中RRA保留30个点,即1分钟数据

#####################################################################

def rrd_create():

data_sources=['DS:cpuid:GAUGE:600:U:U']

rrdtool.create('cpu.rrd','-s 2', data_sources, 'RRA:AVERAGE:0.5:1:30')

#生成数据文件生成  60以前--现在 的图。其中x轴分辨率为4,每4秒显示1次label,label为秒数

###################################################################################

def rrd_graph():

rrdtool.graph('cpu_id.png','--start',str(int(str(time.time()).split('.')[0])-60),'--vertical-label','cpu id(%)','--x-grid','SECOND:4:SECOND:40:SECOND:4:0:%S','DEF:cpu_id=cpu.rrd:cpuid:AVERAGE','LINE2:cpu_id#FF0000:"Cpu_id (%)"')

#################################################################################################################################

#                       执行

#####################################################################################

rrd_create()

#thread.start_new_thread(rrd_update,())

while True:

get_cpu_id()

rrd_graph()

重要概念:

When monitoring the state of a system,it is convenient to have the data available at a constant time interval.Unfortunately,you may not always be able to fetch data at exactly the time you want to.ThereforeRRDtool lets you update the log file at any time you want.It will automatically interpolate the value of the data-source (DS) at the latest official time-slot (interval) and write this interpolated value to the log.The original value you have supplied is stored as well and is also taken into account when interpolating the next log entry.

You may log data at a 1 minute interval,but you might also be interested to know the development of the data over the last year.You could do this by simply storing the data in 1 minute intervals for the whole year.While this would take considerable disk space it would also take a lot of time to analyze the data when you wanted to create a graph covering the whole year.RRDtool offers a solution to this problem through its data consolidation feature.When setting up an Round Robin Database (RRD),you can define at which interval this consolidation should occur,and what consolidation function (CF) (average,minimum,maximum,total,last) should be used to build the consolidated values (see rrdcreate).You can define any number of different consolidation setups within one RRD.They will all be maintained on the fly when new data is loaded into theRRD.

Data values of the same consolidation setup are stored into Round Robin Archives (RRA).This is a very efficient manner to store data for a certain amount of time,while using a known and constant amount of storage space.

It works like this: If you want to store 1'000 values in 5 minute interval,RRDtool will allocate space for 1'000 data values and a header area.In the header it will store a pointer telling which slots (value) in the storage area was last written to.New values are written to the Round Robin Archive in,you guessed it,a round robin manner.This automatically limits the history to the last 1'000 values (in our example).Because you can define severalRRAs within a single RRD,you can setup another one,for storing 750 data values at a 2 hour interval,for example,and thus keep a log for the last two months at a lower resolution.

The use of RRAs guarantees that the RRD does not grow over time and that old data is automatically eliminated.By using the consolidation feature,you can still keep data for a very long time,while gradually reducing the resolution of the data along the time axis.

Using different consolidation functions (CF) allows you to store exactly the type of information that actually interests you: the maximum one minute traffic on the LAN,the minimum temperature of your wine cellar,the total minutes of down time,etc.

全过程:

1创建rrd文件,其中定义数据源积累的方式,以及多久推送一次数据到rra档案(是个保存数据的)。

----开始推送...

2 绘图,抽取rra里面数据绘图

如何让python进程常驻内存_常驻内存程序--python+rrd监控cpu相关推荐

  1. python基于值得内存_为什么说Python采用的是基于值的内存管理模式

    匿名用户 1级 2018-01-31 回答 先从较浅的层面来说,Python的内存管理机制可以从三个方面来讲 (1)垃圾回收 (2)引用计数 (3)内存池机制 一.垃圾回收: python不像C++, ...

  2. python processpoolexector 释放内存_一起看看python 中日志异步发送到远程服务器

    在python中使用日志最常用的方式就是在控制台和文件中输出日志了,logging模块也很好的提供的相应的类,使用起来也非常方便,但是有时我们可能会有一些需求,如还需要将日志发送到远端,或者直接写入数 ...

  3. python processpoolexector 释放内存_关于python:如何在multiprocessing.queue中从Process中释放内存?...

    我有一个程序试图预测一周内发送的每封电子邮件的电子邮件转换(因此,通常是7封). 输出是7个不同的文件,每个客户的预测得分. 串行运行这些可能需要8个小时,因此我尝试使用multiprocessing ...

  4. Python 进程 Process 与线程 threading 区别 - Python零基础入门教程

    目录 一.Python 线程 threading 创建 二.Python 进程 Process 创建 三.Python 进程 Process 和线程 threading 区别 四.Python 进程 ...

  5. python隐藏启动台_如何在Python中启动后台进程?

    如何在Python中启动后台进程? 我正在尝试将shell脚本移植到更易读的python版本. 原始shell脚本在后台使用"&"启动多个进程(实用程序,监视器等). 如何 ...

  6. python读取linux内存_使用python获取CPU和内存信息(linux系统)

    大家都知道,linux里一切皆为文件,在linux/unix的根目录下,有个/proc目录,这个/proc 是一种内核和内核模块用来向进程(process)发送信息的机制(所以叫做"/pro ...

  7. python强制释放内存_强制Python释放对象以释放内存

    我运行以下代码:from myUtilities import myObject for year in range(2006,2015): front = 'D:\\newFilings\\' ba ...

  8. python 进程生命周期_计算客户生命周期价值的python解决方案

    python 进程生命周期 By Lisa Cohen, Zhining Deng, Shijing Fang, and Ron Sielinski 由丽莎·科恩,志宁邓,石井方和罗恩Sielinsk ...

  9. python 释放变量所指向的内存_通俗易懂的Python垃圾回收机制及内存管理

    Python垃圾回收机制及内存管理 内存管理: 先定义一个变量 name='wxl' 那么python会在内存中开辟一小块区域存放"wxl",此时变量的值是我们真正想要存储的,wx ...

最新文章

  1. JZOJ 5395. 【NOIP2017提高A组模拟10.6】Count
  2. 【网络设计】ConvNeXt:A ConvNet for the 2020s
  3. 使用iOS 4越狱iPhone或iPod Touch
  4. 驾校约车html网站源码,html5首汽约车微信感恩活动页面模板
  5. java编程——【Mybatis】之${}和#{}的区别
  6. [转]Vmware ESX 4上虚拟机 Redhat 5.2(CentOS 5.2)启动在Starting udev 停几个小时
  7. 大数据实战之环境搭建(十)
  8. linux网卡为啥叫ens160这些,centos8将网卡名ens160修改为eth0
  9. 红米5a android,红米5A值得买吗?红米5A测评告诉你(附全文)
  10. elasticsearch-head 集群健康值: 未连接
  11. Python中Queue.get()方法阻塞,怎么办?
  12. 【JavaScript】图表分析
  13. 如何编写高质量的程序
  14. Python工具箱系列(十一)
  15. WebView加载网页不显示图片解决办法
  16. tiktok运营全攻略
  17. 抖音滑块以及轨迹分析
  18. 03.JavaScript-数据类型和数据类型转换
  19. 2023年Java面试题大全(最新版版)面试题附答案详解,看完BTA可进
  20. 病毒分析工具和使用方法(一)

热门文章

  1. win10打开文件夹速度慢怎么办
  2. 基于深度学习的道路交通标志数字识别
  3. 世上最杰出程序员,B 语言、Unix 之父嫌计算机发展太慢,让孩子学生物?
  4. SPSS绘制四分位数【箱型图】
  5. 多个excel数据汇总
  6. 7-3 最佳情侣身高差 (10分)
  7. 视觉追踪热图帮Instagram被吐槽新l
  8. 为何汽车从低档位启动,扭矩最大?
  9. 黑平台winterSnow Forex搞PUA 诱惑受害者投资外汇导致36万美金无法出金
  10. 电商系统延时任务机制源码分享