日期:2014-05-16  浏览次数:20711 次

用sigqueue()函数向另一个进程传递一个指针
我做了一个发送信号端,一个接收信号端,可以实现向接收端传一个整型数据,但是如果改成指针的好,就提示出现段错误,segementation fault.代码如下:
发送端:
C/C++ code
#include <signal.h>
#include <stdio.h>
#include <stdlib.h>
#include <sys/time.h>
#include <unistd.h>
#include <sys/types.h>
#include <string.h>

int main(int argc,char**argv)
{
    union sigval mysigval;
    char * pMsg;
    char msg[]="I still believe";
    strcpy(pMsg,msg);
    mysigval.sival_ptr=pMsg;
//  mysigval.sival_int=8;
    pid_t pid;    
    pid=(pid_t)atoi(argv[1]);
    if(sigqueue(pid,SIGRTMIN,mysigval)==-1)
    printf("send error\n");
    sleep(2);
    return 1;
}



信号接收端代码:
C/C++ code

#include <signal.h>
#include <stdio.h>
#include <stdlib.h>
#include <sys/types.h>
#include <unistd.h>
#include <bits/siginfo.h>
void new_op(int signum,siginfo_t *info,void *myact)
{
    printf("receive signal %d", signum);
//  char *msg=(char*)info->si_value.sival_ptr;
//  printf(" receive infomation is:%s\n",msg);
    int msg=info->si_value.sival_int;
    printf("receive information is %d",msg);
    sleep(5);
}
int main()
{    
    pid_t pid;
    pid=getpid();
    printf("my pid is %d",pid);    
    struct sigaction act;
    sigemptyset(&act.sa_mask);
    act.sa_flags=SA_SIGINFO;
    act.sa_sigaction=new_op;
    int result;
    result=sigaction(SIGRTMIN,&act,NULL);
    if(result < 0)
    {
    printf("install sigal error\n");
    }    
    while(1)
    {
    sleep(2);
    printf("wait for the signal\n");
    }
}



望各位大侠给支个招。

------解决方案--------------------
进程空间是不共享的,除非是线程
传指针不行

要不楼主使用共享内存来传递数据吧