簡體   English   中英

使用pThreads的C代碼可以正常編譯,但會產生段錯誤,使用gdb無效

[英]C code using pThreads compiles fine but gives a seg fault, used gdb to no avail

嗨,我正在嘗試在Mac上的終端中運行此代碼,並且可以正常編譯,但是當我嘗試執行它時,段錯誤..當我運行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);
}

如果使用-ggdb進行編譯,則在沒有cli args的情況下在GDB中運行該程序,我發現該段錯誤位於atoi中。 查看代碼,我看到:

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

不能通過檢查argc來驗證argv[1]argv[2]是否存在。

如果我使用以下命令行運行:

 ./a.out 500 8

我得到這個結果:

  Computed PI = 2.616000

簡而言之,我懷疑您運行的程序不正確,並且提供的效果不是很好。

您必須將兩個參數傳遞給程序。 該程序不檢查是否缺少參數是很糟糕的。 我將插入

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

之前

sample_points = atoi(argv[1]);

除了不檢查已回答的argc之外,還不建議使用atoi(),您應該考慮使用strtol()。

您系統上atoi()的手冊頁可能也提到了這一點。

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM