#Error while creating 2 child processes from one single parent

52 messages · Page 1 of 1 (latest)

crude elkBOT
#

When your question is answered use !solved to mark the question as resolved.

Remember to ask specific questions, provide necessary details, and reduce your question to its simplest form. For tips on how to ask a good question run !howto ask.

high venture
#

!f

crude elkBOT
#

I want to create a program where it creates 2 child processes from one parent and then with the given array its adds the even position to child A and odd position to child B. I have got the output but forks() keep pr```cpp
inting multiple statements !!

Here's my code:

#include <stdio.h>
#include <stdlib.h>
#include <sys/types.h>
#include <sys/wait.h>
#include <unistd.h>
int
main(int argc, char argv[]) {
int n = atoi(argv[1]);
int A[n];
int i = 0;
int sumA = 0;
int sumB = 0;
int sumTotal = 0;
int statusA;
int statusB;
pid_t pidA;
pid_t pidB;
int num = 0;

for (i = 0; i < n; i++) {
A[i] = num++;
printf("%d ", A[i]);
}

pidA = fork();
if (pidA == 0) {
// child A
for (i = 1; i < n; i += 2) {
sumA += A[i];
}
printf("subtotal of child A: %d\n", sumA);
}

pidB = fork();
if (pidB == 0) {
// child B
for (i = 0; i < n; i += 2) {
sumB += A[i];
}
printf("subtotal of child B: %d\n", sumB);
}

// parent
wait(pidA /, &statusA, 0 /);
wait(pidB /, &statusB, 0 * /);
sumTotal = sumA + sumB;
printf("total sum: %d\n", sumTotal);

return 0;
}

55555
high venture
#

both the parent and child A are calling fork so you're actually making 3 children

#

also all three children are doing child B's work

#

oh wait

sharp veldt
#

how can I correct this?

high venture
#

nvm, not all three

#

just children B and C

sharp veldt
#

can you run it on your side and check ?

#

cause I need one parent and 2 child ! and no multiple statements

high venture
#

i ran it in my head, basically you need to have child A return once it's done its job

#

and child B

sharp veldt
#

can you format my code and tell me ?

#

cause i cant seem to get it how the nesting of child parent works?

high venture
#

when you call fork, everything below that gets run by each process, the existing ones and the newly created ones. you can branch off of the return value from fork to specify "only the child should run this" or "only the parent should run this", but it doesn't mean execution stops once you reach the end of the if. you can stop it at the end of the if though if you return or exit

sharp veldt
#

i have some errors as well

#

i tried it but i am getting errors thats why i'm telling you if you can format and correct my mistakes

high venture
#

you mean like the random slashes? and you're missing an asterisk for the argv declaration

sharp veldt
#

i mean if you run on your laptop pc that would be easy for me

#

to rectify my mistake so we can format it together

high venture
#

and you're calling wait instead of waitpid

sharp veldt
#

you wont know if you cant run it sir

high venture
#

well i just fixed all those things and ran it

#
$ ./tst 5
0 1 2 3 4 subtotal of child A: 4
0 1 2 3 4 subtotal of child B: 6
0 1 2 3 4 total sum: 0

also you're using a variable length array (vla) which is problematic. vlas are actually slated to be removed from C in upcoming releases because they cause a lot of problems. when you don't know how much space you'll need ahead of time, you should use malloc

#

oh wow

#

just realized that the total sum was wrong, sumA and sumB are local variables, and each fork has its own copy of the local variables

#

if you make them global then they will persist after the subprocesses end (and the waitpids have returned)

#

i should probably also say that you could instead allocate them on the heap in order to avoid using global variables

sharp veldt
#

yah total sum is wrong

#

can you send me the rectify code you had????

#

@high venture ??

#

can you send me the rectified code ?

high venture
#

i'm not going to just hand you the code and prevent you from using your brain

sharp veldt
#

which sum A and sum B you are talking?

high venture
#

the variables, sumA and sumB

#

because they're inside of a function definition, they're local variables that use the stack, and each thread or process gets its own stack on creation, so to fix the sumTotal which depends on the values from sumA and sumB, sumA and sumB have to be moved off the stack, either to the heap by using malloc, or as part of the .bss section by making it either static or global

sharp veldt
#

bro i figured out everything except the total sum part

#

should i send you my fixed code?

high venture
#

yeah

sharp veldt
#

alright

#

here you go

#

i have sent my code in pastebin

#

the only thing i cant figure it out is my total sum ??

#

idk why

high venture
#

the link that space hazart sent?

#

from that link, the first thing that strikes me is that you're still writing the sums to local variables. childA is a local variable on the stack, which means childA.sumA is still a local variable on the stack

#

which means that the child is updating its own copy, whereas the parent's sits unchanged

#

if you did

int main()
{
    static int sumA;
    static int sumB;

    ...


    sumTotal = sumA + sumB;
    ...
}```
then it should work just fine
crude elkBOT
#

This question thread is being automatically closed. If your question is not answered feel free to bump the post or re-ask. Take a look at !howto ask for tips on improving your question.

sharp veldt
#

!solved