signal.c++
#include <stdio.h>
#include <signal.h>
#include<unistd.h>
void handler(int sig)
{
printf("catch a sig : %d\n", sig);
}
int main()
{
while(1){
sleep(1);
printf("进程号:%d\n",getpid());
}
return 0;
}
mykill.c++
#include<iostream>
#include<string>
#include<unistd.h>
#include<signal.h>
#include<sys/types.h>
static void usage(const std::string& prog)
{
std::cerr << "Usage: " << prog << " <pid>" << std::endl;
exit(1);
}
int main(int argc, char* argv[])
{
if(argc != 3)
usage(argv[0]);
pid_t pid = std::stoi(argv[1]);
int signal = std::stoi(argv[2]);
if(pid <= 0)
usage(argv[0]);
std::cout << "kill " << pid << std::endl;
int n =kill(pid, signal);
if(n!=0)
{
std::cerr << "kill failed" << std::endl;
exit(1);
}
return 0;
}