天天看點

Brief Intro to Interprocess Communication of Foundation Framework

Interprocess Communication

The Foundation Framework includes a collection of classes that support process-to-process communication. Specifically, they provide facilities for creating and using communication channels.

Communication via Pipes

The NSPipe class encapsulates a pipe, a one-way channel for communication between processes. The API includes methods to create and initialize a pipe, and retrieve the corresponding NSFileHandleinstance for a pipe. The NSTask class provides several methods for setting the input and output channels for a process(setStandardOutput:, setStandardInput:). Listing 11-18 demonstrates the use of the NSTask and NSPipe APIs to create and launch a task (the Unix command /bin/ls simply lists the files in the current directory), set the task’s standard output, and then retrieve and log the standard output written to the pipe after task completion.

Listing 11-18.  Using NSPipe and NSTask to Invoke a Process

NSTask *task = [[NSTask alloc] init];
[task setLaunchPath:@"/bin/ls"];
NSPipe *outPipe = [NSPipe pipe];
[task setStandardOutput:outPipe];
[task launch];
NSData *output = [[outPipe fileHandleForReading] readDataToEndOfFile];
NSString *lsout = [[NSString alloc] initWithData:output
                                        encoding:NSUTF8StringEncoding];
NSLog(@"/bin/ls output:\n%@", lsout);      

Communication via Ports

NSPort, NSMachPort, NSMessagePort, and NSSocketPort provide low-level mechanisms for communication between threads or processes, typically via NSPortMessage objects.

NSPort is an abstract class. It includes methods to create and initialize a port, create connections to a port, set port information, and monitor a port. NSMachPort, NSMessagePort, and NSSocketPort are concrete subclasses of NSPort used for specific types of communication ports.NSMachPort and NSMessagePort allow local (on the same machine) communication only. In addition, NSMachPort is used with Mach ports, the fundamental communication port in OS X. NSSocketPort allows both local and remote communication, but may be less efficient than the others when used for local communication. Port instances can be supplied as parameters when creating an NSConnection instance (using theinitWithReceivePort:sendPort: method). You can also add a port to a run loop via the NSPort scheduleInRunLoop:forMode: method.

As ports are a very low-level interprocess communication mechanism, you should implement interapplication communication using distributed objects whenever possible and use NSPort objects only when necessary. In addition, when you are finished using a port object, you must explicitly invalidate the object via the NSPort invalidate method.

The NSSocketPort and NSPortMessage classes are only available for use on the OS X platform.

Port Registration

NSPortNameServer, NSMachBootstrapServer,NSMessagePortNameServer, and NSSocketPortNameServer provide an interface to the port registration service, used to retrieve instances ofNSMachPort, NSMessagePort, and NSSocketPort .

NSPortNameServer provides an object-oriented interface to the port registration service used by the distributed objects system.NSConnection objects use it to contact each other and to distribute objects over the network. You should rarely need to interact directly with an NSPortNameServer instance. NSMachBootstrapServer,NSMessagePortNameServer, and NSSocketPortNameServer are subclasses of NSPortNameServer that return corresponding port instances (NSMachPort, NSMessagePort, NSSocketPort).

The NSPortNameServer, NSMachBootstrapServer,NSMessagePortNameServer, and NSSocketPortNameServer classes are only available for use on the OS X platform.