• Open Menu Close Menu
  • Apple
  • Shopping Bag
  • Apple
  • Mac
  • iPad
  • iPhone
  • Watch
  • TV
  • Music
  • Support
  • Search apple.com
  • Shopping Bag
 

Lists

Open Menu Close Menu
  • Terms and Conditions
  • Lists hosted on this site
  • Email the Postmaster
  • Tips for posting to public mailing lists
Re: spawing a server...
[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]

Re: spawing a server...


  • Subject: Re: spawing a server...
  • From: Mont Rothstein <email@hidden>
  • Date: Wed, 9 Feb 2005 16:04:08 -0800

It sounds like you may want threads, rather than a forked process. But if you do end up going with a forked process I'll throw this in because it took me a very long time (and help from Chris in the Foundation team) to figure it out.

If you use fork() you need to follow it with a call to execve() in the child process. The problem is that fork() does not re-initialize everything in the child process and this can cause some very ugly problems. execve() will do this initialization making the child process just like one started on its own. You need some way to identify the child as being a child and not the parent because main() will get called in the child after execve(). I did this by adding an argument to those passed in to execve() as if it was on the command line.

Good luck,
-Mont

On Feb 9, 2005, at 12:17 PM, j o a r wrote:

For networking client / server example code you might want to have a look at the Echo / Echo Client examples found in your /Developer/Examples/Networking folder. Or perhaps the PictureSharing / PictureSharingBrowser found in your /Developer/Examples/Foundation folder, for an example from higher level frameworks. Or check the list archives. Personally I would probably use something from CFNetwork, and / or NSStream APIs.

For communication between threads, you can use "performSelectorOnMainThread:" for one-way communication from a background thread to the main thread, or Distributed Objects (DO) for a two-way link - or pick any old trick from your favourite handbook in C programming.

j o a r

On 2005-02-09, at 19.02, Brian O'Brien wrote:

I want to spawn a communications server in main.mm

MyServer s;
datatype ourDataQueue;

int main(int argc, char *argv[])
{
    try
    {
	s.start(ourDataQueue);
        return NSApplicationMain(argc,  (const char **) argv);
    }
    catch (const char *msg)
    {
        printf("%s\n", msg);
        return(-1);
    }
}

The idea here is that the server start method is a non blocking and
the server puts data into the dataQueue. (And then notifies its parent
thread that new data has arrived.. how I'm not sure yet.)


My question are:

I could have the start method do a fork/exec but is there a preferred
method such as a thread for Cocoa development?  (Pointers please).

If a thread is spawned and places data into the queue then is there
any issues regarding a different thread accessing the queue?

Inter thread communications... Is there a Cocoa way?
_______________________________________________
Do not post admin requests to the list. They will be ignored.
Cocoa-dev mailing list (email@hidden)
Help/Unsubscribe/Update your Subscription:
email@hidden


This email sent to email@hidden

_______________________________________________ Do not post admin requests to the list. They will be ignored. Cocoa-dev mailing list (email@hidden) Help/Unsubscribe/Update your Subscription: This email sent to email@hidden
References: 
 >spawing a server... (From: "Brian O'Brien" <email@hidden>)
 >Re: spawing a server... (From: j o a r <email@hidden>)

  • Prev by Date: Re: handling really BIG images
  • Next by Date: Re: Cocoa-dev Digest, Vol 2, Issue 192
  • Previous by thread: Re: spawing a server...
  • Next by thread: what size is the menu bar font?
  • Index(es):
    • Date
    • Thread