我正在尝试创建一个简单的工具,可以创建、写入和读取命名管道。这是代码。
int main(int argc, const char * argv[])
{
@autoreleasepool
{
//create pipe
system("rm /tmp/myPipe");
system("mkfifo /tmp/myPipe");
system("chmod 666 /tmp/myPipe");
// write to pipe
NSString *text = [NSString stringWithFormat:@"this is a test"];
NSFileManager *fileManager = [NSFileManager defaultManager];
if ([fileManager fileExistsAtPath:@"/tmp/myPipe" isDirectory:NO])
{
NSFileHandle *fileHandle=[NSFileHandle fileHandleForWritingAtPath:@"/tmp/myPipe"];
[fileHandle seekToEndOfFile];
[fileHandle writeData:[text dataUsingEncoding:NSUTF8StringEncoding]];
[fileHandle closeFile];
}
}
return 0;
}
文件已正确创建,权限为 prw-rw-rw- 1 xxx 轮 0 5 月 17 日 09:48 myPipe|
当我运行该工具时,它卡在以下行:
NSFileHandle *fileHandle=[NSFileHandle fileHandleForWritingAtPath:@"/tmp/myPipe"];
我在这里缺少什么?
最佳答案
除了我完全不了解代码的作用之外,这里没有任何问题。没有“挂起”,只是代码在管道中等待外部消息。
我只需要启动终端并输入
echo "foo" > /tmp/myPipe
感谢所有帮助过的人...
关于objective-c - 访问命名管道以进行读写,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/44030859/