0

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.

  • `fork` creates an independent ***process***. Look at it like `fork` create a brand new program that is running independent of the currently running program. As such they can't naturally share data between them, not without extra work. Using processes (or even threads) really makes little to no sense for something like this. – Some programmer dude Jun 02 '20 at 11:07
  • Why do you want to use fork for this? Related: https://stackoverflow.com/q/14170647/6699433 – klutt Jun 02 '20 at 11:10

1 Answers1

1

First, fork() generates new processes not threads of the same process, if you need to make a new thread instead of another process, you can use the pthread library (1) , where you can use pthread_create (2) to generate a new thread, return the result of one thread to another one with pthread_exit (3) and catch it in the desired thread with the pthread_join function (4).

But if you want to use the fork function, you will need to generate some share memory among all the different processes, in that case you also need to be aware of the possible concurrence problems that may appear. (This way is more complex if you do not know anything about concurrence programming).

If you want me to provide you wider explanations, please let me know. I attach here the manual pages regarding the pthread library functions.

  1. pthread overview https://www.man7.org/linux/man-pages/man7/pthreads.7.html
  2. pthread_create https://www.man7.org/linux/man-pages/man3/pthread_create.3.html
  3. pthread_exit https://www.man7.org/linux/man-pages/man3/pthread_exit.3.html
  4. pthread_join https://www.man7.org/linux/man-pages/man3/pthread_join.3.html