I have this code
Code:
#include <stdio.h>
#include <sys/types.h>
#define MAX_COUNT 200
void ChildProcess();
void ParentProcess();
int main()
{
pid_t pid;
pid = fork();
if (pid == 0)
ChildProcess();
else
ParentProcess();
return 0;
}
void ChildProcess() {
int i;
for (i = 1; i <= MAX_COUNT; i++)
printf(" This line is from child, value = %d\n", i);
printf(" *** Child Process is done ***\n");
}
void ParentProcess() {
int i;
//wait();
for (i = 1; i <= MAX_COUNT; i++)
printf("This line is from parent, value = %d\n", i);
printf("*** Parent Process is done ***\n");
}
I have three queries:
1. why child process starts first?
2. Why shometime it prints upto 70(i's value) from child then 200 from parent and then switch again to parent, is it due to the service time from the processor handeled by OS?
3. How I can print alternatively from both process:
like first 1 from child, then 1 from parent then two from child and then 2 from parents and so on?
Is there any way to switch between the processes? Child and Parents.