为什么我的这段代码会出现分段错误?
我在运行此代码时遇到分段错误。 我不明白为什么会发生这种情况 - 有人能看出可能的原因吗? (我已经获取并初始化了信号量的共享内存。)
My code: #include<stdlib.h>
#include<sys/types.h>
#include<sys/shm.h>
#include<sys/ipc.h>
#include<stdio.h>
#include<sys/sem.h>
union semun
{
int val;
struct semid_ds *buf;
unsigned short *array;
} arg;
int main()
{
key_t semkey;
int shmid,semid,nsem,sops;
struct sembuf buf[1];
char *ptrr,*shm,c,*s;
semkey=ftok("/home/mawia/abc.c",'a');
printf("entered into main of producer\n");
if(semkey<0)
{
perror("ftok");
exit(1);
}
shmid=shmget(semkey,30,0777);
if(shmid<0)
{
printf("error");
perror("shmget");
exit(1);
}
shm=shmat(shmid,0,0);
if(shm==(char *) -1)
{
perror("shm");
exit(1);
}
s=shm;
semid=semget(semkey,1,0777);
if(semid<0)
{
printf("error");
perror("semget");
exit(0);
}
ptrr=shm+1;
*s='w';
printf("going to check the value 0th semaphores\n");
buf[0].sem_num=0;
buf[0].sem_op=0;
buf[0].sem_flg=0;
buf[1].sem_num=0;
buf[1].sem_op=1;
buf[1].sem_flg=0;
printf("entered the critical region\n");
//printf("waiting to enter the buffer zone...");
semop(semid,buf,2);
printf("entered the critical region\v");
if(*s!='r')
{
printf("\nPRODUCER IS PRODUCING\n\n\n");
printf("ENTER DATA\n");
while((c=getchar())!='\n')
{
*ptrr++=c;
}
*ptrr='\0';
*s='r';
}
else
printf("RESOURCE IS FULL:CAN'T PRODUCE");
//printf("produced enough for the consumer \nexiting from the buffer area now...");
buf[0].sem_num=0;
buf[0].sem_op=-1;
buf[0].sem_flg=0;
semop(semid,buf,1);
ptrr=shm+1;
if(!strcmp(ptrr,"exit"))
{
printf("exiting...");
exit(0);
}
sleep(1);
return 0;
}
I am getting a segmentation fault while running this code. I can't work out why this is happening - can anyone see a possible reason? (I have already got and initialized the semaphore's shared memory.)
My code:
#include<stdlib.h>
#include<sys/types.h>
#include<sys/shm.h>
#include<sys/ipc.h>
#include<stdio.h>
#include<sys/sem.h>
union semun
{
int val;
struct semid_ds *buf;
unsigned short *array;
} arg;
int main()
{
key_t semkey;
int shmid,semid,nsem,sops;
struct sembuf buf[1];
char *ptrr,*shm,c,*s;
semkey=ftok("/home/mawia/abc.c",'a');
printf("entered into main of producer\n");
if(semkey<0)
{
perror("ftok");
exit(1);
}
shmid=shmget(semkey,30,0777);
if(shmid<0)
{
printf("error");
perror("shmget");
exit(1);
}
shm=shmat(shmid,0,0);
if(shm==(char *) -1)
{
perror("shm");
exit(1);
}
s=shm;
semid=semget(semkey,1,0777);
if(semid<0)
{
printf("error");
perror("semget");
exit(0);
}
ptrr=shm+1;
*s='w';
printf("going to check the value 0th semaphores\n");
buf[0].sem_num=0;
buf[0].sem_op=0;
buf[0].sem_flg=0;
buf[1].sem_num=0;
buf[1].sem_op=1;
buf[1].sem_flg=0;
printf("entered the critical region\n");
//printf("waiting to enter the buffer zone...");
semop(semid,buf,2);
printf("entered the critical region\v");
if(*s!='r')
{
printf("\nPRODUCER IS PRODUCING\n\n\n");
printf("ENTER DATA\n");
while((c=getchar())!='\n')
{
*ptrr++=c;
}
*ptrr='\0';
*s='r';
}
else
printf("RESOURCE IS FULL:CAN'T PRODUCE");
//printf("produced enough for the consumer \nexiting from the buffer area now...");
buf[0].sem_num=0;
buf[0].sem_op=-1;
buf[0].sem_flg=0;
semop(semid,buf,1);
ptrr=shm+1;
if(!strcmp(ptrr,"exit"))
{
printf("exiting...");
exit(0);
}
sleep(1);
return 0;
}
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
快速浏览(非常快)后,我想说这可能是由于
您正在访问缓冲区之外的内存引起的。 buf[1] 在堆栈中仅为一个 struct sembuf 保留内存,您尝试使用 2。在这种情况下,您应该使用
After a quick glance (very quick), i would say that it MAY be caused by
You are accessing memory outside of the buffer. buf[1] reserves memory in the stack for only one struct sembuf, you are trying to use 2. In that case, you should use
啊...当你声明时显然有一些非常错误的东西
,但几行之后就出现了
Ah... there is obviously something very wrong when you declare
but a few lines later do
数组分配太小。
这个例子通常太长,不能被认为是一个好的例子; 尝试找到一个更小的(最小是理想的)情况来复制错误,特别是依赖尽可能少的外部库的情况。 另外,请尝试在调试器中运行并在询问之前单步执行代码。
Array allocation too small.
This example is generally too long to be considered a good example; try to find a smaller (minimal is ideal) case which replicates the error, particularly one which depends on as few external libraries as possible. Also, try running in the debugger and stepping through the code before asking.