使用线程时的分段错误 - c

时间:2013-10-31 16:27:53

标签: c pthreads

这是我第一次使用线程,我开始使用一个简单的程序。该程序获取n个参数并创建n-2个线程。问题是我遇到了分段错误,我不知道为什么。

以下是代码:

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

void *
removeBytes (int i, char* argv[])
{
  printf ("%d, %s\n", i, argv[i]);
  return NULL;
}  


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

  pthread_t threads[argc - 3];
  int err;
  int i;  
  int *ptr[argc - 3];

  printf ("argc = %d\n", argc);

  for (i = 0; i < argc -3; i++)
    {
      err =
        pthread_create (&(threads[i]), NULL,
                        removeBytes(i+1,&argv[i+1]), NULL);
      if (err != 0)
        {
          printf ("\nCan't create thread: [%d]", i);
        }
      else
        { 
          printf ("\nThread created successfully\n");
        }
    }

  for (i = 0; i < argc - 3; i++)
    {
      pthread_join (threads[i], (void **) &(ptr[i]));
      printf("pthread_join - thread %d",i);
    }

  return 0;
}

示例:我的程序名为mythread,因此当我运行它./mythread f1 f2 f3 f4 f5 f6时输出为:

argc = 6

1,f2
Thread created successfully

2,f4
Thread created successfully
3, (null)

为什么f2argv[1]f4argv[2]

更新:

 typedef struct{
    int i;
    char* argv;
  }Data;

  void* removeBytes(void* arg){
    Data* data = (Data*)arg;   
    printf("%d, %s\n",data->i, data->argv);
    free(data);
    return NULL;
  }



  int main(int argc, char** argv){
    Data* data;

    pthread_t threads[argc-3];

    int i;
    int err;
    for(i=0; i < argc-3;i++){
      data = (Data*)malloc(sizeof(Data));
      data->i=i+1;
      data->argv=argv[i+1];
      err = pthread_create(&(threads[i]),NULL,removeBytes,data);
      if(err != 0){
        printf("\nCan't create thread %d",i);
      }
      else{
        printf("Thread created successfully\n");
      }
    }  

    return 0;
  }

for ./mythread f1 f2 f3 f4 f5 f6 f7 f8输出为:

5 x“线程创建成功”。它不打印i或argvi [i]。

4 个答案:

答案 0 :(得分:0)

您未正确使用pthread_create

pthread_create (&(threads[i]), NULL,
                        removeBytes(i+1,&argv[i+1]), NULL);

在这里,您只需调用removeBytes()并将结果(NULL)作为pthread_create()的参数传递。

int pthread_create(pthread_t *thread, const pthread_attr_t *attr,
                          void *(*start_routine) (void *), void *arg);

第三个参数应该是指向void* myThread(void*)函数的指针。如果要将参数传递给线程,则应使用void*参数并将其作为pthread_create的第三个参数传递。

您可以查看this以了解如何使用pthread库。

另外,你可能想要这样的事情:

typedef struct {
    int i;
    char* argv;
} Data;

void * removeBytes (void* arg)
{
  Data* data = (Data*) arg;
  printf ("%d, %s\n", data->i, data->argv);
  free(data);
  return NULL;
}

然后创建这样的线程:

Data* data = (Data*)malloc(sizeof(Data));
data->i = i;
data->argv = argv[i+1];
err = pthread_create (&(threads[i]), NULL, removeBytes, data);

答案 1 :(得分:0)

pthread_create (&(threads[i]), NULL,
                    removeBytes(i+1,&argv[i+1]), NULL);

您正在调用removeBytes()而不是将其作为参数传递。

此外,您只能将一个参数传递给线程函数。所以你需要在结构中放置多个参数。类似的东西:

struct thread_args {
    int i;
    char *argv;
}

#your main code
struct thread_args *thargs;

for (i = 0; i < argc -3; i++)
{
  thargs = malloc(sizeof(*thargs));
  thargs->i = i+1;
  thargs->argv = argv[i+1];
  err =
    pthread_create (&(threads[i]), NULL,
                    removeBytes, thargs);
  if (err != 0)
    {
      printf ("\nCan't create thread: [%d]", i);
    }
  else
    { 
      printf ("\nThread created successfully\n");
    }
}
#make sure to free up thargs as well.

将线程功能更新为

void *removeBytes (void *arg)
{
  int i;
  char *argv;
  struct thread_args *thargs =  (struct thread_args *) arg;
  i = thargs->i;
  argv = thargs->argv;
  printf ("%d, %s\n", i, argv);
  return NULL;
} 

答案 2 :(得分:0)

中存在问题
pthread_create (&(threads[i]), NULL,
                    removeBytes(i+1,&argv[i+1]), NULL);

pthread_create的语法是

int pthread_create(pthread_t *thread, const pthread_attr_t *attr,
                      void *(*start_routine) (void *), void *arg);

它需要启动例程作为回调。在您的情况下,您在生成之前调用主线程中的removeBytes并返回NULL。所以,回调是NULL。

因此,请根据需要相应地修改您的removeBytes并致电

pthread_create(&amp;(threads [i]),NULL,                         removeBytes,argv [i + 1]);

答案 3 :(得分:0)

它将f2作为argv [1]而f4作为argv [2]只是因为当你传递&amp; argv [i + 1]时你实际上将指针传递给数组的第(i + 1)个元素而你也将i + 1作为索引。

因此,如果你有argv等于[mythread,f1,f2,f3,f4,f5,f6],你会在removeBytes中得到[f1,f2,f3,f4,f5,f6]。当你访问i + 1 = 1元素时,你将获得f2。下次你会得到[f2,f3,f4,f5,f6]和i + 1 = 2所以你会得到f4