Hi,
I have a doubt in the following piece of code and its behaviour:
#include <stdio.h>
#include <unistd.h>
#include <stdlib.h>
#define N 5
#define nt 1
int pm[N][2],pid[N];
int i,j,size;
char s[100];
int count=0;
int main()
{
for(i=1;i<N;i++)
{
printf("\n i=%d",i);
if(pid[i]=fork() == -1)
{
printf("\n fork not wrking");
exit(1);
}
else if(pid[i]>0)
{
printf(" \n pid:%3d\n",pid[i]);
break;
}
}
return 0;
}
I initially thought that the code will spawn a process and skip out of the loop.
Thereby,
1 spawns 2 and skips out.
2 spawns 3 and skips out
3 spawns 4 and skips out
4 spawns 5 and skips out.
I tried executing this code and was surprised by the answer i got ( for the printf of i in the code). Here is the output
i=1
i=2
i=3
i=2
i=4
i=3
i=3
i=3
i=4
i=4
i=4
i=4
i=4
i=4
i=4
Can Anyone please explain to me what is going on here. NOTE: I am working on a Solaris machine.