今天一口气把监控模块框架写了.
监控模块设计步骤如下:
建立一个定时器:(用于定时监控每模块是否活着)
建立一个队列:(用于接收各模块的回应消息)
while(recv(queue))
{
switch(msg)
timer:
if(is_all_alive)
发送监控消息给所有模块
else
发送消息给主控
other module:
此模块活着
.....:
此模块活着
}
timer
{
发送定时消息给自己模块
}
再接着把内存管理的代码也写了:
设计要求:
可查询当前内存的申请情况(长度,种类和种类的大小,及申请的文件名,行数)
限制申请长度超过内存最大长度
#define NUM_BLOCKS 128//申请次数
#define NUM_SIZES 64//申请的种类
#define MAX_MEM_SIZES 1024*1024 // 1M 内存
static BlockEntry blocks[NUM_BLOCKS];
static Counter counters[NUM_SIZES];
static void incrementCountForSize(size_t size);
static void decrementCountForSize(size_t size);
static long FS_totalAllocated = 0;
void *mMallocLineFile(size_t size, int line, char *file)
{
int i;
if((FS_totalAllocated+size)>MAX_MEM_SIZES)
return;
void *newAllocation = malloc(size);
for (i = 0; i < NUM_BLOCKS; i++)
{
if (blocks[i].addr == 0)
{
// found empty entry; use it
blocks[i].addr = newAllocation;
blocks[i].size = size;
blocks[i].line = line;
blocks[i].file = file;
incrementCountForSize(size);
break;
}
}
assert(i < NUM_BLOCKS);
FS_totalAllocated += size;
return newAllocation;
}
void mFree(void *blockToFree)
{
int i;
for (i = 0; i < NUM_BLOCKS; i++)
{
if (blocks[i].addr = = blockToFree)
{
// found block being released
decrementCountForSize(blocks[i].size);
FS_totalAllocated -= blocks[i].size;
blocks[i].addr = 0;
blocks[i].size = 0;
break;
}
}
assert(i < NUM_BLOCKS);
free(blockToFree);
}
void mDisplayTable(void)
{
printf("%s", "\nSize\tFreq.");
for (int i = 0; i < NUM_SIZES; i++)
{
if (counters[i].size = = 0) break;
printf("\n%d\t\t%d", counters[i].size, counters[i].count);
}
}
void mClearTable(void)
{
for (int i = 0; i < NUM_SIZES; i++)
{
counters[i].size = 0;
counters[i].count = 0;
}
}