You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

引入共享管道后XOR加密子进程writeToOutFile函数异常排查

Hey there! Let's dig into why your writeToOutFile function's while loop isn't running after adding the shared pipe—since things worked fine before, pipe handling is almost certainly the main suspect, but let's break down both possible causes clearly.

1. Pipe Passing/Handling Issues (Most Likely)

The #1 reason a read() call hangs and never enters a loop is that the pipe's write end hasn't been closed properly by all processes that hold it. Here's what might be happening:

  • When you create a pipe, every process (parent, encryptFile child, writeToOutFile child) gets a copy of both the read and write file descriptors.
  • If the parent process doesn't close its copy of the pipe's write end, or if encryptFile doesn't close the write end after finishing its writes, writeToOutFile's read() will wait forever for more data (it never sees an EOF signal). That's why your "test" string never prints—the loop condition never triggers.
  • Another possible pipe issue: you might be passing the wrong file descriptor to writeToOutFile (e.g., the write end instead of the read end). If that's the case, read() will immediately return an error (like EBADF), and if your code doesn't check for that, the loop will skip entirely.
2. Read Function Statement Problems

While less likely, your read() implementation could be causing the issue:

  • Missing return value checks: If your loop is written like while(read(pipe_fd, buf, sizeof(buf))), it will exit immediately if read() returns 0 (EOF) or -1 (error). You need to explicitly check that the return value is greater than 0 to enter the loop. For example:
    ssize_t bytes_read;
    char buf[1024];
    // Correct way: only enter loop if we actually read data
    while((bytes_read = read(pipe_fd, buf, sizeof(buf))) > 0) {
        printf("test\n");
        // Write to output file logic here
    }
    // Don't forget to handle errors!
    if(bytes_read == -1) {
        perror("read failed");
        exit(EXIT_FAILURE);
    }
    
  • Invalid buffer size: If your buffer is set to 0 or an extremely small value, read() might return 0 immediately, but this is unlikely to be the issue since you said the loop never runs at all.
Quick Troubleshooting Steps

Let's fix this step by step:

  1. Close unused pipe file descriptors everywhere:
    • In encryptFile: After writing all encrypted data, close the pipe's write end with close(pipe_write_fd);
    • In the parent process: Right after forking both children, close both ends of the pipe (you don't need them anymore!) with close(pipe_read_fd); close(pipe_write_fd);
    • In writeToOutFile: Immediately close the pipe's write end (you only need the read end) with close(pipe_write_fd);
  2. Debug the read return value: Add a print statement right after your read() call to see what it returns. For example:
    bytes_read = read(pipe_fd, buf, sizeof(buf));
    printf("Read returned: %zd\n", bytes_read);
    
    This will tell you if you're getting an error (-1), EOF (0), or no data at all (hanging).
  3. Verify file descriptor validity: Print the pipe file descriptor you're using in writeToOutFile to make sure it's the correct read end (not a duplicate or invalid number).

Here's a simplified example of proper pipe usage for your scenario:

int pipe_fd[2];
if(pipe(pipe_fd) == -1) {
    perror("pipe failed");
    exit(EXIT_FAILURE);
}

// Fork encryptFile child
pid_t encrypt_pid = fork();
if(encrypt_pid == 0) {
    // Close unused read end
    close(pipe_fd[0]);
    // Run your XOR encryption logic, write results to pipe_fd[1]
    // ...
    // Close write end when done
    close(pipe_fd[1]);
    exit(0);
}

// Fork writeToOutFile child
pid_t write_pid = fork();
if(write_pid == 0) {
    // Close unused write end
    close(pipe_fd[1]);
    ssize_t bytes_read;
    char buf[1024];
    while((bytes_read = read(pipe_fd[0], buf, sizeof(buf))) > 0) {
        printf("test\n");
        // Write buf to output file
        // ...
    }
    if(bytes_read == -1) {
        perror("read error");
    }
    close(pipe_fd[0]);
    exit(0);
}

// Parent closes both pipe ends
close(pipe_fd[0]);
close(pipe_fd[1]);
// Wait for children to finish
waitpid(encrypt_pid, NULL, 0);
waitpid(write_pid, NULL, 0);

内容的提问来源于stack exchange,提问作者eof

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.05.25 07:10:57