Search code examples
cunixprocessforkargv

fork ( ) - C programming


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'
}

Solution

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