我正在用C编写一个程序来计算访问某些网站的时间站点名存储在url数组的每个元素中如果我去掉for(y=0;y这是什么原因?

char *urls[50]; char str1[20];

void *wget(void *argument)
{
  int threadid;
  threadid = *((int *)argument);
  strcpy(str1, "wget -q --spider ");
  strcat(str1, urls[threadid]);
  system(str1);
}


for (y = 0; y < iterations; y++)
{
  for (j = 0; j < numthreads; j++)
  {
        thread_args[j] = j;

        clock_gettime(CLOCK_REALTIME, &bgn);
        rc = pthread_create(&threads[j], NULL, wget, (void *) &thread_args[j]);
        rc = pthread_join(threads[j], NULL);
        clock_gettime(CLOCK_REALTIME, &nd);
        times[j] = timediff(bgn,nd);
  }
}

最佳答案

一些可能性。。。
str1似乎在所有线程之间共享这是个麻烦的秘诀。
str1只有20个字符长很难相信整个wget命令行(包括URL)将少于20个字符所以你在注销str1的结尾。
考虑在str1中使wget()成为一个局部变量,或者使它成为一个足够大的char数组来处理您可能拥有的最大的wget命令行,或者根据命令行常量部分的长度和当前URL的大小动态地分配它并在wget()中释放它。

07-24 09:46
查看更多