&引用;分段错误:11“;四线程分割进程C程序中的错误
我已经用C编写了我的第一个多线程程序。特别是:该程序接收一个int参数,该参数指定main中int类型数组的大小。数组将被随机分配和填充。我想研究多线程程序和单线程程序之间的时间差异。因此,我编写了上述程序的两个“相同”版本。“标准”程序运行良好,所有操作都正确,例如:&引用;分段错误:11“;四线程分割进程C程序中的错误,c,arrays,multithreading,pthreads,C,Arrays,Multithreading,Pthreads,我已经用C编写了我的第一个多线程程序。特别是:该程序接收一个int参数,该参数指定main中int类型数组的大小。数组将被随机分配和填充。我想研究多线程程序和单线程程序之间的时间差异。因此,我编写了上述程序的两个“相同”版本。“标准”程序运行良好,所有操作都正确,例如: $ ./prog 10000 all done real 0m0.244s user 0m0.238s sys 0m0.004s 这是非线程程序的输出 但是,当我运行线程程序时,会出现以下错误: Seg
$ ./prog 10000
all done
real 0m0.244s
user 0m0.238s
sys 0m0.004s
这是非线程程序的输出
但是,当我运行线程程序时,会出现以下错误:
Segmentation fault: 11
我已经看到这个错误代码是关于指针的错误(通常超出分配的内存区域),但是这次我找不到我提交的错误/bug。会是疲劳,会是我是一个初学者,但现在我看不出我的错误。有人能帮我吗
线程程序的源代码如下:
#include <stdio.h>
#include <stdlib.h>
#include <pthread.h>
void *order(int * v, int x, int y);
void printv(int * v, int dim);
int main (int argc, const char *argv[]) {
int *v = NULL;
int x = 0;
int dim = 0;
pthread_t bot1 = NULL, bot2 = NULL, bot3 = NULL, bot4 = NULL;
void *retval;
if (argc != 2) {
fprintf(stderr, "usage: %s [arraySize]\n", argv[0]);
exit(EXIT_FAILURE);
}
dim = atoi(argv[1]);
if (dim <= 0) {
fprintf(stderr, "usage: %s [arraySize] in which [arraySize] must be an integer > 0\n", argv[0]);
exit(EXIT_FAILURE);
}
v = (int *) malloc (dim * sizeof (int));
if (v == NULL) {
fprintf(stderr, "array allocation error\n");
exit(EXIT_FAILURE);
}
// Initial array
fprintf(stdout, "the initial array is: ");
printv(v, dim);
fprintf(stdout, "\n\n");
// Randomize array
for (x = 0; x < dim; x++) {
v[x] = rand()%100;
}
fprintf(stdout, "the randomized array is: ");
printv(v, dim);
fprintf(stdout, "\n\n");
// Ordering array
// WARNING: "OVERLAP SECTOR BEGIN"
pthread_create(&bot1, NULL, order(v, 0, dim/4), NULL);
pthread_create(&bot2, NULL, order(v, dim/4, dim/2), NULL);
pthread_create(&bot3, NULL, order(v, dim/2, (dim/4)*3), NULL);
pthread_create(&bot4, NULL, order(v, (dim/4)*3, dim), NULL);
pthread_join(bot1, &retval);
pthread_join(bot2, &retval);
pthread_join(bot3, &retval);
pthread_join(bot4, &retval);
// WARNING: "OVERLAP SECTOR END"
fprintf(stdout, "the ordered array is: ");
printv(v, dim);
fprintf(stdout, "\n\n");
// End main
fprintf(stdout, "all done\n");
return 0;
}
void *order (int * v, int x, int y){
int i = 0;
int j = 0;
int tmp = 0;
for (i = x; i < y; i++) {
for (j = x; j < y; j++) {
if (v[i] < v[j]) {
tmp=v[i];
v[i]=v[j];
v[j]=tmp;
}
}
}
return 0;
}
void printv (int * v, int dim) {
int x = 0;
for (x = 0; x < dim; x++) {
fprintf(stdout, "%d ", v[x]);
}
fprintf(stdout, "\n\n\n");
return;
}
#包括
#包括
#包括
无效*顺序(整数*v、整数x、整数y);
无效打印v(整数*v,整数尺寸);
int main(int argc,const char*argv[]{
int*v=NULL;
int x=0;
int dim=0;
pthread_t bot1=NULL,bot2=NULL,bot3=NULL,bot4=NULL;
作废*收回;
如果(argc!=2){
fprintf(stderr,“用法:%s[arraySize]\n”,argv[0]);
退出(退出失败);
}
dim=atoi(argv[1]);
如果(dim您使用的pthread_create不正确。请尝试以下操作:
#include <stdio.h>
#include <stdlib.h>
#include <pthread.h>
struct args {
int* _v;
int _x;
int _y;
};
void *order(void *args_ptr);
void printv(int * v, int dim);
int main (int argc, const char *argv[]) {
int *v = NULL;
int x = 0;
int dim = 0;
pthread_t bot1 , bot2 , bot3 , bot4 ;
void *retval;
struct args args1;
struct args args2;
struct args args3;
struct args args4;
if (argc != 2) {
fprintf(stderr, "usage: %s [arraySize]\n", argv[0]);
exit(EXIT_FAILURE);
}
dim = atoi(argv[1]);
if (dim <= 0) {
fprintf(stderr, "usage: %s [arraySize] in which [arraySize] must be an integer > 0\n", argv[0]);
exit(EXIT_FAILURE);
}
v = (int *) malloc (dim * sizeof (int));
if (v == NULL) {
fprintf(stderr, "array allocation error\n");
exit(EXIT_FAILURE);
}
// Initial array
fprintf(stdout, "the initial array is: ");
printv(v, dim);
fprintf(stdout, "\n\n");
// Randomize array
for (x = 0; x < dim; x++) {
v[x] = rand()%100;
}
fprintf(stdout, "the randomized array is: ");
printv(v, dim);
fprintf(stdout, "\n\n");
// Ordering array
// WARNING: "OVERLAP SECTOR BEGIN"
args1._v = v;
args1._x = 0;
args1._y = dim/4;
args2._v = v;
args2._x = dim/4;
args2._y = dim/2;
args3._v = v;
args3._x = dim/2;
args3._y = (dim/4)*3;
args4._v = v;
args4._x = (dim/4)*3;
args4._y = dim;
pthread_create(&bot1, NULL, order, &args1);
pthread_create(&bot2, NULL, order, &args2);
pthread_create(&bot3, NULL, order, &args3);
pthread_create(&bot4, NULL, order, &args4);
pthread_join(bot1, &retval);
pthread_join(bot2, &retval);
pthread_join(bot3, &retval);
pthread_join(bot4, &retval);
// WARNING: "OVERLAP SECTOR END"
fprintf(stdout, "the ordered array is: ");
printv(v, dim);
fprintf(stdout, "\n\n");
// End main
fprintf(stdout, "all done\n");
return 0;
}
void *order(void *args_ptr ) {
struct args *a = args_ptr;
int* v = a->_v;
int x = a->_x;
int y = a->_y;
int i = 0;
int j = 0;
int tmp = 0;
for (i = x; i < y; i++) {
for (j = x; j < y; j++) {
if (v[i] < v[j]) {
tmp=v[i];
v[i]=v[j];
v[j]=tmp;
}
}
}
return 0;
}
void printv (int * v, int dim) {
int x = 0;
for (x = 0; x < dim; x++) {
fprintf(stdout, "%d ", v[x]);
}
fprintf(stdout, "\n\n\n");
return;
}
然后,在gdb内部:
(gdb) run 10000
(gdb) backtrace
它应该显示错误发生在pthread_create.c中
(gdb) run 10000
(gdb) backtrace