23,121
社区成员
发帖
与我相关
我的任务
分享
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);
}