I am trying to solve the expression (a+bc) / (a-b+d-c) + abcd, with different processes. But I don't know how fork()
works. I know it is like a new thread but how can I join the result to another thread and finish each 'child' when an operation is done?
My code:
int arithOpera (int a, int b, int c, int d){
int pid, pid1, pid2, pid3, pid4, pid5, pid6, pid7, pid8, pid9;
pid = fork();
int term1, term2, term3;
if (pid == 0) {
sol = term1 / term2;
}
else {
pid1 = fork();
if (pid1 == 0) {
term3 *= d;
}
else {
pid2 = fork();
if (pid2 == 0) {
term3 *= c;
}
else {
pid3 = fork();
if (pid3 == 0) {
term3 = a*b;
}
else {
pid4 = fork();
if (pid4 == 0) {
term2 -= c;
}
else {
pid5 = fork();
if (pid5 == 0) {
term2 += d;
}
else {
pid6 = fork();
if (pid6 == 0) {
term2 = a-b;
}
else {
pid7 = fork();
if (pid7 == 0) {
term1 += a;
}
else {
pid8 = fork();
if (pid8 == 0) {
term1 = b * c;
}
else {
sol += term3;
}
}
}
}
}
}
}
}
}
}
return sol;
}
I should do each operation in a different thread.