Until recently I have just been developing applications for Windows where it
is straight forward to launch an application from another application, which
from that point runs completely independent. I have learned that things are
different in Linux where a fork and execv has to be used instead and I guess
this is the culprit.
When a child application is launched by fork and execv from parent
application it seems in some way to belong to the parent application. E.g.
when doing a "ps -fu root" the child is shown in square brackets. When
killing the child one way or another it is not killed completely and when
doing a "ps -fu root" it is still shown in square brackets but now with a
"<defunct>" added. Stopping or killing the parent gets rid of the defunct
child.
Question 1: Isn't it possible to launch an application from another
application and then get completely detached just like Windows? This would
probably resolve my problems.
Question 2: Any clue as to why it get stuck and doesn't release the PID
completely?
Thanks,
Jens.
fork twice let the intermediate process terminate
and have the parent wait at once for the
intermediate process.
>
> Question 2: Any clue as to why it get stuck and doesn't release the PID
> completely?
It is a zombie. A dead process cannot remove itself
from the task list, somebody else have to do. On
Unix the parent is responsible to do that, it is
done with one of the wait system calls that can do
a couple of things:
1) Wait for a child to terminate.
2) Collect the exist status of the child.
3) Remove the child from the task list.
The execve call doesn't change the parent/child
relationship. It only replaces the process image
of one of the processes with a new executable.
http://www.daimi.au.dk/~kasperd/comp.os.linux.development.faq.html#zombie
--
Kasper Dupont -- der bruger for meget tid paa usenet.
For sending spam use mailto:aaa...@daimi.au.dk
/* Would you like fries with that? */
That will do the trick. However, in the meantime I found another solution
that I think I like better.
What I am doing is an application for an embedded system and I want it to be
able to dowload itself from the host PC application. Until now my idea was
to have one small application to handle executable download, starting,
stopping etc. and then the real application doing the required automation
task in the hope that the small command application would be so small,
simple and bugfree that there would general need for downloading it. Well I
realized that in Linux you can actually delete the application file you are
currently running, so here is what I do: The now only application is started
and kept going by adding a respawn in the inittab file. It will respond to
the PC's UDP pings and if the PC figures out is has a newer version, it will
send and overwrite the existing one and stop the program. Linux will then
immediately restart it, but it will then be the new version.
I know it is a dirty trick, but it saves me for keeping track of two
applications, which can be tedious to debug, get out of sync and in addition
allow me to update everything from the PC.
Thoughts?
Your link:
http://www.daimi.au.dk/~kasperd/comp.os.linux.development.faq.html
seems very useful - thanks.
Hilsen,
Jens.
>
> fork twice let the intermediate process terminate
> and have the parent wait at once for the
> intermediate process.
>
>
> E.g.> when doing a "ps -fu root" the
> child is shown in square brackets. When killing the child one
> way or another it is not killed completely and when
> doing a "ps -fu root" it is still shown in square brackets
> but now with a "<defunct>" added. Stopping or killing the
> parent gets rid of the defunct child.
This is because the OS doesn't know if and when the parent
process is going to ask for the exit status of the child.
The OS has to keep it around in order to give it to the parent
when it asks for it. It is kept in the process's data that is
managed by the kernel. Naturally, there is no process anymore,
just this information, so the process is "defunct" or a zombie.
It is only when the parent asks for the exit status or
terminates itself that the zombie vanishes.
The idea is, that if a process spawns another process, it is
going to ask for the exit status of the child sooner or later.
Imagine a backup program gathering files and the spawning
another process to write it to tape. It would nice to know if
the write to tape failed. That's what an exit status is for.
HTH
Martin