fork ( ) - C programming fork ( ) - C programming unix unix

fork ( ) - C programming


I think you are looking for a combination of fork and execl. You can fork to create multiple instances and then replace one of the forked process with another process by using exec(In your case it is the same process). Through execl you can give command line arguments. You may need to use sprintf in the exec'd process and sscanf in the original process. I guess this is enough hint.


I have a file that I want to run multiple instances of 

To do that you have two options :
1. You can use multiple fork() system call to duplicate new child processes and open the file in those processes.
2. You can have multiple threads in your program that open the same file.

But looking at the next three dots, fork() is the choice to go with.

I want a new ID assigned to each process for the file 

When you duplicate processes using fork() each process gets its own unique process Id(pid).

I need to assign a char eg. 'A' that was given through argv[1] to a process 

For this you need to use one of the many calls in the "exec" family.By using "exec"
you can also pass the command line parameters to the newly created processes.
This cannot be done by fork because fork is used to duplicate the current process, whereas if you want to create a totally new process you must use exec calls.

Edit :

In order to get the command line parameters being passed to a process, you need to
know its process id and then you can look for a directory with its name same as the pid
inside the /proc file system( not mounted on actual device ). When you find the directory
you will get the parameters passed to it in a file named "cmdline".
For more detail you can read about "/proc" file system.


You will need to create multiple forking (preferably iteratively) and index your children.* One way to do that is to let the original parent loop, and only let that process do the fork. The original parent loops k times, only creating one child process per iteration. On the created child, you do stuff only the current child process will, such as assign an identifier (such as the loop counter), perform exec, and exit after the child performs everything so it does not go to the next iteration to fork to create grandchild.

Please note that the call fork() is a syscall that causes the original process (now called parent process) to create a duplicate (called child process), as well as return an int value for the parent process only.

One thing you need to observe is that the forked processes are identical with only two exceptions: the value returned by fork() and the process pid (child usually have higher pid). The value returned on the parent is the child's PID. The value on the child process is always zero. Identifying returned value of fork() is the only way to identify it the process is a parent or child.

I have a file that I want to run multiple instances of

You may need to use a combination of fork() and exec. It is not clear which type of file you want to run. Are you reading from a file, writing from a file, or executing a file?

I want a new ID assigned to each process for the file

The PID itself is a new unique ID at the time a new process is created. However, you can use a counter so that only the parent can create multiple child processes, each with a unique ID.

I need to assign a char eg. 'A' that was given through argv[1] to a processargv[1] is a string (char array), not a char.

If there is already a process with the char given, print to stderrIt is possible that you can keep track of all identifier chars on the original parent.

Here is some sample C code where only the parent creates the forking:

int main() {    for (int k = 1; k <= 16; k++) {        int r = fork();        if (r == 0) { // kth CHILD            printf("[%d] %d\n", getpid(), k);            exit(0);        }        else if (r > 0) {            int status;            wait(&status);            printf("[%d] P\n", getpid());        }        else return 1;    }    return 0;}