LinuxQuestions.org
Download your favorite Linux distribution at LQ ISO.
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 02-17-2011, 12:22 PM   #1
KillerCode
Member
 
Registered: Nov 2010
Posts: 57

Rep: Reputation: 0
terminating zombie processes (forked)


ive found this code on "Advanced Linux Programming - Chapter 3 : Processes" it should terminate fork()ed processes after they done their job
but it doesnt, as soon as the forked processes finishes its job and calls exit, the whole application restarts :/


here is the code
Code:
#include <signal.h>
#include <string.h>
#include <sys/types.h>
#include <sys/wait.h>

sig_atomic_t child_exit_status;

void clean_up_child_process (int signal_number)
{
  /* Clean up the child process.  */
  int status;
  wait (&status);
  /* Store its exit status in a global variable.  */ 
  child_exit_status = status;
}

int main ()
{
  /* Handle SIGCHLD by calling clean_up_child_process.  */
  struct sigaction sigchld_action;
  memset (&sigchld_action, 0, sizeof (sigchld_action));
  sigchld_action.sa_handler = &clean_up_child_process;
  sigaction (SIGCHLD, &sigchld_action, NULL);
  /* Now do things, including forking a child process.  */
  /* ...  */
  return 0;
}
and thats how i fork processes
Code:
if (fork() == 0)
{
    /* Call the Function required here */
    rename("/home/killercode/data.zip", "/home/killercode/file.zip");
    exit(0);
}
thanks.

Last edited by KillerCode; 02-17-2011 at 12:24 PM.
 
Old 02-17-2011, 12:51 PM   #2
chymeira
Member
 
Registered: Dec 2008
Location: CH/IL
Distribution: Slackware 13.1 Fedora 12
Posts: 73

Rep: Reputation: 16
Hey mate, I just tried your app and it works fine ...

Try this .... to make things clear

Code:
int main() {
      pid_t pid[10];
      int i, child_status;

      for(i = 0; i < 10; i++)
          if((pid[i] = fork()) == 0)
               exit(0);
      for(i = 0; i < 10; i++) {
          pid_t wpid = waitpid(pid[i], &child_status, 0);
          if(WIFEXITED(child_status))
              printf("Child %d terminated with exit status %d\n",
                               wpid, WEXITSTATUS(child_status));
          else
              printf("Child %d terminated abnormally\n", wpid);
       }

        return 0;
}
Hope it helps .
 
Old 02-17-2011, 01:04 PM   #3
KillerCode
Member
 
Registered: Nov 2010
Posts: 57

Original Poster
Rep: Reputation: 0
yea, but with ur method ill have to call the cleanup code reguraly, im trying to get the one i posted to work since the forked processes sends a SIGCHLD signal whenever it finishes working. so its not blocking any of the app functions.

thanks.

EDIT: this app should sleep for 100 seconds, but it doesnt :/
Code:
#include <signal.h>
#include <string.h>
#include <sys/types.h>
#include <sys/wait.h>
#include <stdlib.h>

sig_atomic_t child_exit_status;

void clean_up_child_process (int signal_number)
{
  /* Clean up the child process.  */
  int status;
  wait (&status);
  /* Store its exit status in a global variable.  */ 
  child_exit_status = status;
}

int main ()
{
  /* Handle SIGCHLD by calling clean_up_child_process.  */
  struct sigaction sigchld_action;
  memset (&sigchld_action, 0, sizeof (sigchld_action));
  sigchld_action.sa_handler = &clean_up_child_process;
  sigaction (SIGCHLD, &sigchld_action, NULL);
  if (fork() == 0)
  {
	exit(0);
  }
  sleep(100);
  return 0;
}

Last edited by KillerCode; 02-17-2011 at 01:13 PM.
 
Old 02-17-2011, 01:14 PM   #4
chymeira
Member
 
Registered: Dec 2008
Location: CH/IL
Distribution: Slackware 13.1 Fedora 12
Posts: 73

Rep: Reputation: 16
Yes you are right , I just wanted to demonstrate the use of the macros WIFEXITED and EXITSTATUS for debugging your code.

Like I said , I ran your code and it works just fine on my system.
 
Old 02-17-2011, 01:24 PM   #5
KillerCode
Member
 
Registered: Nov 2010
Posts: 57

Original Poster
Rep: Reputation: 0
the last one i posted? does it sleep for 100 seconds? cause it exits right away from my terminal :S
 
Old 02-17-2011, 01:54 PM   #6
z1p
Member
 
Registered: Jan 2011
Location: the right coast of the US
Distribution: Ubuntu 10.04
Posts: 80

Rep: Reputation: 23
Quote:
Originally Posted by KillerCode View Post
yea, but with ur method ill have to call the cleanup code reguraly, im trying to get the one i posted to work since the forked processes sends a SIGCHLD signal whenever it finishes working. so its not blocking any of the app functions.

thanks.

EDIT: this app should sleep for 100 seconds, but it doesnt :/
That is because the sigchild signal interrupts the sleep call. Try something like the following to sleep for the full time.
Code:
sleeptime = 100;
while ((sleepTime = sleep(sleepTime)) > 0);
See the man page for sleep for more info on how it works.
 
Old 02-17-2011, 02:09 PM   #7
KillerCode
Member
 
Registered: Nov 2010
Posts: 57

Original Poster
Rep: Reputation: 0
its not just sleep, any function i do, such as receiving file from http server, it restarts the whole application as soon as the download (forked process) completes !!
 
Old 02-17-2011, 02:18 PM   #8
z1p
Member
 
Registered: Jan 2011
Location: the right coast of the US
Distribution: Ubuntu 10.04
Posts: 80

Rep: Reputation: 23
I ran your test code replacing sleep with the while loop I provided and it sleeps the whole time.

What are you doing in the parent process? I assume you have a loop doing something? what is the exit condition of the loop? Other system calls are affected by signals and need to be 'restarted' after a signal occurs in a process.
 
Old 02-17-2011, 07:44 PM   #9
KillerCode
Member
 
Registered: Nov 2010
Posts: 57

Original Poster
Rep: Reputation: 0
even socket calls? like recv?

and yea, ive a loop in the forked process and a loop in the parent process....

Last edited by KillerCode; 02-17-2011 at 07:46 PM.
 
Old 02-17-2011, 09:29 PM   #10
z1p
Member
 
Registered: Jan 2011
Location: the right coast of the US
Distribution: Ubuntu 10.04
Posts: 80

Rep: Reputation: 23
Yes sys calls like recv will return on a signal.

See the man page (http://linux.die.net/man/3/recv), one of the possible errno value it sets is EINTR.

somethng like this handles it.

Code:
errno = 0
while (((cnt = recv(...)) < 0) && errno == EINTR))
 
Old 02-17-2011, 11:36 PM   #11
KillerCode
Member
 
Registered: Nov 2010
Posts: 57

Original Poster
Rep: Reputation: 0
ok, i can do something like that, one more question, is there is no other way to make it uninterrupted? maybe a something when assigning the signal handler? or there is no way to evade the interruption without modifiying recv()?
 
Old 02-18-2011, 07:42 AM   #12
Bagration
LQ Newbie
 
Registered: Dec 2010
Location: Germany
Distribution: SuSe, Ubuntu
Posts: 12

Rep: Reputation: 0
Hello KillerCode,

I tested your original sleep(100)-Code. It works fine. Must really have to do something with your parents process.

Greetings Bernd
 
Old 02-18-2011, 10:13 AM   #13
z1p
Member
 
Registered: Jan 2011
Location: the right coast of the US
Distribution: Ubuntu 10.04
Posts: 80

Rep: Reputation: 23
What I showed is a pretty standard way of dealing with it as it will work everywhere and always.

Take a look at the man page for signal (http://www.kernel.org/doc/man-pages/.../signal.7.html) for a info on signals and interruptable system calls. On linux you can possible use SA_RESTART with your signal handler, but it doesn't all interruptable system calls. You'll notice on the man page that SA_RESTART works for recv, depending on how it is called.
 
  


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
How to kill zombie processes gikpo4 Programming 1 12-03-2010 05:11 PM
Zombie processes Ynot Irucrem General 2 01-11-2007 12:23 AM
C - sharing variables between forked processes? ocularbob Programming 5 03-14-2004 03:29 AM
zombie processes mhr Linux - Newbie 1 06-19-2003 09:49 PM
How to get rid of zombie processes ugge Linux - General 3 10-01-2001 09:09 AM

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

All times are GMT -5. The time now is 05:43 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