signal
#include
typedef void (*sighandler_t)(int);
sighandler_t signal(int signum,sighandler_t handler);
例子
#include
#include
#include
void sigint(int signum)
{
printf("收到SIGINT信号!!!\n");
}
int main()
{
if(signal(SIGINT,sigint) == SIG_ERR)
{
printf("signal!!!\n");
return -1;
}
return 0;
}
kill
#include
int kill (pid_t pid, int sig);
例子
#include
#include
#include
#include
#include
int main(int argc,char *argv[]){
if(argc < 3){
printf("%s sig pid\n",argv[0]);
return -1;
}
int sig = atoi(argv[1]);
pid_t pid = atoi(argv[2]);
int ret = kill(pid,sig);
if(ret == -1){
if(sig == 0 && errno == ESRCH){
printf("%d进程不存在!\n",pid);
}
perror("kill");
return -1;
}
return 0;
}
raise
#include
int raise (int sig);
例子
#include
#include
#include
#include
void sigint(int signum)
{
printf("%u进程收到SIGINT信号.\n",getpid());
}
int main()
{
if(signal(SIGINT,sigint) == SIG_ERR)
{
printf("signal err!!\n");
return -1;
}
raise(SIGINT);
return 0;
}
pause
#include
int pause (void);
例子
#include
#include
#include
#include
#include
void sigint(int signum)
{
printf("%u进程收到SIGINT信号.\n",getpid());
}
int main()
{
pid_t pid = fork();
if(pid == -1)
{
printf("fork err!\n");
return -1;
}
if(pid ==0)
{
if(signal(SIGINT,sigint) == SIG_ERR)
{
printf("signal err!!\n");
return -1;
}
pause();
printf("收到信号,苏醒...\n");
printf("准备退出");
return 0;
}
sleep(3);
kill(pid,SIGINT);
wait(NULL);
return 0;
}
alarm
#include
unsigned int alarm (unsigned int seconds);
例子
#include
#include
#include
#include
#include
void sigalarm(int sig){
time_t t = time(NULL);
struct tm *lt = localtime(&t);
printf(" %2d:%2d:%2d \n",lt->tm_hour,lt->tm_min,lt->tm_sec);
alarm(1);
}
int main(){
if(signal(SIGALRM,sigalarm)==SIG_ERR)
{
perror("signal");
return -1;
}
alarm(3);
int ret = alarm(1);
printf("%d \n",ret);
for(;;);
return 0;
}