分叉进程似乎会占用某些代码行 - Objective-C
这是我的代码的简化版本:
- (IBAction)convert:(id)sender
{
/* these two lines are ignored */
[textbox setStringValue:@"converting"];
[convertButton setEnabled:NO];
pid_t pid;
if((pid=fork())==-1)
{
[log setStringValue:@"couldn't fork a new process."];
converting = 0;
[convertButton setEnabled:YES];
return;
}else if (pid==0)
{
//this is the child
sleep(2);
exit(0);
}else{
int status;
waitpid(pid,&status,0);
}
}
}
这是一个非常基本的 fork() 调用。问题是,最顶部的两行(用注释标记)被忽略......它们似乎直到分叉子进程退出后才执行。为什么?
编辑:我能做些什么来解决这个问题?
Here's a simplified version of my code:
- (IBAction)convert:(id)sender
{
/* these two lines are ignored */
[textbox setStringValue:@"converting"];
[convertButton setEnabled:NO];
pid_t pid;
if((pid=fork())==-1)
{
[log setStringValue:@"couldn't fork a new process."];
converting = 0;
[convertButton setEnabled:YES];
return;
}else if (pid==0)
{
//this is the child
sleep(2);
exit(0);
}else{
int status;
waitpid(pid,&status,0);
}
}
}
It's a pretty basic fork() call. The problem is, the two lines at the very top (marked with a comment) are ignored...they don't seem to execute until after the forked child exits. Why?
Edit: And what can I do to fix it?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
你真的真的真的不想在Cocoa应用程序中调用
fork()
。这样做时会遇到无数不同的问题,主要与各种资源(如 mach 端口和其他系统绑定基础设施)如何跨 fork() 边界生存有关。线程也会导致各种地狱。请改用 NSTask。虽然它在内部有效地执行了
fork()
/exec()
操作,但它在执行此操作时非常小心,以确保正确完成。You really really really don't want to call
fork()
in a Cocoa application. There are about a zillion different gotchas when doing so, mostly related to how various resources like mach ports and other system binding infrastructure survives across thefork()
boundary. Threads cause all kinds of hell, too.Use
NSTask
instead. While it effectively doesfork()
/exec()
internally, it does so with considerable care to make sure it is done correctly.也许是因为您的代码必须在 GUI 更改状态之前返回到主事件循环?或者,略有不同,因为当您的代码执行内核级 wait() 时,运行 GUI 的线程被阻塞?
Perhaps because your code must return to the main event loop before the GUI can change state? Or, a slight variation, because the thread that runs the GUI is blocked while your code is doing a kernel-level wait()?
bbum 说得完全正确。这是我最终使用的代码:
bbum got this exactly right. Here's the code I ended up using: