0

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);
        }

}
gsamaras
  • 71,951
  • 46
  • 188
  • 305
Dreeww
  • 93
  • 6
  • Read http://advancedlinuxprogramming.com/ and spend several days in reading stuff (including [waitpid(2)](http://man7.org/linux/man-pages/man2/waitpid.2.html), [fork(2)](http://man7.org/linux/man-pages/man2/fork.2.html), [pipe(7)](http://man7.org/linux/man-pages/man7/pipe.7.html), [execve(2)](http://man7.org/linux/man-pages/man2/waitpid.2.html), [popen(3)](http://man7.org/linux/man-pages/man3/popen.3.html)..). Your question shows a profound misunderstanding and don't have much sense. Read also about [Operating Systems](http://pages.cs.wisc.edu/~remzi/OSTEP/) – Basile Starynkevitch Sep 20 '17 at 07:12
  • All your arithmetic is integer arithmetic; you'll never get a floating point value from it. Your exit codes are limited to 255; that may cause problems too. The nested ifs with the chain of else's feels clumsy. You'd do better forking and testing for the 0 return value from fork (the child) and doing some action else forking the next child. – Jonathan Leffler Sep 20 '17 at 07:30
  • Yeah thats my problem i need a floating point return. So im wondering will casting after calling waitpid work? – Dreeww Sep 20 '17 at 07:54

1 Answers1

3

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.

gsamaras
  • 71,951
  • 46
  • 188
  • 305