这个就直接上代码了。
#include<stdio.h>
#include <sys/time.h>
#include <sys/resource.h>
#define CORE_SIZE 1024 * 1024 * 500
int core_file_start()
{
struct rlimit rlmt;
if (getrlimit(RLIMIT_CORE, &rlmt) == -1) {
return -1;
}
printf("Before set rlimit CORE dump current is:%d, max is:%d\n", (int)rlmt.rlim_cur, (int)rlmt.rlim_max);
rlmt.rlim_cur = (rlim_t)CORE_SIZE;
rlmt.rlim_max = (rlim_t)CORE_SIZE;
if (setrlimit(RLIMIT_CORE, &rlmt) == -1) {
return -1;
}
if (getrlimit(RLIMIT_CORE, &rlmt) == -1) {
return -1;
}
printf("After set rlimit CORE dump current is:%d, max is:%d\n", (int)rlmt.rlim_cur, (int)rlmt.rlim_max);
return 0;
}
int main()
{
core_file_start();
sleep(1);
int *p = NULL;
*p = 0;
return 0;
}
编译运行:
如果要指定core文件生成的路径,参考:https://blog.csdn.net/zhubao124/article/details/104905111
参考链接:
https://www.21ic.com/tougao/article/4073.html