求助!linux环境下多进程的编程

wanggang198022 2003-11-01 10:20:02
我在red hat9。0的环境下编了一个共享内存的多进程的互斥,编译通过了,但是执行的时
候说段错误。估计可能内存那块出了问题,但是不知道怎么解决
...全文
22 8 打赏 收藏 转发到动态 举报
写回复
用AI写文章
8 条回复
切换为时间正序
请发表友善的回复…
发表回复
wanggang198022 2003-11-05
  • 打赏
  • 举报
回复
谢谢各位问题解决!忘了一个和共享内存断开映射了用shmat()
#include "unistd.h"
#include "sys/types.h"
#include "sys/wait.h"
#include "sys/shm.h"
#include "sys/ipc.h"
#include "sys/sem.h"
#include "stdio.h"
#include "stdlib.h"


#define PERM IPC_CREAT|0777


int main()
{
int semfull;
int semempty;
int semmutexp;
int semmutexc;
int shmid;
struct sembuf semwait;
struct sembuf semsignal;

semwait.sem_num=0;
semwait.sem_op=-1;
semwait.sem_flg=0;
semsignal.sem_num=0;
semsignal.sem_op=1;
semsignal.sem_flg=0;
if((shmid=shmget(IPC_PRIVATE,sizeof(int)*13,PERM))==-1)
{
printf("Create Share Memory Error.");
exit(1);
}


semfull=semget(IPC_PRIVATE,1,PERM);
int j;
for(j=0;j<10;j++)
{
semop(semfull,&semsignal,1);
}
semempty=semget(IPC_PRIVATE,1,PERM);
semmutexp=semget(IPC_PRIVATE,1,PERM);
semop(semmutexp,&semsignal,1);
semmutexc=semget(IPC_PRIVATE,1,PERM);
semop(semmutexc,&semsignal,1);
int child1,child2,child3,child4;
int *shm_addrp1,*shm_addrp2,*shm_addrc1,*shm_addrc2,*shm_main;
shm_main=(int *)shmat(shmid,0,0);
shm_main[12]=0;


if((child1=fork())==0)
{

shm_addrp1=(int*)shmat(shmid,0,0);
shm_addrp1[10]=0;
int i;
for(i=1;i<=1000;i++)
{
semop(semfull,&semwait,1);
semop(semmutexp,&semwait,1);
printf("%d : Produce %d into shm_addr[%d].\n",getpid(),i,shm_addrp1[10]);
shm_addrp1[shm_addrp1[10]]=i;
shm_addrp1[10]=(shm_addrp1[10]+1)%10;
semop(semempty,&semsignal,1);
semop(semmutexp,&semsignal,1);
}
shmdt(shm_addrp1);
exit(0);
}
else
{
if((child2=fork())==0)
{

shm_addrc1=(int*)shmat(shmid,0,0);
shm_addrc1[11]=0;
int i;
for(i=1;i<=1000;i++)
{
semop(semempty,&semwait,1);
semop(semmutexc,&semwait,1);
shm_addrc1[12]+=shm_addrc1[shm_addrc1[11]];
printf("%d : Consumer %d from shm_addr[%d],sum=%d\n",getpid(),shm_addrc1[shm_addrc1[11]],shm_addrc1[11],shm_addrc1[12]);
shm_addrc1[11]=(shm_addrc1[11]+1)%10;
semop(semfull,&semsignal,1);
semop(semmutexc,&semsignal,1);
}
shmdt(shm_addrc1);
exit(0);
}
else
{
if((child3=fork())==0)
{

shm_addrc2=(int*)shmat(shmid,0,0);
shm_addrc2[11]=0;
int i;
for(i=1;i<=1000;i++)
{
semop(semempty,&semwait,1);
semop(semmutexc,&semwait,1);
shm_addrc2[12]+=shm_addrc2[shm_addrc2[11]];
printf("%d : Consumer %d from shm_addr[%d],sum=%d\n",getpid(),shm_addrc2[shm_addrc2[11]],shm_addrc2[11],shm_addrc2[12]);
shm_addrc2[11]=(shm_addrc2[11]+1)%10;
semop(semfull,&semsignal,1);
semop(semmutexc,&semsignal,1);
}
shmdt(shm_addrc2);
exit(0);
}
else
{
if((child4=fork())==0)
{

shm_addrp2=(int*)shmat(shmid,0,0);
shm_addrp2[10]=0;
int i;
for( i=1;i<=1000;i++)
{
semop(semfull,&semwait,1);
semop(semmutexp,&semwait,1);
printf("%d : Produce %d into shm_addr[%d].\n",getpid(),i,shm_addrp2[10]);
shm_addrp2[shm_addrp2[10]]=i;
shm_addrp2[10]=(shm_addrp2[10]+1)%10;
semop(semempty,&semsignal,1);
semop(semmutexp,&semsignal,1);
}
shmdt(shm_addrp2);
exit(0);

}
else
{
waitpid(child1,NULL,0);
waitpid(child2,NULL,0);
waitpid(child3,NULL,0);
waitpid(child4,NULL,0);
semctl(semfull,2,IPC_RMID,0);
semctl(semempty,2,IPC_RMID,0);
semctl(semmutexp,2,IPC_RMID,0);
semctl(semmutexc,2,IPC_RMID,0);
shmdt(shm_main);
shmctl(shmid,IPC_RMID,0);
return 0;
}

}
}

}
}
linaxing 2003-11-03
  • 打赏
  • 举报
回复
编译的时候别忘了带-g选项。
Benni 2003-11-03
  • 打赏
  • 举报
回复
直接安装完Redhat Linux 9之后的gcc/gdb是有问题的, 你先下载gcc3.3.1和gdb6.0,安装一下再说.
另外我个人认为Redhat Linux 7.3不错,我在上面写了大量程序也没有发现gcc/gdb的问题,转到9.0就出错,也郁闷了一段时间!后来升级gcc/gdb之后就好了.
bigbison 2003-11-01
  • 打赏
  • 举报
回复
设置环境变量 ulimit -c unlimited
执行你的程序,断错误会生成core.xxx文件,
用GDB调试,gdb yourprogram core.xxx
执行bt命令,看到调用过程,找到出问题的地方,
修改程序,或再用gdb 调试。
liupengfei81 2003-11-01
  • 打赏
  • 举报
回复
用gdb调试看看
ajiefudan 2003-11-01
  • 打赏
  • 举报
回复
程序目的干什么的?
一般来说,你的程序中
的顺序应该这样,
lock empty
lock mutex

lock mutex
lock store


wanggang198022 2003-11-01
  • 打赏
  • 举报
回复
#include "unistd.h"
#include "sys/types.h"
#include "sys/wait.h"
#include "sys/shm.h"
#include "sys/ipc.h"
#include "sys/sem.h"
#include "stdio.h"
#include "stdlib.h"


#define PERM IPC_CREAT|0777


int main()
{
int semfull;
int semempty;
int semmutexp;
int semmutexc;
int shmid;
struct sembuf semwait;
struct sembuf semsignal;

semwait.sem_num=0;
semwait.sem_op=-1;
semwait.sem_flg=0;
semsignal.sem_num=0;
semsignal.sem_op=1;
semsignal.sem_flg=0;
if((shmid=shmget(IPC_PRIVATE,sizeof(int)*13,PERM))==-1)
{
printf("Create Share Memory Error.");
exit(1);
}


semfull=semget(IPC_PRIVATE,1,PERM);
int j;
for(j=0;j<10;j++)
{
semop(semfull,&semsignal,1);
}
semempty=semget(IPC_PRIVATE,1,PERM);
semmutexp=semget(IPC_PRIVATE,1,PERM);
semop(semmutexp,&semsignal,1);
semmutexc=semget(IPC_PRIVATE,1,PERM);
semop(semmutexc,&semsignal,1);
int child1,child2,child3,child4;
int *shm_addrp1,*shm_addrp2,*shm_addrc1,*shm_addrc2;
if((child1=fork())==0)
{

shm_addrp1=(int*)shmat(shmid,0,0);
shm_addrp1[10]=0;
int i;
for(i=1;i<=1000;i++)
{
semop(semfull,&semwait,1);
semop(semmutexp,&semwait,1);
printf("%d : Produce %d into shm_addr[%d].\n",getpid(),i,shm_addrp1[10]);
shm_addrp1[shm_addrp1[10]]=i;
shm_addrp1[10]=(shm_addrp1[10]+1)%10;
semop(semempty,&semsignal,1);
semop(semmutexp,&semsignal,1);
}

}
else
{
if((child2=fork())==0)
{

shm_addrc1=(int*)shmat(shmid,0,0);
shm_addrc1[11]=0;shm_addrc1[12]=0;
int i;
for(i=1;i<=1000;i++)
{
semop(semempty,&semwait,1);
semop(semmutexc,&semwait,1);
shm_addrc1[12]+=shm_addrc1[shm_addrc1[11]];
printf("%d : Consumer %d from shm_addr[%d],sum=%d\n",getpid(),shm_addrc1[shm_addrc1[11]],shm_addrc1[11],shm_addrc1[12]);
shm_addrc1[11]=(shm_addrc1[11]+1)%10;
semop(semfull,&semsignal,1);
semop(semmutexc,&semsignal,1);
}
}
else
{
if((child3=fork())==0)
{

shm_addrc2=(int*)shmat(shmid,0,0);
shm_addrc2[11]=0;shm_addrc2[12]=0;
int i;
for(i=1;i<=1000;i++)
{
semop(semempty,&semwait,1);
semop(semmutexc,&semwait,1);
shm_addrc2[12]+=shm_addrc2[shm_addrc2[11]];
printf("%d : Consumer %d from shm_addr[%d],sum=%d\n",getpid(),shm_addrc2[shm_addrc2[11]],shm_addrc2[11],shm_addrc2[12]);
shm_addrc2[11]=(shm_addrc2[11]+1)%10;
semop(semfull,&semsignal,1);
semop(semmutexc,&semsignal,1);
}
}
else
{
if((child4=fork())==0)
{

shm_addrp2=(int*)shmat(shmid,0,0);
shm_addrp2[10]=0;
int i;
for( i=1;i<=1000;i++)
{
semop(semfull,&semwait,1);
semop(semmutexp,&semwait,1);
printf("%d : Produce %d into shm_addr[%d].\n",getpid(),i,shm_addrp2[10]);
shm_addrp2[shm_addrp2[10]]=i;
shm_addrp2[10]=(shm_addrp2[10]+1)%10;
semop(semempty,&semsignal,1);
semop(semmutexp,&semsignal,1);
}


}
else
{
waitpid(child1,NULL,0);
waitpid(child2,NULL,0);
waitpid(child3,NULL,0);
waitpid(child4,NULL,0);
semctl(semfull,2,IPC_RMID,0);
semctl(semempty,2,IPC_RMID,0);
semctl(semmutexp,2,IPC_RMID,0);
semctl(semmutexc,2,IPC_RMID,0);
shmctl(shmid,IPC_RMID,0);
return 0;
}

}
}

}
}
wanggang198022 2003-11-01
  • 打赏
  • 举报
回复
谢谢!但是我还是不会亚
段错误 (core dumped)
[root@localhost study]# ls
core.3260 core.3398 linux.c linux.c~ linux.o
[root@localhost study]# gdb linux.o core.3398
GNU gdb Red Hat Linux (5.3post-0.20021129.18rh)
Copyright 2003 Free Software Foundation, Inc.
GDB is free software, covered by the GNU General Public License, and you are
welcome to change it and/or distribute copies of it under certain conditions.
Type "show copying" to see the conditions.
There is absolutely no warranty for GDB. Type "show warranty" for details.
This GDB was configured as "i386-redhat-linux-gnu"...
Core was generated by `./linux.o'.
Program terminated with signal 11, Segmentation fault.
Reading symbols from /lib/tls/libc.so.6...done.
Loaded symbols for /lib/tls/libc.so.6
Reading symbols from /lib/ld-linux.so.2...done.
Loaded symbols for /lib/ld-linux.so.2
#0 0x0804852c in main ()

23,110

社区成员

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

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