在Linux中创建独立的进程 [英] Create independent process in Linux

查看:304
本文介绍了在Linux中创建独立的进程的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在寻求实现与 CreateProcess ,但在Linux上.我进行了大量研究,发现使用双叉的"Fork off and die" 方法在init下运行孩子.也就是说,允许孩子独立于父母进行操作.

I'm looking to implement a function similar to CreateProcess but on Linux. I did a lot of research and found the "Fork off and die" approach which uses a double fork to run the child under init. That is, allow the child to operate independent of the parent.

因为父级需要返回有关新创建的子进程的信息(即pid,名称等),所以我需要知道代码中是否遇到了竞争情况.目前,我分叉并通过管道检索第二个分叉的pid,然后等待第一个分叉退出.

Because the parent needs to return information about the newly created child process (i.e. pid, name, etc.) I need to know if I'm running into a race condition in my code. Currently, I fork and retrieve the second fork's pid via pipes then wait for the first fork to exit.

int child = 0;
int fd[2] = { 0, 0 };

if (pipe (fd) != 0)
    return Process();

int pid = fork();
if (pid < 0)
    return Process();

if (pid == 0) // Child
{
    if (setsid() != -1)
    {
        child = fork();
        if (child == 0)
        {
            umask (0); // Reset umask
            close (0); // Close stdin
            close (1); // Close stdout
            close (2); // Close stderr

            execvp ( ... );
            _exit (0);
        }

        // Do I need waitpid (child) here?
    }

    // Write the child PID using the pipe
    write (fd[1], &child, sizeof (child));
    _exit (0);
}

else // Parent
{
    // Read the child PID using the pipe
    read (fd[0], &child, sizeof (child));

    // Remove zombie process
    waitpid (pid, nullptr, 0);

    // Child must finish exec by this point

    return Process (child);
        // Also gets name
}

问题:

  • 我需要第二个waitpid来等待孩子完成执行吗?
  • waitpid是否在调用exec时返回?
  • 即使在waitpid之前调用了exit或exec,waitpid也会返回吗?

推荐答案

(一点点澄清:您所说的孩子,实际上是您的孙子.这是该过程的孩子,刚刚被分叉而死.)

(A little clarification: what you're calling child, is actually your grandchild. It's the child of the process that just forked off and died.)

我需要第二个waitpid来等待孩子完成执行吗?

Do I need a second waitpid to wait for the child to finish the exec?

不能.这是您的孙子,您只能等待直子.另外,由于您的孙子的父母已经去世,因此您的孙子现在已被重新初始化为init(因此实际上是您的前孙子).

You can't. It's your grandchild and you can only wait on your direct children. Additionally, because your grandchild's parent has died, your grandchild has now been reparented to init (so it's actually your ex-grandchild).

waitpid会在调用exec时返回吗?

Does waitpid return upon a call to exec?

Waitpid在给定的pid死亡/退出时返回,或者如果已经是僵尸则立即返回. exec在孙子中被调用.您的waitpid调用根本不关心不是您的直接子进程的进程(除非您使用的是仅Linux的子子进程功能).

Waitpid returns when the given pid dies/exits or immediately if it's already a zombie. The exec is called in the grandchild. Your waitpid calls are not at all not concerned with processes that aren't your direct children (unless you're using the Linux-only child-subreaper feature).

即使在waitpid之前调用了exit或exec,waitpid也会返回吗?

Does waitpid return even if exit or exec were called before waitpid?

Waitpid仅在等待的pid(必须是您的直接子代)死亡时返回.如果还不是这种情况,它将阻止.

Waitpid only ever returns when the waited on pid (which must be your direct child) is dead. If that's not yet the case, it will block.

这篇关于在Linux中创建独立的进程的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆