Mailing Lists: Apple Mailing Lists
Image of Mac OS face in stamp
Re: Calliing Fork to create a daemon process unloads my framework
[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]

Re: Calliing Fork to create a daemon process unloads my framework



It's not clear to me, and I'm pretty sure that it's not clear to any of us, where the origin of your problem is; this is because you haven't given us a small code snippit that can reproduce the problem, and you haven't really made it clear that what you think is happening - is actually happening.

I have a specific hunch, but it will only be an educated guess until you provide more explicit information.

One obvious issue to me is that if you are using daemon(), then there will be no stdout for your write available, and so you will not see the printf() from the detach in the child process.

A second issue is that the use of printf() in combination with the fork () does not necessarily result in a second buffered output, due to changes that have occurred in the stdio implementation between 10.2 and 10.4.

The second one is at least verifiable - if you use a simple fork() cal, and do not attempt to daemon()-ize the child process (or still use daemon(), but set the 'noclose' argument to 1), AND you explicitly fflush() following the printf() in the attach and detach (or better yet, explicitly call write() with your diagnostic instead), you may very well see the missing messages come out properly.

My recommendation for the daemon() call came from the fact that you may in fact be sending a signal to the child process as a result of the parent exiting (specifically: when the parent exits, a SIGHUP may be sent to the process group, which includes the child, causing them both to exit; daemon() specifically ignores the possible SIGHUP).

I suspect that things are actually working as they should be, but if not, you will need to build a cut-down test case that demonstrates the problem, if we are going to be of any further help to you. At the very least, the cut-down test case might work where the larger set of code fails, giving you further insight into the problem.

Good luck, and let us know how things turn out.

-- Terry

On May 8, 2006, at 8:43 PM, email@hidden wrote:
HI,

Kyle,

The "Init Module" and "terminate Module" are functions that are automatically called at the load and unload of a framework respectively. I cannot call them explicitly from the application that has been linked with a framework.

I would like to further add a point, this module of mine was initially deployed on 10.2, and there I did not face any such problem. Does this mean that there is some difference between OSX 10.2 and 10.4 that has given rise to this problem????


Regards,

Upasana

-----Original Message-----

From: unix-porting-bounces+upasanas=email@hidden

Is your application multi-threaded? You might be running into a

threading problem with your framework.

When you fork(), only the thread calling fork() lives on in the new

process. If your framework has its own threads, they'll disappear.


Also, I'm not sure what you mean by "unload".

The parent is definitely going to exit (based on your code fragment),

so in the parent process, the framework will unload, and I would

expect the "TERMINATE MODULE frmwrk" to be coming from *that*

process, not the child.

Perhaps you can "INIT_MODULE frmwrk" after you fork?

Kyle

-----Original Message-----
From: Upasana Sharma

Hi ,

Thanks Terry/Kevin for your inputs.

Let me first explain my components. We have a hardware that is being accessed through a framework (which I mentioned gets exited at the fork call). To use the functionality of the hardware we support both network and standalone modes. For network mode, we have a server module (that serves remote clients), which we need to run as a daemon/background process. Thus the use of "exec" is ruled out here.

Terry, following your advice, I tried using daemon call, however, it results in almost the same behaviour. The framework gets exited just after the "daemon" call.


I could not understand how to keep the reference count, are you talking about keeping a reference count inside the framework??



For your query about whether the child process continues, yes it does, because all we want is to make the server run as a background process. So just after calling fork we do the normal processing for the server module in the child process. However at any place where the server module tries to talk to the hardware, it fails since the framework has exited along with the parent process. Can we stop this from happening, i.e., association of framework with parent process???


An alternate solution that I can see is to load the framework dynamically instead of linking the server with it, if thats possible. Any comments? Do I need to make it a bundle for that???

Regards,

Upasana


> >
> > I have an application that is linked to a framework (created by
> > me). When this runs as an ordinary application, things work fine.
> > However, if I try to make this application a daemon by calling fork
> > (), the framework gets unloaded as soon as the parent process
> > exits. It can be explained further by the code I use, as shown
> below -
> >
> > if ( (childpid = fork()) < 0 )
> > exit(-1);
> > else if (childpid > 0){
> > printf("\n parent is exiting, child will continue....");
> > exit(0); // Parent returns and framework also gets unloaded.
> > }
> >
> > When I run my application, the following stuff gets printed -
> > INIT_MODULE framwork
> > parent is exiting, child will continue....
> > TERMINATE_MODULE frmwrk
> >
> > This results in error to any function call of my framework from the
> > child process since the framework has already been unloaded.
> >
> > Has anyone faced this before ? Is there a soution to this?? Can
> > there be a way through which I can delay the loading of my
> > framework to its first function call??
> >
> > Looking forward to some help!!!
> >
> > Regards,
> > Upasana
>
>
> The information contained in this electronic mail transmission may
> be privileged and confidential, and therefore, protected from
> disclosure. If you have received this communication in error, please
> notify us immediately by replying to this message and deleting it
> from your computer without copying or disclosing it.
> _______________________________________________
> Do not post admin requests to the list. They will be ignored.
> Unix-porting mailing list (email@hidden)
> Help/Unsubscribe/Update your Subscription:
>
> This email sent to email@hidden



The information contained in this electronic mail transmission may be privileged and confidential, and therefore, protected from disclosure. If you have received this communication in error, please notify us immediately by replying to this message and deleting it from your computer without copying or disclosing it.

_______________________________________________ Do not post admin requests to the list. They will be ignored. Unix-porting mailing list (email@hidden) Help/Unsubscribe/Update your Subscription: This email sent to email@hidden
References: 
 >Calliing Fork to create a daemon process unloads my framework (From: email@hidden)



Visit the Apple Store online or at retail locations.
1-800-MY-APPLE

Contact Apple | Terms of Use | Privacy Policy

Copyright © 2011 Apple Inc. All rights reserved.