2

I'm having issues working out where a good starting point for this is, I have made dot points on what I exactly need to do but am unsure if this is entirely possible.

  • I have a file that I want to run multiple instances of
  • I want a new ID assigned to each process for the file
  • I need to assign a char eg. 'A' that was given through argv[1] to a process
  • If there is already a process with the char given, print to stderr

So far,

what I am thinking is, having something like the function below. But i'm really not too sure, any help would be awesomeness.

int createProcess(char *argv[]){

    //argv[1] is given 'A'
    //fork() 
    //getPID()
    //assign PID to 'A'
}
3
  • 1
    to clarify - I'm not expecting someone to write my function, just maybe an explanation if this is possible or not :) Commented Sep 16, 2014 at 15:05
  • 2
    Multiple instances of a file? What? Commented Sep 16, 2014 at 15:11
  • 1
    assign PID to 'A', what does that mean? Commented Sep 16, 2014 at 15:22

4 Answers 4

3

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.

Sign up to request clarification or add additional context in comments.

Comments

1
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.

Comments

1

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 process argv[1] is a string (char array), not a char.

If there is already a process with the char given, print to stderr It 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;
}

Comments

1

If I understand what you want correctly is to "assign" different chars to different instances of the forked process. You can do something like this:

#include <stdio.h>
#include <unistd.h>

int main(int argc, char *argv[]){
     char chr = *argv[1];
     pid_t res;

     res = fork();
     if (!res)
         chr++;
     printf("%c \n",  chr);
     return 0;
}   

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.