In: Computer Science
C programming language
The program first creates a child process CP. So, there are two processes:
The parent process does the following:
a. compute the summary of all the even number from 1, 2, .. 1000, and output this summary;
b. wait for the termination of the child process CP, then terminate;
The child process does the following:
a. compute the summary of all the odd number from 1, 2, .. 1000, and output this summary;
b. terminates;
/************************************************Code ********************************************************************************/
#include <stdio.h>
#include <sys/types.h>
#include <unistd.h>
int main(){
int i;
int sum;
if (fork() == 0) {
printf("This is Child process CP computing sum of odd numbers from
1 to 1000 ");
sum = 0;
for(i=1; i < 1000; i=i+2){
sum = sum + i;
}
printf("So Child process computed the sum: %d ", sum);
}
else{
printf("This is Parent process computing sum of even numbers from 1
to 1000 ");
sum = 0;
for(i=2; i <= 1000; i=i+2){
sum = sum + i;
}
printf("So Parent process computed the sum: %d ", sum);
wait(NULL); //wait for child process to terminate
printf("Child process has terminated ");
}
return 0;
}
/************************************* Output Screen Shot ********************************************************************/