Skip to content

Instantly share code, notes, and snippets.

@rajabishek
Created March 19, 2015 16:30
Show Gist options
  • Select an option

  • Save rajabishek/009de36a8bed012d09c3 to your computer and use it in GitHub Desktop.

Select an option

Save rajabishek/009de36a8bed012d09c3 to your computer and use it in GitHub Desktop.
Process creation - fork
#include<stdio.h>
#include<stdlib.h>
#include<unistd.h>
int main()
{
pid_t pid;
int i,j,k,n;
int esum=0,osum=0,tsum=0;
printf("\nBefore Call of fork ");
printf("\nEnter the value of n");
scanf("%d", &n);
pid=fork();
if(pid==0)
{
printf("\nWe are in the child process" );
for(i=2;i<=n;i=i+2)
esum=esum+i;
printf("\nThe sum of even terms is %d", esum);
}
else
{
printf("\nWe are in parent process");
for(j=1;j<=n;j=j+2)
osum=osum+j;
printf("\nThe sum of odd terms is %d", osum);
}
tsum=esum+osum;
printf("\nThe sum of the terms is %d", tsum);
printf("\nBack to main");
return 0;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment