带生产者/消费者文件复制的SegFault [英] SegFault with Producer/Consumer File Copy
问题描述
回到另一个segfault。不知道为什么,因为这readdir行是相同的,我一直在使用其他文件副本分配。以下是我制作的代码,代替作业分享此处。
这是copyDirs.cpp
pre> // cmd:./a.out [#ofConsumerThreads] [src directory] [dest directory]
#includecopyDirs.h
int main(int ac,char * av [])
{
pthread_mutex_init(& buflock,NULL);
pthread_t prodT;
if(ac == 4)
{
int consNum = atoi(av [1]);
pthread_t thread [consNum];
strcpy(f1,av [2]);
strcpy(f2,av [3]);
directory = opendir(f1);
pthread_create(& prodT,NULL,producer,NULL); // segfault发生在生产者函数
pthread_join(prodT,NULL);
for(int i = 0; i {
pthread_create(& thread [i] ,NULL,consumer,NULL);
pthread_join(thread [i],NULL);
}
closedir(directory);
}
else cout<<再试一次;)<< endl;
pthread_mutex_destroy(& buflock);
pthread_exit(NULL);
return 0;
}
和相关的头文件;
#ifndef COPYDIRS_H
#define COPYDIRS_H
#include< iostream>
#include< stdio.h>
#include< sys / stat.h>
#include< string.h>
#include< dirent.h>
#include< pthread.h>
#include< time.h>
#include< stack>
using namespace std;
struct FD
{
public:
char * i;
char * o;
FD(){}
FD(char * input,char * output)
{
i = input;
o = output;
}
};
char f1 [PATH_MAX];
char f2 [PATH_MAX];
struct dirent * curDir;
DIR * directory = NULL;
pthread_mutex_t buflock;
stack< FD>缓冲;
void * producer(void *)
{
cout<<SegTest<< endl;
//显示
while(curDir = readdir(directory))//在此行上的segfault
{
cout<<SegTest< endl;
//不显示
char * file = curDir - > d_name;
char * i = new char [256];
char * o = new char [256];
strcpy(i,f1);
strcpy(0,f2);
strcat(i,file);
strcat(o,file);
FD prodFD(i,o);
cout<<Pushing<< file<<to buffer!<< endl;
pthread_mutex_lock(& buflock);
buffer.push(prodFD);
pthread_mutex_unlock(& buflock);
i = NULL;
o = NULL;
}
pthread_exit(NULL);
}
void * consumer(void *)
{
FD consFD;
char c;
consFD = buffer.top();
buffer.pop();
// ERROR:语句无法解析重载函数的地址
cout<<复制文件:<< consfd.i<< endl;
pthread_mutex_lock(& buflock);
FILE * consIF = fopen(consFD.i,r);
FILE * consOF = fopen(consFD.o,w);
pthread_mutex_unlock (& buflock);
pthread_exit(NULL);
}
#endif
在调用之前,请尝试检查
目录
你有这行
directory = opendir(f1);
但不检查返回值是否为NULL,这可能是segfault的原因。至少这将防止segfault,如果你传递一个无效的命令行参数的目录。
back with another segfault. Not sure why, as this readdir line is the same one i've been using in other file copy assignments. Below is the code I crafted in lieu of the assignment shared here. I have commented where the segfault occurs in hopes of aiding better minds find my flaw!
This is copyDirs.cpp
//cmd: ./a.out [#ofConsumerThreads] [src directory] [dest directory]
#include "copyDirs.h"
int main(int ac,char* av[])
{
pthread_mutex_init(&buflock,NULL);
pthread_t prodT;
if(ac == 4)
{
int consNum = atoi(av[1]);
pthread_t thread[consNum];
strcpy(f1,av[2]);
strcpy(f2,av[3]);
directory = opendir(f1);
pthread_create(&prodT,NULL,producer,NULL); //segfault happens in producer function
pthread_join(prodT, NULL);
for(int i=0;i<consNum && buffer.size() > 0;i++)
{
pthread_create(&thread[i],NULL,consumer,NULL);
pthread_join(thread[i],NULL);
}
closedir(directory);
}
else cout<<"Try that again ;)"<<endl;
pthread_mutex_destroy(&buflock);
pthread_exit(NULL);
return 0;
}
and the relevant header file;
#ifndef COPYDIRS_H
#define COPYDIRS_H
#include <iostream>
#include <stdio.h>
#include <sys/stat.h>
#include <string.h>
#include <dirent.h>
#include <pthread.h>
#include <time.h>
#include <stack>
using namespace std;
struct FD
{
public:
char* i ;
char* o;
FD(){}
FD(char* input, char* output)
{
i=input;
o=output;
}
};
char f1[PATH_MAX];
char f2[PATH_MAX];
struct dirent *curDir;
DIR* directory = NULL;
pthread_mutex_t buflock;
stack <FD> buffer;
void* producer(void*)
{
cout<<"SegTest"<<endl;
//shows
while (curDir = readdir(directory)) //segfault on this line
{
cout<<"SegTest"<<endl;
//doesn't show
char* file = curDir -> d_name;
char* i = new char[256];
char* o = new char[256];
strcpy(i,f1);
strcpy(o,f2);
strcat(i,file);
strcat(o,file);
FD prodFD(i,o);
cout<<"Pushing "<<file<<" to buffer!"<<endl;
pthread_mutex_lock(&buflock);
buffer.push(prodFD);
pthread_mutex_unlock(&buflock);
i = NULL;
o = NULL;
}
pthread_exit(NULL);
}
void* consumer(void*)
{
FD consFD;
char c;
consFD = buffer.top();
buffer.pop();
//ERROR: "statement cannot resolve address of overloaded function
cout << "Copying file: "<<consFD.i<<endl;
pthread_mutex_lock(&buflock);
FILE * consIF = fopen(consFD.i,"r");
FILE * consOF = fopen(consFD.o,"w");
pthread_mutex_unlock(&buflock);
pthread_exit(NULL);
}
#endif
Try checking that the value of directory
is not NULL before calling readdir
. You have this line
directory = opendir(f1);
but are not checking if the return value is NULL which could be the cause of the segfault. At the very least this will prevent a segfault in the event that you pass in an invalid command line argument for the directories.
这篇关于带生产者/消费者文件复制的SegFault的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!