问题描述
我正在尝试编写一个在生产者和消费者之间共享队列的多线程程序。我在头文件中包含了要传递给pthread_create(最终包含cmd行参数)的结构的声明,我的共享队列结构和函数原型。
我已经尝试注释掉了我主要功能中的所有内容,以查看是否可以使我的代码至少打印出一些内容,但是那行不通。抱歉,冗长的帖子。
下面,我包含了.h和.c文件。
multilookup.h
#ifndef MULTILOOKUP_H_
#define MULTILOOKUP_H_
#include <stdio.h>
#include <stdlib.h>
#include <pthread.h>
#include <unistd.h>
#include "/home/user/Desktop/PA3/util.c"
#define MAX_REQUESTER_THREADS 10
#define MAX_RESOLVER_THREADS 5
#define MAX_INPUT_FILES 10
#define MAXSIZE 20
struct arguments {
struct queue *q;
char *input;
char *resLog;
char *reqLog;
char line[100];
char result[100];
char ipv6[100];
};
struct node {
char name[100];
struct node *next;
};
struct queue {
int size;
struct node *head;
struct node *tail;
};
void init(struct queue *);
void pop(struct queue *);
void push(struct queue *,char *);
void* requesterThread(void *);
//void* resolverThread(queue *,char *,char*);
#endif
multi-lookup.c
#include "multilookup.h"
/*----------------------Struct Functions------------------------*/
void init(struct queue *q) {
q->head = NULL;
q->tail = NULL;
q->size = 0;
}
/* pop: remove and return first name from a queue */
void pop(struct queue *q)
{
q->size--;
//printf("%s\n",q->head->name);
struct node *tmp = q->head;
q->head = q->head->next;
free(tmp);
//pthread_exit(NULL);
}
/* push: add name to the end of the queue */
void push(struct queue *q,char *name)
{
q->size++;
if (q->head == NULL) {
q->head = (struct node *)malloc(sizeof(struct node));
//q->head->name = name;
strcpy(q->head->name,name);
q->head->next == NULL;
q->tail = q->head;
} else {
q->tail->next = (struct node *)malloc(sizeof(struct node));
//q->tail->next->name = name;
strcpy(q->tail->next->name,name);
q->tail->next->next = NULL;
q->tail = q->tail->next;
}
//pthread_exit(NULL);
}
/*--------------------------End of struct functions------------------*/
void* requesterThread(void *receivedStruct) {
struct arguments *args_ptr;
args_ptr = (struct arguments*) receivedStruct;
FILE *fptr;
FILE *sptr;
int *fileCounter = 0;
printf("%s\n",args_ptr->input);
//Check for proper file paths
if ((fptr = fopen(args_ptr->input,"r")) == NULL) {
fprintf(stderr,"Error! Bogus input file path.\n");
// Thread exits if file pointer returns NULL.
exit(1);
}
if ((sptr = fopen(args_ptr->reqLog,"w")) == NULL) {
fprintf(stderr,"Error! Bogues output path.\n");
exit(1);
}
//Read from input file and push to shared queue
printf("Adding to Queue...\n");
while (fscanf(fptr,"%[^\n]%*c",args_ptr->line) != EOF) {
while (args_ptr->q->size == MAXSIZE) {
printf("Queue is full!\n");
//block all threads trying to write to shared queue
return 0; //remove later and replace with break;
}
push(args_ptr->q,args_ptr->line);
//Update requesterLog and print logged hostnames
fprintf(sptr,"%s,\n",args_ptr->line);
printf("Hostname Logged: %s\n",args_ptr->line);
/*LINE TO WRITE "Thread <id> serviced ## files" TO serviced.txt
fprintf(sptr,"Thread %d serviced %d files",pthread_self(),fileCounter);
*/
} fileCounter++; //when fileCounter == numInputFiles send poison pill in queue to let resolver kNow that the requesterThreads have finished.
fclose(fptr);
pthread_exit(NULL);
return 0;
}
int main(/*int argc,char *argv[]*/) {
printf("1");
int num_req_threads;
int num_res_threads;
int rc1;
int rc2;
//instance of shared queue struct
struct queue *q;
init(q);
printf("2");
//instance of arguments struct
struct arguments args;
args.q = q;
args.input = "/home/user/Desktop/PA3/input/names1.txt";//argv[5];
args.reqLog = "/home/user/Desktop/PA3/serviced.txt";//argv[3];
args.resLog = "/home/user/Desktop/PA3/results.txt"; //argv[4];
return 0;
}
解决方法
撰写时:
struct queue *q;
init(q);
没有创建类型为queue
的结构,只有一个没有指向任何内容的指针。您应该尝试:
struct queue q;
init(&q);
,以便在尝试分配给其成员之前创建该结构。