开发者

Retaining variable values from one execution to another

开发者 https://www.devze.com 2023-03-02 02:01 出处:网络
Hi Consider I have two programs a.c and b.c. The program b.c is executed a number of times from a.c using execve(). I wish t开发者_Python百科o retain the values of the variables and data structures de

Hi Consider I have two programs a.c and b.c. The program b.c is executed a number of times from a.c using execve(). I wish t开发者_Python百科o retain the values of the variables and data structures defined in b.c from one execution to another. Is there a way to do it?

Thanks


You could have a.c set up a chunk of shared memory and then b.c could store its state data in that shared memory. Of course b.c would have to depend on a.c to manage its state but that shouldn't be a problem if b.c exists only to be called by a.c.

Alternatively, you could store all the state data in a single struct and use fwrite and fread to store and retrieve the state. Or you could use mmap to make this approach easier. You'd have to be careful if some of your state involved a pointer though. You don't have to use a single struct for this approach but it would be easier.


have b.c load and save its state from and to a file.

0

精彩评论

暂无评论...
验证码 换一张
取 消

关注公众号