在linux中保存会话或进程状态

时间:2022-03-14 15:30:55

I have to create a functionality for my project like saving session and further resume it from the same position in future. So I need to know how save the state of a process and then read from disk and resume it afterwards.

我必须为我的项目创建一个功能,比如保存会话,并在将来从同一个位置进一步恢复它。所以我需要知道如何保存进程的状态,然后从磁盘读取并在之后恢复它。

PS. The application is an interactive shell for a language and I need to extend it include a save session and resume session. But we want the code to be generic enough to be used in other places too.

PS。该应用程序是一种语言的交互式shell,我需要扩展它包括一个保存会话和恢复会话。但我们希望代码足够通用,以便在其他地方使用。

2 个解决方案

#1


1  

This is quite a challenging task. The basic idea is to interrupt the process with a signal, at which point the OS puts the state of all registers (including the instruction pointer) in memory where you can access them if your shell has spawn the process you want to interrupt.

这是一项极具挑战性的任务。基本思想是用一个信号中断进程,此时操作系统将所有寄存器(包括指令指针)的状态放入内存,如果你的shell已经产生了你想要中断的进程,你可以访问它们。

For more detail, you can look how checkpointing utilities handle that problem:

有关更多详细信息,您可以查看检查点工具如何处理该问题:

#2


0  

That is quite hard to answer in general other than “save the program's entire state to a file and load it from there on resume”. That can be very tricky because you might need to restore things like file handles and sockets, which may not even be possible if things have changed during the suspended state. But it may suffice for you to support something less than that and only save the information necessary to approximate the previous state (e.g., save the list of program files to load, or the save user's command history and replay it, etc).

除了“将程序的整个状态保存到文件并在恢复时从那里加载”之外,这一点很难回答。这可能非常棘手,因为您可能需要恢复文件句柄和套接字之类的东西,如果在挂起状态期间事情发生了变化,甚至可能无法恢复。但是,您可以支持少于此的东西,并且只保存接近先前状态所需的信息(例如,保存要加载的程序文件列表,或保存用户的命令历史记录并重放它等)。

#1


1  

This is quite a challenging task. The basic idea is to interrupt the process with a signal, at which point the OS puts the state of all registers (including the instruction pointer) in memory where you can access them if your shell has spawn the process you want to interrupt.

这是一项极具挑战性的任务。基本思想是用一个信号中断进程,此时操作系统将所有寄存器(包括指令指针)的状态放入内存,如果你的shell已经产生了你想要中断的进程,你可以访问它们。

For more detail, you can look how checkpointing utilities handle that problem:

有关更多详细信息,您可以查看检查点工具如何处理该问题:

#2


0  

That is quite hard to answer in general other than “save the program's entire state to a file and load it from there on resume”. That can be very tricky because you might need to restore things like file handles and sockets, which may not even be possible if things have changed during the suspended state. But it may suffice for you to support something less than that and only save the information necessary to approximate the previous state (e.g., save the list of program files to load, or the save user's command history and replay it, etc).

除了“将程序的整个状态保存到文件并在恢复时从那里加载”之外,这一点很难回答。这可能非常棘手,因为您可能需要恢复文件句柄和套接字之类的东西,如果在挂起状态期间事情发生了变化,甚至可能无法恢复。但是,您可以支持少于此的东西,并且只保存接近先前状态所需的信息(例如,保存要加载的程序文件列表,或保存用户的命令历史记录并重放它等)。