Re: Terminating subtasks reliably
Re: Terminating subtasks reliably
- Subject: Re: Terminating subtasks reliably
- From: Greg Parker <email@hidden>
- Date: Tue, 30 Mar 2010 13:20:39 -0700
On Mar 30, 2010, at 1:01 PM, McLaughlin, Michael P. wrote:
> I have a Cocoa app (Leopard) which launches several Foundation Tool subtasks
> (since threads are not sufficient in this case). Currently, I terminate
> these subtasks via the app-delegate method
>
> -(NSApplicationTerminateReply)applicationShouldTerminate:(NSApplication
> *)sender
>
> This works provided one Quits the main app using the Quit menu item.
> However, if I force-quit or if I abort using the Stop icon in Xcode, then
> the main app terminates without terminating the subtasks.
>
> Is there a more reliable hook (e.g., delegate method) that would always get
> called even on force-quit or Stop from Xcode so that these subtasks would
> always get terminated? [Otherwise, I have to terminate them from Activity
> Monitor.]
There's no perfectly-reliable way for the parent task to kill the child. In the worst case, the parent stops suddenly because of a hang or deadlock and never executes another instruction before being killed. You could try adding signal handlers and atexit() handlers to catch more cases, but you'll never catch them all.
The reliable solution is to provide a way for the child to notice when the parent dies, and quit itself in response. This usually means opening an IPC channel between the parent and child, like a pipe or Mach port. No data is actually sent across this channel. Instead, the child watches if the parent's end of the channel is open. When the parent dies it's half of the channel is closed by the kernel no matter how the parent died, so the child gets a reliable notification that the parent is gone.
--
Greg Parker email@hidden Runtime Wrangler
_______________________________________________
Cocoa-dev mailing list (email@hidden)
Please do not post admin requests or moderator comments to the list.
Contact the moderators at cocoa-dev-admins(at)lists.apple.com
Help/Unsubscribe/Update your Subscription:
This email sent to email@hidden