LinuxQuestions.org

LinuxQuestions.org (/questions/)
-   Programming (http://www.linuxquestions.org/questions/programming-9/)
-   -   what does kill -9 and killall -9 do and don't? (http://www.linuxquestions.org/questions/programming-9/what-does-kill-9-and-killall-9-do-and-dont-4175451693/)

fantasy1215 02-25-2013 08:31 PM

what does kill -9 and killall -9 do and don't?
 
Code:

static sem_t s_threadsem;
int main(void)
{
        //initialize resources
        #define NONE_PROCESS_SHARE 0
        const unsigned int THREADCNT = 2;
        if( 0 != sem_init(&s_threadsem, NONE_PROCESS_SHARE, THREADCNT) )
        {
                cout<<"sem_init fail" <<endl;
                exit(-1);
        }
       
       
        while(1)
        {
                dowork();
                sleep(2);
        }
       
        //destroy resources
        sem_destroy(&s_threadsem);
        return 0;
}

The code above do a infinite loop (like a server), If I use kill -9 or killall -9 to kill this process, Will the linux operating system reclaim the sem_t resource correctly? And what else kind resources would linux operating system not reclaim correctly(that we should take care)?
I wonder what mechanism do you guru use to stop and restart your process while not using kill -9?
Thanks in advance!

pan64 02-26-2013 03:23 AM

kill -9 will not allow to cleanup, so you should not use it. You should implement your own signal handler (sigint, siguser1, whatever) to stop your server and properly clean up.

NevemTeve 02-26-2013 04:57 AM

I don't know what happens with posix semaphores when the process ends; SystemV semaphores on the other hand can be used with option SEM_UNDO, meaning: undo the changes when the process terminates (see man 2 semop)


All times are GMT -5. The time now is 08:03 PM.