linux发送信号函数之-alarm()

alarm函数只能发送信号给当前进程,只能发送SIGALRM信号
raise函数也是发送给自身进程,但是可以指定信号,而alarm函数只能发送SIGALRM信号
 
 
 
 
/*
 ============================================================================
 Name        : linux_c.c
 Author      : vince
 Version     :
 Copyright   : Your copyright notice
 Description : Hello World in C, Ansi-style
 ============================================================================
 */

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

void handle(){
	printf("hello\n");
}

int main(void) {
	int i;
	int time;
	signal(SIGALRM, handle);
	alarm(3);
	for (i = 1; i< 5;i++){
		printf("sleep %d .... \n", i);
		sleep(1);
	}
	alarm(3);
	sleep(2);
	time = alarm(0);
	printf("time=%d\n", time);
	for(i = 1; i< 3; i++) {
		printf("sleep %d ....\n", i);
		sleep(1);
	}
	return 0;
}

输出:

sleep 1 .... 
sleep 2 .... 
sleep 3 .... 
hello
sleep 4 .... 
time=1
sleep 1 ....
sleep 2 ....

你可能感兴趣的:(linux发送信号函数之-alarm())