Need to reexecute the same process

Hello All,

I am running a program and inside that I need to so a check . if some process failed I need to wait for 3 minute and reexcute the same process with similar argument. Is this possible ? Can i do it with currect process id ?

Thanks,
Arun

A process ID is assigned by the kernel when a process is forked. After that process dies, no other process will get that same process ID for a "long time". Normally process IDs start at 1 for the 1st process started after the kernel is booted and are assigned sequentially until the highest process ID used by that operating system is assigned. Then process IDs (that are not still in use) are recycled starting from the lowest no longer active process ID.

A process is an abstraction of a running program in memory. You can't re-execute a process, but you can re-execute a program. Just start the program with the same arguments - that's it. If you don't know with which arguments the program was started before, you can check it with ps while the process is still running. If the process already finished its execution, there is no way to know how it was started.

Assuming the process has made no significant changes to its arguments, environment variables, and file descriptors, it's not that hard to re-execute the exact same process.

As long as you can get the original arguments, anyway.

In general, with C/C++:

exec( argv[ 0 ], argv );

With Bourne shell:

exec $0 "$@"

There are some things to be aware of - a process that changes directory might make the arguments "wrong", and if a process is started with a different zeroth argument than the actual command/executable file name. For example, a login bash shell is usually started with the zeroth argument set to "-bash", even though the bash executable is usually something like /bin/bash. If you try to do "exec $0" from a bash login shell, it'll probably fail.

The problem in C/C++ is getting the original arguments if you're deep inside an executable. On Linux, you can probably find the original arguments in the global "__libc_argv" symbol. On Solaris, it's "___Argv". These are in libc.so on both operating systems, which is linked into just about every running process when it starts. (Linux binaries can still be statically linked, so beware if you're dealing with one of those.)

And in any case they have to be preserved. If something along the way modifies them, you can't recover them.