How to programmatically clear the filesystem memory cache in C++ on a Linux system?

I'm writing a benchmark tool in C++ where I want to clear the filesystem memory cache between experiments. I'm aware of the following console commands:

sync
echo 3 > /proc/sys/vm/drop_caches

My question is how can i do this programmatically directly within C++?

Any help is appreciated!


Just write to it :

sync();

std::ofstream ofs("/proc/sys/vm/drop_caches");
ofs << "3" << std::endl;


int fd;
char* data = "3";

sync();
fd = open("/proc/sys/vm/drop_caches", O_WRONLY);
write(fd, data, sizeof(char));
close(fd);

This is a better answer because it includes a sync. Which is important because drop_caches is a debuggingtool and it does not always get the data on disk before dropping it. –   Zan Lynx  Jul 25 '11 at 15:54
 
A better answer that what? Both of the answers I see include a sync. –   James Kanze  Jul 25 '11 at 16:12
 
@James : Mine was edited. –   kbok  Jul 25 '11 at 22:09
 
If that's true then this is a bit dangerous - what happens if a process writes a file in the background? Is the data written between sync() and open() just lost? –   Malvineous  Jun 17 '12 at 12:33
 
@ZanLynx: drop_caches is non-destructive and will only free clean caches (ones that have not been synced to disk). Thus calling sync() before doesn't make the operation any more safe, but rather increases the number of caches that can be dropped. See: /usr/src/linux/Documentation/sysctl/vm.txt –   kalaxy  Oct 10 '12 at 22:40
 
@kalaxy: When I used to run -mm kernels in 2008 it was definitely not safe. –   Zan Lynx  Oct 10 '12 at 22:51
 
If drop_caches is unsafe, your kernel is bugged. No modern kernel will have a problem with this. –  Paul Betts  Oct 10 '12 at 23:50



自己编写代码如下: 

#include <stdio.h>
#include <string.h>
#include <sys/types.h>
#include <sys/stat.h>
#include <fcntl.h>
#include <unistd.h>

#define TRUE 1
#define FALSE 0

typedef unsigned int INT32U;
typedef unsigned char INT8U;



INT8U Get_SysCachedMem(INT32U *cachedmem_size);
INT8U Clean_SysCachedMem(void);

int main(void)
{
	INT32U cachedmem_size;
	Get_SysCachedMem(&cachedmem_size);
	printf("cachedmem_size = %d\n", cachedmem_size);
	
	Clean_SysCachedMem();
		
	Get_SysCachedMem(&cachedmem_size);
	printf("cachedmem_size = %d\n", cachedmem_size);
	
	
	return 0;
}

INT8U Get_SysCachedMem(INT32U *cachedmem_size)
{
	char 	buffer[128];
	char 	args01[64];
	INT32U  args02;
	char 	args03[64];
	FILE 	*fp = NULL;
	INT8U	isGetCachedArgs = FALSE;
	
	fp = fopen("/proc/meminfo", "r");
	fseek(fp, 0, SEEK_SET);
	
	//从/proc/meminfo文件中一行一行过滤出cached参数
	memset(buffer, 0, sizeof(buffer));
	while (fgets(buffer, sizeof(buffer), fp) != NULL) 
	{
		sscanf(buffer, "%s\t%ld%s", args01, &args02, args03);
		printf("buffer = %s %d %s\n", args01, args02, args03);
	
		if(strncmp(args01, "Cached:", strlen("Cached:")) == 0)
		{
			isGetCachedArgs = TRUE;
			break;
		}
		
		memset(buffer, 0, sizeof(buffer));
	}
	fclose(fp);
	
	if( !isGetCachedArgs )
	{
		*cachedmem_size = 0;
		return FALSE;
	}
	
	*cachedmem_size = args02;
	
	return TRUE;
}

INT8U Clean_SysCachedMem(void)
{
	int fd;
	const char* data = "3";

	sync();
	fd = open("/proc/sys/vm/drop_caches", O_WRONLY);
	if(fd < 0)
	{
		return FALSE;
	}
	
	write(fd, data, sizeof(char));
	close(fd);

	return TRUE;
}


测试已通过



1、资源项目源码均已通过严格测试验证,保证能够正常运行; 2、项目问题、技术讨论,可以给博主私信或留言,博主看到后会第一时间与您进行沟通; 3、本项目比较适合计算机领域相关的毕业设计课题、课程作业等使用,尤其对于人工智能、计算机科学与技术等相关专业,更为适合; 4、下载使用后,可先查看README.md或论文文件(如有),本项目仅用作交流学习参考,请切勿用于商业用途。 5、资源来自互联网采集,如有侵权,私聊博主删除。 6、可私信博主看论文后选择购买源代码。 1、资源项目源码均已通过严格测试验证,保证能够正常运行; 2、项目问题、技术讨论,可以给博主私信或留言,博主看到后会第一时间与您进行沟通; 3、本项目比较适合计算机领域相关的毕业设计课题、课程作业等使用,尤其对于人工智能、计算机科学与技术等相关专业,更为适合; 4、下载使用后,可先查看README.md或论文文件(如有),本项目仅用作交流学习参考,请切勿用于商业用途。 5、资源来自互联网采集,如有侵权,私聊博主删除。 6、可私信博主看论文后选择购买源代码。 1、资源项目源码均已通过严格测试验证,保证能够正常运行; 2、项目问题、技术讨论,可以给博主私信或留言,博主看到后会第一时间与您进行沟通; 3、本项目比较适合计算机领域相关的毕业设计课题、课程作业等使用,尤其对于人工智能、计算机科学与技术等相关专业,更为适合; 4、下载使用后,可先查看README.md或论文文件(如有),本项目仅用作交流学习参考,请切勿用于商业用途。 5、资源来自互联网采集,如有侵权,私聊博主删除。 6、可私信博主看论文后选择购买源代码。
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值