Brief Intro to Interprocess Communication of Foundation Framework

94 篇文章 0 订阅
32 篇文章 0 订阅

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

NSPortNSMachPortNSMessagePort, 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. NSMachPortNSMessagePort, 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

NSPortNameServerNSMachBootstrapServer,NSMessagePortNameServer, and NSSocketPortNameServer provide an interface to the port registration service, used to retrieve instances ofNSMachPortNSMessagePort, 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 (NSMachPortNSMessagePortNSSocketPort).

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

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值