if ( fork() == 0 )
{
union sigval sval;
char *data = new char[10];
memset(data,0,sizeof(data));
for(int i=0;i < 5;i++)
data[i]='2';
//sval.sival_int = 5;
sval.sival_ptr = data;
sigqueue(getppid(), 35, sval);
}
向父进程发送信号,父进程确实收到了信号,但是打印出来的siginfo_t.si_ptr确是空的,如果使用sival_int的话确又能正常显示,为什么会这样呢?sa_flags已经设置为SA_SIGINFO.
C/C++ code[liangdong@bb-browser-test00.vm.baidu.com c_project]$ ./main
hello world
[liangdong@bb-browser-test00.vm.baidu.com c_project]$ cat main.c
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <signal.h>
#include <unistd.h>
#include <sys/types.h>
#include <errno.h>
void sig_action(int signo, siginfo_t *info, void *addr) {
printf("%s\n", info->si_value.sival_ptr);
}
#if 0
union sigval {
int sival_int;
void *sival_ptr;
};
#endif
int main(int argc, char* const argv[]) {
struct sigaction act;
act.sa_sigaction = sig_action;
act.sa_flags = SA_SIGINFO;
sigaction(SIGUSR1, &act, NULL);
pid_t pid;
if ((pid = fork()) == -1) {
return 1;
} else if (pid == 0) {
union sigval val;
val.sival_ptr = "hello world";
sigqueue(getppid(), SIGUSR1, val);
}
while (((pid = wait()) == -1 && errno == EINTR) || pid > 0) {
}
return 0;}