甚至进入 main() 之前的代码段错误

Code segfaults before even entering main()

我正在尝试编写一个在生产者和消费者之间共享队列的多线程程序。我在我的头文件中包含了我打算传递给 pthread_create 的结构的声明(最终包含 cmd 行参数)、我的共享队列结构和我的函数原型。

我已经尝试基本上注释掉我的主要功能中的所有内容,以查看我是否可以让我的代码至少打印一些东西,但是它没有用。提前抱歉冗长 post.

下面,我包含了我的 .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 *, char*);

#endif


多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);

以便在您尝试分配给其成员之前创建该结构。