Jump to content
xisto Community
Sign in to follow this  
kvarnerexpress

Killing Parent Processes

Recommended Posts

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.kvarnerexpress

Share this post


Link to post
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now
Sign in to follow this  

×
×
  • Create New...

Important Information

Terms of Use | Privacy Policy | Guidelines | We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.