LinuxQuestions.org
Latest LQ Deal: Latest LQ Deals
Home Forums Tutorials Articles Register
Go Back   LinuxQuestions.org > Forums > Non-*NIX Forums > Programming
User Name
Password
Programming This forum is for all programming questions.
The question does not have to be directly related to Linux and any language is fair game.

Notices


Reply
  Search this Thread
Old 09-28-2004, 07:52 AM   #1
coolfrog
Member
 
Registered: Sep 2004
Location: India
Distribution: Slackware 10.0
Posts: 77

Rep: Reputation: 15
Killing child process


Hi,
i am currently working on the Diner Philosophers problem. I have this code :
Code:
while(ctr < 5)
	{
		switch(pid=fork())
		{
			case 0:	p.pid = ctr;
				for(i=0;i<iteration;i++)
				{
					cout << "\n" << p.pid << ": Thinking.....";
					sleep(rand());	//	THINKING
					cout << "\n" << p.pid << ": Acquiring right chopstick.....";
					lock(p.pid);	//	LOCK RIGHT CHOPSTICK
					cout << "\n" << p.pid << ": Acquiring left chopstick.....";
					lock((p.pid+1)%5);	//	LOCK LEFT CHOPSTICK
					cout << "\n" << p.pid << ": Eating.....";
					sleep(rand());
					cout << "\n" << p.pid << ": Releasing chopsticks.....";
					unlock(p.pid);	//	LOCK RIGHT CHOPSTICK
					unlock((p.pid+1)%5);	//	LOCK LEFT CHOPSTICK				
				}
				cout << "\nProcess " << p.pid << " exiting....";
				exit(0);
				break;
			case -1: cout << "\nError";
				break;
			default: ctr++;
				continue;
		}
		break;
	}
Now after fork the child process does enter case 0 but does not enter for loop. Also after the entire program terminates and when i execute the command 'ps' i still have 5 a.out processes running. How do i kill the processes permanently through C/C++ code ? What about the for loop problem ?
 
Old 09-28-2004, 11:11 AM   #2
koyi
Member
 
Registered: Jul 2003
Location: Osaka, Japan
Distribution: Arch, Ubuntu
Posts: 421

Rep: Reputation: 31
As far as I know, there are two ways to prevent the child process from becoming a zombie process.

1. Call wait() from the parent process.
2. After the first fork(), fork() again and kill the child process immediately thus handing the grand-child process to the init process. I am not sure whether you should do a wait() in the parent process in this case. So please do experiment yourself.

Search for "kill zombie process" may help you understand why those child processes exist.
 
Old 09-28-2004, 12:48 PM   #3
trickykid
LQ Guru
 
Registered: Jan 2001
Posts: 24,149

Rep: Reputation: 269Reputation: 269Reputation: 269
Moved: Seems more suitable in the Programming forum.
 
Old 09-28-2004, 02:58 PM   #4
aluser
Member
 
Registered: Mar 2004
Location: Massachusetts
Distribution: Debian
Posts: 557

Rep: Reputation: 43
sleep() takes its time in seconds, and rand returns a number between 0 and RAND_MAX. On my system, RAND_MAX is over 200 million, so your program would sleep, on average, around 3 years at the first "THINKING" comment. That sounds wrong.

To kill a process, see the kill() syscall's man page.
 
  


Reply



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

BB code is On
Smilies are On
[IMG] code is Off
HTML code is Off



Similar Threads
Thread Thread Starter Forum Replies Last Post
how a father process know which child process send the signal SIGCHLD icoming Programming 10 07-20-2010 07:26 AM
How to kill a Child and all its subsequent child process in C shayer009 Programming 3 12-04-2007 12:40 AM
can destroy child process alaios Programming 2 06-01-2005 08:06 AM
Killing a child process from another child marri Programming 6 10-01-2004 07:08 PM
Bash Scripting - child process affecting parent process mthaddon Linux - General 1 05-02-2004 01:19 PM

LinuxQuestions.org > Forums > Non-*NIX Forums > Programming

All times are GMT -5. The time now is 11:27 AM.

Main Menu
Advertisement
My LQ
Write for LQ
LinuxQuestions.org is looking for people interested in writing Editorials, Articles, Reviews, and more. If you'd like to contribute content, let us know.
Main Menu
Syndicate
RSS1  Latest Threads
RSS1  LQ News
Twitter: @linuxquestions
Open Source Consulting | Domain Registration