This is the mail archive of the cygwin mailing list for the Cygwin project.


Index Nav: [Date Index] [Subject Index] [Author Index] [Thread Index]
Message Nav: [Date Prev] [Date Next] [Thread Prev] [Thread Next]
Other format: [Raw text]

Re: Problem using kill for child process


Pierre Mallard wrote:

> Maybe this is a little bit stupid from me but it
> appears I got some problem killing an innocent child
> process (I show the code below ...)
> 
> Got Cygwin from cygwin.com about 15 days ago...running
> on Windows2000

This actually has nothing to do with Cygwin, it's standard unix
programming.

> If I call from my parent process a simple kill(pid)
> then it doesn't stop the child process at all, or
> maybe after more than one minute but I don't think
> so...
> If I send from my parent process a kill(pid,x) where x
> is 1 or 9, then when I wait after in parent for child
> termination with waitfunction I have a segmentation
> fault during wait...
> PS if parent is killed with Ctrl-C, I can kill child
> then from shell with kill pid

Your problem is that you are not calling wait() and kill() correctly. 
Because you didn't include the necessary .h header files, however, the
compiler was unable to notify you of this.  This is a good example of
how enabling all compiler warnings is a good idea when you're learning,
as it would have alerted you that you are calling functions that have
not been declared, and so the compiler cannot do any argument checking
for you.  Thus if you pass too many or too few arguments, or they are of
the wrong type, you will corrupt the stack and cause havoc.

>From the manpages:

NAME
     kill -- send signal to a process

LIBRARY
     Standard C Library (libc, -lc)

SYNOPSIS
     #include <sys/types.h>
     #include <signal.h>

     int
     kill(pid_t pid, int sig);


NAME
     wait, waitpid, wait4, wait3 -- wait for process termination

LIBRARY
     Standard C Library (libc, -lc)

SYNOPSIS
     #include <sys/types.h>
     #include <sys/wait.h>

     pid_t
     wait(int *status);


So, first of all you should include signal.h, sys/wait.h, and
sys/types.h.  Note that you used a backslash in your include, and this
is probably not a good habit to get into, especially if you're doing
unix/posix programming.

Secondly, calling kill() with a single argument is undefined.  You must
call it as it's declared.  And for the signal you shouldn't use a bare
integer.  The signal.h file defines these for you, and so you should
always use the symbolic value SIGKILL (or SIGINT, SIGUSR1, whatever.)
and not numbers.

Finally, wait() takes a pointer to an int which is filled in with the
status value.  It is undefined to call wait() without arguments.  This
can result in a segmentation violation, as you saw.

If you had included the proper header files the compiler would have
generated an error when you tried to call kill() with a single argument
or wait() with no arguments.  The use of these functions is not up for
interpretation, they must be used with the precise arguments with which
they are declared.  C is not a language that lets you leave off
arguments at will.

If you make the above changes your program works as expected.

Note: Cygwin's C library does not contain complete documentation for
every function, so if you try to do a "man 2 wait" or "man 2 kill", you
will be disappointed.  Cygwin's libc does have summary documentation in
texinfo format, so if you do "info libc" you will be able to at least
see summary info for the functions.

Additionally, since these are standard unix libc functions you can use
other libc references to help you.  If you go to gnu.org and select the
glibc documentation, you will find a wealth of info.  Likewise
freebsd.org has extensive manpages, which include their standard C
library.  Sun has manpages for Solaris at docs.sun.com (look for
"Reference Manual Collection".)  Just make sure you realize that
Cygwin's libc != GNU libc != FreeBSD libc != Solaris libc.  However, for
functions such as these that are defined in POSIX, it's safe to assume
that most C libraries should implement them in the same way.

Brian

--
Unsubscribe info:      http://cygwin.com/ml/#unsubscribe-simple
Problem reports:       http://cygwin.com/problems.html
Documentation:         http://cygwin.com/docs.html
FAQ:                   http://cygwin.com/faq/


Index Nav: [Date Index] [Subject Index] [Author Index] [Thread Index]
Message Nav: [Date Prev] [Date Next] [Thread Prev] [Thread Next]