Write a C program that illustrates the creation of child process using fork system call. One process finds sum of even series and other process finds sum of odd series - Code

Latest

programs

Thursday, October 6, 2011

Write a C program that illustrates the creation of child process using fork system call. One process finds sum of even series and other process finds sum of odd series


Write a C program that illustrates the creation of child  process using fork system call. One process finds sum of even series and other process finds sum of odd series




#include <stdio.h>
#include <sys/types.h>
#include <unistd.h>
#include <fcntl.h>
int main()
{
int i,n,sum=0;
pid_t pid;
system(“clear”);
printf(“Enter n value:”);
scanf(“%d”,&n)
pid=fork();
if(pid==0)
{
printf(“From child process\n”);
for(i=1;i<n;i+=2)
{
printf(“%d\”,i);
sum+=i;
}
printf(“Odd sum:%d\n”,sum);
}
else
{
printf(“From process\n”);
for(i=0;i<n;i+=2)
{
printf(“%d\”,i);
sum+=i;
}
printf(“Even sum:%d\n”,sum);
}
}

1 comment: