 |
|
 |
|
05-23-05 07:48 AM
Hi,
I'm just playing around with forking processes and was wondering how can you
keep a child alive as long as the parent is alive. To give an example here
is my code.
-----------------------------------------------------------------------
for(i=0; i<2; i++) {
if((pid = fork()) == -1) //run the fork command
{
perror("fork");
}
else if (pid == 0)
{
if (i ==0) //using parent process
{
//start the timer function
time = 20;
timer(time);
}
else if () //using children processes
{
//start the timer function
timer = 10;
timer(time);
exit(1);
}
}
void timer(int time)
{
sleep(time);
}
-------------------------------------------------------
I want to keep the child alive even though his timer is smaller than the
parents. I need to check if the parent is still alive before killing the
child, if the parent is then allow the child to remain alive.
Regards,
Matt
[ Post a follow-up to this message ]
|
|
|
 |
|
 |
|
 |
|
05-23-05 01:00 PM
On 2005-05-23, Gvs <trishw@westnet.com.au> wrote:
> Hi,
>
> I'm just playing around with forking processes and was wondering how can y
ou
> keep a child alive as long as the parent is alive. To give an example here
> is my code.
>
> -----------------------------------------------------------------------
> for(i=0; i<2; i++) {
>
> if((pid = fork()) == -1) //run the fork command
> {
> perror("fork");
> }
>
> else if (pid == 0)
> {
> if (i ==0) //using parent process
> {
> //start the timer function
> time = 20;
> timer(time);
> }
> else if () //using children processes
> {
> //start the timer function
> timer = 10;
> timer(time);
> exit(1);
> }
> }
>
> void timer(int time)
> {
> sleep(time);
> }
>
> -------------------------------------------------------
Hm. Your code is incorrect, and does not illustrate/explain what you are
trying to ask. Normally the child stays alive even if the parent exits.
Well. If you hit Ctrl-C in the shell, then shell will kill both parent
and the child. So, if you want to avoid this, you should detach child
from parent by for example using 'daemon' function.
>
> I want to keep the child alive even though his timer is smaller than the
> parents. I need to check if the parent is still alive before killing the
> child, if the parent is then allow the child to remain alive.
Even more confusing. If the child has shorter timer than parent, then
after the timer has expired, the child will terminate, since it has
nothing else to do. If you want it to stay alive, then tell it to do so.
If it is supposed to wait for a signal from parent, then establish
signal handler and call sigsuspend. Or maybe parent and child
communicate thru pipe? Then just read from pipe. This will block the
child untill the parent writes something into that pipe.
--
Minds, like parachutes, function best when open
[ Post a follow-up to this message ]
|
|
|
 |
|
 |
|
 |
|
05-23-05 01:00 PM
Hello Matt,
> I'm just playing around with forking processes and was wondering how
can you
> keep a child alive as long as the parent is alive.
Strange requirement... Usually, that's the other way around: you keep
the parent alive as long as the child [and Unix offers functions for
that: wait() and waitpid()]...
> I want to keep the child alive even though his timer is smaller than
the
> parents. I need to check if the parent is still alive before killing
the
> child, if the parent is then allow the child to remain alive.
What are you trying to achieve exactly?
Of course, you can wait in the child for the parent to terminate. But
you have to use indirect mechanisms.
Cheers,
Loic.
[ Post a follow-up to this message ]
|
|
|
 |
|
 |
|
 |
|
05-23-05 10:59 PM
loic-dev@gmx.net wrote:
> Hello Matt,
>
>
>
> can you
>
>
>
> Strange requirement... Usually, that's the other way around: you keep
> the parent alive as long as the child [and Unix offers functions for
> that: wait() and waitpid()]...
>
>
>
> the
>
>
> the
>
>
>
> What are you trying to achieve exactly?
>
> Of course, you can wait in the child for the parent to terminate. But
> you have to use indirect mechanisms.
>
> Cheers,
> Loic.
>
If you use the getppid call, you can see if your parent is a process,
or 1 if it is init. As long as getppid returns > 1, you can keep your
child alive. However, I would recommend using a sleep between calls
to getppid to prevent a hard spin in your code. I've used this tactic
to identify if the parent has died in the past.
--
Fletcher Glenn
[ Post a follow-up to this message ]
|
|
|
 |
|
 |
|
 |
|
05-23-05 10:59 PM
"Gvs" <trishw@westnet.com.au> wrote:
# Hi,
#
# I'm just playing around with forking processes and was wondering how can y
ou
# keep a child alive as long as the parent is alive. To give an example here
# is my code.
One way is to have a lock file for each process which it does an exclusive l
ock on.
Other processes can then try to get an exclusive lock without waiting; if th
ey
succeed, the other process has died, because the kernel frees locks on proce
ss exit.
You have to lock at the appropriate places if locks can be inheritted over f
orks.
--
SM Ryan http://www.rawbw.com/~wyrmwif/
Wow. A sailboat.
[ Post a follow-up to this message ]
|
|
|
 |
|
 |
|
 |
|
05-23-05 10:59 PM
Good Evening,
Glen> If you use the getppid call, you can see if your parent is a
Glen> process, or 1 if it is init. As long as getppid returns > 1, you
Glen> can keep your child alive. However, I would recommend using a
Glen> sleep between calls to getppid to prevent a hard spin in your
Glen> code. I've used this tactic to identify if the parent has died in
Glen> the past.
This is a possible solution. The only drawback is that you're polling
for parent termination.
Ryan> One way is to have a lock file for each process which it does an
Ryan> exclusive lock on. Other processes can then try to get an
Ryan> exclusive lock without waiting; if they succeed, the other process
Ryan> has died, because the kernel frees locks on process exit.
Ryan>
Ryan> You have to lock at the appropriate places if locks can be
Ryan> inheritted over forks.
Also an interesting approach... But here too, you have to poll for
parent termination.
Note however that you might choose to wait until the lock is granted. If
you perform the wait in another thread, you may that way be notified
asynchronously that the parent has terminated.
Another possibility that comes to my mind is to create a pipe between
the parent and the child (parent = write end, child = read end). The
child shall see an "end of file" - that is, read() returns 0 - when the
parent terminates while reading from the pipe.
HTH,
Loic.
[ Post a follow-up to this message ]
|
|
|
 |
|
 |
|
 |
|
05-24-05 01:49 AM
In article <d6s533$4pr$04$1@news.t-online.com>,
Thomas Reinhardt <reinhardt.thomas@t-online.de> wrote:
> Hi Gvs,
>
> Take a look at wait or waitpid. These should do the trick.
Wait() and waitpid() only allow you to wait for a child process. He
wants the child to wait for the parent.
--
Barry Margolin, barmar@alum.mit.edu
Arlington, MA
*** PLEASE post questions in newsgroups, not directly to me ***
[ Post a follow-up to this message ]
|
|
|
 |
|
|
|
|
Sponsored Links |
 |
 |
|
|
 |
All times are GMT. The time now is 06:37 AM. |
 |
|
|
 |
|
 |
|
|
 |
|
Forum Rules:
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts
|
HTML code is OFF
vB code is ON
Smilies are ON
[IMG] code is OFF
|
|
|
|
Medical and Health forum | Computer Games Reviews | Graphics design forum
|
 |
|
 |
|