找回密码
 注册
楼主: bitstream

关于fork()函数的问题

[复制链接]
发表于 2003-5-7 21:13:17 | 显示全部楼层
推荐一本书,《UNIX环境高级编程》(Advanced Programming in the UNIX Environment),简称APUE,Richard Stevens写的。

你说的程序输出顺序取决于系统的进程控制,fork()以后父子进程那个先运行是由系统决定的,运行多长时间也是由系统决定的,所以各种组合都可能出现,我们只能看到有限的几种是因为程序太小了,在一个时间片里执行两条printf的可能性很大,再有一个,系统运行的进程数也有影响,你可以打开XWindow,开N个窗口,或在printf之前加一些其它语句试试。

关于这个问题,APUE里专门有控制那个进程必须等待另一个进程先运行的办法。
回复

使用道具 举报

发表于 2003-5-7 21:37:12 | 显示全部楼层
yes, just a discussion.
回复

使用道具 举报

 楼主| 发表于 2003-5-8 16:56:49 | 显示全部楼层
哦,谢谢大家。
还有一点不明白。
fork()会失败吗
要是失败它会返回什么呢
或者输出一条信息后会不会有什么突然事件使程序只有一个输出或者只有两个输出呢
回复

使用道具 举报

发表于 2003-5-8 21:48:15 | 显示全部楼层
pls read man fork to get more info, here is a c&p:
On failure, a -1 will be returned in the parent's context,  no child process will be created, and errno will be set appropriately.
ERRORS
       EAGAIN fork cannot allocate sufficient memory to copy the parent's page tables and allocate a task structure for the child.
       ENOMEM fork failed to allocate the necessary kernel structures  because memory is tight.

u can send a signal to any process to kill it. but the question does not mention this.
回复

使用道具 举报

发表于 2003-5-10 21:54:37 | 显示全部楼层
呵呵,漏看后面的讨论了,再说就属于画蛇添足了.

删之删之
回复

使用道具 举报

您需要登录后才可以回帖 登录 | 注册

本版积分规则

GMT+8, 2025-2-25 07:26 , Processed in 0.039782 second(s), 12 queries .

© 2001-2025 Discuz! Team. Powered by Discuz! X3.5.

快速回复 返回顶部 返回列表