How to pass arguments to processes created by fork()

27,054

Solution 1

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 myvar to each of two child processes but I want each to have a different value from the parent process:

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 myvar with it's own value.

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.

Solution 2

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.

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.

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.

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.

Solution 3

See the exec() family of functions.

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

Solution 4

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

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

Share:
27,054
Basti
Author by

Basti

Updated on August 05, 2022

Comments

  • Basti
    Basti almost 2 years

    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.

    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)

    for (int i = 0; i < 4; ++i) {
        fork();
        // pass a unique value to new processes.
    }
    
  • Basile Starynkevitch
    Basile Starynkevitch over 11 years
    No, please don't. clone is a complex, Linux specific, syscall which requires a very good knowledge of Linux, and is mostly reserved to the few gurus implementing thread libraries (à la pthread). See also futex(7) man page.
  • Linuxios
    Linuxios over 11 years
    @BasileStarynkevitch: Exactly. Only use clone if you're doing some real low level hacking -- the only point of clone is to allow you to do some things in userspace that before could only happen in kernel space, such as thread implementation. It is not a general use syscall.