Warning: file_get_contents(/data/phpspider/zhask/data//catemap/6/multithreading/4.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
C 甚至在进入main()之前都会出现代码错误_C_Multithreading_Producer Consumer - Fatal编程技术网

C 甚至在进入main()之前都会出现代码错误

C 甚至在进入main()之前都会出现代码错误,c,multithreading,producer-consumer,C,Multithreading,Producer Consumer,我试图编写一个多线程程序,在生产者和消费者之间共享一个队列。我在头文件中包含了要传递给pthread_create的结构(最终包含cmd行参数)、共享队列结构和函数原型的声明 我已经试着注释了我的主函数中的所有内容,看看我是否可以让我的代码至少打印一些东西,但它不起作用。提前为这篇冗长的文章感到抱歉 下面是我的.h和.c文件 多重查找 #ifndef MULTILOOKUP_H_ #define MULTILOOKUP_H_ #include <stdio.h> #include

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

我已经试着注释了我的主函数中的所有内容,看看我是否可以让我的代码至少打印一些东西,但它不起作用。提前为这篇冗长的文章感到抱歉

下面是我的.h和.c文件

多重查找

#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


\ifndef多重查找\u H_
#定义多重查找_
#包括
#包括
#包括
#包括
#包括“/home/user/Desktop/PA3/util.c”
#定义最大请求线程数10
#定义最大解析程序线程数5
#定义最大输入文件10
#定义最大尺寸20
结构参数{
结构队列*q;
字符*输入;
char*resLog;
char*reqLog;
字符行[100];
字符结果[100];
字符ipv6[100];
};
结构节点{
字符名[100];
结构节点*下一步;
};
结构队列{
整数大小;
结构节点*头部;
结构节点*尾部;
};
void init(结构队列*);
void pop(结构队列*);
无效推送(结构队列*,字符*);
void*请求线程(void*);
//void*解析读取(队列*,字符*,字符*,字符*);
#恩迪夫
多重查找.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;
}



#包括“multilookup.h”
/*----------------------结构函数------------------------*/
void init(结构队列*q){
q->head=NULL;
q->tail=NULL;
q->size=0;
}
/*pop:从队列中删除并返回名字*/
void pop(结构队列*q)
{
q->尺寸--;
//printf(“%s\n”,q->head->name);
结构节点*tmp=q->head;
q->head=q->head->next;
免费(tmp);
//pthread_exit(NULL);
}
/*push:将名称添加到队列的末尾*/
void push(结构队列*q,字符*name)
{
q->size++;
如果(q->head==NULL){
q->head=(结构节点*)malloc(sizeof(结构节点));
//q->head->name=name;
strcpy(q->head->name,name);
q->head->next==NULL;
q->tail=q->head;
}否则{
q->tail->next=(结构节点*)malloc(sizeof(结构节点));
//q->tail->next->name=name;
strcpy(q->tail->next->name,name);
q->tail->next->next=NULL;
q->tail=q->tail->next;
}   
//pthread_exit(NULL);
}
/*--------------------------结构函数的结尾------------------*/
void*requesterThread(void*receivedStruct){
结构参数*args_ptr;
args_ptr=(结构参数*)receivedStruct;
文件*fptr;
文件*sptr;
int*fileCounter=0;
printf(“%s\n”,参数\u ptr->input);
//检查文件路径是否正确
if((fptr=fopen(args_ptr->input,“r”))==NULL){
fprintf(stderr,“错误!伪输入文件路径。\n”);
//如果文件指针返回NULL,线程将退出。
出口(1);
}
如果((sptr=fopen(args_ptr->reqLog,“w”))==NULL){
fprintf(stderr,“Error!Bogues输出路径。\n”);
出口(1);
}
//从输入文件读取并推送到共享队列
printf(“添加到队列…\n”);
而(fscanf(fptr,“%[^\n]%*c”,args_ptr->line)!=EOF){
while(args_ptr->q->size==MAXSIZE){
printf(“队列已满!\n”);
//阻止所有试图写入共享队列的线程
返回0;//以后删除并替换为break;
}
推送(参数ptr->q,参数ptr->行);
//更新requesterLog并打印记录的主机名
fprintf(sptr,“%s\n”,参数值->行);
printf(“记录的主机名:%s\n”,args\u ptr->line);
/*将“线程服务的##文件”写入serviced.txt的行
fprintf(sptr,“线程%d已服务%d个文件”,pthread_self(),fileCounter);
*/
}fileCounter++;//当fileCounter==numInputFiles在队列中发送毒药丸时,让解析器知道请求线程已完成。
fclose(fptr);
pthread_exit(NULL);
返回0;
}
int main(/*int argc,char*argv[]*/){
printf(“1”);
int num_req_线程;
int num_res_线程;
int rc1;
int rc2;
//共享队列结构的实例
结构队列*q;
init(q);
printf(“2”);
//参数结构的实例
结构参数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];
返回0;
}
当你写:

struct queue *q;
init(q);
没有创建
queue
类型的结构,只有一个不指向任何内容的指针。你应该试试:

struct queue q;
init(&q);

因此,在您尝试分配给其成员之前,将创建结构。

继续努力生成最小但可重复的示例。我看到一个include(util.c),没有显示任何代码。您的
printf
调用是缓冲的,因此可能不会在您期望的时候写入。请学习如何使用调试器捕获崩溃。检查传递给
init的内容以及函数如何使用它。也就是说,考虑<代码> Q>代码的值是什么。调试器现在将是一个很棒的工具,它将为您节省大量时间。在所有printf调用的末尾添加一个换行符。如果输出到一个终端,这将刷新缓冲区。不过有一个提示:未初始化的局部变量实际上是未初始化的。现在想想
main
函数中的指针
q
指向哪里……谢谢!这很有道理。@Bach如果这个答案对你有帮助,请接受它!