Private archive copy · Unofficial site · Content copyright belongs to Runoob Tutorial and the original authors · View Original

C library function - kill()

C library function - kill()

C Standard Library - <signal.h> C Standard Library -

Description

kill The function is one of the functions in the C standard library, used to send a signal to a specified process or process group. It is defined in the header file, and allows a program to send signals to other processes to affect their behavior.

Declaration

Here is the declaration of the kill() function.


int kill(pid_t pid, int sig);

Parameters

- If `pid > 0`, the signal `sig` will be sent to the process whose process ID equals `pid`. - If `pid == 0`, the signal `sig` will be sent to all processes in the same process group as the calling process. - If `pid < -1`, the signal `sig` will be sent to all processes whose process group ID equals `pid`. - If `pid == -1`, the signal `sig` will be sent to all processes for which the caller has permission to send signals (except the process with process ID 1).

Return Value

Example

The following is a simple example program demonstrating how to use the kill function to send a SIGUSR1 signal to another process:

Example

#include

#include

#include

int

main

(

)

{

    pid_t pid

=

getpid

(

)

;

// Get the process ID of the current process

   

// Send the SIGUSR1 signal to the current process

   

if

(

kill

(

pid

,

SIGUSR1

)

==

1

)

{

       

perror

(

"kill"

)

;

       

return

1

;

   

}

   

printf

(

"SIGUSR1 signal sent to process %d.\n"

,

(

int

)

pid

)

;

   

return

0

;

}

After compiling and running the program, the output will be:


SIGUSR1 signal sent to process [进程ID].

C Standard Library - <signal.h> C Standard Library -