C 中的并发进程

发布于 2024-11-03 07:26:33 字数 974 浏览 0 评论 0原文

如何让 3 个进程并行运行?下面这个解决方案正确吗? 在我的解决方案中,我放置了一些代码来查看经过的时间,我认为这是在顺序模式下进行调整。我需要让pid1、pid2和pid3同时运行。

pid = fork();
if(pid == 0) {
        //code...
    exit(EXIT_SUCCESS);
} else if(pid > 0) {
        pid1 = fork();
        if(pid1 == 0) {
                //pid1 code...
            exit(EXIT_SUCCESS);
    } else if(pid1 > 0) {
            waitpid(pid1, &status, 0);
    } else {
        printf("Fork error %d.\n", errno);
    }

    pid2 = fork();
    if(pid2 == 0) {
                //pid2 code...
        exit(EXIT_SUCCESS);
    } else if(pid2 > 0) {
        waitpid(pid2, &status, 0);
    } else {
        printf("Fork error %d.\n", errno);
    }

    pid3 = fork();
    if(pid3 == 0) {
                //pid3 code...
                exit(EXIT_SUCCESS);
    } else if(pid3 > 0) {
        waitpid(pid3, &status, 0);
    } else {
            printf("Fork error %d.\n", errno);
    }
        waitpid(pid, &status, 0);
}

How can I have 3 processes running in parallel? Is this solution below correct?
In my solution I put some code to see the time elapsed and I think this is tunning in sequential mode. I need to put pid1, pid2 and pid3 running at the same time.

pid = fork();
if(pid == 0) {
        //code...
    exit(EXIT_SUCCESS);
} else if(pid > 0) {
        pid1 = fork();
        if(pid1 == 0) {
                //pid1 code...
            exit(EXIT_SUCCESS);
    } else if(pid1 > 0) {
            waitpid(pid1, &status, 0);
    } else {
        printf("Fork error %d.\n", errno);
    }

    pid2 = fork();
    if(pid2 == 0) {
                //pid2 code...
        exit(EXIT_SUCCESS);
    } else if(pid2 > 0) {
        waitpid(pid2, &status, 0);
    } else {
        printf("Fork error %d.\n", errno);
    }

    pid3 = fork();
    if(pid3 == 0) {
                //pid3 code...
                exit(EXIT_SUCCESS);
    } else if(pid3 > 0) {
        waitpid(pid3, &status, 0);
    } else {
            printf("Fork error %d.\n", errno);
    }
        waitpid(pid, &status, 0);
}

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

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

发布评论

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

评论(3

柠檬色的秋千 2024-11-10 07:26:33

您一直在等待一个孩子完成,然后再启动下一个。尝试类似:

for (int i = 0; i < 3; ++i)
{
    pid = fork();

    if (pid < 0)
        error

    if (pid == 0)
    {
        child does thing
        exit
    }
}

for (int i = 0; i < 3; ++i)
{
    wait(&status);
}

编辑

所以只需将您的代码更改为类似这样的内容并等待最后。

    if (pid2 == 0)
    {
        //pid2 code...
        exit(EXIT_SUCCESS);
    }
    else
        if (pid2 < 0)
        {
            printf("Fork error %d.\n", errno);
        }

   //....same outline for 3, etc.

You keep waiting for one child to complete before you launch the next. Try something like:

for (int i = 0; i < 3; ++i)
{
    pid = fork();

    if (pid < 0)
        error

    if (pid == 0)
    {
        child does thing
        exit
    }
}

for (int i = 0; i < 3; ++i)
{
    wait(&status);
}

EDIT

So just change your code to something like this and wait at the end.

    if (pid2 == 0)
    {
        //pid2 code...
        exit(EXIT_SUCCESS);
    }
    else
        if (pid2 < 0)
        {
            printf("Fork error %d.\n", errno);
        }

   //....same outline for 3, etc.
哆兒滾 2024-11-10 07:26:33

查看我的 wrap 程序,特别是 的代码包装。它会分叉两次,将一个子进程执行到 wrap 中,读取、写入 wrap,然后读回另一个子进程。

Check out my wrap program, specifically the code for wrapc. It forks itself twice, execs one child into wrap, reads, writes to wrap, then reads back in the other child.

摇划花蜜的午后 2024-11-10 07:26:33

你的问题有点模糊——你是否看到3个进程并行运行?
根据您的代码编写方式,您将立即退出子进程。

Your question is somewhat vague -- are you seeing 3 processes running in parallel or not?
the way your code is written you are exiting the child processes straight away.

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