这个段错误,如何解决?

软件和网站开发以及相关技术探讨
回复
kpshare
帖子: 3
注册时间: 2008-11-15 18:34
送出感谢: 0
接收感谢: 0

这个段错误,如何解决?

#1

帖子 kpshare » 2009-02-24 19:21

本人照着书(书名为<linux c编程>)输入代码,编译后运行,出现错误提示为:段错误
然后用这种方法找错:
root@ubuntu:~/lc/第9章# gcc -g -rdynamic 实例9-9.c
root@ubuntu:~/lc/第9章# gdb ./a.out
GNU gdb 6.8-debian
Copyright (C) 2008 Free Software Foundation, Inc.
License GPLv3+: GNU GPL version 3 or later <http://gnu.org/licenses/gpl.html>
This is free software: you are free to change and redistribute it.
There is NO WARRANTY, to the extent permitted by law. Type "show copying"
and "show warranty" for details.
This GDB was configured as "i486-linux-gnu"...
(gdb) r
Starting program: /root/lc/第9章/a.out

Program received signal SIGSEGV, Segmentation fault.
0xb7eb3608 in ?? () from /lib/tls/i686/cmov/libc.so.6



请问如何解决啊。程序的功能向进程本身发送信号,并传递指针参数。代码如下:

#include <signal.h>
#include <sys/types.h>
#include <unistd.h>
#include <string.h>
#include <stdio.h>
#include <stdlib.h>
void new_op(int,siginfo_t*,void*);
int main(int argc,char** argv)
{
struct sigaction act;
union sigval mysigval;
int i;
int sig;
pid_t pid;
char data[10];
memset(data,0,sizeof(data));
for(i=0;i < 5;i++)
data='2';
mysigval.sival_ptr=data;

sig=atoi(argv[1]);
pid=getpid();

sigemptyset(&act.sa_mask);
act.sa_sigaction=new_op;/*三参数信号处理函数*/
act.sa_flags=SA_SIGINFO;/*信息传递开关*/
if(sigaction(sig,&act,NULL) < 0)
{
printf("install sigal error\n");
}
while(1)
{
sleep(2);
printf("wait for the signal\n");
sigqueue(pid,sig,mysigval);/*向本进程发送信号,并传递附加信息*/
}

}

void new_op(int signum,siginfo_t *info,void *myact)/*三参数信号处理函数的实现*/
{
int i;
for(i=0;i<10;i++)
{
printf("%d\n ", i);
}
printf("handle signal %d over;",signum);
}
回复

回到 “软件/网站开发”