用 fork() 打印斐波那契数

发布于 2024-07-17 03:16:00 字数 1123 浏览 4 评论 0原文

我遇到的问题是,例如,当用户输入 7 时,显示屏会显示:

0 11 2 3 5 8 13 21 child ends.

我似乎无法弄清楚如何修复 11 以及为什么它在序列中显示那么多数字! 有人可以帮忙吗?

序列号将在命令行中提供。 例如,如果提供 5,则子进程将输出斐波那契数列中的前五个数字。 由于父进程和子进程都有自己的数据副本,因此子进程需要输出序列。 让父进程调用 wait() 来等待子进程完成,然后再退出程序。 执行必要的错误检查以确保在命令行上传递非负数。

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

int main()
{
   int a=0, b=1, n=a+b,i,ii;
   pid_t pid;

   printf("Enter the number of a Fibonacci Sequence:\n");
   scanf("%d", &ii);

   if (ii < 0)
      printf("Please enter a non-negative integer!\n");
   else
   {
      pid = fork();
      if (pid == 0)
      {
         printf("Child is producing the Fibonacci Sequence...\n");
         printf("%d %d",a,b);
         for (i=0;i<ii;i++)
         {
            n=a+b;
            printf("%d ", n);
            a=b;
            b=n;
         }
         printf("Child ends\n"); 
      }
      else 
      {
         printf("Parent is waiting for child to complete...\n");
         wait(NULL);
         printf("Parent ends\n");
      }
   }
   return 0;
}

The problem I am having is that when say for instance the user enters 7, then the display shows:

0 11 2 3 5 8 13 21 child ends.

I cannot seem to figure out how to fix the 11 and why is it displaying that many numbers in the sequence! Can anyone help?

The number of the sequence will be provided in the command line. For example, if 5 is provided, the first five numbers in the Fibonacci sequence will be output by the child process. Because the parent and child processes have their own copies of the data, it will be necessary for the child to output the sequence. Have the parent invoke the wait() call to wait for the child process to complete before exiting the program. Perform necessary error checking to ensure that a non-negative number is passed on the command line.

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

int main()
{
   int a=0, b=1, n=a+b,i,ii;
   pid_t pid;

   printf("Enter the number of a Fibonacci Sequence:\n");
   scanf("%d", &ii);

   if (ii < 0)
      printf("Please enter a non-negative integer!\n");
   else
   {
      pid = fork();
      if (pid == 0)
      {
         printf("Child is producing the Fibonacci Sequence...\n");
         printf("%d %d",a,b);
         for (i=0;i<ii;i++)
         {
            n=a+b;
            printf("%d ", n);
            a=b;
            b=n;
         }
         printf("Child ends\n"); 
      }
      else 
      {
         printf("Parent is waiting for child to complete...\n");
         wait(NULL);
         printf("Parent ends\n");
      }
   }
   return 0;
}

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(6

烈酒灼喉 2024-07-24 03:16:00

11实际上是两个1,它们之间没有空格。 第一个来自这里的第二个 %d,因为 b 的值为 1:

printf("%d %d",a,b);

第二个来自循环中的第一个 printf,其中 n = 1:

printf("%d ", n);

The 11 is actually two 1's with no space between them. The first comes from the second %d here, because b's value is 1:

printf("%d %d",a,b);

The second comes from the first printf in the loop, where n = 1:

printf("%d ", n);
2024-07-24 03:16:00

如果没有这里的结尾空格,

printf("%d %d",a,b);

您第一次这样做时就会遇到麻烦。

printf("%d ", n);

您可以做的最优雅的事情是更改循环中的 printf 以在前面添加所需的空格,例如这样:

printf(" %d", n);

这样你就不会在最后留下一个悬挂空间...


你会显示“太多”元素,因为你没有计算第一个 printf 中写入的元素...

Without the ending space here

printf("%d %d",a,b);

you get into trouble the first time you do

printf("%d ", n);

The most elegant thing you could do would be to change the printf in the loop to prepend the needed space like this:

printf(" %d", n);

That way you're not left with a hanging space at the end...


You get "too many" elements displayed because you haven't counted the one written in first printf...

过潦 2024-07-24 03:16:00

在我看来,你的程序正确地生成了斐波那契数列,也许前两个“1”字符看起来像“11”,因为它们在屏幕上被挤压在一起?

It looks to me like your program is correctly producing the fibonacci sequence, maybe the first two '1' characters look like an '11' because they are squashed together on your screen?

七色彩虹 2024-07-24 03:16:00

您的代码有一些误导性的表达式......

您正在获取作为键盘输入的值,而不是作为命令行参数......
所以你可以按如下方式修改它...

        main(int argc, char *argv[])

然后你可以获取该值并将其存储为整数值...

        int num = atoi(argv[1]);

并且你还可以检查用户是否可以使用以下方式输入该值...

        if(argc < 2)
        {
            printf("You must enter a value to proceed this operation..\n");
            return;         
        }

如果你在那里输入 no 1 ,它会打印 0 1。但答案应该只是 0;
所以为了避免这种情况,你可以使用两个 if 条件......
如果(n==0)
printf("0");
如果(n==1)
printf("1");
通过使用上面两个 if 条件,你也可以避免打印输出 11....

your code has some misleading expressions......

u are getting the value as keyboard input, not as the command line argument...
so u can modify it as follows...

        main(int argc, char *argv[])

and then u can get the value and store it as a integer value...

        int num = atoi(argv[1]);

and also u can check that user may enter the value by using...

        if(argc < 2)
        {
            printf("You must enter a value to proceed this operation..\n");
            return;         
        }

and if u enter no 1 there, it will print 0 1. but the answer should be only 0;
so to avoid this u can use two if conditions...
if(n == 0)
printf("0 ");
if(n == 1)
printf("1 ");
by using above two if conditions u can avoid printing output 11 as well....

只想待在家 2024-07-24 03:16:00

printf("%d %d ",a,b);

代替

printf("%d %d",a,b);

write

printf("%d %d ",a,b);

instead of

printf("%d %d",a,b);

心的憧憬 2024-07-24 03:16:00

去掉 printf("%d %d"ab); 并让 n 打印所有值。

首先将第一个变量设置为 a = 1,然后设置第二个变量:b = 0 和第三个变量 c=a+b。 现在,首先不做任何更改地打印 c (printf("%d",c);),然后执行 a=b; b=c;

for (i=0; i<ii; i++)
{ 
    c=a+b;
    printf("%d ", c);
    a=b;
    b=c;
}

此外,在您的程序中您已经执行了两次n=a+b,这不是必需的。

Take off the printf("%d %d"a b); and let the n print all values.

First set the first variable as e.g a = 1, then set second: b = 0 and third c=a+b. Now first print c without any changes (printf("%d",c);) then do a=b; b=c;:

for (i=0; i<ii; i++)
{ 
    c=a+b;
    printf("%d ", c);
    a=b;
    b=c;
}

Also, in your program you've done n=a+b twice, which is not required.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文