Collat​​z结构循环

时间:2016-10-08 12:54:49

标签: c collatz

我正在尝试创建一个Collat​​z结构,询问用户他们想要运行多少次。然后它将每次递增的代码循环3(n = n + 3)。虽然代码部分有效,但它继续重复之前完成的过程,例如,输入为5并且运行过程3次"孩子1 = 5,16,8,4,2,1和#34;和"孩子2 = 8,4,2,1"和#34;孩子3 = 11,34,17,52,26,13等"
问题是,它循环次数太多并且多次运行每个Child。有一次它是正确的,第二次运行它,它在" 1开始序列。"
我是从Linux Debian运行的。要编译,我使用" gcc -o filename filename.c"然后执行我使用" ./ filename 5"其中5是传递给" n"对于Collat​​z结构。然后它会提示运行循环的次数 我知道我可能会离开,但我完全迷失了,非常感谢任何帮助。

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

int main(int argc, char *argv[])
{
    pid_t pid;
    int n, j, x;
    printf ("How many times would you like this to run?\n");
    scanf ("%d",&j);

    if (argc == 1) {
        fprintf (stderr,"Usage: ./a.out <starting value>\n");
        return -1;
    }
    printf("\nMain program's process ID: %d\n",getpid());
    n = atoi(argv[1]);

    for (x=1; x <= j; x++){
        pid = fork();

        if (pid < 0) {
            fprintf(stderr, "Unable to fork child\n");
            return -1;
        }
        else if (pid == 0) { /*child process */
            printf("\nChild %d (ID: %d)\n",x,getpid());
            printf("\nStart sequence at: %d\n",n);
            while (n != 1) {
                n = n % 2 ? 3 * n + 1 : n / 2;
                printf("\n(Child %d) %d ",x,n);

            }
            printf("\n\nAbout to end execution (I'm process  %d) .\n",getpid());
        }
        else { /* parent process */
            wait(NULL);
            n = n + 3;
        }
    }   
    return 0;
}

1 个答案:

答案 0 :(得分:0)

在我看来,一旦它完成,你就不会终止子进程,你只是让它继续父进程产生更多进程的主循环。此外,您正在运行一个进程并等待它,它只会让您的父进程只进行计算而不会产生任何结果 - 而是生成所有子进程并等待它们完成,每个进程都在其自己的时间内完成。我重写了你的代码以结合上面和一些风格调整:

"This will NOT fail."

SAMPLE RUN

"THIS will fail."

如果乱序结果让您烦恼,可以考虑使用线程并返回主线程打印的结果,或者使用某种锁定来同步输出。或让孩子们将结果写入父母在最后总结的临时文件或管道。

最后一个样式注释,不要从#include <stdio.h> #include <unistd.h> #include <stdlib.h> #include <sys/wait.h> int main(int argc, char *argv[]) { if (argc == 1) { fprintf(stderr,"Usage: ./a.out <starting value>\n"); return EXIT_FAILURE; } int j; printf ("How many times would you like this to run?\n"); scanf ("%d", &j); (void) fpurge(stdin); printf("\nMain program's process ID: %d\n", getpid()); int n = atoi(argv[1]); for (int x = 1; x <= j; x++) { pid_t pid = fork(); if (pid == -1) { fprintf(stderr, "Unable to fork child\n"); return EXIT_FAILURE; } else if (pid == 0) { /* child process */ pid_t child_pid = getpid(); printf("\nChild %d (ID: %d)\n", x, child_pid); printf("\nStart sequence at: %d\n", n); while (n != 1) { n = n % 2 ? 3 * n + 1 : n / 2; printf("\n(Child %d) %d ", x, n); } printf("\n\nAbout to end execution (I'm process %d).\n", child_pid); return EXIT_SUCCESS; /* child terminates */ } else { /* parent process */ n = n + 3; } } for (int x = 1; x <= j; x++) { wait(NULL); } return EXIT_SUCCESS; } 返回-1,也不要返回> ./a.out 5 How many times would you like this to run? 4 Main program's process ID: 1164 Child 1 (ID: 1165) Start sequence at: 5 (Child 1) 16 (Child 1) 8 (Child 1) 4 (Child 1) 2 (Child 1) 1 About to end execution (I'm process 1165). Child 3 (ID: 1167) Start sequence at: 11 (Child 3) 34 (Child 3) 17 (Child 3) 52 (Child 3) 26 (Child 3) 13 Child 2 (ID: 1166) (Child 3) 40 (Child 3) 20 Start sequence at: 8 (Child 3) 10 (Child 3) 5 (Child 2) 4 (Child 3) 16 (Child 2) 2 (Child 3) 8 (Child 2) 1 (Child 3) 4 (Child 3) 2 About to end execution (I'm process 1166). (Child 3) 1 About to end execution (I'm process 1167). Child 4 (ID: 1168) Start sequence at: 14 (Child 4) 7 (Child 4) 22 (Child 4) 11 (Child 4) 34 (Child 4) 17 (Child 4) 52 (Child 4) 26 (Child 4) 13 (Child 4) 40 (Child 4) 20 (Child 4) 10 (Child 4) 5 (Child 4) 16 (Child 4) 8 (Child 4) 4 (Child 4) 2 (Child 4) 1 About to end execution (I'm process 1168). > - 尽管返回-1表示系统子例程有错误,main()返回的值操作系统应该在0(成功)到255的范围内,其中1(失败)是一般错误指示符。