LinuxQuestions.org
Welcome to the most active Linux Forum on the web.
Go Back   LinuxQuestions.org > Forums > Linux Forums > Linux - Newbie
User Name
Password
Linux - Newbie This Linux forum is for members that are new to Linux.
Just starting out and have a question? If it is not in the man pages or the how-to's this is the place!

Notices


Reply
  Search this Thread
Old 04-17-2015, 06:27 AM   #1
aj1991
LQ Newbie
 
Registered: Apr 2015
Posts: 1

Rep: Reputation: Disabled
kill zombie process via C program without killing main process


Hi ,
In my program, for a process A executing operation, there are multiple threads created.
One of the thread A checks whether process is active or not
Second thread B executes a function
One of the periodic thread C calls a callback function that executes an audit after every 30 secs.

In the audit , the function uses g_spawn_commmand_line_sync() function to spawn a child process that executes a command line (shell script).

Since g_spawn_commmand_line_sync() spawns a new child process .
In the error case scenario, the spawned child process got stuck and became zombie process. It did not get killed.Also it acquired sockets which were assigned to a thread B executing operation.

Even though the thread B got terminated ,the child process did not release the sockets that were connected to it.


My question is ,I have to automate this in C program that if it finds spawned process of g_spawn_commmand_line_sync() defunct, the
health check thread B should be able to clean it without klling the main process.

i could not find any spawn function that will help me find the PID of g_spawn_commmand_line_sync() spawned process.

I get the details from ps commands. But how can I find it in a c-program?
 
Old 04-17-2015, 08:28 AM   #2
jpollard
Senior Member
 
Registered: Dec 2012
Location: Washington DC area
Distribution: Fedora, CentOS, Slackware
Posts: 4,602

Rep: Reputation: 1241Reputation: 1241Reputation: 1241Reputation: 1241Reputation: 1241Reputation: 1241Reputation: 1241Reputation: 1241Reputation: 1241
Zombie processes cannot be "killed" - they are already dead. They also don't usually hang around long as the process that started them is usually waiting for their exit signal. Once the exit signal is handled the zombie process is removed.

One of the instances that can keep a zombie process around (nearly forever) is hardware malfunction. In this case, the (now) zombie process is holding some kernel locks within the device driver, and the device is not signalling completion back to the driver (which would then release the locks, and release the process). One case in software causing this is NFS - if the NFS mount is identified as "hard", then the locks will not be released if the server crashes - until the server comes back, and the timeout recovery can then decide to release the locks. When NFS filesystem is mounted "soft", then local interrupts/aborts/... can interrupt the driver, and force the release of the locks.

The problem you have is that g_spawn_commmand_line_sync() is designed to clean up after the process exits. Since the process hasn't exited, it can't give any pids as they are internal data. If you want the pid, you have to use g_spawn_command_line_async(), and do the waiting for process completion separately.
 
Old 04-17-2015, 12:07 PM   #3
rtmistler
Moderator
 
Registered: Mar 2011
Location: Sutton, MA. USA
Distribution: MINT Debian, Angstrom, SUSE, Ubuntu
Posts: 4,087
Blog Entries: 10

Rep: Reputation: 1521Reputation: 1521Reputation: 1521Reputation: 1521Reputation: 1521Reputation: 1521Reputation: 1521Reputation: 1521Reputation: 1521Reputation: 1521Reputation: 1521
What needs to be done is the parent of that process needs to deal with it, the main way being paying attention to signals from that process to ensure that closure occurs.

I wrote a blog entry how to do exactly this, and for the same reason, I forked children and didn't wait for their termination signals, then found I grew zombies over time, How to kill those Zombies. Read the section titled "More Complex Case" where it covers architectures that can't afford to wait and stop processing. You can periodically re-check for signals from your children.

Unfortunately even though they are your children, once they become zombies, you have to shoot them in the head.

I would add that providing you end up waiting for a signal from your child processes, what this now means is that you will no longer grow zombies. A child process may temporarily become a zombie, for instance say you only check once per second via a timer expiration, then up to that one second time span you could've had a child process which completed and is a zombie until you the parent processes the exit signal.

Last edited by rtmistler; 04-17-2015 at 12:09 PM.
 
Old 04-17-2015, 01:04 PM   #4
jpollard
Senior Member
 
Registered: Dec 2012
Location: Washington DC area
Distribution: Fedora, CentOS, Slackware
Posts: 4,602

Rep: Reputation: 1241Reputation: 1241Reputation: 1241Reputation: 1241Reputation: 1241Reputation: 1241Reputation: 1241Reputation: 1241Reputation: 1241
Just using SIGCHLD will eliminate the polling.

The thing is, the function being used IS cleaning up when it gets the signal.
 
  


Reply


Thread Tools Search this Thread
Search this Thread:

Advanced Search

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
Kill a zombie process - which process is the parent that I should kill? Mountain Linux - General 3 12-31-2011 03:44 PM
Killing a zombie process? Scottish-Stephanie Linux - General 8 10-16-2010 08:32 PM
kill zombie process qwertyjjj Linux - Newbie 3 08-07-2010 11:09 PM
how to kill zombie process bharatlalgupta Red Hat 1 06-18-2009 11:17 AM
how to kill zombie process mokku Linux - Server 5 10-25-2007 10:21 PM


All times are GMT -5. The time now is 02:54 PM.

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
Facebook: linuxquestions Google+: linuxquestions
Open Source Consulting | Domain Registration