我想知道你是否可以改变参数
waitpid()
目前,我需要连续变量输出(0.50
)才能打印出来。但是,考虑到waitpid()
在我尝试打印时只接受整数,它给了我0.不确定如何解决这个问题,或者这是否是问题。
计算看起来像(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);
}
}
答案 0 :(得分:3)
pid_t waitpid(pid_t pid, int *status, int options);
的第三个参数是int
,因此您无法在那里传递浮点值。
事实上,在那里传递这样的值是没有意义的。你需要更加努力学习并重新考虑你的方法。