Mailing-List: contact cygwin-help AT cygwin DOT com; run by ezmlm List-Subscribe: List-Archive: List-Post: List-Help: , Sender: cygwin-owner AT cygwin DOT com Mail-Followup-To: cygwin AT cygwin DOT com Delivered-To: mailing list cygwin AT cygwin DOT com Message-ID: <40089DA3.DC889E8A@dessent.net> Date: Fri, 16 Jan 2004 18:27:47 -0800 From: Brian Dessent Organization: My own little world... MIME-Version: 1.0 To: cygwin AT cygwin DOT com Subject: Re: Problem using kill for child process References: <20040117011732 DOT 53374 DOT qmail AT web20305 DOT mail DOT yahoo DOT com> Content-Type: text/plain; charset=us-ascii Content-Transfer-Encoding: 7bit X-AntiAbuse: This header was added to track abuse, please include it with any abuse report X-AntiAbuse: Primary Hostname - host.linuxsv3.net X-AntiAbuse: Original Domain - cygwin.com X-AntiAbuse: Originator/Caller UID/GID - [47 12] / [47 12] X-AntiAbuse: Sender Address Domain - dessent.net X-IsSubscribed: yes Reply-To: cygwin AT cygwin DOT com 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 #include int kill(pid_t pid, int sig); NAME wait, waitpid, wait4, wait3 -- wait for process termination LIBRARY Standard C Library (libc, -lc) SYNOPSIS #include #include 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/