我为了使不同proccesses运行,但打印的代码counfuses我一个简单的message.The结果使用fork()..看看代码:fork()和标准错误与终端
#include <stdlib.h>
#include <stdio.h>
#include <unistd.h>
#include <errno.h>
#include <math.h>
#include <sys/types.h>
#include <sys/ipc.h>
#include <sys/sem.h>
#include <time.h>
int main(void)
{
fork();
fork();
fork();
fprintf(stderr,"hello world\n");
}
,输出为:
[email protected]:~/OS$ ./main
hello world
hello world
hello world
hello world
hello world
hello world
[email protected]:~/OS$ hello world
hello world
[email protected]:~/OS$
请注意,我在终端的第一行执行程序,但输出是不是我的预期。请帮帮我!提前致谢!如果fprintf用printf(“......”)改变,也会发生同样的情况
编辑:我不明白为什么打印是这种方式。在终端线旁边有6个,之后有1个...
你看过'fork'函数的工作原理吗?尝试猜猜为什么你使用3把叉子8次'hello world'。提示,8 = 2^3。 –
我知道这个事实!我的问题不是8打印,而是它们被终端命令打乱。我不觉得它合理的程序打印6之前,终端线(马里奥@ Ubuntu的:〜/ OS $你好世界)旁边和一个.. – JmRag