信号signal ---带数据的信号的发送及安装

signalsend.c

#include
#include
#include
#include
#include
#include

int main(int argc, char **argv){
	
	//第一个参数指定接收信号的ID
	pid_t pid; 
	//第二个参数确定即将发送的信号
	int signum; 
	//第三个参数指定了信号传递的参数
	union sigval my_sigval; 
	
	pid = atoi(argv[1]);
	signum = SIGINT;
	my_sigval.sival_int = 100;
	
	
	if(sigqueue(pid, signum, my_sigval) == -1) {
		printf("send error!\n");
		exit(0);
	}
	else{
		printf("send success!\n");
	}
	
	return 0;
}

signalreceive.c

#include
#include
#include
#include
#include

void my_handler(int, siginfo_t*, void*);

int main(int argc, char **argv){
	
	struct sigaction act;
	
	sigemptyset(&act.sa_mask); //初始化信号集合为空
	act.sa_sigaction = my_handler;
	act.sa_flags = SA_SIGINFO;
	
	if((sigaction(SIGINT, &act, NULL)) < 0) {
		printf("install signal error!\n");
		exit(0);
	}
	
	printf("wait for the signal...\n");
	while(1) {
		sleep(2);	
	}

	return 0;
}

void my_handler(int signum, siginfo_t* info, void *myact){
	
	printf("the int value is %d \n", info->si_int);
}

 

你可能感兴趣的:(进程之间的通信)