Unix Programming - SIGCHLD handler kills parent

This is Interesting: Free IT Magazines  
Home > Archive > Unix Programming > October 2005 > SIGCHLD handler kills parent





You are viewing an archived Text-only version of the thread. To view this thread in it's original format and/or if you want to reply to this thread please [click here]

Author SIGCHLD handler kills parent
wallygato11@gmail.com

2005-10-24, 3:47 pm

Hi:

I have been fighting with this problem for quite a while and am not
sure what to do.

I am writing a simple unix shell and am trying to implement background
processes.

What I want is to be able to run a process in the background and when
it finishes to print a message saying that it is done.

ie. printf("\n[%d] Done\n",jobnum);

What I have been trying is if the shell detects that the user wants a
background process, it forks/execve's and the parent process
initializes the SIGCHLD signal handler

signal(SIGCHLD, catch_chld);

the handler looks thusly

void catch_chld(int sig_num)
{

while (waitpid (-1, NULL, WNOHANG) > 0)
; /* don't hang, if no kids are dead yet */

// print the message
printf("\n[%d] Done\n",jobnum);

}

The problem is that the once the handler is done my whole shell
exits... I have no idea why. I am new to all of this so I might be
doing something stupid. Any help would be appreciated. If more info is
needed I can provide it.

Ryan

Gordon Burditt

2005-10-24, 3:47 pm

>I have been fighting with this problem for quite a while and am not
>sure what to do.
>
>I am writing a simple unix shell and am trying to implement background
>processes.
>
>What I want is to be able to run a process in the background and when
>it finishes to print a message saying that it is done.
>
>ie. printf("\n[%d] Done\n",jobnum);
>
>What I have been trying is if the shell detects that the user wants a
>background process, it forks/execve's and the parent process
>initializes the SIGCHLD signal handler
>
>signal(SIGCHLD, catch_chld);
>
>the handler looks thusly
>
>void catch_chld(int sig_num)
>{
>
> while (waitpid (-1, NULL, WNOHANG) > 0)
> ; /* don't hang, if no kids are dead yet */
>
> // print the message
> printf("\n[%d] Done\n",jobnum);


printf() is not signal-safe to use in a signal handler.
I've run into problems with segementation faults when trying
this with debug code. Usually *intermittent* segmentation
faults, which are much harder to track down. Also, the problems
often appear to go away when run under a debugger.
>}


>The problem is that the once the handler is done my whole shell
>exits... I have no idea why. I am new to all of this so I might be
>doing something stupid. Any help would be appreciated. If more info is
>needed I can provide it.


Gordon L. Burditt
Vi Si

2005-10-24, 3:47 pm

So do u suggest to use stdout instead of printf?

Maxim Yegorushkin

2005-10-24, 3:47 pm


Vi Si wrote:
> So do u suggest to use stdout instead of printf?


Format message in a buffer, use write() system call to output the
message. Note that you can not safely use snprintf() for formatting the
message in a signal handler.

Sponsored Links






Free braindumps | Software forum | Database administration forum

Copyright 2003 - 2008 webservertalk.com