段错误 (核心已转储)

gzj_1101 2015-12-08 04:36:29
在Ubuntu上面进行线程的同步和互斥操作时出现这种错误,但是在red hat上面却没有问题。
调试的时候出现的错误信息

root@ubuntu:/C_Language/实验四# gdb ./A core
GNU gdb (Ubuntu 7.7.1-0ubuntu5~14.04.2) 7.7.1
Copyright (C) 2014 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 "x86_64-linux-gnu".
Type "show configuration" for configuration details.
For bug reporting instructions, please see:
<http://www.gnu.org/software/gdb/bugs/>.
Find the GDB manual and other documentation resources online at:
<http://www.gnu.org/software/gdb/documentation/>.
For help, type "help".
Type "apropos word" to search for commands related to "word"...
Reading symbols from ./A...done.
/C_Language/实验四/core: 没有那个文件或目录.
(gdb) run
Starting program: /C_Language/实验四/A

Program received signal SIGSEGV, Segmentation fault.
0x000000000040097b in main () at A.c:37


代码

#include<stdio.h>
#include<stdlib.h>
#include<unistd.h>
#include<sys/types.h>
#include<linux/sem.h>
#include<time.h>
#include<errno.h>

#define MAXSHM 10

int SA;
int SO;
int mutex;

int main()
{
struct sembuf P,V;
union semun arg;
int dishid;
int sumid;
int t;

char *viraddr;
int *sum;
int *get;

int father,son,daughter,mother;


//创建共享内存
dishid=shmget(IPC_PRIVATE,sizeof(char)*MAXSHM,IPC_CREAT|0666);
viraddr=(char*)shmat(dishid,0,0);
sumid=shmget(IPC_PRIVATE,sizeof(int),IPC_CREAT|0666);
t=shmget(IPC_PRIVATE,sizeof(int),IPC_CREAT|0666);

sum=(int*)shmat(sumid,0,0);
*sum=0;

get=(int*)shmat(t,0,0);
*get=10;

//创建信号量并初始化
SA=semget(IPC_PRIVATE,1,IPC_CREAT|0666);
SO=semget(IPC_PRIVATE,1,IPC_CREAT|0666);
mutex=semget(IPC_PRIVATE,1,IPC_CREAT|0666);

//初始化信号量
arg.val=1;
if(semctl(mutex,0,SETVAL,arg)==-1)
perror("semctl setval error 1");
arg.val=0;
if(semctl(SA,0,SETVAL,arg)==-1)
perror("semctl setval error 2");
arg.val=0;
if(semctl(SO,0,SETVAL,arg)==-1)
perror("semctl setval error 3");

//初始化PV操作
P.sem_num=0;
P.sem_op=-1;
P.sem_flg=SEM_UNDO;
V.sem_num=0;
V.sem_op=1;
V.sem_flg=SEM_UNDO;
//创建线程
while((father=fork())==-1);
if(father==0)
{
while(*sum<10)
{
semop(mutex,&P,1);
printf("the father is putting an apple\n");
strcpy(viraddr,"apple");
(*sum)++;
semop(SA,&V,1);
sleep(2);
}
}
else
{
while((mother=fork())==-1);
if(mother==0)
{
while(*sum<10)
{
semop(mutex,&P,1);
printf("the mother is putting an orange");
strcpy(viraddr,"orange");
(*sum)++;
semop(SO,&P,1);
sleep(2);
}

}
else
{
while((daughter=fork())==-1);
if(daughter==0)
{
while(*get>0)
{
semop(SO,&P,1);
printf("daughter is eating orange\n");
strcpy(viraddr,"");
(*get)--;
semop(mutex,&V,1);
sleep(1);
}

}
else
{
while((son=fork())==-1);
if(son==0)
{
while(*get>0)
{
semop(SA,&P,1);
printf("son is eating an orange\n");
(*get)--;
strcpy(viraddr,"");
semop(mutex,&V,1);
sleep(1);
}
}
}
}
}



//回收子线程
wait(0);
wait(0);
wait(0);
wait(0);
//撤销共享内存
shmdt(viraddr);
shmctl(dishid,IPC_RMID,0);

//撤销三个信号集
semctl(SA,IPC_RMID,0);
semctl(SO,IPC_RMID,0);
semctl(mutex,IPC_RMID,0);
exit(0);

}
...全文
1715 8 打赏 收藏 转发到动态 举报
写回复
用AI写文章
8 条回复
切换为时间正序
请发表友善的回复…
发表回复
justkk 2015-12-28
  • 打赏
  • 举报
回复
没怎么看你的程序要做什么,但是我试了一下确实core掉了。 修改了一点之后,倒是不再core了。 1. 添加两个头文件,编译时不再警告 #include<string.h> #include<sys/shm.h> 2. 修改一个头文件,linux目录下的头文件不是给一般应用开发使用的 #include <linux/sem.h> 改为 #include <sys/sem.h> 3. 添加一个联合定义 typedef union u_semun { int val; struct semid_ds *buf; unsigned short *array; } USEMUN; 4. 修改你程序中使用的类型 union u_semun arg;
常书 2015-12-17
  • 打赏
  • 举报
回复
引用 5 楼 chj4129 的回复:
1.从gdb输出的调试信息来看,Segmentation fault的地址是0x000000000040097b,这是程序代码段的地址,现在只有一种可能就是函数shmat出错了。 2.建议检查函数shmat的返回值,需要注意的是,shmat出错时返回(void *)-1
+1 估计使用指针出问题了,打印下sum看值是多少
keer_zu 2015-12-17
  • 打赏
  • 举报
回复
Program received signal SIGSEGV, Segmentation fault. 0x0000000000400952 in main () at sem_test.c:38 38 *sum=0; Missing separate debuginfos, use: debuginfo-install glibc-2.12-1.132.el6.x86_64 看看这个博客是否有帮助: http://blog.csdn.net/testcs_dn/article/details/19565411
一根烂笔头 2015-12-09
  • 打赏
  • 举报
回复
使用-g编译程序,段错误后,使用gdb查看转储文件,确定出错点
云霏阳 2015-12-09
  • 打赏
  • 举报
回复
1.从gdb输出的调试信息来看,Segmentation fault的地址是0x000000000040097b,这是程序代码段的地址,现在只有一种可能就是函数shmat出错了。 2.建议检查函数shmat的返回值,需要注意的是,shmat出错时返回(void *)-1
nswcfd 2015-12-08
  • 打赏
  • 举报
回复
最大的可能性是attach失败,检查attach的返回值吧。
赵4老师 2015-12-08
  • 打赏
  • 举报
回复 1
进程意外退出会在当前目录下产生‘core’文件或形如‘core.数字’的文件比如‘core.1234’ 使用命令 gdb 运行程序名 core或core.数字 进入gdb然后使用bt命令 可以查看进程意外退出前函数调用的堆栈,内容为从上到下列出对应从里层到外层的函数调用历史。 如果进程意外退出不产生core文件,参考“ulimit -c core文件最大块大小”命令
gzj_1101 2015-12-08
  • 打赏
  • 举报
回复
不知道提示的37行到底出现了什么问题

23,121

社区成员

发帖
与我相关
我的任务
社区描述
Linux/Unix社区 应用程序开发区
社区管理员
  • 应用程序开发区社区
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告
暂无公告

试试用AI创作助手写篇文章吧