Ubuntu fread占用过多cache

john_liqinghan john_liqinghan 墨衍会员 · AI 创作全网分发 墨衍智能分发 通过墨衍一键同步至各平台 可同步平台 CSDN微信公众号微博知乎掘金百家号博客园抖音小红书 你的文章也可以这样分发 墨衍会员 ¥399起/年,写一次发全网 我也要 + 加入社区
最近在ubuntu的平台上碰到这个问题,使用fopen和fread读取文件发现会把ubuntu的cache占用的很大,基本是文件有多大,cache就得有多大?在嵌入式海思的平台上使用,如果使用多个fread读取大文件,就会把cache占用尽了,内存就损耗过度!发生oom killer , Out of memory这种问题!测试代码如下:
#include <stdio.h>
#include <unistd.h>
#include <stdlib.h>
#include <string.h>
#include <signal.h>
#include<fcntl.h>

unsigned char *pu8Buf = NULL;

void ExitHandleSig(int signo){
if (SIGINT == signo || SIGTSTP == signo){
if(pu8Buf != NULL) free(pu8Buf);
printf("\033[0;31mprogram exit abnormally!\033[0;39m\n");
}
exit(0);
}
int main(){

#if 1
//fread fopen 版本
FILE *fpStrm=NULL;

int s32UsedBytes = 0, s32ReadLen = 0;
unsigned int bufSize=1280 * 720 * 3 / 2;
pu8Buf = (unsigned char*)malloc(bufSize);
fpStrm = fopen("../videofile/1280_720_H264.h264", "rb");
if(fpStrm == NULL){
printf("Can't open file \n");
exit(0);
}

signal(SIGINT, ExitHandleSig);
signal(SIGTERM, ExitHandleSig);

while(1){
fseek(fpStrm, s32UsedBytes, SEEK_SET);
s32ReadLen = fread(pu8Buf, 1, bufSize, fpStrm);
if(s32ReadLen != bufSize){
printf("end of file \n");
fseek(fpStrm, 0, SEEK_SET);
s32UsedBytes = 0;
s32ReadLen = 0;
}
s32UsedBytes = s32UsedBytes +s32ReadLen ;
sleep(5);
}
#else
//read open 版本

int fileFd;
int s32UsedBytes = 0, s32ReadLen = 0;
unsigned int bufSize=720 * 480 * 3 / 2;
pu8Buf = (unsigned char*)malloc(bufSize);
fileFd = open("../videofile/720_480_H264.h264",O_RDONLY,S_IRUSR);
if(fileFd == -1)
printf("Can't open file \n");

signal(SIGINT, ExitHandleSig);
signal(SIGTERM, ExitHandleSig);

while(1){
lseek(fileFd, s32UsedBytes, SEEK_SET);
s32ReadLen = read(fileFd,pu8Buf,bufSize);
if(s32ReadLen != bufSize){
printf("end of file \n");
lseek(fileFd, 0, SEEK_SET);
s32UsedBytes = 0;
s32ReadLen = 0;
}
s32UsedBytes = s32UsedBytes +s32ReadLen ;
usleep(50000);
}
#endif
return 1;
}


无论是fread还是read都会如此!Ubuntu怎么有这个bug?
...全文

相关推荐

ubuntu中总是提示无法获得锁 (资源暂时不可用) 进程被占用 解决办法

ubuntu中总是提示无法获得锁 (资源暂时不可用) 进程被占用 解决办法,虽然有很多相关的帖子,但还是没解决掉这个问题,琢磨了下发现这样是可行的 root@klw-HP-Z400-Workstation:/# apt install npm E: Could not get lock /var/lib/dpkg/lock-frontend. It is held by process 1475 (unattended-upgr) - open (11: 资源暂时不可用) N: Be aware that removing the lock file is not a solution and may break your system. E: 无法获取 dpkg 前端锁 (/var/lib/dpkg/lock-frontend),是否有其他进程正占用它? root@klw-HP-Z400-Workstation:/# sudo rm /var/lib/dpkg/lock rm: 无法删除'/var/lib/dpkg/lock': 没有那个文件或目录 root@klw-HP-Z400-Workstation:/# sudo rm /var/cache/apt/archives/lock rm: 无法删除'/var/cache/apt/archives/lock': 没有那个文件或目录 #上面的无法解决,注意错误提示信息 在用ps-e |grep apt 也找不到PID的情况下 E: Could not get lock /var/lib/dpkg/lock-frontend. It is held by process 1475 (unattended-upgr) - open (11: 资源暂时不可用) 这里被占用了PID为1475 在用ps-e |grep apt 也找不到PID的情况下 kill -9 1475就可以解决调了

快了的农民工

查看ubuntu系统资源占用命令大全.docx

查看ubuntu系统资源占用命令大全.docx

Lidx0710

Ubuntu提示无法获锁进程被占用解决

资源下载链接为: https://pan.quark.cn/s/22ca96b7bd39 在Ubuntu系统中,如果出现“无法获得锁(资源暂时不可用)进程被占用”的提示,可以尝试以下方法解决。当使用命令“apt install npm”时,可能会遇到类似“E: Could not get lock /var/lib/dpkg/lock-frontend. It is held by process 1475 (unattended-upgr) - open (11: 资源暂时不可用)”的错误。此时,按照常规的解决办法,如执行“sudo rm /var/lib/dpkg/lock”和“sudo rm /var/cache/apt/archives/lock”命令来删除锁文件,但可能会提示“没有那个文件或目录”,说明这种方法无法解决问题。 进一步分析错误提示信息,可以发现是进程1475(unattended-upgr)占用了锁。然而,在使用“ps -e | grep apt”命令查找PID时,却找不到对应的PID。这种情况下,直接使用“kill -9 1475”命令来终止进程1475,就可以成功解决问题。

crossfiredashi

cache性能分析实验

该报告利用simple scalar模拟器,针对cache的性能进行分析

snowleopard_bin

Ubuntu离线安装软件的方法

ubuntu apt-get方法安装软件都是依赖网络的,而自己下载相应的软件包又可能存在依赖的问题,用这种方法可以在本地建立源,这样不用上网也可以采用apt-get方法安装软件又不用考虑依赖关系的问题

吃月饼
评论 3
收藏 已收藏
分享
返回顶部