When I run the code bellow it gave me a strange output
#include <stdio.h>
#include <stdlib.h>
#include <sys/types.h>
#include <unistd.h>
int main (int argc, char ** argv)
{
int pid1, pid2;
if ((pid1=fork())<0)
{
printf("Error\n");
exit(1);
}
if (pid1=!0)
printf("A\n");
printf("B %d", getpid());
if ((pid2=fork())<0)
{
printf("Error\n");
exit(2);
}
printf("A\n");
return 0;
}
It should be something like
A
A
BA
BA
A
A
But instead the output is
A
A
BA
BA
BA
BA
And I don't know why. Any advice or explanation pls.