2

こんにちは、Mac のターミナルでこのコードを実行しようとしていますが、問題なくコンパイルされますが、実行しようとすると seg faults.. gdb を実行すると、無効なメモリ アドレス 000000 または何かが示されます。前もって感謝します。

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


#define MAX_THREADS 512

void *compute_pi( void * );


int sample_points;
int total_hits;
int total_misses;
int hits[ MAX_THREADS ];
int sample_points;
int sample_points_per_thread;
int num_threads;



int main( int argc, char *argv[] )
{
  /* local variables */
  int ii;
  int retval;
  pthread_t p_threads[MAX_THREADS];
  pthread_attr_t attr;
  double computed_pi;

  /* initialize local variables */
  retval = 0;


  pthread_attr_init( &attr );
  pthread_attr_setscope( &attr, PTHREAD_SCOPE_SYSTEM );

  /* parse command line arguments into sample points and number of threads */
  /* there is no error checking here!!!!! */
  sample_points = atoi(argv[1]);
  num_threads = atoi(argv[2]);

  /* uncomment this block if you want interactive input!!!! */
  /* if so...comment out the two statements above */
  /*  
  printf( "Enter number of sample points: " );
  scanf( "%d", &sample_points );
  printf( "Enter number of threads: " );
  scanf( "%d%", &num_threads );
  */

  total_hits = 0;
   sample_points_per_thread = sample_points / num_threads;

  for( ii=0; ii<num_threads; ii++ )
    {
      hits[ii] = ii;
      pthread_create( &p_threads[ ii ], &attr, compute_pi, (void *) &hits[ii] );
    }

  for( ii=0; ii<num_threads; ii++ )
    {
       pthread_join( p_threads[ ii ], NULL );
       total_hits += hits[ ii ];
    }

   computed_pi = 4.0 * (double) total_hits / ((double) (sample_points));


   printf( "Computed PI = %lf\n", computed_pi );


  /* return to calling environment */
  return( retval );
}


void *compute_pi( void *s )
{
  int seed;
  int ii;
  int *hit_pointer;
  int local_hits;
  double rand_no_x;
  double rand_no_y;

  hit_pointer = (int *) s;
  seed = *hit_pointer;
  local_hits = 0;

  for( ii=0; ii < sample_points_per_thread; ii++ )
    {
      rand_no_x = (double) (rand_r( &seed ))/(double)RAND_MAX;
      rand_no_y = (double) (rand_r( &seed ))/(double)RAND_MAX;
      if(((rand_no_x - 0.5) * (rand_no_x - 0.5) +
      (rand_no_y - 0.5) * (rand_no_y - 0.5)) < 0.25)
    local_hits++;
      seed *= ii;
    }

  *hit_pointer = local_hits;
  pthread_exit(0);
}
4

3 に答える 3

1

-ggdb でコンパイルし、CLI 引数なしで GDB でプログラムを実行すると、segfault が atoi にあることがわかります。コードを見ると、次のことがわかります。

sample_points = atoi(argv[1]);
num_threads = atoi(argv[2]);

argcそれを確認するためのチェックによって進められず、そこargv[1]argv[2]あるでしょう。

このコマンドラインで実行すると:

 ./a.out 500 8

私はこの結果を得ます:

  Computed PI = 2.616000

要するに、あなたはプログラムを間違って実行しているのではないかと思います。

于 2013-03-11T18:13:50.343 に答える
0

プログラムに2つのパラメータを渡す必要があります。このプログラムがパラメータが欠落しているかどうかをチェックしないのは悪いことです。挿入します

if(argc<3){
    printf("Usage: %s [samples] [threads]\n",argv[0]);
    return 0;
}

sample_points = atoi(argv[1]);
于 2013-03-11T18:19:45.747 に答える
0

すでに回答されている argc をチェックしないという問題に加えて、atoi() は非推奨になっているため、代わりに strtol() の使用を検討する必要があります。

お使いのシステムの atoi() の man ページにも、これについて言及されている可能性があります。

于 2013-03-12T10:46:36.920 に答える