The kill() Function in C

The easiest way to send a signal to a process is via the kill() signal.

#include <stdio.h>
#include <stdlib.h>
#include <unistd.h>
#include <signal.h>

int main(void){

    pid_t retVal;

    retVal = fork();

    if(retVal > 0){
        int i = 0;
        while(i++ < 5){
            printf("in the parent process.\n");
            sleep(1);
        }
        //kill the child process
        kill(retVal, SIGKILL);

    } else if (retVal == 0){
        int i = 0;
        //will not ever get to 15, because
        //the parent process will kill it
        while(i++ < 15){
            printf("In the child process.\n");
            sleep(1);
        }
    } else {
        //something bad happened.
        printf("Something bad happened.");
        exit(EXIT_FAILURE);
    }

    return 0;

}

Remember, the fork function returns a process identifier that identifies the context in which the process is running.  If the process ID returned is zero, then the process is the newly created child process. If the return value is greater than zero, then that value is the ID of the child process, and we know that we are in the parent process.

If the kill() function fails, it returns -1, otherwise it returns 0. We can use the return value from the kill() function to determine if the process was successfully terminated.

#include <stdio.h>
#include <stdlib.h>
#include <unistd.h>
#include <signal.h>
#include <sys/wait.h>

int main(void){

    int status;
    pid_t killReturnValue, forkReturnValue;

    if((forkReturnValue=fork())<0){
        //error occurred.
        printf("Danger! Danger, Will Robinson!\n");
        exit(EXIT_FAILURE);
    }

    if(forkReturnValue==0){
        sleep(100);
        exit(EXIT_SUCCESS);
    } else {
        killReturnValue = kill(forkReturnValue, SIGKILL);
        if(killReturnValue){
            printf("Unable to kill child process.\n");
            waitpid(forkReturnValue, &status, 0);
        } else {
            printf("Child process killed.\n");
        }
    }
    
    return 0;
}

Although the kill() function is often used to terminate methods, the function can actually send other signals as well. For instance, we can send the SIGSTOP signal to stop a process, and the SIGCONT signal to continue a process.

#include <stdio.h>
#include <stdlib.h>
#include <unistd.h>
#include <signal.h>
#include <sys/wait.h>

int main(void){

    int status;
    pid_t killReturnValue, forkReturnValue;

    if((forkReturnValue=fork())<0){
        //error occurred.
        printf("Danger! Danger, Will Robinson!\n");
        exit(EXIT_FAILURE);
    }

    if(forkReturnValue==0){
        sleep(100);
        exit(EXIT_SUCCESS);
    } else {
        killReturnValue = kill(forkReturnValue, SIGKILL);
        if(killReturnValue){
            printf("Unable to kill child process.\n");
            waitpid(forkReturnValue, &status, 0);
        } else {
            printf("Child process killed.\n");
        }
    }
    
    return 0;
}

Next time we will cover signal handling with the signal() function.

 

 

Advertisements

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s