我有一个多线程程序的奇怪问题,我将只报告部分代码。当我尝试运行它时,我收到一个分段错误错误。使用 gdb 和 valingrind 我能够发现问题出在我尝试取消引用时info
,例如在for(i=0; i<info->subm_n; i++)
. 最奇怪的是,如果我在info=(c_args*)a
之后进行strncpy()
强制转换,只有在收集器的线程退出时才会出现分段错误。我使用的是 64 位操作系统,并且我读到这有时会在投射到void*
in时出现问题pthread_create()
,我什至不知道是不是这样。有人有什么想法吗?
PS 带有大写字母的系统调用只是对女巫中功能的重新定义,我也测试失败
typedef struct collector_arguments{
int subm_n;
int chronon;
planet_t *p;
}c_args;
static void* collector(void* a) {
int fd_skt,fd_sincro,tmp,i=0;
c_args *info;
struct sockaddr_un sa;
info=(c_args*) a;
strncpy(sa.sun_path,"visual.sck" ,MAXPATH);
sa.sun_family=AF_UNIX;
if((fd_sincro=open("SINCRO",O_RDWR))==-1) {
perror("collector unable to open SINCRO fifo");fflush(stdout);
pthread_exit(&errno);
}
for(i=0; i<info->subm_n; i++) {
if (read(fd_sincro,&tmp,sizeof(int))==-1){
perror ("collector Unable to read");fflush(stdout);
pthread_exit(&errno);
}
fd_skt=Socket(AF_UNIX,SOCK_STREAM,0);
while (connect(fd_skt,(struct sockaddr*)&sa, sizeof(sa)) == -1 ) {
if ( errno == ENOENT ) sleep(1);
else {
perror ("client unable to connect to socket");fflush(stdout);
pthread_exit (&errno);
}
}
Write(fd_skt,&i,sizeof(int));
Close(fd_skt);
}
Close(fd_sincro);
pthread_exit((void*) 0);
}
static pthread_mutex_t fifo_mtx = PTHREAD_MUTEX_INITIALIZER;
static void* dispatcher(void* a) {
coordinate *c;
wator_t* w;
int i,j,fifo;
pthread_t tid_collector;
c_args *info=malloc (sizeof(c_args));
w=(wator_t*) a;
c=(coordinate*) malloc(sizeof(coordinate));
c->numr=2;
c->numc=2;
while ( ((w->plan->nrow / c->numr) * (w->plan->ncol / c->numc))>NWORK_DEF && (w->plan->nrow > 2*c->numr) && (w->plan->ncol > 2*c->numc) ){
if ( (w->plan->nrow / c->numr) >= (w->plan->ncol / c->numc) )
c->numr=c->numr*2;
else
c->numc=c->numc*2;
}
if ((w->plan->nrow % c->numr)==0) i=(w->plan->nrow / c->numr);
else i=(w->plan->nrow / c->numr)+1;
if ((w->plan->ncol % c->numc)==0) j=(w->plan->ncol / c->numc);
else j=(w->plan->ncol / c->numc)+1;
info->subm_n=i*j;
info->chronon=0;
info->p=w->plan;
while(1){
reset_updated(w);
(info->chronon)++;
Pt_create( &tid_collector, NULL,&collector,(void*) info);
for(i=0; i< w->plan->nrow; i+=c->numr)
for(j=0; j< w->plan->ncol; j+=c->numc){
if((fifo=open("FIFO",O_WRONLY))==-1){
perror("dispatcher unable to open FIFO");fflush(stdout);
pthread_exit(&errno);
}
c->rowi=i;
c->coli=j;
Write(fifo, c, sizeof(*c));
Close(fifo);
}
i=( (i/c->numr) * (j/c->numc) );
Pt_join( tid_collector,NULL);
}
return NULL;
}