![]() |
|
|
|
|
|||||||
| Forums | Portal | Register | Forum Rules | FAQ | Contribute | Members List | Arcade | Search | Today's Posts | Mark Forums Read |
| UNIX for Dummies Questions & Answers If you're not sure where to post a UNIX or Linux question, post it here. All UNIX and Linux newbies welcome !! |
|
|
||||
| Thread | Thread Starter | Forum | Replies | Last Post |
| Monitoring Processes - Killing hung processes | ukndoit | UNIX for Advanced & Expert Users | 4 | 01-17-2008 01:30 AM |
| Killing parent shells from subshells (KSH) | rockysfr | Shell Programming and Scripting | 4 | 06-28-2007 04:53 PM |
| forking n number of processes in a loop and not 2^n | rvan | High Level Programming | 4 | 01-17-2007 12:37 AM |
| Parent/Child Processes | yoi2hot4ya | Shell Programming and Scripting | 2 | 05-31-2006 10:27 AM |
| what are parent and child processes all about? | xyyz | UNIX for Dummies Questions & Answers | 1 | 04-26-2002 12:53 PM |
|
|
Submit Tools | LinkBack | Thread Tools | Display Modes |
|
#1
|
|||
|
|||
|
forking and killing parent processes
Hi everybody,
I'm having some problems wiriting a program in UNIX using the "fork" and "kill" system calls. I have to create a C program P0, which creates 9 other processes P1, P2, ..., P9, where P0 is the father of P1, P1 the father of P2, and so on. All the processes contain an infinite loop. When all the processes are created P9 executes the command "ps". Then P9 kills P8, execute "ps", then kills P7, executes "ps", and so on till only the process P0 and P9 remain. I've created the 9 processes in the following way: P0 does a fork. The child process executes p1 and the parent process enters and infinite loop. p1 to p8 do the same thing. p9 then executes ps. Up to here there are no problems, ps is executed and everything seems to be working fine, the PID and PPID are correct, and all the processes are marked as running. The problem I have is how to kill the parents of p9. I use the kill function, but for this I need the PID of the processes to kill. How can this be found? To see at least if the kill process does what's it supposed to do, I use the getppid() system call to retrieve the PID of process p9's father and loop it back from there (not very elegant but should work in theory). With this pid I then use the kill function with the SIGKILL signal and the pid of the process to be killed. The problem here is that only p8 gets killed while the rest of the processes keep running! Therefore the process p9 does something like this: Code:
int main() {
execute_ps()
kill_parents()
}
void execute_ps() {
pid = fork();
if (fork != 0)
wait();
else
execl("/bin/ps","",0)
}
void kill_parents() {
parent = getppid();
for (i=0; i<8; i++) {
kill(parent, SIGKILL);
parent--;
execute_ps();
}
execute_ps();
}
Any help/suggestions are welcome! Thanks a lot David |
|||
| Google The UNIX and Linux Forums |
| Forum Sponsor | ||
|
|
| Thread Tools | |
| Display Modes | |
|
|