如何解决消费者生产者目录问题
im在C中实现一个程序,该程序使用使用者生产者问题,并且遇到分段错误。基本上我有1个生产者线程和N个消费者线程,生产者线程必须通过我的文件系统进行递归搜索,然后将目录添加到缓冲区中。
使用者线程从缓冲区中获取一个元素,并检查这些目录中是否存在具有给定名称的文件。我使用2个信号量和2个互斥量来解决此问题。问题在于,在完成一些工作之后,该程序要么阻塞,要么给出分段错误错误,而我找不到问题。
使用的变量:
void *recursive(char *diretorio,int indent);
//array of directories (buffer)
char *diretorios[20];
//consumption semaphore
sem_t sem_consumir;
//semaphore for producing
sem_t sem_produzir;
int prodptr=0,consptr=0;
//consumption mutex
pthread_mutex_t mutexCons;
//producing mutex
pthread_mutex_t mutexProd;
生产者代码:
void *produtor()
{
char * diretorio="/home/user";
recursive(diretorio,0);
}
void *recursive(char * diretorio,int indent)
{
DIR *dir;
struct dirent *entry;
int i;
int value;
if (!(dir = opendir(diretorio)))
pthread_exit(NULL);
while ((entry = readdir(dir)) != NULL)
{
if (entry->d_type == DT_DIR)
{
char path[PATH_MAX];
if (strcmp(entry->d_name,".") == 0 || strcmp(entry->d_name,"..") == 0)
continue;
snprintf(path,sizeof(path),"%s/%s",diretorio,entry->d_name);
printf("%*s[%s]\n",indent,"",entry->d_name);
sem_wait(&sem_produzir);
pthread_mutex_lock(&mutexProd);
diretorios[prodptr]=strdup(path);
prodptr=(prodptr+1)%10;
pthread_mutex_unlock(&mutexProd);
sem_post(&sem_consumir);
recursive(path,indent + 2);
}
}
closedir(dir);
//pthread_exit(NULL);
}
消费者代码:
//consumer code
void * consumir(MYFIND_DATA * data)
{
char fullpath[PATH_MAX + 1];
char *path;
while(1)
{
char * diretorio;
DIR *dir;
struct dirent *entry;
int value;
//existe um diretorio para consumir
sem_wait(&sem_consumir);
pthread_mutex_lock(&mutexCons);
diretorio = diretorios[consptr];
consptr = (consptr+1) % 10;
pthread_mutex_unlock(&mutexCons);
sem_post(&sem_produzir);
// }
if ((dir = opendir(diretorio)) != NULL )
{
if(Findname(diretorio,data->nome))
{
printf("FILE ENCONTRADO: %s em %s\n",entry->d_name);
data->ocorrencias++;
}
//read directory entries
while ((entry = readdir(dir)) != NULL)
{
if (strcmp(entry->d_name,".")!=0
&& strcmp(entry->d_name,"..")!=0 )
{
//look for a match
if(Findname(entry->d_name,data->nome))
{
printf("FILE FOUND: %s\n",entry->d_name);
data->ocorrencias++;
}
}
}
}
}
printf("acabou o produtor");
pthread_exit(NULL);
}
主要功能代码:
int main(int argc,char *argv[])
{
struct sigaction action;
action.sa_handler = signal_handler;
sigemptyset(&action.sa_mask);
sigaction(SIGINT,&action,NULL);
int t;
pthread_t tid[NUM_THREADS];
pthread_t ttid;
pthread_t tid_state;
MYFIND_DATA data[NUM_THREADS];
sem_init(&sem_produzir,10);
sem_init(&sem_consumir,0);
pthread_mutex_init(&mutexCons,NULL);
pthread_mutex_init(&mutexProd,NULL);
//create producer thread
pthread_create(&ttid,&produtor,NULL);
//create consumer thread
for (t = 0; t<1; t++)
{
data[t].nome = "worksheet";
data[t].ocorrencias=0;
pthread_create (&tid[t],(void *)consumir,&data[t]);
}
pthread_join(ttid,NULL);
for (t = 0; t<1; t++)
{
pthread_join (tid[t],NULL);
printf("\nThread %d joined,ocorrencias = %d",t,data[t].ocorrencias);
}
}
当我运行程序时,结果如下: program running in terminal
我已经检查过了,生产者完成了填充数组的工作,我认为问题出在生产用尽目录时,但是我不确定。任何想法可能是什么问题?
预先感谢
版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 dio@foxmail.com 举报,一经查实,本站将立刻删除。