-2

I have a problem figuring out how to use active child processes after they are created in the fork(). I see from another terminal that they are still active until I execute the exit success. If I wanted to say for example: I want to make the child process with pid 10243, active at the moment, do this method. I would like to call the method after all the children have been initialized, so after the fork. Do I have to type execvp ()? execvp (methodName (a))?

EDIT: I will try to add a similar code in order to be more specific

So let's say that I did a cycle where I create n child

            
        switch (pid = fork()) {     
            case -1:
                /* Handle error */
                fprintf(stderr, "%s, %d: Errore (%d) nella fork\n",
                __FILE__, __LINE__, errno);
                exit(EXIT_FAILURE);
            case 0: 

                    //here I have saved the pid of the child process        
                
            break;
            
        
            default:

            /* PARENT CODE: nothing here */
            wait(NULL);     
            exit(0);            

            break;
        }
    }```

So at that point I have created n number of child process. Now I want, for example, that the child with the pid 10342 do a method called "method1()", and the child with the pid 10343 do a method called "method2()".

How can I say that? Is that possibile? Because I need first to create all the child and then use them while the "main process" is in stand by with an infinte cycle. Like: 

```int u = 0;
    while(u == 0){         I will handle the end of this with a signal 
        
        }
    }
exit(EXIT_SUCCESS);
merli
  • 3
  • 4
  • That question could uses a lot more specific information on what you are trying (and have tried) to achieve. Examples of your own efforts that we can comment upon are appreciated. In general: You might want to read a tutorial about Unix child processes. Your parent process has much less direct access to its childs than you seem to assume. – tofro Jun 12 '21 at 09:14
  • I have added more information :) – merli Jun 12 '21 at 09:55

1 Answers1

0

From the man page of fork:

The child process and the parent process run in separate memory spaces. At the time of fork() both memory spaces have the same content.

That means, all functions in the memory can be called by the parent and the child processes, but there is no way, that one process can call a function which resides in the memory space of another process.

For that you need IPC (Inter Process Communication).

Addendum:

After fork, you're able to run every function you like (in the child process - i hope you know what that means, if not, the provided link will help you out). But to 'make' a child process active and run a certain method is only possible via IPC.

Erdal Küçük
  • 4,810
  • 1
  • 6
  • 11
  • I have edited my question to be more understandable. Thank you for your answer, so at that point I think that what I was trying to do is impossibile unless I use an IPC right? – merli Jun 12 '21 at 09:58
  • @merli I saw your edit, but that doesn't change anything. Btw. you cannot use 'an' IPC, IPC is a concept. But i think, for what you're trying to do, have you ever thought about using threads? Threads are lightweight processes and they run all in the same address space. – Erdal Küçük Jun 12 '21 at 10:02
  • Yes I would like to use threads but I must use process in this code unfortunately haha so by IPC you mean something like pipes or message queues? – merli Jun 12 '21 at 10:07
  • Yes exactly. Or shared memory objects, sockets, files, file mappings, etc. – Erdal Küçük Jun 12 '21 at 10:08
  • Thank you so much, I will try to use shared memory, I will let you know if it's working :) – merli Jun 12 '21 at 10:12
  • A little hint about shared memory: You cann create mutexes and condition variables (pthread_xxx) on shared memory (see mmap), which can be used by all processes. And happy workings :) – Erdal Küçük Jun 12 '21 at 10:15