gpt4 book ai didi

c - 在 C 中写入大量文件后 Ubuntu 死机

转载 作者:太空宇宙 更新时间:2023-11-03 23:58:01 31 4
gpt4 key购买 nike

我的整个系统(Ubuntu 18.04)总是在我的 c 程序不断将一些日志写入文件大约一小时后卡住。创建的每个文件大约为 100 到 200MB,系统停机前这些文件的总量约为 40-60GB。通常,此时我还有 150GB 的可用 SSD 空间。

我通过系统监视器检查了系统状况,但没有发现任何问题。当我的程序运行时,八个内核中只有一个具有 100% 使用率。其他的都挺低的。系统宕机前,15.5GB内存只使用了2.5GB。每次我重新启动机器时,最新创建的 4-6 个文件都是空的。尽管它们中的大多数在卡住的那一刻都显示出一些尺寸。 (看起来它们并没有真正写入 SSD)

我的c代码可以简化如下:

#define MEM_LEN 50000 
#define FILE_LEN 10000*300

struct log_format {
long cnt;
long tv_sec;
long tv_nsec;
unsigned int user;
char rw;
char pathbuffer[256];
size_t count;
long long pos;
};

int main(int argc, const char *argv[])
{
int fd=0;
struct log_format *addr = NULL;
int i=0;
FILE *file;
char filestr[20];
int data_cnt = 0;
int file_cnt =0;

// open shared memory device //
fd = open("/dev/remap_pfn", O_RDWR);
if (fd < 0) {
perror("....open shared memory device1 failed\n");
exit(-1); }
// memory mapping to shared memory device //
addr = mmap(NULL, BUF_SIZE, PROT_READ | PROT_WRITE, MAP_SHARED | MAP_LOCKED, fd, OFFSET);
if (!addr) {
perror("....mmap1 failed\n");
exit(-1); }

// open a file //
sprintf(filestr, "%d.csv", file_cnt);
file = fopen(filestr, "w");
printf("%s created\n",filestr);

// continuously check the memory replacement of last, and write to file //
while(1){

fprintf(file, "%lu,%lu,%lu,%u,%c,%s,%zu,%lld\n", addr[i].cnt, addr[i].tv_sec,
addr[i].tv_nsec, addr[i].user, addr[i].rw, addr[i].pathbuffer,
addr[i].count, addr[i].pos);
i++;
data_cnt++;
if(i>=MEM_LEN)
i=0;

// when reaching a threshold, create another file to write //
if(data_cnt>=FILE_LEN){
data_cnt = 0;
fclose(file);
file_cnt++;

// open a file //
sprintf(filestr, "%d.csv", file_cnt);
file = fopen(filestr, "w");
printf("%s created\n",filestr);
}
}

fclose(file);

return 0;
}

我没有从 syslog 和 kern.log 中找到任何错误消息。它只是卡住。有谁知道可能是什么问题。谢谢。

最佳答案

我试图在我的 While 循环中添加一些延迟,以减慢写入速度:(因为 1 纳秒对于循环来说仍然太长,我让它每 10 次运行只休眠)

While(1){
struct timespec ts = {0,1L};
if(data_cnt%10==0)
nanosleep(&ts, NULL);
......
}

卡住问题现在似乎消失了。

所以...这可能是什么原因?目前,我只看到 Write 变慢了,并且该内核的 CPU 负载下降到 50%。中间是否有写入缓冲区,我的程序超出了它的限制并破坏了系统?

(如果是过热问题导致机器宕机我也会跟踪)

关于c - 在 C 中写入大量文件后 Ubuntu 死机,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/57225486/

31 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com