fork时产生的错误

发布于 2024-12-09 02:00:13 字数 1337 浏览 0 评论 0原文

我有以下代码,它分叉两个新进程来获取其中一个进程的标准输出内容并将其保存到文件中。它运行得很好并保存了文件,但是在返回调用函数中的以下行(无论它是什么)后,会抛出 EXC_BAD_ACCESS 错误。为什么?

void test(vector<string> inp,int i){
    int fds[2]; // file descriptors
    long count;  // used for reading from stdout
    int fd;     // single file descriptor
    char c;     // used for writing and reading a character at a time
    pid_t pid;  // will hold process ID; used with fork()

    pipe(fds);

    // child process #1.
    fd = open((inp[i+1]).c_str(), O_RDWR | O_CREAT, 0666);
    if (fork() == 0) {
        if (fd < 0) {
            return;
        }

        dup2(fds[0], 0);

        // Don't need stdout end of pipe.
        close(fds[1]);

        // Read from stdout...
        while ((count = read(0, &c, 1)) > 0)
            write(fd, &c, 1); // Write to file.

        exit(0);
        // child process #2
    } else if ((pid = fork()) == 0) {
        dup2(fds[1], 1);

        // Don't need stdin end of pipe.
        close(fds[0]);

        // Output contents of the given file to stdout.
        char **arguments = getArguments(inp[i]);
        execvp(arguments[0], arguments);
        perror("execvp failed");
        exit(0);
        // parent process
    } else {
        waitpid(pid, NULL, 0);
        close(fds[0]);
        close(fds[1]);
    }
}

I have the following code which forks two new processes to take the contents of the stdout of one and saves it to a file. It runs just fine and saves the file, but after it returns the following line in the calling function (no matter what it is) throws a EXC_BAD_ACCESS error. Why?

void test(vector<string> inp,int i){
    int fds[2]; // file descriptors
    long count;  // used for reading from stdout
    int fd;     // single file descriptor
    char c;     // used for writing and reading a character at a time
    pid_t pid;  // will hold process ID; used with fork()

    pipe(fds);

    // child process #1.
    fd = open((inp[i+1]).c_str(), O_RDWR | O_CREAT, 0666);
    if (fork() == 0) {
        if (fd < 0) {
            return;
        }

        dup2(fds[0], 0);

        // Don't need stdout end of pipe.
        close(fds[1]);

        // Read from stdout...
        while ((count = read(0, &c, 1)) > 0)
            write(fd, &c, 1); // Write to file.

        exit(0);
        // child process #2
    } else if ((pid = fork()) == 0) {
        dup2(fds[1], 1);

        // Don't need stdin end of pipe.
        close(fds[0]);

        // Output contents of the given file to stdout.
        char **arguments = getArguments(inp[i]);
        execvp(arguments[0], arguments);
        perror("execvp failed");
        exit(0);
        // parent process
    } else {
        waitpid(pid, NULL, 0);
        close(fds[0]);
        close(fds[1]);
    }
}

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

走过海棠暮 2024-12-16 02:00:13

这对我来说很有效:

#include <unistd.h>
#include <sys/types.h>
#include <sys/stat.h>
#include <fcntl.h>
#include <stdio.h>
#include <sys/wait.h>

void test(/*vector<string> inp,int i*/){
    int fds[2]; // file descriptors
    long count;  // used for reading from stdout
    int fd;     // single file descriptor
    char c;     // used for writing and reading a character at a time
    pid_t pid;  // will hold process ID; used with fork()

    pipe(fds);

    // child process #1.
    fd = open(/*(inp[i+1]).c_str()*/"/tmp/output", O_RDWR | O_CREAT, 0666);
    if (fork() == 0) {
        if (fd < 0) {
            return;
        }

        dup2(fds[0], 0);

        // Don't need stdout end of pipe.
        close(fds[1]);

        // Read from stdout...
        while ((count = read(0, &c, 1)) > 0)
            write(fd, &c, 1); // Write to file.

        _exit(0);
        // child process #2
    } else if ((pid = fork()) == 0) {
        dup2(fds[1], 1);

        // Don't need stdin end of pipe.
        close(fds[0]);

        // Output contents of the given file to stdout.
        char **arguments = new char*[2];/*getArguments(inp[i]);*/
        arguments[0]=(char*)"/bin/bash";
        arguments[1]=0;
        execvp(arguments[0], arguments);
        perror("execvp failed");
        _exit(0);
        // parent process
    } else {
        waitpid(pid, NULL, 0);
        close(fds[0]);
        close(fds[1]);
    }
}

int main(int argc, char* argv[]){
    test();
}

尝试告诉你的错误到底出现在哪里,或者显示你的程序的更多内容,这样我就可以尝试复制你的条件。

This works well for me:

#include <unistd.h>
#include <sys/types.h>
#include <sys/stat.h>
#include <fcntl.h>
#include <stdio.h>
#include <sys/wait.h>

void test(/*vector<string> inp,int i*/){
    int fds[2]; // file descriptors
    long count;  // used for reading from stdout
    int fd;     // single file descriptor
    char c;     // used for writing and reading a character at a time
    pid_t pid;  // will hold process ID; used with fork()

    pipe(fds);

    // child process #1.
    fd = open(/*(inp[i+1]).c_str()*/"/tmp/output", O_RDWR | O_CREAT, 0666);
    if (fork() == 0) {
        if (fd < 0) {
            return;
        }

        dup2(fds[0], 0);

        // Don't need stdout end of pipe.
        close(fds[1]);

        // Read from stdout...
        while ((count = read(0, &c, 1)) > 0)
            write(fd, &c, 1); // Write to file.

        _exit(0);
        // child process #2
    } else if ((pid = fork()) == 0) {
        dup2(fds[1], 1);

        // Don't need stdin end of pipe.
        close(fds[0]);

        // Output contents of the given file to stdout.
        char **arguments = new char*[2];/*getArguments(inp[i]);*/
        arguments[0]=(char*)"/bin/bash";
        arguments[1]=0;
        execvp(arguments[0], arguments);
        perror("execvp failed");
        _exit(0);
        // parent process
    } else {
        waitpid(pid, NULL, 0);
        close(fds[0]);
        close(fds[1]);
    }
}

int main(int argc, char* argv[]){
    test();
}

Try to tell where exactly your error appears or show more of your program, so i can try to replicate your conditions.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文