I am trying to write a program that will print numbers from 1 to 10, but I want the first five numbers to be printed by the child processes, and the last 5 should be printed by the parent processes:
#include <unistd.h>
#include <stdio.h>
#include "process.h"
/**
* main - Entry point for my program
*
* Return: On success, it returns 0.
* On error, it returns 1
*/
int main(void)
{
int id = fork();
int n, i;
if (id == 0)
n = 1;
else
n = 6;
for (i = n; i < n + 5; i++)
printf("%d\n", i);
return (0);
}
The output is:
6
7
8
9
10
1
2
3
4
5
I am new to UNIX processes, so I dont understand why the parent process output (from 6 - 10) is being printed first. Does the execution of the parent process take precedence over the child process? If I want the child processes to run first (i.e. 1 - 5 printed first), how can I do it?