iPhone上的Fork() [英] Fork() on iPhone

查看:149
本文介绍了iPhone上的Fork()的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

iPhone SDK是否允许 fork() pipe(),传统的unix功能?我似乎无法让它们发挥作用。

Does the the iPhone SDK allow fork() and pipe(), the traditional unix functions? I can't seem to make them work.

编辑

问题解决了。在这里,我为遇到类似我的问题的人提供解决方案。我受到了这个帖子中答案的启发。

Problem solved. Here, I offer a solution to anybody who encounters problems similar to me. I was inspired by the answers in this thread.

在iPhone中,没有办法分叉一个进程。但是,实施管道并非不可能。在我的项目中,我创建了一个新的POSIX线程(阅读Apple的文档以了解如何执行此操作)。子线程将与父线程共享由pipe()创建的文件描述符。子线程和父线程可以通过管道进行通信。例如,我的孩子将dup2()fd [1]线程化为其标准输出。因此,任何标准输出都可以在父线程中捕获。类似于fd [0]和标准输入。

In iPhone, there is no way to fork a process. However, it's not impossible to implement piping. In my project, I create a new POSIX thread (read Apple's documentation for how to do this). The child thread would share a file descriptor created by pipe() with the parent thread. Child and parent threads can communicate via pipes. For example, my child thread dup2() fd[1] to its standard output. So any standard output could be caught in the parent thread. Similar to fd[0] and standard input.

Pseudocode(我没有可用的代码,但你知道了):

Pseudocode (I don't have the code available but you get the idea):

int fd[2];
pipe(fd);
create_posix_thread(&myThread, fd);
char buffer[1024];
read(fd[0], buffer, 1024);
printf("%s", buffer); // == "Hello World"    

void myThread(int fd[])
{
  dup2(fd[1], STANDARD_OUTPUT);
  printf("Hello World");
}

如果您想在其中使用第三方库,该策略非常方便你的iPhone应用程序。但是,问题是使用printf()的标准调试不再可用。在我的项目中,我只是将所有调试输出指向标准错误,XCode会将输出显示到其控制台。

The strategy is very handy if you want to use a third-party library within your iPhone application. However, a problem is that standard debug using printf() is no longer available. In my project, I simply directed all debug outputs to standard error, XCode would display the outputs to its console.

推荐答案

你可以'fork()但你可以使用尽可能多的线程,现在你甚至可以在iPhone上使用GCD来帮助保持线程合理使用。

You can't fork() but you can use as many threads as you like, and now you even have GCD on the iPhone to help keep thread use reasonable.

为什么你想要fork()而不是仅使用更多线程?

Why do you want to fork() instead of just using more threads?

这篇关于iPhone上的Fork()的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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