LinuxQuestions.org

LinuxQuestions.org (/questions/)
-   Programming (https://www.linuxquestions.org/questions/programming-9/)
-   -   how to differentiate processes (https://www.linuxquestions.org/questions/programming-9/how-to-differentiate-processes-420776/)

ugp 03-01-2006 08:59 PM

how to differentiate processes
 
how to differentiate processes

If there are 1-parent & 4-concurrent same level children, how to differntiate those

processes.
I.e: 4 child processes should able to call diffenent functions.

Child1 should call function-A while
Child2 should call function-B
Child3 should call function-c
Child4 should call function-D


Can i use pid of processes to do this...
If so how???

Are there any other method.

chrism01 03-02-2006 12:31 AM

Need more info: how are the processes created, what lang eg C using fork ?

kshkid 03-02-2006 12:52 AM

yes it is fork()
fork that many process from the same level
and not subsequent forking ( that would establish parent-child-grandchild--)

hence fork from the first level itself.

after forking bind each of the function to each of the child.

ugp 03-02-2006 01:36 AM

Quote:

Originally Posted by chrism01
Need more info: how are the processes created, what lang eg C using fork ?


Here, according to this code it create 4 concurrent children.


#include <signal.h>
#include <stdio.h>
#include <sys/types.h>
#include <unistd.h>
#include <stdlib.h>

main(){
int pid;
int i;
for (i = 1; i <= 4; i++){
if(pid != 0)
pid = fork();

}

sleep(10);
}



From those 4 processes i need to use one process as "RECEPTIONIST" & other three as "AGENTS".
To do so, how can i differentiate the processes.
RECEPTIONIST should read from a file and should send those to message queue.
AGENTS should read from that message queue.
(I need to have 1 RECEPTIONIST FUNCTION & 1 AGENT FUNTION.So to which process i should write those functions)

How can i do that...

chrism01 03-02-2006 10:41 PM

From the fork() manpage:

RETURN VALUE
On success, the PID of the child process is returned in the parent’s
thread of execution, and a 0 is returned in the child’s thread of exe-
cution. On failure, a -1 will be returned in the parent’s context, no
child process will be created, and errno will be set appropriately.

so you just check the rtn code from the fork() call, which tells you which process each code is running in.
Call fork() 3 times to use each child as an AGENT, & parent as RECEPTIONIST.
This isn't a homework qn by any chance ...?

jlliagre 03-02-2006 11:45 PM

ugp, your program has a bug, pid is not initialized when used for the first time.


All times are GMT -5. The time now is 11:06 PM.