为什么我的代码(它在C编程中实现了流水线)不能按预期输出一个连接的字符串?

dfddblmv  于 2023-03-29  发布在  其他
关注(0)|答案(1)|浏览(57)

我在一个网站上找到了一段示例代码,它演示了C编程中的流水线是如何工作的,特别是管道的使用()和fork().提供的程序是把一个输入字符串和一个放进程序的固定字符串串联起来的,代码看起来没有什么错误,然而,每次运行它的时候,根本就没有输出字符串,现在删除了主要的库,只是为了使代码更清晰。

//Two pipes used
//One pipe goes from parent process (process 1) to the child process (process 2)

//Remember, '0' in the array means 'read end'
//'1' in the array means write end

int p12[2];
int p21[2];

//Now declare the strings
char fixed_str[] = "at tutorialspoint";
char input_str[100];

//Making the first parent process 
pid_t P; 

//Making sure the pipes are properly made and declared
if (pipe(p12)==-1 || pipe(p21)==-1)
{
    //fprintf simply sends formatted output to a stream
    //'stderr' is the standard error message
    //essentially, print below if there is an error 
    
    fprintf(stderr, "fork Failed");
    return 1;
}

//Now to take in the input string from user
scanf("%99s", input_str);

//Now to make the second/child process 
P = fork();

//If <0, the process creation failed
if ( P < 0 )
{
    fprintf(stderr, "fork Failed");
    return 1;
}
//If P > 0, which will be the process ID of the child 
//process to the parent process
//Essentially, this is the parent process which will
//be executed
else if ( P > 0 )
{
    //Declare the concatenated string
    char concat_str[100];
    
    //Close the 'read end' for pipe going from pipe 1 to pipe 2
    //It is not needed write now.
    close(p12[0]);
    
    //Write to the 'write end' for pipe going from pipe 1 to pipe 2 
    write(p12[1], input_str, strlen(input_str)+1);
    
    //Now close the write end 
    close(p12[1]);
    
    //Now, focus on the pipe from process 2 (child) back to process 1 (parent)
    //First, have the program wait so everything else can happen 
    wait(NULL);
    
    //Next, close the write end of the pipe from process 2 to 1 
    //It is not needed 
    close(p21[0]);
    
    //Now, read the concatenated string from the child process (2)
    //The child process would have sent it through the pipe from process 2 to process 1 
    //Therefore, get it from there 
    read(p21[0], concat_str, 100);
    printf("Concatenated string: %s", concat_str);
    
    //Then close the read end of this pipe. It is not needed anymore
    close(p21[0]);
    
    //Exit the parent process 
    exit(0);
}

//Now, process 2, the child process. 
//Remember this is shown by = 0
else
{
    //Close the write end of the pipe from process 1 to 2. You dont need it 
    //Redeclare the concatenated string 
    char concat_str[100] = ".";
    
    //Now, to take the string sent from process 1 (parent),
    //which is currently sitting in pipe 1 to 2. 
    //The process will read it and call it 'concat_str'
    read(p12[0], concat_str, 100);
    
    //This is where the addition of the new string begins
    //See how many characters are in the inputted string 
    int k = strlen(concat_str);
    
    //Declare an integer for the new fixed string 
    //Reminder: char fixed_str[] = " at tutorialspoint";
    int i; 
    
    //Now to do it
    for (i = 0; i < strlen(fixed_str); i++)
    {
        concat_str[k++] = fixed_str[i];
        concat_str[k] = '\0';
    }
    //concat_str[k] = '\0';
    
    //Close the read ends of the pipes. They are not needed 
    close(p12[0]);
    close(p21[0]);
    
    //Now finally, write this new string back to process 1 (parent process)
    write(p21[1], concat_str, strlen(concat_str)+1);
    
    //Now, close this pipe and exit 
    close(p21[1]);
    exit(0);
}
return 0;
wz3gfoph

wz3gfoph1#

线路关闭(p21[0]);正在关闭从进程2到进程1的管道的读取端。当父进程稍后尝试从管道读取时,这将导致管道损坏错误。您希望改为关闭写入端,因此:

//Now, focus on the pipe from process 2 (child) back to process 1 (parent)
//First, have the program wait so everything else can happen 
wait(NULL);
    
//Next, close the write end of the pipe from process 2 to 1 
//It is not needed 
close(p21[0]);

应改为:

//Now, focus on the pipe from process 2 (child) back to process 1 (parent)
//First, have the program wait so everything else can happen 
wait(NULL);
    
//Next, close the write end of the pipe from process 2 to 1 
//It is not needed 
close(p21[1]);

相关问题