Search code examples
cfunctionforkcalculatorwaitpid

Changing Parameters of waitpid()


I'm wondering if you can change the parameters of waitpid() At the moment I require continuous variable output ( 0.50 ) to be what is printed. However given that waitpid() only accepts integers when I try and printout it gives me 0. Unsure how to fix this or if this even is the problem.

Calculation is meant to look like (1+(2 * 3)/(2 * 7)) = 0.5

#include <stdio.h>
#include <stdlib.h>
#include <unistd.h>
#include <sys/types.h>
#include <sys/wait.h>

int main(){


    int a = 1, b = 2, c = 3, d = 2, e = 7;

    int a_plus_pid, b_times_c_pid, d_times_e_pid;

    int a_plus, b_times_c, d_times_e;

    a_plus_pid = fork();
    if(a_plus_pid)
        {
            b_times_c_pid = fork();
            if(b_times_c_pid)
            {
                    d_times_e_pid = fork();
                    if(d_times_e_pid)
                    {

                        waitpid(a_plus_pid, &a_plus, 0);
                        waitpid(b_times_c_pid, &b_times_c, 0);
                        waitpid(d_times_e_pid, &d_times_e, 0);

                        //Supposed to print 0.50
                        printf("%d" , ((a + (b_times_c))) / d_times_e);

                    }
                    else
                    {
                        exit(d*e);
                    }
            }
            else
            {
                exit(b*c);
            }
        }
        else
        {
             exit(a);
        }

}

Solution

  • The third parameter of pid_t waitpid(pid_t pid, int *status, int options); is an int, thus you cannot pass a floating point value there.

    It wouldn't make sense to pass such a value there as a matter of fact. You need to study harder and reconsider your approach.