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. |
Need more info: how are the processes created, what lang eg C using fork ?
|
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. |
Quote:
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... |
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 ...? |
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. |