Ich habe ein C++ Programm, das fünf untergeordnete Prozesse erstellt.Unterschiedliches Verhalten von endl und n mit fork()
#include<iostream>
#include<unistd.h>
#include<sys/types.h>
using namespace std;
int main()
{
pid_t pid;
pid=fork();
if(pid==0)
{
cout<<"Child Process 1"<<endl;
}
pid=fork();
if(pid==0)
{
cout<<"Child Process 2"<<endl;
}
if(pid==0)
{
pid=fork();
if(pid==0)
{
cout<<"child process 3"<<endl;
}
}
return 0;
}
Die Ausgabe des obigen Programms ist:
Child Process 1
Child Process 2
Child Process 2
child process 3
child process 3
Aber das Problem ist, wenn ich \ n anstelle von endl verwenden die Ausgabe
#include<iostream>
#include<unistd.h>
#include<sys/types.h>
using namespace std;
int main()
{
pid_t pid;
pid=fork();
if(pid==0)
{
cout<<"Child Process 1\n";
}
pid=fork();
if(pid==0)
{
cout<<"Child Process 2\n";
}
if(pid==0)
{
pid=fork();
if(pid==0)
{
cout<<"child process 3\n";
}
}
return 0;
}
Die Ausgabe des obigen Programms ist anders using \ n ist:
Child Process 1
Child Process 2
Child Process 1
Child Process 2
Child Process 2
child process 3
Child Process 1
Child Process 2
child process 3
Warum ist die Ausgabe für endl und \ n im selben Programm unterschiedlich?
Ja, das ist die bessere Antwort der beiden. – Bathsheba