|
|
|
| |||||||||
![]() |
|
|
«
Previous Thread
|
Next Thread
»
|
Thread Tools | Search this Thread | Rate Thread | Display Modes |
|
#1
|
|||
|
|||
|
forking and killing parent processes
Hi everybody,
I have to do an excercise in C for my Operating Systems course and I am having some problems. The excercise is as follows: Write 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 are to contain an infinite loop. When all the processes are created P9 is to execute the command "ps". Then P9 is to kill P8, execute "ps", then kill P7, execute "ps", and so on till only the process P0 and P9 remain. What i've done is to create the nine processes. 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() function to retrive the PID of process p9 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 |
|
#2
|
||||
|
||||
|
Please read the 'stikies' around the forums. This is not a place for posting homework questions. This sort of post is generally frowned upon and you're more likely to get trolled/flamed.
christo
__________________
. Spiration channels: Free scripts, programming tutorials and articles Dotcut alerts: Online Press cuttings / news alerts Clearprop: UK microlight school, wiltshire Uk dating: UK safe dating with Topdates About Christo . . |
|
#3
|
|||
|
|||
|
I agree with christo but I will give you a hint. What are some arguments that you can give to "ps" to see more information about the process table? One of them allows you to see the parent PID. A "man ps" may help.
__________________
Need Java help? Want to help people who do? Sit down with a cup of Java at the hotjoe forums. |
![]() |
| Viewing: Dev Shed Forums > Operating Systems > UNIX Help > forking and killing parent processes |
| Thread Tools | Search this Thread |
| Display Modes | Rate This Thread |
|
|
|
|