简体   繁体   English

在C ++中使用fork()

[英]Working of fork() in C++

I have this program in C++. 我用C ++编写了这个程序。

#include <iostream>
#include <unistd.h>
#include  <sys/types.h>


using namespace std;

int main()
{
    cout<<"*\n";
    fork();
    cout<<"A\n";
    fork();
    cout<<"B\n";
    fork();
    cout<<"C\n";
    return 0;
}

The output is: 输出是:

*
A
B
C
*
A
B
C

I think it should be: 我认为它应该是:

*
A
B
C
A
B
C
B
C
C

Explanation: '*' should be printed on by one process. 说明:'*'应由一个进程打印。 Now, after fork() 2 'A' should be printed and son for 'B' & 'C'. 现在,在fork()之后fork() '应该打印,儿子用'B'和'C'。

Link to code 链接到代码

Based on comments on the question, the answer is that fork in the ideone.com online compiler is simply limiting you to one instance of your process, and buffering your output. 基于对该问题的评论,答案是ideone.com在线编译器中的fork只是限制您进程的一个实例,并缓冲您的输出。

Update : Actually, it's also lying: http://ideone.com/oXqqwM shows that fork() claims to succeed, but only the first produces a new copy of your process. 更新 :实际上,它也在说谎: http//ideone.com/oXqqwM显示fork()声称成功,但只有第一个产生了您的进程的新副本。

I suspect if you checked the return values of fork() you'd get some clues as to the surprising behaviour you're seeing -- all but the first will have returned -1 -- and if you used std::endl instead you might avoid the first * . 我怀疑你是否检查了fork()的返回值,你会得到一些关于你所看到的令人惊讶的行为的线索 - 除了第一个将返回-1 - 如果你使用std::endl而不是你可能会避免第一个*

The expected result is that each fork() duplicates the running process at that moment, and so you'd expect to see twice each character compared to the one before, except that buffering means you might still have previous characters in the buffer at the time of fork() . 预期的结果是每个fork()在那个时刻复制正在运行的进程,因此你希望每个字符与前一个字符相比看到两次,除了缓冲意味着你当时可能仍然有缓冲区中的前一个字符fork()

So expect 1 or more "*", 2 or more "A", 4 or more "B" or 8 or more "C", except that if fork() fails, the numbers will be capped. 所以期望1或更多“*”,2或更多“A”,4或更多“B”或8或更多“C”,除非fork()失败,数字将被限制。

Your expectation of 1 "*", 2 "A", 3 "B" and 4 "C" indicates that you overlooked that both processes coming out of a fork() will hit the next fork() , so you double, rather than incrementing by one. 你对1“*”,2“A”,3“B”和4“C”的期望表明你忽略了fork()中的两个进程都会到达下一个fork() ,所以你加倍,而不是递增一。

so... if you are getting more than one * then there is something strange going on, and I suspect that it is implementation specific... fork will give each of the child processes a copy of the file descriptors, including stdout ... so in your implementation it is also getting a valid copy of the std::cout stream... which already has a buffer loaded with * ... 所以...如果你得到的不止一个*那么就会发生一些奇怪的事情,我怀疑它是特定于实现的...... fork会给每个子进程一个文件描述符的副本,包括stdout ..所以在你的实现中它也获得了std :: cout流的有效副本......它已经有一个加载了*的缓冲区...

I think if you change your program to include a flush, you will see the same thing that I did: 我想如果你改变你的程序以包含同花顺,你会看到我做的同样的事情:

#include <iostream>
#include <unistd.h>
#include  <sys/types.h>


using namespace std;

int main()
{
    cout<< "*\n";
    flush(cout);
    fork();
    cout<<"A\n";
    flush(cout);
    fork();
    cout<<"B\n";
    flush(cout);
    fork();
    cout<<"C\n";
    flush(cout);
    return 0;
}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM