c - 在 C 中读取结构体并将其写入管道

标签 c struct fork pipe

我正在尝试编写一个由字符数组、整数值和指向管道的指针组成的结构。该结构表示单链表中的一个节点。

//Define a linked-list node object
typedef struct node{
    char word[128];
    int frequency;
    struct node *next;
} NODE;

该程序的目标是使用管道将节点从多个并发子进程传递到父进程。我实现的管道结构似乎可以很好地处理常规字符数组,但如果我尝试传递整个节点对象,则无法正常工作。

    //for each file argument, create a child process
        for (i = 1; i < argc; i ++)
        {
            pipe(p[i-1]);
            pid = fork();

            if (pid == 0)
            {
                //child process
                close(p[i-1][0]);
                NODE *tmp;
                NODE *out = freqCheck(argv[i], tmp);
                write(p[i-1][1], out, sizeof(NODE));
                exit(0);
            }
        }
if (pid > 0){
                //parent process
                int j;
                for (j = 0; j < argc-1; j++)
                {
                    close(p[j][1]);
                    NODE *tmp;
                    read(p[j][0], tmp, sizeof(NODE));

                    printf("%s\n", tmp->word);
                }


            }

当父进程尝试从管道读取并解释结构的属性之一时,我只会返回空值。

我使用一个包含 2 个元素的整数数组的数组来跟踪每个子进程的管道。上面的 printf 语句返回 null 恢复时间。

知道我做错了什么吗?

FrqCheck 方法的代码:

//Method for determining the most occuring word
NODE * freqCheck(char *file, NODE *max)
{
    int i; 
    FILE *f;
    char str[128];

    //Set up head and tail nodes
    NODE *head = NULL;
    NODE *tail = NULL;

    if ((f = fopen(file, "r")) == NULL)
        {
            //sprintf(output, "%s could not be opened.", file);
        }else
        {
            //scan each word of the input file
            while(fscanf(f, "%s ", str) != EOF)
            {
                //if the linked-list has no nodes, create one
                if (head == NULL)
                {
                    NODE *n;
                    n = (NODE *)malloc(sizeof(NODE));
                    strcpy(n->word, str);
                    n->frequency = 1;
                    n->next = NULL;
                    head = n;
                    tail = n;

                }else{  //search the linked list for the found word.

                    NODE *current = head;
                    int found = 0;

                    while((current != NULL) && (found == 0))
                    {
                        //if the word is found increment the frequency
                        if (strcmp(current->word, str) == 0)
                        {
                            current->frequency ++;
                            found = 1;
                        }else
                        {
                            current = current->next;
                        }
                    }

                    //if the word is not found, create a node and add to the liked-list
                    if (found == 0)
                    {
                        NODE *new;
                        new = (NODE *)malloc(sizeof(NODE));
                        strcpy(new->word, str);
                        new->frequency = 1;
                        new->next = NULL;
                        tail->next = new;
                        tail = new;
                    }
                }
            }
            //traverse the linked-list and find the word with the maximum frequency
            NODE *tmp = head;
            max = tmp;

            while (tmp != NULL)
            {
                if (tmp->frequency > max->frequency)
                {
                    max = tmp;
                }else
                {
                    tmp = tmp->next;
                }
            }
            //sprintf(output, "%s %s %d", file, max->word, max->frequency);
        }
    //printf("%s\n", max->word);
    return max;
}

最佳答案

您在哪里为 NODE 结构分配存储空间? freqCheck 是否分配存储空间?在父进程中,当您调用read()然后调用printf时,您传入了一个未初始化的NODE指针,所以您当然获得未定义的行为。

关于c - 在 C 中读取结构体并将其写入管道,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/4823154/

相关文章:

c++ - fork 并执行许多不同的进程,并从每个进程中获取结果

C 中的检查循环 - scan() 未在第二次运行时运行

c - getline 中 memcpy 出现奇怪的段错误

c - 在C中初始化结构内部的数组

无法确定段错误的原因

perl - 从 BEGIN block 中调用 Perl fork

c - 规则 14.2 for 循环应符合 MISRA C 2012 的格式

c - 让 printf() 删除值的尾随 ".0"

无法从 C 中的结构打印字符串

fork 和(失败的)exec 后 C 文件指针发生变化