• criu简单例子


    CRIU(Checkpoint/Restore In Userspace)是运行在linux操作系统上的一个开源软件,其功能是在用户空间实现Checkpoint/Restore功能。

    github地址如下:https://github.com/checkpoint-restore/criu

    本人选取的版本是3.12,本人的操作系统是centos7.6

    先写一个简单的程序代码(criutest.c):

    #include 
    #include 
    #include 
    
    int main()
    {
    	int i = 0;
    	FILE *fp = NULL;
    	char szWrite[50] = {0};
    	fp = fopen("/chkpnt/criutest.txt", "a");
    	if(fp==NULL)
    	{
    		printf("fopen failed\n");
    		return -1;
    	}
    	while(1)
    	{
    		printf("[%d], Hello world!\n", i);
    		sprintf(szWrite, "[%d], Hello world!\n", i);
    		fwrite(szWrite, strlen(szWrite), 1, fp);
    		fflush(fp);
    		sleep(3);
    		i++;
    	}
    
    	return 0;
    }
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11
    • 12
    • 13
    • 14
    • 15
    • 16
    • 17
    • 18
    • 19
    • 20
    • 21
    • 22
    • 23
    • 24
    • 25
    • 26
    • 27

    该程序会往控制台以及文件中写入字符串,
    在这里插入图片描述

    现在开始dump该进程,代码如下(libcriudumptest.c):

    #include "criu.h"
    #include 
    #include 
    #include 
    #include 
    #include 
    #include 
    
    int main(int argc, char **argv)
    {
    	int pid = 0;
    	int fd = 0;
    	int ret = 0;
    	int childpid = 0;
    
    	if(argc < 2)
    	{
    		printf("argc is 1\n");
    		return -1;
    	}
    	pid = atoi(argv[1]);
    
    	criu_init_opts();
    	criu_set_pid(pid);
    	criu_set_leave_running(false);
    	fd = open("/chkpnt/img/", O_DIRECTORY);
    	criu_set_images_dir_fd(fd);
    	criu_set_shell_job(true);
    	ret = criu_dump();
    	if(ret < 0)
    	{
    			printf("criu_dump failed\n");
    			return -1;
    	}
    	printf("criu_dump succeed\n");
    	return 0;
    }
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11
    • 12
    • 13
    • 14
    • 15
    • 16
    • 17
    • 18
    • 19
    • 20
    • 21
    • 22
    • 23
    • 24
    • 25
    • 26
    • 27
    • 28
    • 29
    • 30
    • 31
    • 32
    • 33
    • 34
    • 35
    • 36
    • 37

    其中该程序运行时,需要输入一个参数,待dump的进程pid,dump路径代码中写死了,为/chkpnt/img。
    在这里插入图片描述
    可以看到,显示dump成功,在/chkpnt/img目录下,可以看到生成的dump文件
    在这里插入图片描述

    代码中criu_set_leave_running(false)表示dump后杀死进程,criutest最后的运行结果如下:
    在这里插入图片描述
    可以看到,criutest在打印到98后,被dump然后接着被杀死。
    恢复运行时,就应该从99开始。

    现在编写restore功能,代码如下(libcriurestoretest.c):

    #include "criu.h"
    #include 
    #include 
    #include 
    #include 
    #include 
    #include 
    
    int main(int argc, char **argv)
    {
    	int fd = 0;
    	int ret = 0;
    	char *dumpdir;
    	if(argc < 2)
    	{
    	printf("argc is 1\n");
    	return -1;
    	}
    	dumpdir = argv[1];
    
    	criu_init_opts();
    	fd = open(dumpdir, O_DIRECTORY);
    	criu_set_images_dir_fd(fd);
    	criu_set_shell_job(true);
    	ret = criu_restore();
    	if(ret < 0)
    	{
    			printf("criu_restore failed\n");
    			return -1;
    	}
    	printf("criu_restore succeed\n");
    	return 0;
    }
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11
    • 12
    • 13
    • 14
    • 15
    • 16
    • 17
    • 18
    • 19
    • 20
    • 21
    • 22
    • 23
    • 24
    • 25
    • 26
    • 27
    • 28
    • 29
    • 30
    • 31
    • 32
    • 33

    很明显,该程序需要一个输入参数,dump路径,运行该程序,如下图所示:
    在这里插入图片描述
    很明显,打印从99开始。

  • 相关阅读:
    Web1到 Web3,互联网经历了什么?
    [极客大挑战 2019]LoveSQL1 题目分析与详解
    【1774. 最接近目标价格的甜点成本】
    sql server 查询所有表所有字段中包含某个字符
    MAC如何在根目录创建文件
    618大促:手机品牌“神仙打架”,高端市场“谁主沉浮”?
    【Image captioning】ruotianluo/self-critical.pytorch之4—模型训练之train.py代码解析
    Windows环境单节点部署kafka最新版本3.2.1实战(超简单)
    计算机领域8月SCI/EI期刊列表已更新,是你在找的1区TOP审稿快刊吗?
    Springboot写电商系统(2)
  • 原文地址:https://blog.csdn.net/tusong86/article/details/133648063