使用pthread的Fibbonaci序列不起作用

使用pthread的Fibbonaci序列不起作用

本文介绍了使用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

推荐答案

#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]




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

10-20 21:12