使用 Pipes 的三个进程未运行第三个进程

Three processes using Pipes is not running third process

提问人:Samantha Garcia 提问时间:11/10/2023 最后编辑:Samantha Garcia 更新时间:11/10/2023 访问量:39

问:

我正在为计算机体系结构课做一个项目,我无法弄清楚为什么我的 C 代码中的第三个进程没有运行。 助教给了我一个骨架代码,我昨天参加了一个额外的会议,以弄清楚为什么我无法让它工作。看来我不是唯一一个。还有同学说是因为骨架代码错了,助教好像不太确定,说觉得对就提交。

我可以让我的代码将正确的结果打印到屏幕上,但它不会保存到文件中,并且一直卡在第三个过程中。我们实际上学习的概念是管道和延迟,所以重点不一定是 C,而是管道。我想我需要添加睡眠功能,因为同时有多个进程在进行,但无论我做什么,它都会卡住。

我的主要问题是: 骨架代码真的错了,还是我只是做错了剩下的作业? 我是否有可能修复骨架代码,以便我能够真正理解这个概念?(我不能以这种方式转动它,但至少我会知道发生了什么)

我将在下面区分哪些部分是骨架代码,并记下我尝试过的一些事情。

骨架代码:

#include <stdio.h>
#include <stdlib.h>
#include <unistd.h>
#include <string.h>
#include <ctype.h>
#define READ_END 0
#define WRITE_END 1
int main(int argc, char* argv[]) {
// declare pipes
    int fd1[2], fd2[2];
    pid_t pid;
    if (pipe(fd1) == -1 || pipe(fd2) == -1) 
    {
        fprintf(stderr, "Pipe failed");
        return 1;
    }
    pid = fork();
    if (pid < 0) 
    {
        fprintf(stderr, "Fork Failed");
        return 1;
    }
    if (pid > 0) 
    { // Parent process
        close(fd1[READ_END]);
        close(fd2[WRITE_END]);
        FILE* fp = fopen("input.txt", "r");

        //
        //FILE* fp2 = fopen("output.txt", "w");

        //

        if (fp == NULL) {
        fprintf(stderr, "Failed to open file\n");
        exit(1);
        }
        char line[BUFSIZ];
        while (fgets(line, BUFSIZ, fp)) 
        {
            write(fd1[WRITE_END], line, strlen(line));
            memset(line, 0, BUFSIZ);
            read(fd2[READ_END], line, BUFSIZ);
            printf("%s", line);
            //fprintf(fp2, "%s", line); this isn't allowed
        }
        fclose(fp);
        //fclose(fp2);
        close(fd1[WRITE_END]);
        close(fd2[READ_END]);
    }
    else 
    { // Child process
        pid = fork();
        if (pid > 0) 
        { // Second process
            close(fd1[WRITE_END]);
            close(fd2[READ_END]);
            char line[BUFSIZ];
            while (read(fd1[READ_END], line, BUFSIZ)) 


//see note 1*

            {
                for (int i = 0; i < strlen(line); i++) {
                    // Change lower to upper and upper to lower cases
                    // write code here
                        if(isupper(line[i]))
                        {
                            line[i]=tolower(line[i]);
                        } else if(islower(line[i])) 
                        {
                            line[i]=toupper(line[i]);
                        }
                    }

//this is skeleton code

                write(fd2[WRITE_END], line, strlen(line));
                memset(line, 0, BUFSIZ);
            }
            close(fd1[READ_END]);
            close(fd2[WRITE_END]);
        }
        else 
        { // Third process


//see 2*

            // Close unused pipes
            //write code here
            close(fd1[READ_END]);
            close(fd2[WRITE_END]);
            close(fd1[WRITE_END]);

//The rest of the code is skeleton code, but I needed to change the code inside the while loop to write to output.txt. 

            FILE* fp = fopen("output.txt", "w");
            char line[BUFSIZ];


            //the code below is commented out because it keeps getting stuck in this loop
        
            /*
            while (read(fd2[READ_END], line, BUFSIZ)) 
            {
                // prints to the output.txt file
                // write code here
                fprintf(fp,"%s", line);

            }
            */ 

            fclose(fp);
            close(fd2[READ_END]);
        }
    }
    return 0;
}

任何人对这里发生的事情有任何建议将不胜感激。

1* 下一部分是我必须编写流程的地方。这将正常运行并打印到屏幕上。我尝试编辑它以通过在循环开始时添加 sleep(2) 和在循环结束时添加 sleep(3) 并在第三个进程中执行相反操作来运行第三个进程(因此它们总共 5 秒,每次迭代直到前一个完成之后才会开始) - 我只在第三个进程的开头尝试了 sleep(10),所以第二个进程将始终运行第一个,并在第三个过程开始之前完成。到目前为止,睡眠不起作用。我也在各个地方尝试了wait(NULL);这也没有用。- 我尝试过很多东西,老实说,我都记不住了。

2* 下一部分我必须自己写,并且是它一直卡住的地方。如果我注释掉循环,代码将完全运行。如果我不这样做,代码就会卡在循环中,但它不会运行其中的任何代码。我尝试在循环之前和循环中编写打印语句。如果它在循环之前,它将运行一次,并且输出很奇怪。有时,它会在代码运行完成几秒钟后运行打印行。 我尝试将 read(fd2[READ_END], line, BUFSIZ)) 的值打印到屏幕上 - 有时它给出 0,有时它给出一个数字 - 不知道为什么,但认为它与我运行代码的时间有关。我还尝试将 BUFSIZ 打印到屏幕上 - 结果对我来说真的没有意义。

C 工艺 同步

评论


答: 暂无答案