GIDForums  

Go Back   GIDForums > Computer Programming Forums > C Programming Language
User Name
Password
Register FAQ Members List Calendar Search Today's Posts Mark Forums Read

 
 
Thread Tools Search this Thread Rate Thread
  #1  
Old 16-Jul-2004, 08:34
blee3 blee3 is offline
New Member
 
Join Date: Apr 2004
Posts: 3
blee3 is on a distinguished road

newbie question about kill


I have heard that killing a process is bad because it does not allow the process to clean up. I do not know what this means.

Does it mean that the memory allocated by the process is not released by the OS causing a huge memory leak? Or does it just mean that the process cannot catch the kill signal and therefore cannot do things like saving your work?

What exactly happens when a process is killed?
  #2  
Old 16-Jul-2004, 14:47
dsmith's Avatar
dsmith dsmith is offline
Senior Member
 
Join Date: Jan 2004
Location: Utah, USA
Posts: 1,351
dsmith is a glorious beacon of lightdsmith is a glorious beacon of lightdsmith is a glorious beacon of lightdsmith is a glorious beacon of lightdsmith is a glorious beacon of light
Quote:
Originally Posted by blee3
I have heard that killing a process is bad because it does not allow the process to clean up. I do not know what this means.

Does it mean that the memory allocated by the process is not released by the OS causing a huge memory leak? Or does it just mean that the process cannot catch the kill signal and therefore cannot do things like saving your work?

What exactly happens when a process is killed?

I am not up to speed on exactly how certain O/S'es may execute a process. But I believe that most O/S'es will run each process in its own seperate memory space but allocate data memory for the process from the heap. So, I do believe that there may be a chance of data memory not being "freed" when a process is abnormally terminated. Others may be able to give more detailed advice about this.

Worse yet, is that the kill signal will just try to end the process and will not allow any saving of data, etc or allow the process to save data to a temporary file, etc. To me this is the true meaning of not "cleaning up."

I can handle a reboot to reclaim stranded memory, but loosing work is just frustrating.

BTW - what O/S are you talking about. The term kill tends me to believe that this is a *nix type system?
  #3  
Old 16-Jul-2004, 16:01
blee3 blee3 is offline
New Member
 
Join Date: Apr 2004
Posts: 3
blee3 is on a distinguished road
I am writing a program with a multiplatform toolkit (Qt) and did not realize that different OS's might kill differently. I'm targeting Linux and Windows.

The toolkit allows you to start and kill processes. The kill function in Unix sends the SIGKILL signal, and I think in Windows it is implemented similarly.
  #4  
Old 21-Jul-2004, 04:24
Garth Farley Garth Farley is offline
Invalid Email Address
 
Join Date: May 2002
Location: Ireland
Posts: 638
Garth Farley is a jewel in the roughGarth Farley is a jewel in the roughGarth Farley is a jewel in the rough
Well killing (-9) a process is usually a last resort in terminating a program, it shouldn't be used regularily. But there are many different severities of killing, from a meek SIGINT (interrupt: kill -2) or a polite SIGTERM (terminate: kill -15) to a violent SIGKILL (kill -9). See all that Unix supports by typing kill -l.

SIGTERM can be caught by your program, and thus can call a routine to tidy up, i.e. deallocate memory, close connections, pipes & files etc. Signals can be caught by the signal function:
void *signal( int signo, void *func )
where func is a pointer to a function. Eg:

CPP / C++ / C Code:
#include <stdio.h>
#include <signal.h>
 
void sigproc(void);      //catch function
void quitproc(void);    //die gracefully fn
 
main(){
     signal(SIGINT, sigproc);   //set handler for interrupt
     signal(SIGTERM, quitproc); //terminate

     //whatever code, have a infinite loop to see how handy this is

}

/* Handler for interrupt signal */
void sigproc(){ 
    printf("you have pressed ctrl-c\n");
}
 
/* Handler for terminate signal */
void quitproc(){
    printf("ctrl-z pressed to quit\n");
    exit(0);
}

SIGKILL and SIGQUIT cannot be caught.

Windows I don't know about, I don't code in it really. But if your compiler is ANSI compliant, it should be identical to Unix.

Note, some OS's reset the signal handler once it's been used once. So for instance pressing ^C once for the above will work, but again will do the default OS thing, usually stopping it. So to prevent this, you should redefine signal() inside the signal handler!

GF
  #5  
Old 25-Jul-2004, 08:06
dsmith's Avatar
dsmith dsmith is offline
Senior Member
 
Join Date: Jan 2004
Location: Utah, USA
Posts: 1,351
dsmith is a glorious beacon of lightdsmith is a glorious beacon of lightdsmith is a glorious beacon of lightdsmith is a glorious beacon of lightdsmith is a glorious beacon of light
Thanks for this Garth. This is very useful info. FYI, on the linux implementation, the signal interrupt function wants a int parameter which is the signal number.

I was wondering if anyone has any experience in signal handling with threads? My threaded program doesn't seem to like interupt handling routines. The threads are getting abandoned and/or interrupt routines are getting called multiple times.

Is the sigaction function better to use than signal in this case?
 
 

Recent GIDBlogWriting a book by crystalattice

Thread Tools Search this Thread
Search this Thread:

Advanced Search
Rate This Thread
Rate This Thread:

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

vB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Forum Jump

Similar Threads
Thread Thread Starter Forum Replies Last Post
Grouping data from MySQL with PHP - Newbie question. giobbi MySQL / PHP Forum 12 27-Feb-2004 00:34
question of practice magiccreative C++ Forum 1 06-Feb-2004 07:17
Problem with my Geforce video card Shivs Computer Hardware Forum 2 30-Jan-2004 20:54
a C input question tmike C Programming Language 1 16-Sep-2003 02:31
Code problem (a newbie question) monkster87 C++ Forum 3 11-Aug-2003 12:46

Network Sites: GIDNetwork · GIDWebHosts · GIDSearch · Learning Journal by J de Silva, The

All times are GMT -6. The time now is 06:54.


vBulletin, Copyright © 2000 - 2008, Jelsoft Enterprises Ltd.