使用pthread的Fibbonaci序列不起作用。 [英] Fibbonaci sequence using pthread not working.

查看:57
本文介绍了使用pthread的Fibbonaci序列不起作用。的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

#include<pthread.h>
#include<stdio.h>
#include<stdlib.h>

int fib;

void* fibonacci(void *n)
{
    int a=0,b=1,c,i;
    if(atoi(n)==1)
    {
        printf("The Fibbonaci sequence for the entered number is\n");
        printf("%d\n",a);
        exit(0);
    }

    else if(atoi(n)==2)
    {
        printf("The Fibbonaci sequence for the entered number is\n");
        printf("%d\t%d",a,b);
        exit(0);
    }

    else
    {
        printf("The Fibbonaci sequence for the entered number is\n");
        printf("%d\t%d",a,b);
        for(i=0;i<atoi(n)-2;i++)
        {
            c=a+b;
            printf("\t%d",c);
            a=b;
            b=c;
        }

        pthread_exit(0);
    }


int main(int argc,char* argv[])
{
    pthread_t thrd;

    if(argc!=2)
    {
        fprintf(stderr,"Syntax: ./a.out <integer value>");
        return -1;
    }

    if(atoi(argv[1])<0)
    {
        fprintf(stderr,"Argument %d must be positive value\n",atoi(argv[1]));
        return -1;
    }

    pthread_create(&thrd,NULL,fibbonaci,(void*)argv[1]);

    pthread_join(thrd,NULL);

    exit(0);
}









编译代码给出了以下错误:







The code on compilation gives the following errors:

In function ‘main’:
FibnThread.c:56:27: error: ‘fibbonaci’ undeclared (first use in this function)
FibnThread.c:56:27: note: each undeclared identifier is reported only once for each function it appears in
FibnThread.c: In function ‘fibonacci’:
FibnThread.c:61:1: error: expected declaration or statement at end of input

推荐答案

您(正确地)将该函数实现为 fibonacci Leonardo Fibonacci [ ^ ])但是将其作为 fib b onacci 调用。在函数调用中删除虚假的 b



[update]

你实际上忘了一个右大括号('}')。这里是固定代码:

You (correctly) implemented the function as fibonacci (Leonardo Fibonacci[^]) but invoked it as fibbonacci. Remove the spurious b in the function call.

[update]
You actually forgot a closing brace ('}'). Here you are the fixed code:
#include<pthread.h>
#include<stdio.h>
#include<stdlib.h>

int fib;

void* fibonacci(void *n)
{
  int a=0,b=1,c,i;
  if(atoi(n)==1)
  {
    printf("The Fibonacci sequence for the entered number is\n");
    printf("%d\n",a);
    exit(0);
  }
  else if(atoi(n)==2)
  {
    printf("The Fibonacci sequence for the entered number is\n");
    printf("%d\t%d",a,b);
    exit(0);
  }
  else
  {
    printf("The Fibonacci sequence for the entered number is\n");
    printf("%d\t%d",a,b);
    for(i=0;i<atoi(n)-2;i++)
    {
      c=a+b;
      printf("\t%d",c);
      a=b;
      b=c;
    }
    pthread_exit(0);
  }
}

int main(int argc,char* argv[])
{
  pthread_t thrd;

  if(argc!=2)
  {
    fprintf(stderr,"Syntax: ./a.out <integer value>");
    return -1;
  }
  if(atoi(argv[1])<0)
  {
    fprintf(stderr,"Argument %d must be positive value\n",atoi(argv[1]));
    return -1;
  }

  pthread_create(&thrd,NULL,fibonacci,(void*)argv[1]);

  pthread_join(thrd,NULL);

  exit(0);
}



[/ update]


看看这个link.same问题



这里
just look at this link.same problem

here


斐波纳契函数中的最后一个条件缺少一个括号。
A brace was missing for the last else condition in the fibonacci function.


这篇关于使用pthread的Fibbonaci序列不起作用。的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆