更改 waitpid() 的参数
Changing Parameters of waitpid()
我想知道你是否可以更改参数
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);
}
}
pid_t waitpid(pid_t pid, int *status, int options);
的第三个参数是一个int
,因此你不能在那里传递浮点值。
事实上,在那里传递这样的值是没有意义的。你需要更加努力地学习并重新考虑你的方法。
我想知道你是否可以更改参数
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);
}
}
pid_t waitpid(pid_t pid, int *status, int options);
的第三个参数是一个int
,因此你不能在那里传递浮点值。
事实上,在那里传递这样的值是没有意义的。你需要更加努力地学习并重新考虑你的方法。