LinuxSir.cn,穿越时空的Linuxsir!

 找回密码
 注册
搜索
热搜: shell linux mysql
查看: 818|回复: 1

求助:调用fcntl记录锁怎么会出现SIGSEGV错误?(付代码)

[复制链接]
发表于 2006-6-4 16:00:25 | 显示全部楼层 |阅读模式
调试信息:
Starting program: /root/a
fcntl=0

Program received signal SIGSEGV, Segmentation fault.
0x009778a6 in strcpy () from /lib/tls/libc.so.6

这部分的源程序:
struct mymesg{
char presentnum[5];
char ordernum[5];
}

/*要求和释放一把锁*/
lock_reg(int sfd_lock,int cmd,int type,off_t offset,int whence,off_t len)
{
int i=-1;
struct flock lock;
lock.l_type=type;                /*F_RDLCK,F_WRLCK,F_UNLCK*/
lock.l_start=offset;                        /*锁的起始地址*/
lock.l_whence=whence;                        /*锁的中止地址*/
lock.l_len=len;                                        /*区域的长度*/
while(i==-1)                                        /*要求和释放一把锁*/
  {i=fcntl(sfd_lock,cmd,&lock);printf("fcntl=%d\n",i);}
}

/*写锁路径名为pathname的共享文件并打开其输入输出流*/
olsh(int sfd,FILE *sd)
{
/*打开名为pathname的共享文件的标准输入输出流*/
if((sd=fopen(pathname,"r+"))==NULL)
        report_error("fopen");
lock_reg(sfd,F_SETLKW,F_WRLCK,0,SEEK_SET,0);/*锁住整个共享文件*/
}
/*关闭已打开的文件指针为sd的共享文件的输入输出流并对其进行解锁*/
cush(int sfd,FILE *sd)
{
  if(fclose(sd)==EOF)
report_error("fclose");
lock_reg(sfd,F_SETLK,F_UNLCK,0,SEEK_SET,0);
}

/*error*/
report_error(char *s)                    
{
printf("receiver:error in %s,errno=%d\n",s,errno);
perror("error type");
exit(1);
}

main()
{
pid_t pid;                                        /*进程号*/
int sh_fd;                                        /*共享文件描述符*/
FILE *sd;
struct mymesg *ini;
int sockfd,newfd;                                /*套接字描述符*/
struct sockaddr_in my_addr,their_addr;        /*套接字的数据结构*/
int sin_size;
volatile int true=1;
sh_fd=open(pathname,O_RDWR|O_CREAT|O_EXCL,S_IRWXU);/*创建共享文件*/
if(sh_fd==-1)
        report_error("open");
olsh(sh_fd,sd);                                /*写锁路径名为pathname的共享文件并打开其输入输出流*/
printf("olsh success.../n");
/*initialize the share memory*/
strcpy((*ini).ordernum,"");
strcpy((*ini).presentnum,"");
printf("ini success.../n");
if(fwrite(ini,sizeof(struct mymesg),1,sd)!=sizeof(struct mymesg))/*初始化共享文件中的数据*/
   report_error("fwrite");
/*关闭已打开的文件指针为sd的共享文件的输入输出流并对其进行解锁*/
cush(sh_fd,sd);
发表于 2006-6-4 20:53:12 | 显示全部楼层
好像你的strcpy中ini还没有初始化阿
回复 支持 反对

使用道具 举报

您需要登录后才可以回帖 登录 | 注册

本版积分规则

快速回复 返回顶部 返回列表