I'm trying to understand how child and parent processes work....this is my output.
Child0Parent0
ChildParent10
Child2
Parent0
Child3
Parent0
Child4
Parent0
Can anyone tell me why Parent isn't incrementing? My code is below:
Code:
#include <stdio.h>
#include <sys/types.h>
#include <unistd.h>
#include <string.h>
char mynum='0';
int main(void)
{
int i;
pid_t fork_return;
static char buffer[10];
fork_return = fork();
if (fork_return == 0)
{
strcpy(buffer, "CHILD");
for (i=0; i<5; ++i)
{
mynum=i + '0';
sleep(1);
write(1, buffer, sizeof(buffer));
write(1, &mynum, 1);
write(1, "\n", 1);
}
return 0;
}
else
{
strcpy(buffer, "PARENT");
for (i=0; i<5; ++i)
{
sleep(1); /*5 times each*/
write(1, buffer, sizeof(buffer));
write(1, &mynum, 1);
write(1, "\n", 1);
}
return 0;
}
}