Warning: file_get_contents(/data/phpspider/zhask/data//catemap/4/c/68.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/4/unix/3.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
C Fork有两个子进程,一个子进程计算N的和,另一个子进程计算N的阶乘_C_Unix_Fork - Fatal编程技术网

C Fork有两个子进程,一个子进程计算N的和,另一个子进程计算N的阶乘

C Fork有两个子进程,一个子进程计算N的和,另一个子进程计算N的阶乘,c,unix,fork,C,Unix,Fork,我必须从命令行获取一个整数参数。创建两个子进程。第一个计算正整数之和,第二个计算阶乘。父级还必须等待每个子级完成,然后父级用自己的标识符打印“完成” 下面是一个输出示例 [ID = 101] Sum of positive integers up to 5 is 15 [ID = 102] Factorial of 5 is 120 [ID = 100] Done 但我得到的结果是 [ ID = 4262] Factorial of 5 is 120 [ID = 4262] DONE [ID

我必须从命令行获取一个整数参数。创建两个子进程。第一个计算正整数之和,第二个计算阶乘。父级还必须等待每个子级完成,然后父级用自己的标识符打印“完成”

下面是一个输出示例

[ID = 101] Sum of positive integers up to 5 is 15
[ID = 102] Factorial of 5 is 120
[ID = 100] Done
但我得到的结果是

 [ ID = 4262] Factorial of 5 is 120
[ID = 4262] DONE
[ID = 4260] DONE
[ ID = 4261] sum of positive integers up to 5 is 15
这是我的密码

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

int main (int argc, char * argv[])
{
pid_t ret;
int i, num,sum=0,fact=1;

ret=fork();
    for(i=1;i<argc; i++){
        num=atoi(argv[1]);

    }
if (ret == 0) {
    for(i=1;i<=num;i++){
    sum+=i;
    }
    printf("\n [ ID = %d] sum of positive integers up to %d is %d\n",getpid(),num,sum);
}

else{
    if(fork()==0){
    for(i=1;i<=num; i++){
        fact*=i;    
    }printf("\n [ ID = %d] Factorial of %d is %d\n",getpid(),num,fact);
    }
    wait(NULL);
    printf("\n[ID = %d] DONE",getpid());
}

return 0;
}
#包括
#包括
#包括
#包括
#包括
int main(int argc,char*argv[])
{
pid_t ret;
int i,num,sum=0,fact=1;
ret=fork();

对于(i=1;i我建议您首先创建两个单独的函数,
sum()
fact()
,它们实现每个子进程应该执行的任务:

void sum(int num) { /* ... */ }
void fact(int num) { /* ... */ }
然后,让指针函数引用这些函数:

typedef (*child_task_t)(int);

child_task_t child_task[2];
child_task[0] = sum;
child_task[1] = fact;
这样,您就可以通过
for
循环轻松地
fork()
生两个孩子:

for (int i = 0; i < 2; i++) {
   switch(fork()) {
   case 0: // parent
      continue;
   case -1: // error
      return -1;
   default: // child
      task_child[i](num); // <-- run the task
      exit(0); // child process finishes
   }
}

// parent waits for the children to finish
for (int i = 0; i < 2; i++)
   wait(NULL);
for(int i=0;i<2;i++){
开关(fork()){
案例0://家长
继续;
案例1://错误
返回-1;
默认值://child

task_child[i](num);//您的代码中存在一些问题,我对其进行了注释:

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

int main (int argc, char * argv[])
{
    pid_t ret;
    int i, num,sum=0,fact=1;

    ret=fork();
    for (i=1; i < argc; i++) {
        num = atoi(argv[1]);
    }
    if (ret == 0) 
    {
        /* in first child */
        for(i=1;i<=num;i++){
            sum+=i;
        }
        printf("\n [ ID = %d] sum of positive integers up to %d is %d\n",getpid(),num,sum);
        /* here, you should exit from first child 
           with exit or return */

    }
    else
    {
        /* here, we are in parent */
        if(fork()==0)
        {
            /* here, in second child */
            for(i=1;i<=num; i++){
                fact*=i;    
            }
            printf("\n [ ID = %d] Factorial of %d is %d\n",getpid(),num,fact);
            /* here, you should exit from second child 
                with exit or return */

        }

        /* warning, second child and parent get there */
        /* warning, here you only wait for one child */
        wait(NULL);
        printf("\n[ID = %d] DONE",getpid());
    }

    return 0;
}
#包括
#包括
#包括
#包括
#包括
int main(int argc,char*argv[])
{
pid_t ret;
int i,num,sum=0,fact=1;
ret=fork();
对于(i=1;i对于(i=1;ii如果改为写入错误流,结果会有所不同吗?错误流通常是无缓冲的。如果使用函数而不是
main()
中的所有内联函数来完成工作,那么代码将更容易理解。不要忘了
wait()
用于所有子进程。@Alnitak谢谢!您还需要确保每个子进程在调用
task\u child
后确实显式地
exit()
(或
返回
),否则子进程的执行流将在
开关之后继续执行。
@Alnitak谢谢,最好显式调用
exit()
开关中
而不是依赖调用
exit()
的任务函数。可能是的。
返回
只有在
main
中,它相当于隐式退出时才行。
#include <stdio.h>
#include <stdlib.h>
#include <unistd.h>
#include <sys/types.h>
#include <sys/wait.h>

int main (int argc, char * argv[])
{
    pid_t ret;
    int i, num,sum=0,fact=1;

    /* get parameter (you should test the value of argc before) */
    num = atoi(argv[1]);    

    /* create a first child */
    if (0 == fork()) 
    {
        /* in first child */
        for(i=1;i<=num;i++){
            sum+=i;
        }
        printf("\n [ ID = %d] sum of positive integers up to %d is %d\n",getpid(),num,sum);

        /* exit from first child */
        exit(0);    
    }

    /* this point will not be reached by first child */

    /* create a second child */
    if ( 0 == fork())
    {
        /* here, in second child */
        for(i=1;i<=num; i++){
            fact*=i;    
        }
        printf("\n [ ID = %d] Factorial of %d is %d\n",getpid(),num,fact);
        /* exit from second child */
        exit(0);           
    }

    /* this point will not be reached by second child */

    /* wait for one child */
    wait(NULL);

    /* wait for a second child */
    wait(NULL);

    printf("\n[ID = %d] DONE",getpid());

    return 0;
}