Linux 如何将参数传递给由 fork() 创建的进程

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/13461548/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-06 17:49:51  来源:igfitidea点击:

How to pass arguments to processes created by fork()

clinuxoperating-systemforksystem-calls

提问by Basti

I want to create copies of a process using fork()in C. I cant figure out how to pass arguments to the copies of my process. For example,I want to pass an integer to the process copies.

我想在 C中使用fork()创建进程的副本。我不知道如何将参数传递给我的进程副本。例如,我想将一个整数传递给进程副本。

Or I what to do, if I have a loop in which I call fork() and want to pass a unique value to processes (e.g. 0...N)

或者我该怎么做,如果我有一个循环,我在其中调用 fork() 并希望将唯一值传递给进程(例如 0...N)

for (int i = 0; i < 4; ++i) {
    fork();
    // pass a unique value to new processes.
}

采纳答案by Mike

The nice part about fork()is that each process you spawn automatically gets a copy of everything the parent has, so for example, let's say we want to pass an int myvarto each of two child processes but I want each to have a different value from the parent process:

好的部分fork()是您生成的每个进程都会自动获取父进程所有内容的副本,例如,假设我们想将一个 int 传递myvar给两个子进程中的每一个,但我希望每个进程都具有与父进程不同的值过程:

int main()
{
    int myvar = 0;
    if(fork())
        myvar = 1;
    else if(fork())
        myvar = 2;
    else
        myvar = 3;

    printf("I'm %d: myvar is %d\n", getpid(), myvar);
    return 0;
}

So doing this allows each process to have a "copy" of myvarwith it's own value.

因此,这样做可以让每个进程拥有一个myvar具有自己价值的“副本” 。

I'm 8517: myvar is 1
I'm 8518: myvar is 2
I'm 8521: myvar is 3

If you didn't change the value, then each fork'd process would have the same value.

如果您没有更改该值,那么每个 fork 的进程将具有相同的值。

回答by ezod

See the exec()familyof functions.

请参阅函数exec()

EDIT: If you're trying to initialize copies of the same program as the base process, just continue using variables as suggested by duskwuff.

编辑:如果您尝试初始化与基本进程相同的程序的副本,只需按照 duskwuff 的建议继续使用变量。

回答by imreal

You can use clone()(which is actually used by fork() itself). It lets you pass an argto your entry function.

您可以使用clone()(实际上由 fork() 本身使用)。它允许您将arg传递给您的入口函数。

http://linux.die.net/man/2/clone

http://linux.die.net/man/2/clone

回答by duskwuff -inactive-

Local and global variables are inherently preserved across a fork(), so there's no need to "pass arguments". If you're calling a function in the forked process, you can do something like:

局部变量和全局变量本质上保留在 a 中fork(),因此无需“传递参数”。如果您在分叉进程中调用函数,您可以执行以下操作:

pid_t pid = fork();
if (pid == 0) {
    funcToCallInChild(argument);
    exit(0);
}

回答by user2246302

I'm late to respond, but here is how I do it:

我迟到了回复,但这是我的做法:

const char *progname = "./yourProgName";
const char *argument1 = "arg1";
const char *argument2 = "arg2";

if (fork() == 0)
{
    // We are the child process, so replace the process with a new executable.  
    execl(progname, progname, argument1, argument2, (char *)NULL);
}
//  The parent process continues from here.

First, you fork() the process to create a new process. It still has the same memory space as the old one. fork() returns for both parent and child processes. If fork() returns zero, you are the child process. The child process then uses execl() to replace the process memory with one from a new file.

首先,您 fork() 进程以创建一个新进程。它仍然具有与旧的相同的内存空间。fork() 返回父进程和子进程。如果 fork() 返回零,则您是子进程。然后子进程使用 execl() 用新文件中的内存替换进程内存。

Notice that progname is given twice to execl(). The first is what execl() will actually try to run, the second is argv[0]. You must provide both or the argument count will be off by one. Progname must contain all the required path information to find the desired executable image.

请注意,将 progname 两次提供给 execl()。第一个是 execl() 实际尝试运行的内容,第二个是 argv[0]。您必须同时提供两者,否则参数计数将减一。Progname 必须包含查找所需可执行映像所需的所有路径信息。

I give two arguments in this example, but you can pass as many as you want. it must be terminated with NULL, and I think you have to cast it as (char *) like I show.

我在这个例子中给出了两个参数,但你可以传递任意多的参数。它必须以 NULL 结尾,我认为你必须像我展示的那样将它转换为 (char *) 。

This approach gives you a fully independent process with arguments and a unique pid. It can continue running long after the parent process terminates, or it may terminate before the parent.

这种方法为您提供了一个完全独立的过程,带有参数和唯一的 pid。它可以在父进程终止后继续运行很长时间,也可以在父进程之前终止。