MacOSX Lion上的进程间通信 [英] InterProcess Communication on MacOSX Lion

查看:80
本文介绍了MacOSX Lion上的进程间通信的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我试图弄清楚如何在自定义应用程序和预制程序之间设置IPC.
我正在使用MacOSX Lion 10.7.2和Xcode 4.2.1.

I'm trying to figure out how to set up IPC between my custom app and a pre-made program.
I'm using MacOSX Lion 10.7.2 and Xcode 4.2.1.

实际上什么程序完全无关紧要,因为我相信类似的推理可以应用于任何类型的外部过程.
为了进行测试,我使用了一个简单的bash脚本:

It doesn't matter actually what program exactly, since I believe that a similar reasoning may be applied to any kind of external process.
For testing purposes I'm using a simple bash script:

#test.sh
echo "Starting"
while read out 
do 
    echo $out
done

我想要实现的是使用我的应用向其发送输入并读取其输出来重定向此脚本的输入和输出.

我尝试如下使用NSTaskNSPipeNSFileHandle:

-(void)awakeFromNib {

    task = [[NSTask alloc] init];

    readPipe = [NSPipe pipe];
    writePipe = [NSPipe pipe];

    [task setStandardOutput:readPipe];
    [task setStandardInput:writePipe];    

    [task setLaunchPath:@"/path/test.sh"];

    [[NSNotificationCenter defaultCenter] addObserver:self
                                             selector:@selector(read:)
                                                 name:NSFileHandleReadCompletionNotification
                                               object:nil];

    [[readPipe fileHandleForReading] readInBackgroundAndNotify];

    [task launch];

}

-(IBAction)write:(id)sender {

    NSLog(@"Write called: %d %@\n",[task isRunning],writePipe);

    NSFileHandle *writeHandle = [writePipe fileHandleForWriting];

    NSString *message = @"someString";

    [writeHandle writeData:[message dataUsingEncoding:NSUTF8StringEncoding] ];

}

-(void)read:(NSNotification*)notification {

    NSString *output = [[NSString alloc] initWithData:[[notification userInfo] valueForKey: NSFileHandleNotificationDataItem]
                                             encoding:NSUTF8StringEncoding];

    NSLog(@"%@",output);

    [output release];

    [[notification object] readInBackgroundAndNotify]; 

}

,但是我只能读取test.sh的输出,而不能发送任何输入.

but I'm able only to read the output of test.sh, not to send it any input.

实际上,我在网络上看到的任何其他示例都与我的代码非常相似,因此我不确定此问题是由于我的某些错误还是其他问题(例如应用程序的MacOS Lion沙箱)引起的

Actually any other example I saw on the web is pretty similar to my code, so I'm not sure if this issue is due to some mistake(s) of mine or to other issues (like app's sandboxing of MacOS Lion).

我已经检查了XPC文档,但是,根据我的研究,为了将XPC API用于IPC,双方都应连接到同一服务.
这不是我想要的,因为我不想以任何方式更改脚本,我只想重定向其输入和输出.

I've checked XPC documentation, but, according to my researches, in order to use XPC API to IPC, both sides should connect to the same service.
That's not what I'm looking for since I don't want to alter the script in any way, I just want redirect its input and output.

我的问题是由于缺少XPC和/或应用程序的沙箱吗?

Is my issue due to the lack of XPC and/or to app's sandboxing?

如果是,是否可以在不修改脚本的情况下使用XPC?
如果不是,那么有人可以向我解释我在做什么错吗?

If yes, is there a way to use XPC without modifying the script?
If no, then may somebody explain me what I'm doing wrong?

推荐答案

您不需要XPC.不会有任何改变.

You don't need XPC for this. Won't make any difference.

当您在命令行中将某些内容传递给脚本/外部进程时,您的脚本/外部进程是否能够读取输入

Is your script / external process able to read input when you pipe something to it on the command line

% echo "foobar" | /path/test.sh

?

您要发送多少数据.写入将被缓冲. IIRC -synchronizeFile将刷新缓冲区-与fsync(2)相同.

How much data are you sending to it. Writing will be buffered. IIRC -synchronizeFile will flush buffers -- same as fsync(2).

这篇关于MacOSX Lion上的进程间通信的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆