- html - 出于某种原因,IE8 对我的 Sass 文件中继承的 html5 CSS 不友好?
- JMeter 在响应断言中使用 span 标签的问题
- html - 在 :hover and :active? 上具有不同效果的 CSS 动画
- html - 相对于居中的 html 内容固定的 CSS 重复背景?
我想用 vfork() 编写一个程序,父级创建 n 个子级,我想用参数插入儿子的数量。然后我想计算儿子的数量,例如:
./sum 4
The sum of the child: 10
The sum of the parent: 10
(1+2+3+4)
这是我想出的小代码,但我得到了无限循环。
int n = atoi(argv[1]);
int i = 1;
pid_t pid;
int sumchild = 0;
int sumparent = 0;
while(i <= n){
pid = vfork();
if(pid == 0){
sumchild = sumchild + i;
}
i++;
}
printf("The sum of the child: %i ", sumchild);
sumparent = (1 + n) * (n / 2);
printf("The sum of the parent: %i \n", sumparent);
我听说你不需要像 fork()
中那样的 wait()
,但我不知道为什么这里会出现无限循环。
我应该如何使用vfork()
?我写的代码是正确的还是犯了一些错误?
最佳答案
以下代码
pid = vfork();
if(pid == 0){
sumchild = sumchild + i;
将导致未定义的行为,根据vfork
:
The vfork() function has the same effect as fork(2), except that the behavior is undefined if the process created by vfork() either modifies any data other than a variable of type pid_t used to store the return value from vfork(), or returns from the function in which vfork() was called, or calls any other function before successfully calling _exit(2) or one of the exec(3) family of functions.
关于c - 使用 vfork() 对子级数求和,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/23168279/
我需要对反正切值执行泰勒级数 50 次。表示 arctan Taylor 级数的域之间的 50 个数字,即 [-1,1]。我已经用手动用户输入对其进行了测试并且它工作正常,但是我在代码中递增 0.01
我在网上看了几个小时,想看看我是否能找到解决方案,虽然我已经找到了很多解决方案,但我教授的指示如下: Write a program to estimate PI (π) using the foll
我最近在编程测试中被问到这个问题。我似乎无法理解为什么我会得到答案“1”。我是 C 编程语言的初学者。 这是我的代码: #include int main() { float c = 0;
我是一名优秀的程序员,十分优秀!