Path: utzoo!utgpu!news-server.csri.toronto.edu!cs.utexas.edu!sdd.hp.com!uakari.primate.wisc.edu!zaphod.mps.ohio-state.edu!rpi!uupsi!sunic!news.funet.fi!funic!santra!fuug!demos!avg From: avg@hq.demos.su (Vadim G. Antonov) Newsgroups: comp.unix.questions Subject: Re: Restarting a process after a shutdown Message-ID: <1990Sep3.105523.8220@hq.demos.su> Date: 3 Sep 90 10:55:23 GMT References: <5759@ethz.UUCP> Reply-To: avg@hq.demos.su (Vadim G. Antonov) Organization: DEMOS, Moscow, USSR Lines: 58 In article <5759@ethz.UUCP> tilo@tilo@nmr.lpc.ethz.ch (Tilo Levante) writes: >Is there a way to save the status of a process and restart it >after the shutdown? Of course, Unix allows you to do such things. You can do it as follows: Write a signal handler for SIGKILL: - scan through file descriptors (with fstat) and detect opened ones and their types (TTY, disk file, directory) and offsets from the beginning; - open a save-file - store open files status information into the file You have to keep names of opened files (use your own version of _open and _create syscalls). - save the stack (from the top of stack, depends on your system/machine) to this file by write syscall - save the data segment (from _etext ptr to _end ptr or from address 0 to _end - if you use a separate mode). Some systems require more detailed investigation of segments layout. - make setjmp C-library routine with the given jmp_buf IF it returns 0 THEN save jmp_buf into the file _exit(0) FI - read file descriptors information from the save-file and perform appropriate setup (open files, seek, etc) - perform a program re-initialization actions (it may be redrawing screen, etc) - return Write a special branch into _main: - if there is no save file, run as usually - otherwise call an assembly routine, which goes the following way: - expands the stack by touching the stack's lower memory cell (or calls a stack-expanding routine on some systems) - reads the stack segment in (using read system call, NOT read C-library routine) - reads the data segment - restores the stack ptr & instruction pointer from the saved jmp_buf Of course, this way is extremely tricky and requires an assembly programming and the brilliant knowledges on internals of the ur system; but it's possible. I've done such a thing seven years ago on Unix v6. Lucky Hacking! Vadim Antonov, DEMOS, Moscow, USSR (It is NOT a joke!)