waitpid question

waitpid question

Post by Ken Stee » Mon, 16 Jun 2003 02:26:55



When using waitpid in a program where several processes are forked and then
die very quicky using pid = waitpid(-1, &status, WNOHANG) results in
zombies.  If I use while (pid = waitpid(-1, &status, WNOHANG) > 0)  
I don't get zombies but it always returns 1 as the pid.  

I have included a short program as an example.   Hopefully someone can explain
this to me.

Thanks,
Ken

#include<stdio.h>
#include<signal.h>
#include<sys/types.h>
#include<sys/wait.h>

void
received_sigchld(int sig)
{
        int status;
        pid_t pid;

        /* This always returns 1 for the pid */
        while (pid = waitpid(-1, &status, WNOHANG) > 0)
                fprintf(stdout, "pid is %d\n", pid);

        /* Using this instead will result in zombies */
        /*
          pid = waitpid(-1, &status, WNOHANG);
                fprintf(stdout, "pid is %d\n", pid);
                */

Quote:}

int
start_background_job(char *cmd)
{
        pid_t pid;
        char *args[4];

        if ((pid = fork()) == -1)
                return -1;

        if (pid == 0)
        {
                args[0] = "sh";
                args[1] = "-c";
                args[2] = cmd;
                args[3] = NULL;

                execvp(args[0], args);
                exit(-1);
        }
        return 0;

Quote:}

int
main()
{
        struct sigaction sigchld_action;
        int done = 0;

        sigchld_action.sa_handler = received_sigchld;
        sigemptyset(&sigchld_action.sa_mask);
        sigchld_action.sa_flags = SA_RESTART;
        sigaction(SIGCHLD, &sigchld_action, NULL);

        while (!done)
        {
                /* I just used this as an example.  It does not seem to
                 * matter if the process returns an error or not.
                 */
                start_background_job("ls nonexistentfilename1");
                start_background_job("ls nonexistentfilename2");
                start_background_job("ls nonexistentfilename3");
                start_background_job("ls nonexistentfilename4");
                start_background_job("ls nonexistentfilename5");
                start_background_job("ls nonexistentfilename6");

                fprintf(stdout, "Enter q to quit\n Anything else to continue\n: ");

                if ((getchar() == 'q'))
                        done = 1;
        }

        return 0;

Quote:}

-----= Posted via Newsfeeds.Com, Uncensored Usenet News =-----
http://www.newsfeeds.com - The #1 Newsgroup Service in the World!
-----==  Over 80,000 Newsgroups - 16 Different Servers! =-----
 
 
 

waitpid question

Post by Ralf Fasse » Mon, 16 Jun 2003 02:48:20



|       /* This always returns 1 for the pid */
|       while (pid = waitpid(-1, &status, WNOHANG) > 0)

man precedence.  Try

  while ((pid = waitpid(-1, &status, WNOHANG)) > 0)

R'

 
 
 

waitpid question

Post by Ken Stee » Mon, 16 Jun 2003 03:02:50




> |  /* This always returns 1 for the pid */ while (pid = waitpid(-1,
> |  &status, WNOHANG) > 0)

> man precedence.  Try

>   while ((pid = waitpid(-1, &status, WNOHANG)) > 0)

> R'

Thanks.  A good example of my bad programming.  But I still don't
understand why the while loop is needed.  I thought that when the signal
handler was setup it would block the signal until the handler was
finished and then the blocked signal would be sent.

Ken

-----= Posted via Newsfeeds.Com, Uncensored Usenet News =-----
http://www.newsfeeds.com - The #1 Newsgroup Service in the World!
-----==  Over 80,000 Newsgroups - 16 Different Servers! =-----

 
 
 

waitpid question

Post by Marc Rochkin » Mon, 16 Jun 2003 03:03:07




> |  /* This always returns 1 for the pid */
> |  while (pid = waitpid(-1, &status, WNOHANG) > 0)

> man precedence.  Try

> while ((pid = waitpid(-1, &status, WNOHANG)) > 0)

> R'

And don't forget to check pid against -1. Also, it's a good idea to check
the error return from all other functions (e.g., sigaction).

--Marc

 
 
 

waitpid question

Post by Jens.Toerr.. » Mon, 16 Jun 2003 03:41:50





>> |      /* This always returns 1 for the pid */ while (pid = waitpid(-1,
>> |      &status, WNOHANG) > 0)

>> man precedence.  Try

>>   while ((pid = waitpid(-1, &status, WNOHANG)) > 0)

>> R'

> Thanks.  A good example of my bad programming.  But I still don't
> understand why the while loop is needed.  I thought that when the signal
> handler was setup it would block the signal until the handler was
> finished and then the blocked signal would be sent.

Yes, but when there are two or more signals coming in while you're in
the handler all of them get blocked and you will still only get one
signal, there's no counting of blocked signals. Looping until there
aren't any more dead children* around will take care of this.

                                        Regards, Jens
--
      _  _____  _____

  _  | |  | |    | |
 | |_| |  | |    | |          http://www.veryComputer.com/~toerring
  \___/ens|_|homs|_|oerring

 
 
 

waitpid question

Post by Ken Stee » Mon, 16 Jun 2003 04:01:02






>>> |  /* This always returns 1 for the pid */ while (pid = waitpid(-1,
>>> |  &status, WNOHANG) > 0)

>>> man precedence.  Try

>>>   while ((pid = waitpid(-1, &status, WNOHANG)) > 0)

>>> R'

>> Thanks.  A good example of my bad programming.  But I still don't
>> understand why the while loop is needed.  I thought that when the
>> signal handler was setup it would block the signal until the handler
>> was finished and then the blocked signal would be sent.

> Yes, but when there are two or more signals coming in while you're in
> the handler all of them get blocked and you will still only get one
> signal, there's no counting of blocked signals. Looping until there
> aren't any more dead children* around will take care of this.

>                                         Regards, Jens

Thanks,  That is what I did not understand.

Ken

-----= Posted via Newsfeeds.Com, Uncensored Usenet News =-----
http://www.veryComputer.com/ - The #1 Newsgroup Service in the World!
-----==  Over 80,000 Newsgroups - 16 Different Servers! =-----

 
 
 

1. wait(), waitpid question?

in my code , I just remove from line 16 to 31. ( routine for checking Time
out and wait child process finish it's execution )

then , even if child process take much time , it is OK.
I think becase I must use wait wrong way,this problem occured.
could you give a some example code for wait() or waitpid().
or detail explain.

I'd like to thank you for your time.
have a day.

16.                       signal ( SIGALRM, SigAlarm );
17.                       alarm ( TIMEOUT );
18.                       while ( wait ( &st ) == -1 )
19.                       {
 20.                           if ( errno == EINTR )
 21.                           {
 22.                               errno = 0;
23.                                 printf ("%s: time out \n" , argv[1] );
24.
25.                           }
26.                          else
27,                          {
28.                                  alarm(0);
29.                                  perror ( argv[0] );
30.                                  break;
31.                       }

2. Problems with find

3. question about waitpid

4. ftp problem

5. SIGIOT/waitpid()/wait3() Question

6. Kernel Stack on Usb

7. UNP Book Question - waitpid in SIGCHLD signal handler

8. SMP Nameserver

9. EXEC fails. waitpid() No child processes

10. Help with wait or waitpid

11. waitpid returns 0

12. Is using waitpid() reset SIGCHLD?

13. waitpid