UNIX Help
 
Forums: » Register « |  User CP |  Games |  Calendar |  Members |  FAQs |  Sitemap |  Support | 
User Name:
Password:
Remember me
Go Back   Dev Shed ForumsOperating SystemsUNIX Help

Reply
Add This Thread To:
  Del.icio.us   Digg   Google   Spurl   Blink   Furl   Simpy   Y! MyWeb 
Thread Tools Search this Thread Rate Thread Display Modes
 
Unread Dev Shed Forums Sponsor:
  #1  
Old October 17th, 2005, 03:02 AM
davewilliams20 davewilliams20 is offline
Registered User
Dev Shed Newbie (0 - 499 posts)
 
Join Date: Mar 2003
Posts: 14 davewilliams20 User rank is Just a Lowly Private (1 - 20 Reputation Level) 
Time spent in forums: 1 h 32 m 47 sec
Reputation Power: 0
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

Reply With Quote
  #2  
Old October 17th, 2005, 03:42 AM
christo's Avatar
christo christo is offline
Introspective
Dev Shed Loyal (3000 - 3499 posts)
 
Join Date: Nov 2001
Location: London, UK
Posts: 3,297 christo User rank is Second Lieutenant (5000 - 10000 Reputation Level)christo User rank is Second Lieutenant (5000 - 10000 Reputation Level)christo User rank is Second Lieutenant (5000 - 10000 Reputation Level)christo User rank is Second Lieutenant (5000 - 10000 Reputation Level)christo User rank is Second Lieutenant (5000 - 10000 Reputation Level)christo User rank is Second Lieutenant (5000 - 10000 Reputation Level)christo User rank is Second Lieutenant (5000 - 10000 Reputation Level) 
Time spent in forums: 3 Days 1 h 10 m 50 sec
Reputation Power: 104
Send a message via ICQ to christo Send a message via Yahoo to christo
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
Comments on this post
stdunbar agrees!

Reply With Quote
  #3  
Old October 17th, 2005, 11:23 AM
stdunbar stdunbar is offline
Contributing User
Dev Shed Intermediate (1500 - 1999 posts)
 
Join Date: May 2004
Location: Superior, CO, USA
Posts: 1,730 stdunbar User rank is Major (30000 - 40000 Reputation Level)stdunbar User rank is Major (30000 - 40000 Reputation Level)stdunbar User rank is Major (30000 - 40000 Reputation Level)stdunbar User rank is Major (30000 - 40000 Reputation Level)stdunbar User rank is Major (30000 - 40000 Reputation Level)stdunbar User rank is Major (30000 - 40000 Reputation Level)stdunbar User rank is Major (30000 - 40000 Reputation Level)stdunbar User rank is Major (30000 - 40000 Reputation Level)stdunbar User rank is Major (30000 - 40000 Reputation Level)stdunbar User rank is Major (30000 - 40000 Reputation Level) 
Time spent in forums: 1 Month 2 Days 23 h 22 m 53 sec
Reputation Power: 375
Send a message via ICQ to stdunbar Send a message via Yahoo to stdunbar
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.

Reply With Quote
Reply

Viewing: Dev Shed ForumsOperating SystemsUNIX Help > forking and killing parent processes


Thread Tools  Search this Thread 
Search this Thread:

Advanced Search
Display Modes  Rate This Thread 
Rate This Thread:


Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

vB code is On
Smilies are On
[IMG] code is On
HTML code is Off
View Your Warnings | New Posts | Latest News | Latest Threads | Shoutbox
Forum Jump


Forums: » Register « |  User CP |  Games |  Calendar |  Members |  FAQs |  Sitemap |  Support | 
  
 





© 2003-2008 by Developer Shed. All rights reserved. DS Cluster 5 hosted by Hostway
Stay green...Green IT