Operating System - HP-UX
1847780 Members
3474 Online
104021 Solutions
New Discussion

Re: Restore c programs data segment to reset globals

 
James Myers
Occasional Contributor

Restore c programs data segment to reset globals

We have a large application we currently fork once per process. I would like to change it to loop so it does not need to be forked every time, but I beleive we have lots of code where global variabless are changed to non-initialized values, and the process would need all variables as if its the first time its running to behave consistently (no matter what).

Is there a simple way to save the state of all global and static variables before the program runs, and restore the values.

Can I save the entire Text segment before execution, and restore it for the next cycle/thread. Is it text or data I need ?

What calls can I use to store and retrieve the entire text segment.

How efficient would this be compared to forking the programs.
6 REPLIES 6
A. Clay Stephenson
Acclaimed Contributor

Re: Restore c programs data segment to reset globals

Looping would be much more efficient than fork()'ing a new process although the downside is that you are now singly-threaded. There is no system call to load just the text segment nor would you want to because the text is already loaded but you would need a fresh data segment and there's no way to load that by itself either. You could do a man 3 end but it's not going to help.

My approach would be to put all your globals
inside a struct and initialize the values once. They remain untouched and each loop uses its own struct copied from the original in a single assignment statement.
If it ain't broke, I can fix that.
Biswajit Tripathy
Honored Contributor

Re: Restore c programs data segment to reset globals

One way would be to write a new routine (say
save_current_state()) to store the state (i.e current
values of all the global variables) into a file (in binary
format). Write another routine, say
load_saved_state(), and read the saved binary file
exactly the way you stored. This is a simple technique
that should work fine. You could even define a new
large data structure with each field storing one of the
global variable and write/read the data structure in
one fwrite()/fread() call. You could name the states
and store/retrieve multiple states etc. etc..

- Biswajit
:-)
Biswajit Tripathy
Honored Contributor

Re: Restore c programs data segment to reset globals

One more thing. My previous reply is more suited if
you want to save/restore the state of the program
from one instance to another (like, if it gets killed and
restarts with the previous state restored). If you are
not doing that, then writing to / reading from file is not
a good idea (slow disk read/write). In that case, you
could follow my last suggestion, but avoid storing in a
file and store in memory itself.

- Biswajit

:-)
James Myers
Occasional Contributor

Re: Restore c programs data segment to reset globals

I guess my question was more about if there was any useful system calls to grab all memory associated with ALL global variables without having to know what they all are, or access them individually.

This project is mature and has had and will have many people working on it, and theres lotes of variables, and I'm sure they will continue to be added, If I had a universal type store and restore, those developers would not have to make any changes, they could just keep using globals, and I would not have to worry about missing any.

Is there something to give me the address of this segment I need, and could I just save the whole segmetn and restore the whole thing,
Stephen Keane
Honored Contributor

Re: Restore c programs data segment to reset globals

Where global variables are stored is pretty much down to the compiler/machine architecture combination.

The text segment is the code, not the variables. The data segment stores the variables.

I'm still not sure I understand what it is you are trying to achieve in the first place mind you.

A. Clay Stephenson
Acclaimed Contributor

Re: Restore c programs data segment to reset globals

What you are asking for just doesn't exist and you aren't even thinking very deeply. When you say "all global variables", you mean all of "your" global variables but how about global variabes like errno or those associated with library functions (e.g. *optarg,optind,opterr,optopt associated with getopt() --- as an example off the top of my head)? It would be extremely difficult for any utility to distinguish between "your" global variables and the others.

Large projects like yours are a good example why global variables should be used sparingly because there can be all sorts of side effects. Again, the least evil solution to your situation that I can conceive of is to gather all your globals inside a struct (and yes that means code changes though even if this were 50 files, I think I could do it easily in a day).
If it ain't broke, I can fix that.