Splitting a parent process into 2 child and then merging result.
ProgrammingThis forum is for all programming questions.
The question does not have to be directly related to Linux and any language is fair game.
Notices
Welcome to LinuxQuestions.org, a friendly and active Linux Community.
You are currently viewing LQ as a guest. By joining our community you will have the ability to post topics, receive our newsletter, use the advanced search, subscribe to threads and access many other special features. Registration is quick, simple and absolutely free. Join our community today!
Note that registered members see fewer ads, and ContentLink is completely disabled once you log in.
If you have any problems with the registration process or your account login, please contact us. If you need to reset your password, click here.
Having a problem logging in? Please visit this page to clear all LQ-related cookies.
Get a virtual cloud desktop with the Linux distro that you want in less than five minutes with Shells! With over 10 pre-installed distros to choose from, the worry-free installation life is here! Whether you are a digital nomad or just looking for flexibility, Shells can put your Linux machine on the device that you want to use.
Exclusive for LQ members, get up to 45% off per month. Click here for more info.
Splitting a parent process into 2 child and then merging result.
Hi All,
I am new to this group, also havent worked much on linux.
What i am trying to do is i want to add numbers from 1 to 100.
but that too using multiprocessing. So i made a c programme and using fork() command made two child processes. Now in one child process i am adding from 1 to 50. and in another i am adding 51 to 100. and then in the parent process adding the two results to get the final one.
Now the result from the two function i am getting correctly. But after the wait() call the value returned is lost :
See the programme below for reference
# include<stdio.h>
# include<unistd.h>
# include<sys/wait.h>
# include<stdlib.h>
int *sum1to50()
{
int i;
int sum1=0;
printf("\nWill add 1 to 50\n");
for (i=1;i<51;i++)
sum1+=i;
printf("\nfrom function 1%d",sum1);
//sum1=sum+sum1;
return &sum1;
}
int *sum51to100()
{
int i,sum2=0;
printf("\nWill add 51 to 100\n");
for (i=51;i<101;i++)
sum2+=i;
//sum2=sum+sum2;
return &sum2;
}
int main()
{
pid_t pid1,pid2,wpid1,wpid2;
int status1,status2;//,sum1,sum2,sum;
static int sum1=0;
static int sum2=0;
static int* sum=0;
system("date");
//int sum3;
//int sum;
//creating the first child program to add 1 to 50
pid1 = fork();
if(pid1==-1)
perror("\nError creating process\n");
else if(pid1==0)
{
printf("\nInside Process 1\n");
sleep(15);
sum1=*sum1to50();
printf("\nFunction 1 returned sum 1 to 50 %d\n",sum1);
*sum=sum+sum1;
//printf("\nFrom loop 1 %d\n",*sum);
printf("\nExiting process 1\n");
exit(1);
}
//continue with the main program
//creating the second child program to add 51 to 100
pid2 = fork();
if(pid2==-1)
perror("\nError creating process\n");
else if(pid2==0)
{
sleep(5);
printf("\nInside process 2\n");
sum2=*sum51to100();
printf("\nFunction 2 returned sum 51 to 100 %d\n",sum2);
*sum=sum+sum2;
printf("\n from loop 2 %d\n",*sum);
printf("\nExiting Process 2\n");
exit(1);
}
wpid1=wait(&status1);
if(wpid1 == (pid_t)-1)
perror("\nerror\n");
as described in the previous post, you can't use fork(2) the way you did. You do basically have at least two choices: (a) use a multi-threading approach as already described or (b) to use a multi-process approach. While the multi-threading approach is simpler from the communication aspect as the virtual memory is shared by all threads, the multi-process approach allows a greater flexibility and scalability. The tricky part of the multi-process approach is the communication aspect between between the parent and all the children who might possibly distributed to different hosts. Just some keywords which might help you to get some more details:
hi akuehn,
thanks for the reply... but i do know that using thread we can do so... but i was jus trying to do the same by creating more processess. Cant we do like this?
Also one more thing when i take a addition inside the if loop like (given below in bold) and then print the statement , then it doesnt print that line (the compiler doesnt go there....confused why)
else if(pid1==0)
{
printf("\nInside Process 1\n");
sleep(15);
sum1=*sum1to50();
printf("\nFunction 1 returned sum 1 to 50 %d\n",sum1); *sum=sum+sum1; printf("\nFrom loop 1 %d\n",*sum);
printf("\nExiting process 1\n");
exit(1);
}
is that a drawback of creating processes that we cannot return a value from them.. Then where exactly v can use them.
Last edited by chandan_maheshwari16; 09-15-2010 at 11:07 PM.
Reason: to let the person know for which i am giving reply
as described in the previous post, you can't use fork(2) the way you did. You do basically have at least two choices: (a) use a multi-threading approach as already described or (b) to use a multi-process approach. While the multi-threading approach is simpler from the communication aspect as the virtual memory is shared by all threads, the multi-process approach allows a greater flexibility and scalability. The tricky part of the multi-process approach is the communication aspect between between the parent and all the children who might possibly distributed to different hosts. Just some keywords which might help you to get some more details:
as described in the previous post, you can't use fork(2) the way you did. You do basically have at least two choices: (a) use a multi-threading approach as already described or (b) to use a multi-process approach. While the multi-threading approach is simpler from the communication aspect as the virtual memory is shared by all threads, the multi-process approach allows a greater flexibility and scalability. The tricky part of the multi-process approach is the communication aspect between between the parent and all the children who might possibly distributed to different hosts. Just some keywords which might help you to get some more details:
hello i have used MPI also,
here also confused where to add the final result means in which process(i have used two nodes and 1 task /node)
# include "mpi.h"
# include <stdio.h>
int main(argc,argv)
int argc;
char *argv[];
{
int numtasks,rank=-1, rc;
int sum1=0,sum2=0,sum=0,i;
rc= MPI_Init(&argc,&argv);
if(rc != MPI_SUCCESS)
{
printf("\nerror starting MPI program \n");
MPI_Abort(MPI_COMM_WORLD,rc);
}
MPI_Comm_size(MPI_COMM_WORLD,&numtasks);
MPI_Comm_rank(MPI_COMM_WORLD,&rank);
if(rank==0)
{
printf("no. of tasks=%d My rank=%d\n",numtasks,rank);
for(i=0;i<11;i++)
{
sum1+=i;
}
sum=sum+sum1;
printf("hello exiting %d process\n",rank);
printf("the sum from1 to 10 is %d\n",sum1);
}
if(rank==1)
{
printf("no. of tasks=%d My rank=%d\n",numtasks,rank);
for(i=11;i<21;i++)
{
sum2+=i;
}
sum=sum+sum2;
printf("the sum from11 to 21 is %d\n",sum2);
printf("hello exiting %d process\n",rank);
}
printf("the total sum is%d \n",sum);
MPI_Finalize();
}
the output its giving is :....(on AIX)
no. of tasks=2 My rank=0
hello exiting 0 process
the sum from1 to 10 is 55
no. of tasks=2 My rank=1
the sum from11 to 21 is 155
hello exiting 1 process
the total sum is55
the total sum is155
Keeping the posting from "ForzaItalia2006" in mind, all that holds generally!
You try to create parallel instances of functions (sum1, sum2,...) but you create processes by using fork(). That is the underlying problem.
Linux can run only processes in parallel. Thus, in all cases we end up in what "ForzaItalia2006" posted. It is the communication and synchronisation of your worker functions. MPI simplifies some aspects but is actually oversized for your intention.
Coming back to your fork() idea and a communication solution. You can create a pipe() to communicate between child and parent. An example can be found here: http://www.cs.cf.ac.uk/Dave/C/node23.html
LinuxQuestions.org is looking for people interested in writing
Editorials, Articles, Reviews, and more. If you'd like to contribute
content, let us know.